Upstream version 11.39.266.0
[platform/framework/web/crosswalk.git] / src / third_party / npapi / npspy / extern / nspr / md / _nspr_pthread.h
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* 
3  * The contents of this file are subject to the Mozilla Public
4  * License Version 1.1 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of
6  * the License at http://www.mozilla.org/MPL/
7  * 
8  * Software distributed under the License is distributed on an "AS
9  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
10  * implied. See the License for the specific language governing
11  * rights and limitations under the License.
12  * 
13  * The Original Code is the Netscape Portable Runtime (NSPR).
14  * 
15  * The Initial Developer of the Original Code is Netscape
16  * Communications Corporation.  Portions created by Netscape are 
17  * Copyright (C) 1998-2000 Netscape Communications Corporation.  All
18  * Rights Reserved.
19  * 
20  * Contributor(s):
21  * 
22  * Alternatively, the contents of this file may be used under the
23  * terms of the GNU General Public License Version 2 or later (the
24  * "GPL"), in which case the provisions of the GPL are applicable 
25  * instead of those above.  If you wish to allow use of your 
26  * version of this file only under the terms of the GPL and not to
27  * allow others to use your version of this file under the MPL,
28  * indicate your decision by deleting the provisions above and
29  * replace them with the notice and other provisions required by
30  * the GPL.  If you do not delete the provisions above, a recipient
31  * may use your version of this file under either the MPL or the
32  * GPL.
33  */
34
35 #ifndef nspr_pthread_defs_h___
36 #define nspr_pthread_defs_h___
37
38 #include <pthread.h>
39 #include "prthread.h"
40
41 #if defined(PTHREADS_USER)
42 /*
43 ** Thread Local Storage 
44 */
45 extern pthread_key_t current_thread_key;
46 extern pthread_key_t current_cpu_key;
47 extern pthread_key_t last_thread_key;
48 extern pthread_key_t intsoff_key;
49
50 #define _MD_CURRENT_THREAD()                    \
51                         ((struct PRThread *) pthread_getspecific(current_thread_key))
52 #define _MD_CURRENT_CPU()                               \
53                         ((struct _PRCPU *) pthread_getspecific(current_cpu_key))
54 #define _MD_LAST_THREAD()                               \
55                         ((struct PRThread *) pthread_getspecific(last_thread_key))
56         
57 #define _MD_SET_CURRENT_THREAD(newval)                  \
58         pthread_setspecific(current_thread_key, (void *)newval)
59
60 #define _MD_SET_CURRENT_CPU(newval)                     \
61         pthread_setspecific(current_cpu_key, (void *)newval)
62
63 #define _MD_SET_LAST_THREAD(newval)                             \
64         pthread_setspecific(last_thread_key, (void *)newval)
65
66 #define _MD_SET_INTSOFF(_val)
67 #define _MD_GET_INTSOFF()       1
68         
69 /*
70 ** Initialize the thread context preparing it to execute _main.
71 */
72 #define _MD_INIT_CONTEXT(_thread, _sp, _main, status)                   \
73     PR_BEGIN_MACRO                                                                                      \
74         *status = PR_TRUE;                                                              \
75                 if (SAVE_CONTEXT(_thread)) {                                                    \
76                 (*_main)();                                                                                     \
77                 }                                                                                                               \
78                 _MD_SET_THR_SP(_thread, _sp);                                                   \
79                 _thread->no_sched = 0;                                                                  \
80     PR_END_MACRO
81
82 #define _MD_SWITCH_CONTEXT(_thread)                                                             \
83     PR_BEGIN_MACRO                                                                                                      \
84         PR_ASSERT(_thread->no_sched);                                                                   \
85         if (!SAVE_CONTEXT(_thread)) {                                                                   \
86                 (_thread)->md.errcode = errno;                                                          \
87                 _MD_SET_LAST_THREAD(_thread);                                                           \
88                 _PR_Schedule();                                                                                 \
89     } else {                                                                                                            \
90                  (_MD_LAST_THREAD())->no_sched = 0;                                                     \
91         }                                                                                                                               \
92     PR_END_MACRO
93
94 /*
95 ** Restore a thread context, saved by _MD_SWITCH_CONTEXT
96 */
97 #define _MD_RESTORE_CONTEXT(_thread)                                                            \
98     PR_BEGIN_MACRO                                                                                                      \
99     errno = (_thread)->md.errcode;                                                                      \
100     _MD_SET_CURRENT_THREAD(_thread);                                                            \
101         _thread->no_sched = 1;                                                                                  \
102     GOTO_CONTEXT(_thread);                                                                                      \
103     PR_END_MACRO
104
105
106 /* Machine-dependent (MD) data structures */
107
108 struct _MDThread {
109     jmp_buf             jb;
110     int                         id;
111     int                         errcode;
112         pthread_t               pthread;
113         pthread_mutex_t pthread_mutex;
114         pthread_cond_t  pthread_cond;
115         int                             wait;
116 };
117
118 struct _MDThreadStack {
119     PRInt8 notused;
120 };
121
122 struct _MDLock {
123         pthread_mutex_t mutex;
124 };
125
126 struct _MDSemaphore {
127     PRInt8 notused;
128 };
129
130 struct _MDCVar {
131         pthread_mutex_t mutex;
132 };
133
134 struct _MDSegment {
135     PRInt8 notused;
136 };
137
138 /*
139  * md-specific cpu structure field
140  */
141 #define _PR_MD_MAX_OSFD FD_SETSIZE
142
143 struct _MDCPU_Unix {
144     PRCList ioQ;
145     PRUint32 ioq_timeout;
146     PRInt32 ioq_max_osfd;
147     PRInt32 ioq_osfd_cnt;
148 #ifndef _PR_USE_POLL
149     fd_set fd_read_set, fd_write_set, fd_exception_set;
150     PRInt16 fd_read_cnt[_PR_MD_MAX_OSFD],fd_write_cnt[_PR_MD_MAX_OSFD],
151                                 fd_exception_cnt[_PR_MD_MAX_OSFD];
152 #else
153         struct pollfd *ioq_pollfds;
154         int ioq_pollfds_size;
155 #endif  /* _PR_USE_POLL */
156 };
157
158 #define _PR_IOQ(_cpu)                   ((_cpu)->md.md_unix.ioQ)
159 #define _PR_ADD_TO_IOQ(_pq, _cpu) PR_APPEND_LINK(&_pq.links, &_PR_IOQ(_cpu))
160 #define _PR_FD_READ_SET(_cpu)           ((_cpu)->md.md_unix.fd_read_set)
161 #define _PR_FD_READ_CNT(_cpu)           ((_cpu)->md.md_unix.fd_read_cnt)
162 #define _PR_FD_WRITE_SET(_cpu)          ((_cpu)->md.md_unix.fd_write_set)
163 #define _PR_FD_WRITE_CNT(_cpu)          ((_cpu)->md.md_unix.fd_write_cnt)
164 #define _PR_FD_EXCEPTION_SET(_cpu)      ((_cpu)->md.md_unix.fd_exception_set)
165 #define _PR_FD_EXCEPTION_CNT(_cpu)      ((_cpu)->md.md_unix.fd_exception_cnt)
166 #define _PR_IOQ_TIMEOUT(_cpu)           ((_cpu)->md.md_unix.ioq_timeout)
167 #define _PR_IOQ_MAX_OSFD(_cpu)          ((_cpu)->md.md_unix.ioq_max_osfd)
168 #define _PR_IOQ_OSFD_CNT(_cpu)          ((_cpu)->md.md_unix.ioq_osfd_cnt)
169 #define _PR_IOQ_POLLFDS(_cpu)           ((_cpu)->md.md_unix.ioq_pollfds)
170 #define _PR_IOQ_POLLFDS_SIZE(_cpu)      ((_cpu)->md.md_unix.ioq_pollfds_size)
171
172 #define _PR_IOQ_MIN_POLLFDS_SIZE(_cpu)  32
173
174 struct _MDCPU {
175     jmp_buf                     jb;
176         pthread_t                       pthread;
177         struct _MDCPU_Unix      md_unix;
178 };
179
180 /*
181 #define _MD_NEW_LOCK(lock) PR_SUCCESS
182 #define _MD_FREE_LOCK(lock)
183 #define _MD_LOCK(lock)
184 #define _MD_UNLOCK(lock)
185 */
186
187 extern pthread_mutex_t _pr_heapLock;
188
189 #define _PR_LOCK(lock) pthread_mutex_lock(lock)
190
191 #define _PR_UNLOCK(lock) pthread_mutex_unlock(lock)
192
193
194 #define _PR_LOCK_HEAP() {                                                                       \
195                                 if (_pr_primordialCPU) {                                        \
196                                         _PR_LOCK(_pr_heapLock);                                 \
197                                 }
198
199 #define _PR_UNLOCK_HEAP()       if (_pr_primordialCPU)  {               \
200                                         _PR_UNLOCK(_pr_heapLock);                               \
201                                 }                                                                                       \
202                           }
203
204 NSPR_API(PRStatus) _MD_NEW_LOCK(struct _MDLock *md);
205 NSPR_API(void) _MD_FREE_LOCK(struct _MDLock *lockp);
206
207 #define _MD_LOCK(_lockp) _PR_LOCK(&(_lockp)->mutex)
208 #define _MD_UNLOCK(_lockp) _PR_UNLOCK(&(_lockp)->mutex)
209
210 #define _MD_INIT_IO()
211 #define _MD_IOQ_LOCK()
212 #define _MD_IOQ_UNLOCK()
213 #define _MD_CHECK_FOR_EXIT()
214
215 NSPR_API(PRStatus) _MD_InitThread(struct PRThread *thread);
216 #define _MD_INIT_THREAD _MD_InitThread
217 #define _MD_INIT_ATTACHED_THREAD _MD_InitThread
218
219 NSPR_API(void) _MD_ExitThread(struct PRThread *thread);
220 #define _MD_EXIT_THREAD _MD_ExitThread
221
222 NSPR_API(void) _MD_SuspendThread(struct PRThread *thread);
223 #define _MD_SUSPEND_THREAD _MD_SuspendThread
224
225 NSPR_API(void) _MD_ResumeThread(struct PRThread *thread);
226 #define _MD_RESUME_THREAD _MD_ResumeThread
227
228 NSPR_API(void) _MD_SuspendCPU(struct _PRCPU *thread);
229 #define _MD_SUSPEND_CPU _MD_SuspendCPU
230
231 NSPR_API(void) _MD_ResumeCPU(struct _PRCPU *thread);
232 #define _MD_RESUME_CPU _MD_ResumeCPU
233
234 #define _MD_BEGIN_SUSPEND_ALL()
235 #define _MD_END_SUSPEND_ALL()
236 #define _MD_BEGIN_RESUME_ALL()
237 #define _MD_END_RESUME_ALL()
238
239 NSPR_API(void) _MD_EarlyInit(void);
240 #define _MD_EARLY_INIT _MD_EarlyInit
241
242 #define _MD_FINAL_INIT _PR_UnixInit
243
244 NSPR_API(void) _MD_InitLocks(void);
245 #define _MD_INIT_LOCKS _MD_InitLocks
246
247 NSPR_API(void) _MD_CleanThread(struct PRThread *thread);
248 #define _MD_CLEAN_THREAD _MD_CleanThread
249
250 NSPR_API(PRStatus) _MD_CreateThread(
251                         struct PRThread *thread,
252                         void (*start) (void *),
253                         PRThreadPriority priority,
254                         PRThreadScope scope,
255                         PRThreadState state,
256                         PRUint32 stackSize);
257 #define _MD_CREATE_THREAD _MD_CreateThread
258
259 extern void _MD_CleanupBeforeExit(void);
260 #define _MD_CLEANUP_BEFORE_EXIT _MD_CleanupBeforeExit
261
262 NSPR_API(void) _MD_InitRunningCPU(struct _PRCPU *cpu);
263 #define    _MD_INIT_RUNNING_CPU _MD_InitRunningCPU
264
265 /* The _PR_MD_WAIT_LOCK and _PR_MD_WAKEUP_WAITER functions put to sleep and
266  * awaken a thread which is waiting on a lock or cvar.
267  */
268 NSPR_API(PRStatus) _MD_wait(struct PRThread *, PRIntervalTime timeout);
269 #define _MD_WAIT _MD_wait
270
271 NSPR_API(PRStatus) _MD_WakeupWaiter(struct PRThread *);
272 #define _MD_WAKEUP_WAITER _MD_WakeupWaiter
273
274 NSPR_API(void) _MD_SetPriority(struct _MDThread *thread,
275         PRThreadPriority newPri);
276 #define _MD_SET_PRIORITY _MD_SetPriority
277
278 #endif /* PTHREADS_USER */
279
280 #endif /* nspr_pthread_defs_h___ */