Sun, 11 Aug 2024 13:26:17 +0200
refactore EventHandler: add common fields to base struct
1 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
4 | * Copyright 2008 Sun Microsystems, Inc. All rights reserved. | |
5 | * | |
6 | * THE BSD LICENSE | |
7 | * | |
8 | * Redistribution and use in source and binary forms, with or without | |
9 | * modification, are permitted provided that the following conditions are met: | |
10 | * | |
11 | * Redistributions of source code must retain the above copyright notice, this | |
12 | * list of conditions and the following disclaimer. | |
13 | * Redistributions in binary form must reproduce the above copyright notice, | |
14 | * this list of conditions and the following disclaimer in the documentation | |
15 | * and/or other materials provided with the distribution. | |
16 | * | |
17 | * Neither the name of the nor the names of its contributors may be | |
18 | * used to endorse or promote products derived from this software without | |
19 | * specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
22 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
23 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
24 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER | |
25 | * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
26 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
27 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | |
28 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | |
30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
32 | */ | |
33 | ||
34 | /* | |
35 | * systhr.c: Abstracted threading mechanisms | |
36 | * | |
37 | * Rob McCool | |
38 | */ | |
39 | ||
40 | ||
41 | #include "systhr.h" | |
29
e8619defde14
added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
42 | //include "ereport.h" |
1 | 43 | |
40 | 44 | //include "prinit.h" |
45 | //include "prthread.h" | |
41
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
46 | //include "private/pprthred.h" |
1 | 47 | |
48 | #include "systems.h" | |
49 | ||
50 | #ifdef XP_UNIX | |
51 | #include <poll.h> | |
52 | #endif | |
53 | ||
54 | #define DEFAULT_STACKSIZE (64*1024) | |
55 | ||
56 | static unsigned long _systhr_stacksize = DEFAULT_STACKSIZE; | |
57 | ||
58 | NSAPI_PUBLIC | |
59 | void systhread_set_default_stacksize(unsigned long size) | |
60 | { | |
61 | _systhr_stacksize = size; | |
62 | } | |
63 | ||
64 | ||
65 | ||
29
e8619defde14
added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
66 | SYS_THREAD systhread_start(int prio, int stksz, thrstartfunc fn, void *arg) { |
e8619defde14
added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
67 | pthread_t thr = 0; |
e8619defde14
added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
68 | pthread_attr_t attr; |
e8619defde14
added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
69 | |
e8619defde14
added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
70 | pthread_attr_init(&attr); |
e8619defde14
added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
71 | if(stksz) { |
e8619defde14
added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
72 | pthread_attr_setstacksize(&attr, stksz); |
e8619defde14
added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
73 | } |
e8619defde14
added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
74 | |
e8619defde14
added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
75 | if(pthread_create(&thr, &attr, (posix_thrstartfunc)fn, arg) != 0) { |
e8619defde14
added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
76 | /* error */ |
e8619defde14
added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
77 | } |
e8619defde14
added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
78 | |
e8619defde14
added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
79 | return thr; |
1 | 80 | } |
81 | ||
29
e8619defde14
added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
82 | SYS_THREAD systhread_current(void) { |
e8619defde14
added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
83 | return pthread_self(); |
1 | 84 | } |
85 | ||
40 | 86 | void systhread_yield(void) { |
29
e8619defde14
added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
87 | sched_yield(); |
1 | 88 | } |
89 | ||
90 | ||
40 | 91 | void systhread_timerset(int usec) { |
29
e8619defde14
added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
92 | |
e8619defde14
added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
93 | } |
e8619defde14
added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
94 | |
40 | 95 | SYS_THREAD systhread_attach(void) { |
29
e8619defde14
added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
96 | /* TODO: what to do? */ |
e8619defde14
added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
97 | return 0; |
1 | 98 | } |
99 | ||
40 | 100 | void systhread_detach(SYS_THREAD thr) { |
29
e8619defde14
added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
101 | pthread_detach(thr); |
1 | 102 | } |
103 | ||
40 | 104 | void systhread_terminate(SYS_THREAD thr) { |
29
e8619defde14
added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
105 | //PR_Interrupt((PRThread *)thr); |
1 | 106 | } |
107 | ||
40 | 108 | void systhread_sleep(int msec) { |
29
e8619defde14
added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
109 | if(msec > 0) { |
41
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
110 | poll(NULL, 0, msec); |
29
e8619defde14
added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
111 | } else { |
e8619defde14
added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
112 | sched_yield(); |
e8619defde14
added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
113 | } |
1 | 114 | } |
115 | ||
40 | 116 | NSAPI_PUBLIC int systhread_newkey() { |
117 | pthread_key_t key; | |
118 | pthread_key_create(&key, NULL); | |
119 | ||
120 | return (int)key; // TODO: don't use int | |
121 | } | |
122 | ||
123 | NSAPI_PUBLIC void* systhread_getdata(int key) { | |
124 | return pthread_getspecific((pthread_key_t)key); | |
125 | } | |
126 | ||
127 | NSAPI_PUBLIC void systhread_setdata(int key, void *data) { | |
128 | pthread_setspecific((pthread_key_t)key, data); | |
129 | } | |
130 | ||
1 | 131 | NSAPI_PUBLIC void systhread_init(char *name) |
132 | { | |
40 | 133 | //if (!PR_Initialized()) { |
134 | // PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 256); | |
135 | //} | |
1 | 136 | // XXX: ruslan - this bug can potentially exist on all plafroms due to |
137 | // possible priority inversion on NSPR spin locks. This code will | |
138 | // need to be remove as we get new NSPR drop | |
139 | // <WORKAROUND> | |
140 | /* VB: This is to fix bug# 364813 coupled with NSPR not wanting to patch | |
141 | their problems. The fix is to prevent NSPR itself from | |
142 | using atomic stacks. | |
143 | */ | |
144 | // ruslan: this problem exists on DEC also. We will roll it back when | |
145 | // we have the right fix from NSPR group. It has smth. to do with | |
146 | // atomic operations on DEC, it's an assembly code which is different | |
147 | // for every platform. NSPR_FD_CACHE_SIZE_HIGH env var will cause the | |
148 | // same effect as this fix. Debug version of NSPR always works as it doesn't | |
149 | // have FD stack. | |
150 | ||
40 | 151 | //int maxPRFdCache = 8192; |
152 | //PR_SetFDCacheSize(0, maxPRFdCache); | |
1 | 153 | // </WORKAROUND> |
154 | } | |
155 | ||
40 | 156 | NSAPI_PUBLIC void systhread_dummy(void) { |
1 | 157 | } |