Back to index

lightning-sunbird  0.9+nobinonly
_nspr_pthread.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #ifndef nspr_pthread_defs_h___
00039 #define nspr_pthread_defs_h___
00040 
00041 #include <pthread.h>
00042 #include "prthread.h"
00043 
00044 #if defined(PTHREADS_USER)
00045 /*
00046 ** Thread Local Storage 
00047 */
00048 extern pthread_key_t current_thread_key;
00049 extern pthread_key_t current_cpu_key;
00050 extern pthread_key_t last_thread_key;
00051 extern pthread_key_t intsoff_key;
00052 
00053 #define _MD_CURRENT_THREAD()                     \
00054                      ((struct PRThread *) pthread_getspecific(current_thread_key))
00055 #define _MD_CURRENT_CPU()                        \
00056                      ((struct _PRCPU *) pthread_getspecific(current_cpu_key))
00057 #define _MD_LAST_THREAD()                        \
00058                      ((struct PRThread *) pthread_getspecific(last_thread_key))
00059        
00060 #define _MD_SET_CURRENT_THREAD(newval)                  \
00061        pthread_setspecific(current_thread_key, (void *)newval)
00062 
00063 #define _MD_SET_CURRENT_CPU(newval)                     \
00064        pthread_setspecific(current_cpu_key, (void *)newval)
00065 
00066 #define _MD_SET_LAST_THREAD(newval)                            \
00067        pthread_setspecific(last_thread_key, (void *)newval)
00068 
00069 #define _MD_SET_INTSOFF(_val)
00070 #define _MD_GET_INTSOFF()   1
00071        
00072 /*
00073 ** Initialize the thread context preparing it to execute _main.
00074 */
00075 #define _MD_INIT_CONTEXT(_thread, _sp, _main, status)                 \
00076     PR_BEGIN_MACRO                                                                         \
00077         *status = PR_TRUE;                                                   \
00078               if (SAVE_CONTEXT(_thread)) {                                                 \
00079               (*_main)();                                                                         \
00080               }                                                                                                 \
00081               _MD_SET_THR_SP(_thread, _sp);                                                \
00082               _thread->no_sched = 0;                                                              \
00083     PR_END_MACRO
00084 
00085 #define _MD_SWITCH_CONTEXT(_thread)                                                        \
00086     PR_BEGIN_MACRO                                                                                       \
00087        PR_ASSERT(_thread->no_sched);                                                              \
00088        if (!SAVE_CONTEXT(_thread)) {                                                              \
00089               (_thread)->md.errcode = errno;                                               \
00090               _MD_SET_LAST_THREAD(_thread);                                                       \
00091               _PR_Schedule();                                                                            \
00092     } else {                                                                                             \
00093                (_MD_LAST_THREAD())->no_sched = 0;                                                 \
00094        }                                                                                                               \
00095     PR_END_MACRO
00096 
00097 /*
00098 ** Restore a thread context, saved by _MD_SWITCH_CONTEXT
00099 */
00100 #define _MD_RESTORE_CONTEXT(_thread)                                                       \
00101     PR_BEGIN_MACRO                                                                                       \
00102     errno = (_thread)->md.errcode;                                                                \
00103     _MD_SET_CURRENT_THREAD(_thread);                                                       \
00104        _thread->no_sched = 1;                                                                            \
00105     GOTO_CONTEXT(_thread);                                                                        \
00106     PR_END_MACRO
00107 
00108 
00109 /* Machine-dependent (MD) data structures */
00110 
00111 struct _MDThread {
00112     jmp_buf          jb;
00113     int                            id;
00114     int                            errcode;
00115        pthread_t            pthread;
00116        pthread_mutex_t      pthread_mutex;
00117        pthread_cond_t       pthread_cond;
00118        int                         wait;
00119 };
00120 
00121 struct _MDThreadStack {
00122     PRInt8 notused;
00123 };
00124 
00125 struct _MDLock {
00126        pthread_mutex_t mutex;
00127 };
00128 
00129 struct _MDSemaphore {
00130     PRInt8 notused;
00131 };
00132 
00133 struct _MDCVar {
00134        pthread_mutex_t mutex;
00135 };
00136 
00137 struct _MDSegment {
00138     PRInt8 notused;
00139 };
00140 
00141 /*
00142  * md-specific cpu structure field
00143  */
00144 #define _PR_MD_MAX_OSFD FD_SETSIZE
00145 
00146 struct _MDCPU_Unix {
00147     PRCList ioQ;
00148     PRUint32 ioq_timeout;
00149     PRInt32 ioq_max_osfd;
00150     PRInt32 ioq_osfd_cnt;
00151 #ifndef _PR_USE_POLL
00152     fd_set fd_read_set, fd_write_set, fd_exception_set;
00153     PRInt16 fd_read_cnt[_PR_MD_MAX_OSFD],fd_write_cnt[_PR_MD_MAX_OSFD],
00154                             fd_exception_cnt[_PR_MD_MAX_OSFD];
00155 #else
00156        struct pollfd *ioq_pollfds;
00157        int ioq_pollfds_size;
00158 #endif /* _PR_USE_POLL */
00159 };
00160 
00161 #define _PR_IOQ(_cpu)                     ((_cpu)->md.md_unix.ioQ)
00162 #define _PR_ADD_TO_IOQ(_pq, _cpu) PR_APPEND_LINK(&_pq.links, &_PR_IOQ(_cpu))
00163 #define _PR_FD_READ_SET(_cpu)             ((_cpu)->md.md_unix.fd_read_set)
00164 #define _PR_FD_READ_CNT(_cpu)             ((_cpu)->md.md_unix.fd_read_cnt)
00165 #define _PR_FD_WRITE_SET(_cpu)            ((_cpu)->md.md_unix.fd_write_set)
00166 #define _PR_FD_WRITE_CNT(_cpu)            ((_cpu)->md.md_unix.fd_write_cnt)
00167 #define _PR_FD_EXCEPTION_SET(_cpu) ((_cpu)->md.md_unix.fd_exception_set)
00168 #define _PR_FD_EXCEPTION_CNT(_cpu) ((_cpu)->md.md_unix.fd_exception_cnt)
00169 #define _PR_IOQ_TIMEOUT(_cpu)             ((_cpu)->md.md_unix.ioq_timeout)
00170 #define _PR_IOQ_MAX_OSFD(_cpu)            ((_cpu)->md.md_unix.ioq_max_osfd)
00171 #define _PR_IOQ_OSFD_CNT(_cpu)            ((_cpu)->md.md_unix.ioq_osfd_cnt)
00172 #define _PR_IOQ_POLLFDS(_cpu)             ((_cpu)->md.md_unix.ioq_pollfds)
00173 #define _PR_IOQ_POLLFDS_SIZE(_cpu) ((_cpu)->md.md_unix.ioq_pollfds_size)
00174 
00175 #define _PR_IOQ_MIN_POLLFDS_SIZE(_cpu)    32
00176 
00177 struct _MDCPU {
00178     jmp_buf                 jb;
00179        pthread_t                   pthread;
00180        struct _MDCPU_Unix   md_unix;
00181 };
00182 
00183 /*
00184 #define _MD_NEW_LOCK(lock) PR_SUCCESS
00185 #define _MD_FREE_LOCK(lock)
00186 #define _MD_LOCK(lock)
00187 #define _MD_UNLOCK(lock)
00188 */
00189 
00190 extern pthread_mutex_t _pr_heapLock;
00191 
00192 #define _PR_LOCK(lock) pthread_mutex_lock(lock)
00193 
00194 #define _PR_UNLOCK(lock) pthread_mutex_unlock(lock)
00195 
00196 
00197 #define _PR_LOCK_HEAP()     {                                                              \
00198                             if (_pr_primordialCPU) {                                \
00199                                    _PR_LOCK(_pr_heapLock);                                 \
00200                             }
00201 
00202 #define _PR_UNLOCK_HEAP()   if (_pr_primordialCPU)      {             \
00203                                    _PR_UNLOCK(_pr_heapLock);                        \
00204                             }                                                                            \
00205                        }
00206 
00207 NSPR_API(PRStatus) _MD_NEW_LOCK(struct _MDLock *md);
00208 NSPR_API(void) _MD_FREE_LOCK(struct _MDLock *lockp);
00209 
00210 #define _MD_LOCK(_lockp) _PR_LOCK(&(_lockp)->mutex)
00211 #define _MD_UNLOCK(_lockp) _PR_UNLOCK(&(_lockp)->mutex)
00212 
00213 #define _MD_INIT_IO()
00214 #define _MD_IOQ_LOCK()
00215 #define _MD_IOQ_UNLOCK()
00216 #define _MD_CHECK_FOR_EXIT()
00217 
00218 NSPR_API(PRStatus) _MD_InitThread(struct PRThread *thread);
00219 #define _MD_INIT_THREAD _MD_InitThread
00220 #define _MD_INIT_ATTACHED_THREAD _MD_InitThread
00221 
00222 NSPR_API(void) _MD_ExitThread(struct PRThread *thread);
00223 #define _MD_EXIT_THREAD _MD_ExitThread
00224 
00225 NSPR_API(void) _MD_SuspendThread(struct PRThread *thread);
00226 #define _MD_SUSPEND_THREAD _MD_SuspendThread
00227 
00228 NSPR_API(void) _MD_ResumeThread(struct PRThread *thread);
00229 #define _MD_RESUME_THREAD _MD_ResumeThread
00230 
00231 NSPR_API(void) _MD_SuspendCPU(struct _PRCPU *thread);
00232 #define _MD_SUSPEND_CPU _MD_SuspendCPU
00233 
00234 NSPR_API(void) _MD_ResumeCPU(struct _PRCPU *thread);
00235 #define _MD_RESUME_CPU _MD_ResumeCPU
00236 
00237 #define _MD_BEGIN_SUSPEND_ALL()
00238 #define _MD_END_SUSPEND_ALL()
00239 #define _MD_BEGIN_RESUME_ALL()
00240 #define _MD_END_RESUME_ALL()
00241 
00242 NSPR_API(void) _MD_EarlyInit(void);
00243 #define _MD_EARLY_INIT _MD_EarlyInit
00244 
00245 #define _MD_FINAL_INIT _PR_UnixInit
00246 
00247 NSPR_API(void) _MD_InitLocks(void);
00248 #define _MD_INIT_LOCKS _MD_InitLocks
00249 
00250 NSPR_API(void) _MD_CleanThread(struct PRThread *thread);
00251 #define _MD_CLEAN_THREAD _MD_CleanThread
00252 
00253 NSPR_API(PRStatus) _MD_CreateThread(
00254                         struct PRThread *thread,
00255                         void (*start) (void *),
00256                         PRThreadPriority priority,
00257                         PRThreadScope scope,
00258                         PRThreadState state,
00259                         PRUint32 stackSize);
00260 #define _MD_CREATE_THREAD _MD_CreateThread
00261 
00262 extern void _MD_CleanupBeforeExit(void);
00263 #define _MD_CLEANUP_BEFORE_EXIT _MD_CleanupBeforeExit
00264 
00265 NSPR_API(void) _MD_InitRunningCPU(struct _PRCPU *cpu);
00266 #define    _MD_INIT_RUNNING_CPU _MD_InitRunningCPU
00267 
00268 /* The _PR_MD_WAIT_LOCK and _PR_MD_WAKEUP_WAITER functions put to sleep and
00269  * awaken a thread which is waiting on a lock or cvar.
00270  */
00271 NSPR_API(PRStatus) _MD_wait(struct PRThread *, PRIntervalTime timeout);
00272 #define _MD_WAIT _MD_wait
00273 
00274 NSPR_API(PRStatus) _MD_WakeupWaiter(struct PRThread *);
00275 #define _MD_WAKEUP_WAITER _MD_WakeupWaiter
00276 
00277 NSPR_API(void) _MD_SetPriority(struct _MDThread *thread,
00278        PRThreadPriority newPri);
00279 #define _MD_SET_PRIORITY _MD_SetPriority
00280 
00281 #endif /* PTHREADS_USER */
00282 
00283 #endif /* nspr_pthread_defs_h___ */