]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libpthread/thread/thr_private.h
Use growable stacks for thread stacks that are the default stack size.
[FreeBSD/FreeBSD.git] / lib / libpthread / thread / thr_private.h
1 /*
2  * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by John Birrell.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * Private thread definitions for the uthread kernel.
33  *
34  * $Id: pthread_private.h,v 1.20 1999/06/20 08:28:08 jb Exp $
35  */
36
37 #ifndef _PTHREAD_PRIVATE_H
38 #define _PTHREAD_PRIVATE_H
39
40 /*
41  * Evaluate the storage class specifier.
42  */
43 #ifdef GLOBAL_PTHREAD_PRIVATE
44 #define SCLASS
45 #else
46 #define SCLASS extern
47 #endif
48
49 /*
50  * Include files.
51  */
52 #include <setjmp.h>
53 #include <signal.h>
54 #include <sys/queue.h>
55 #include <sys/types.h>
56 #include <sys/time.h>
57 #include <sched.h>
58 #include <spinlock.h>
59 #include <pthread_np.h>
60
61 /*
62  * Kernel fatal error handler macro.
63  */
64 #define PANIC(string)   _thread_exit(__FILE__,__LINE__,string)
65
66 /* Output debug messages like this: */
67 #define stdout_debug(_x)        _thread_sys_write(1,_x,strlen(_x));
68 #define stderr_debug(_x)        _thread_sys_write(2,_x,strlen(_x));
69
70
71 /*
72  * Priority queue manipulation macros (using pqe link):
73  */
74 #define PTHREAD_PRIOQ_INSERT_HEAD(thrd) _pq_insert_head(&_readyq,thrd)
75 #define PTHREAD_PRIOQ_INSERT_TAIL(thrd) _pq_insert_tail(&_readyq,thrd)
76 #define PTHREAD_PRIOQ_REMOVE(thrd)      _pq_remove(&_readyq,thrd)
77 #define PTHREAD_PRIOQ_FIRST()           _pq_first(&_readyq)
78
79 /*
80  * Waiting queue manipulation macros (using pqe link):
81  */
82 #if defined(_PTHREADS_INVARIANTS)
83 #define PTHREAD_WAITQ_REMOVE(thrd)      _waitq_remove(thrd)
84 #define PTHREAD_WAITQ_INSERT(thrd)      _waitq_insert(thrd)
85 #define PTHREAD_WAITQ_CLEARACTIVE()     _waitq_clearactive()
86 #define PTHREAD_WAITQ_SETACTIVE()       _waitq_setactive()
87 #else
88 #define PTHREAD_WAITQ_REMOVE(thrd)      TAILQ_REMOVE(&_waitingq,thrd,pqe)
89 #define PTHREAD_WAITQ_INSERT(thrd) do {                                 \
90         if ((thrd)->wakeup_time.tv_sec == -1)                           \
91                 TAILQ_INSERT_TAIL(&_waitingq,thrd,pqe);                 \
92         else {                                                          \
93                 pthread_t tid = TAILQ_FIRST(&_waitingq);                \
94                 while ((tid != NULL) && (tid->wakeup_time.tv_sec != -1) && \
95                     ((tid->wakeup_time.tv_sec < (thrd)->wakeup_time.tv_sec) ||  \
96                     ((tid->wakeup_time.tv_sec == (thrd)->wakeup_time.tv_sec) && \
97                     (tid->wakeup_time.tv_nsec <= (thrd)->wakeup_time.tv_nsec)))) \
98                         tid = TAILQ_NEXT(tid, pqe);                     \
99                 if (tid == NULL)                                        \
100                         TAILQ_INSERT_TAIL(&_waitingq,thrd,pqe);         \
101                 else                                                    \
102                         TAILQ_INSERT_BEFORE(tid,thrd,pqe);              \
103         }                                                               \
104 } while (0)
105 #define PTHREAD_WAITQ_CLEARACTIVE()
106 #define PTHREAD_WAITQ_SETACTIVE()
107 #endif
108
109 /*
110  * Work queue manipulation macros (using qe link):
111  */
112 #define PTHREAD_WORKQ_INSERT(thrd) do {                                 \
113         TAILQ_INSERT_TAIL(&_workq,thrd,qe);                             \
114         (thrd)->flags |= PTHREAD_FLAGS_IN_WORKQ;                        \
115 } while (0)
116 #define PTHREAD_WORKQ_REMOVE(thrd) do {                                 \
117         TAILQ_REMOVE(&_workq,thrd,qe);                                  \
118         (thrd)->flags &= ~PTHREAD_FLAGS_IN_WORKQ;                       \
119 } while (0)
120
121
122 /*
123  * State change macro without scheduling queue change:
124  */
125 #define PTHREAD_SET_STATE(thrd, newstate) do {                          \
126         (thrd)->state = newstate;                                       \
127         (thrd)->fname = __FILE__;                                       \
128         (thrd)->lineno = __LINE__;                                      \
129 } while (0)
130
131 /*
132  * State change macro with scheduling queue change - This must be
133  * called with preemption deferred (see thread_kern_sched_[un]defer).
134  */
135 #if defined(_PTHREADS_INVARIANTS)
136 #define PTHREAD_NEW_STATE(thrd, newstate) do {                          \
137         if (_thread_kern_new_state != 0)                                \
138                 PANIC("Recursive PTHREAD_NEW_STATE");                   \
139         _thread_kern_new_state = 1;                                     \
140         if ((thrd)->state != newstate) {                                \
141                 if ((thrd)->state == PS_RUNNING) {                      \
142                         PTHREAD_PRIOQ_REMOVE(thrd);                     \
143                         PTHREAD_WAITQ_INSERT(thrd);                     \
144                 } else if (newstate == PS_RUNNING) {                    \
145                         PTHREAD_WAITQ_REMOVE(thrd);                     \
146                         PTHREAD_PRIOQ_INSERT_TAIL(thrd);                \
147                 }                                                       \
148         }                                                               \
149         _thread_kern_new_state = 0;                                     \
150         PTHREAD_SET_STATE(thrd, newstate);                              \
151 } while (0)
152 #else
153 #define PTHREAD_NEW_STATE(thrd, newstate) do {                          \
154         if ((thrd)->state != newstate) {                                \
155                 if ((thrd)->state == PS_RUNNING) {                      \
156                         PTHREAD_PRIOQ_REMOVE(thrd);                     \
157                         PTHREAD_WAITQ_INSERT(thrd);                     \
158                 } else if (newstate == PS_RUNNING) {                    \
159                         PTHREAD_WAITQ_REMOVE(thrd);                     \
160                         PTHREAD_PRIOQ_INSERT_TAIL(thrd);                \
161                 }                                                       \
162         }                                                               \
163         PTHREAD_SET_STATE(thrd, newstate);                              \
164 } while (0)
165 #endif
166
167 /*
168  * Define the signals to be used for scheduling.
169  */
170 #if defined(_PTHREADS_COMPAT_SCHED)
171 #define _ITIMER_SCHED_TIMER     ITIMER_VIRTUAL
172 #define _SCHED_SIGNAL           SIGVTALRM
173 #else
174 #define _ITIMER_SCHED_TIMER     ITIMER_PROF
175 #define _SCHED_SIGNAL           SIGPROF
176 #endif
177
178 /*
179  * Priority queues.
180  *
181  * XXX It'd be nice if these were contained in uthread_priority_queue.[ch].
182  */
183 typedef struct pq_list {
184         TAILQ_HEAD(, pthread)   pl_head; /* list of threads at this priority */
185         TAILQ_ENTRY(pq_list)    pl_link; /* link for queue of priority lists */
186         int                     pl_prio; /* the priority of this list */
187         int                     pl_queued; /* is this in the priority queue */
188 } pq_list_t;
189
190 typedef struct pq_queue {
191         TAILQ_HEAD(, pq_list)    pq_queue; /* queue of priority lists */
192         pq_list_t               *pq_lists; /* array of all priority lists */
193         int                      pq_size;  /* number of priority lists */
194 } pq_queue_t;
195
196
197 /*
198  * TailQ initialization values.
199  */
200 #define TAILQ_INITIALIZER       { NULL, NULL }
201
202 /* 
203  * Mutex definitions.
204  */
205 union pthread_mutex_data {
206         void    *m_ptr;
207         int     m_count;
208 };
209
210 struct pthread_mutex {
211         enum pthread_mutextype          m_type;
212         int                             m_protocol;
213         TAILQ_HEAD(mutex_head, pthread) m_queue;
214         struct pthread                  *m_owner;
215         union pthread_mutex_data        m_data;
216         long                            m_flags;
217         int                             m_refcount;
218
219         /*
220          * Used for priority inheritence and protection.
221          *
222          *   m_prio       - For priority inheritence, the highest active
223          *                  priority (threads locking the mutex inherit
224          *                  this priority).  For priority protection, the
225          *                  ceiling priority of this mutex.
226          *   m_saved_prio - mutex owners inherited priority before
227          *                  taking the mutex, restored when the owner
228          *                  unlocks the mutex.
229          */
230         int                             m_prio;
231         int                             m_saved_prio;
232
233         /*
234          * Link for list of all mutexes a thread currently owns.
235          */
236         TAILQ_ENTRY(pthread_mutex)      m_qe;
237
238         /*
239          * Lock for accesses to this structure.
240          */
241         spinlock_t                      lock;
242 };
243
244 /*
245  * Flags for mutexes. 
246  */
247 #define MUTEX_FLAGS_PRIVATE     0x01
248 #define MUTEX_FLAGS_INITED      0x02
249 #define MUTEX_FLAGS_BUSY        0x04
250
251 /*
252  * Static mutex initialization values. 
253  */
254 #define PTHREAD_MUTEX_STATIC_INITIALIZER   \
255         { PTHREAD_MUTEX_DEFAULT, PTHREAD_PRIO_NONE, TAILQ_INITIALIZER, \
256         NULL, { NULL }, 0, 0, 0, 0, TAILQ_INITIALIZER, \
257         _SPINLOCK_INITIALIZER }
258
259 struct pthread_mutex_attr {
260         enum pthread_mutextype  m_type;
261         int                     m_protocol;
262         int                     m_ceiling;
263         long                    m_flags;
264 };
265
266 /* 
267  * Condition variable definitions.
268  */
269 enum pthread_cond_type {
270         COND_TYPE_FAST,
271         COND_TYPE_MAX
272 };
273
274 struct pthread_cond {
275         enum pthread_cond_type          c_type;
276         TAILQ_HEAD(cond_head, pthread)  c_queue;
277         pthread_mutex_t                 c_mutex;
278         void                            *c_data;
279         long                            c_flags;
280
281         /*
282          * Lock for accesses to this structure.
283          */
284         spinlock_t                      lock;
285 };
286
287 struct pthread_cond_attr {
288         enum pthread_cond_type  c_type;
289         long                    c_flags;
290 };
291
292 /*
293  * Flags for condition variables.
294  */
295 #define COND_FLAGS_PRIVATE      0x01
296 #define COND_FLAGS_INITED       0x02
297 #define COND_FLAGS_BUSY         0x04
298
299 /*
300  * Static cond initialization values. 
301  */
302 #define PTHREAD_COND_STATIC_INITIALIZER    \
303         { COND_TYPE_FAST, TAILQ_INITIALIZER, NULL, NULL, \
304         0, _SPINLOCK_INITIALIZER }
305
306 /*
307  * Cleanup definitions.
308  */
309 struct pthread_cleanup {
310         struct pthread_cleanup  *next;
311         void                    (*routine) ();
312         void                    *routine_arg;
313 };
314
315 struct pthread_attr {
316         int     sched_policy;
317         int     sched_inherit;
318         int     sched_interval;
319         int     prio;
320         int     suspend;
321         int     flags;
322         void    *arg_attr;
323         void    (*cleanup_attr) ();
324         void    *stackaddr_attr;
325         size_t  stacksize_attr;
326 };
327
328 /*
329  * Thread creation state attributes.
330  */
331 #define PTHREAD_CREATE_RUNNING                  0
332 #define PTHREAD_CREATE_SUSPENDED                1
333
334 /*
335  * Miscellaneous definitions.
336  */
337 #define PTHREAD_STACK_DEFAULT                   65536
338 #ifdef _PTHREAD_GSTACK
339 /* Size of red zone at the end of each stack. */
340 #define PTHREAD_STACK_GUARD                     4096
341 /* Maximum size of initial thread's stack.  This perhaps deserves to be larger
342  * than the stacks of other threads, since legacy applications are likely to run
343  * almost entirely on this stack. */
344 #define PTHREAD_STACK_INITIAL                   0x100000
345 /* Address immediately beyond the beginning of the initial thread stack. */
346 #if defined(__FreeBSD__)
347 #  if defined(__alpha__)
348 #    define PTHREAD_STACK_TOP                   0x160022000
349 #  else
350 #    define PTHREAD_STACK_TOP                   0xbfbde000
351 #  endif
352 #else
353 #  error "Don't recognize this operating system!"
354 #endif
355 #endif
356 #define PTHREAD_DEFAULT_PRIORITY                64
357 #define PTHREAD_MAX_PRIORITY                    126
358 #define PTHREAD_MIN_PRIORITY                    0
359 #define _POSIX_THREAD_ATTR_STACKSIZE
360
361 /*
362  * Clock resolution in nanoseconds.
363  */
364 #define CLOCK_RES_NSEC                          10000000
365
366 /*
367  * Time slice period in microseconds.
368  */
369 #define TIMESLICE_USEC                          100000
370
371 struct pthread_key {
372         spinlock_t      lock;
373         volatile int    allocated;
374         volatile int    count;
375         void            (*destructor) ();
376 };
377
378 struct pthread_rwlockattr {
379         int             pshared;
380 };
381
382 struct pthread_rwlock {
383         pthread_mutex_t lock;   /* monitor lock */
384         int             state;  /* 0 = idle  >0 = # of readers  -1 = writer */
385         pthread_cond_t  read_signal;
386         pthread_cond_t  write_signal;
387         int             blocked_writers;
388 };
389
390 /*
391  * Thread states.
392  */
393 enum pthread_state {
394         PS_RUNNING,
395         PS_SIGTHREAD,
396         PS_MUTEX_WAIT,
397         PS_COND_WAIT,
398         PS_FDLR_WAIT,
399         PS_FDLW_WAIT,
400         PS_FDR_WAIT,
401         PS_FDW_WAIT,
402         PS_FILE_WAIT,
403         PS_POLL_WAIT,
404         PS_SELECT_WAIT,
405         PS_SLEEP_WAIT,
406         PS_WAIT_WAIT,
407         PS_SIGSUSPEND,
408         PS_SIGWAIT,
409         PS_SPINBLOCK,
410         PS_JOIN,
411         PS_SUSPENDED,
412         PS_DEAD,
413         PS_DEADLOCK,
414         PS_STATE_MAX
415 };
416
417
418 /*
419  * File descriptor locking definitions.
420  */
421 #define FD_READ             0x1
422 #define FD_WRITE            0x2
423 #define FD_RDWR             (FD_READ | FD_WRITE)
424
425 /*
426  * File descriptor table structure.
427  */
428 struct fd_table_entry {
429         /*
430          * Lock for accesses to this file descriptor table
431          * entry. This is passed to _spinlock() to provide atomic
432          * access to this structure. It does *not* represent the
433          * state of the lock on the file descriptor.
434          */
435         spinlock_t              lock;
436         TAILQ_HEAD(, pthread)   r_queue;        /* Read queue.                        */
437         TAILQ_HEAD(, pthread)   w_queue;        /* Write queue.                       */
438         struct pthread          *r_owner;       /* Ptr to thread owning read lock.    */
439         struct pthread          *w_owner;       /* Ptr to thread owning write lock.   */
440         char                    *r_fname;       /* Ptr to read lock source file name  */
441         int                     r_lineno;       /* Read lock source line number.      */
442         char                    *w_fname;       /* Ptr to write lock source file name */
443         int                     w_lineno;       /* Write lock source line number.     */
444         int                     r_lockcount;    /* Count for FILE read locks.         */
445         int                     w_lockcount;    /* Count for FILE write locks.        */
446         int                     flags;          /* Flags used in open.                */
447 };
448
449 struct pthread_poll_data {
450         int     nfds;
451         struct pollfd *fds;
452 };
453
454 union pthread_wait_data {
455         pthread_mutex_t mutex;
456         pthread_cond_t  cond;
457         const sigset_t  *sigwait;       /* Waiting on a signal in sigwait */
458         struct {
459                 short   fd;             /* Used when thread waiting on fd */
460                 short   branch;         /* Line number, for debugging.    */
461                 char    *fname;         /* Source file name for debugging.*/
462         } fd;
463         struct pthread_poll_data * poll_data;
464         spinlock_t      *spinlock;
465 };
466
467 /*
468  * Thread structure.
469  */
470 struct pthread {
471         /*
472          * Magic value to help recognize a valid thread structure
473          * from an invalid one:
474          */
475 #define PTHREAD_MAGIC           ((u_int32_t) 0xd09ba115)
476         u_int32_t               magic;
477         char                    *name;
478
479         /*
480          * Lock for accesses to this thread structure.
481          */
482         spinlock_t              lock;
483
484         /* Queue entry for list of all threads: */
485         TAILQ_ENTRY(pthread)    tle;
486
487         /* Queue entry for list of dead threads: */
488         TAILQ_ENTRY(pthread)    dle;
489
490         /*
491          * Thread start routine, argument, stack pointer and thread
492          * attributes.
493          */
494         void                    *(*start_routine)(void *);
495         void                    *arg;
496         void                    *stack;
497         struct pthread_attr     attr;
498
499 #if (defined(__FreeBSD__) || defined(__NetBSD__)) && defined(__i386__)
500         /*
501          * Saved floating point registers on systems where they are not
502          * saved in the signal context.
503          */
504         char    saved_fp[108];
505 #endif
506
507         /*
508          * Saved signal context used in call to sigreturn by
509          * _thread_kern_sched if sig_saved is TRUE.
510          */
511         struct  sigcontext saved_sigcontext;
512
513         /* 
514          * Saved jump buffer used in call to longjmp by _thread_kern_sched
515          * if sig_saved is FALSE.
516          */
517         jmp_buf saved_jmp_buf;
518
519         /*
520          * TRUE if the last state saved was a signal context. FALSE if the
521          * last state saved was a jump buffer.
522          */
523         int     sig_saved;
524
525         /*
526          * Current signal mask and pending signals.
527          */
528         sigset_t        sigmask;
529         sigset_t        sigpend;
530
531         /* Thread state: */
532         enum pthread_state      state;
533
534         /* Time that this thread was last made active. */
535         struct  timeval         last_active;
536
537         /* Time that this thread was last made inactive. */
538         struct  timeval         last_inactive;
539
540         /*
541          * Number of microseconds accumulated by this thread when
542          * time slicing is active.
543          */
544         long    slice_usec;
545
546         /*
547          * Incremental priority accumulated by thread while it is ready to
548          * run but is denied being run.
549          */
550         int     inc_prio;
551
552         /*
553          * Time to wake up thread. This is used for sleeping threads and
554          * for any operation which may time out (such as select).
555          */
556         struct timespec wakeup_time;
557
558         /* TRUE if operation has timed out. */
559         int     timeout;
560
561         /*
562          * Error variable used instead of errno. The function __error()
563          * returns a pointer to this. 
564          */
565         int     error;
566
567         /* Join queue head and link for waiting threads: */
568         TAILQ_HEAD(join_head, pthread)  join_queue;
569
570         /*
571          * The current thread can belong to only one scheduling queue at
572          * a time (ready or waiting queue).  It can also belong to (only)
573          * one of:
574          *
575          *   o A queue of threads waiting for a mutex
576          *   o A queue of threads waiting for a condition variable
577          *   o A queue of threads waiting for another thread to terminate
578          *     (the join queue above)
579          *   o A queue of threads waiting for a file descriptor lock
580          *   o A queue of threads needing work done by the kernel thread
581          *     (waiting for a spinlock or file I/O)
582          *
583          * Use pqe for the scheduling queue link (both ready and waiting),
584          * and qe for other links.
585          */
586
587         /* Priority queue entry for this thread: */
588         TAILQ_ENTRY(pthread)    pqe;
589
590         /* Queue entry for this thread: */
591         TAILQ_ENTRY(pthread)    qe;
592
593         /* Wait data. */
594         union pthread_wait_data data;
595
596         /*
597          * Allocated for converting select into poll.
598          */
599         struct pthread_poll_data poll_data;
600
601         /*
602          * Set to TRUE if a blocking operation was
603          * interrupted by a signal:
604          */
605         int             interrupted;
606
607         /* Signal number when in state PS_SIGWAIT: */
608         int             signo;
609
610         /*
611          * Set to non-zero when this thread has deferred signals.
612          * We allow for recursive deferral.
613          */
614         int             sig_defer_count;
615
616         /*
617          * Set to TRUE if this thread should yield after undeferring
618          * signals.
619          */
620         int             yield_on_sig_undefer;
621
622         /* Miscellaneous data. */
623         int             flags;
624 #define PTHREAD_FLAGS_PRIVATE   0x0001
625 #define PTHREAD_EXITING         0x0002
626 #define PTHREAD_FLAGS_IN_CONDQ  0x0004  /* in condition queue using qe link*/
627 #define PTHREAD_FLAGS_IN_WORKQ  0x0008  /* in work queue using qe link */
628 #define PTHREAD_FLAGS_IN_WAITQ  0x0010  /* in waiting queue using pqe link*/
629 #define PTHREAD_FLAGS_IN_PRIOQ  0x0020  /* in priority queue using pqe link*/
630 #define PTHREAD_FLAGS_TRACE     0x0040  /* for debugging purposes */
631
632         /*
633          * Base priority is the user setable and retrievable priority
634          * of the thread.  It is only affected by explicit calls to
635          * set thread priority and upon thread creation via a thread
636          * attribute or default priority.
637          */
638         char            base_priority;
639
640         /*
641          * Inherited priority is the priority a thread inherits by
642          * taking a priority inheritence or protection mutex.  It
643          * is not affected by base priority changes.  Inherited
644          * priority defaults to and remains 0 until a mutex is taken
645          * that is being waited on by any other thread whose priority
646          * is non-zero.
647          */
648         char            inherited_priority;
649
650         /*
651          * Active priority is always the maximum of the threads base
652          * priority and inherited priority.  When there is a change
653          * in either the base or inherited priority, the active
654          * priority must be recalculated.
655          */
656         char            active_priority;
657
658         /* Number of priority ceiling or protection mutexes owned. */
659         int             priority_mutex_count;
660
661         /*
662          * Queue of currently owned mutexes.
663          */
664         TAILQ_HEAD(, pthread_mutex)     mutexq;
665
666         void            *ret;
667         const void      **specific_data;
668         int             specific_data_count;
669
670         /* Cleanup handlers Link List */
671         struct pthread_cleanup *cleanup;
672         char                    *fname; /* Ptr to source file name  */
673         int                     lineno; /* Source line number.      */
674 };
675
676 #ifdef _PTHREAD_GSTACK
677 /* Spare thread stack. */
678 struct stack {
679         SLIST_ENTRY(stack)      qe; /* Queue entry for this stack. */
680 };
681 #endif
682
683 /*
684  * Global variables for the uthread kernel.
685  */
686
687 /* Kernel thread structure used when there are no running threads: */
688 SCLASS struct pthread   _thread_kern_thread;
689
690 /* Ptr to the thread structure for the running thread: */
691 SCLASS struct pthread   * volatile _thread_run
692 #ifdef GLOBAL_PTHREAD_PRIVATE
693 = &_thread_kern_thread;
694 #else
695 ;
696 #endif
697
698 /* Ptr to the thread structure for the last user thread to run: */
699 SCLASS struct pthread   * volatile _last_user_thread
700 #ifdef GLOBAL_PTHREAD_PRIVATE
701 = &_thread_kern_thread;
702 #else
703 ;
704 #endif
705
706 /*
707  * Ptr to the thread running in single-threaded mode or NULL if
708  * running multi-threaded (default POSIX behaviour).
709  */
710 SCLASS struct pthread   * volatile _thread_single
711 #ifdef GLOBAL_PTHREAD_PRIVATE
712 = NULL;
713 #else
714 ;
715 #endif
716
717 /* List of all threads: */
718 SCLASS TAILQ_HEAD(, pthread)    _thread_list
719 #ifdef GLOBAL_PTHREAD_PRIVATE
720 = TAILQ_HEAD_INITIALIZER(_thread_list);
721 #else
722 ;
723 #endif
724
725 /*
726  * Array of kernel pipe file descriptors that are used to ensure that
727  * no signals are missed in calls to _select.
728  */
729 SCLASS int              _thread_kern_pipe[2]
730 #ifdef GLOBAL_PTHREAD_PRIVATE
731 = {
732         -1,
733         -1
734 };
735 #else
736 ;
737 #endif
738 SCLASS int              volatile _queue_signals
739 #ifdef GLOBAL_PTHREAD_PRIVATE
740 = 0;
741 #else
742 ;
743 #endif
744 SCLASS int              _thread_kern_in_sched
745 #ifdef GLOBAL_PTHREAD_PRIVATE
746 = 0;
747 #else
748 ;
749 #endif
750
751 /* Last time that an incremental priority update was performed: */
752 SCLASS struct timeval   kern_inc_prio_time
753 #ifdef GLOBAL_PTHREAD_PRIVATE
754 = { 0, 0 };
755 #else
756 ;
757 #endif
758
759 /* Dead threads: */
760 SCLASS TAILQ_HEAD(, pthread) _dead_list
761 #ifdef GLOBAL_PTHREAD_PRIVATE
762 = TAILQ_HEAD_INITIALIZER(_dead_list);
763 #else
764 ;
765 #endif
766
767 /* Initial thread: */
768 SCLASS struct pthread *_thread_initial
769 #ifdef GLOBAL_PTHREAD_PRIVATE
770 = NULL;
771 #else
772 ;
773 #endif
774
775 /* Default thread attributes: */
776 SCLASS struct pthread_attr pthread_attr_default
777 #ifdef GLOBAL_PTHREAD_PRIVATE
778 = { SCHED_RR, 0, TIMESLICE_USEC, PTHREAD_DEFAULT_PRIORITY, PTHREAD_CREATE_RUNNING,
779         PTHREAD_CREATE_JOINABLE, NULL, NULL, NULL, PTHREAD_STACK_DEFAULT };
780 #else
781 ;
782 #endif
783
784 /* Default mutex attributes: */
785 SCLASS struct pthread_mutex_attr pthread_mutexattr_default
786 #ifdef GLOBAL_PTHREAD_PRIVATE
787 = { PTHREAD_MUTEX_DEFAULT, PTHREAD_PRIO_NONE, 0, 0 };
788 #else
789 ;
790 #endif
791
792 /* Default condition variable attributes: */
793 SCLASS struct pthread_cond_attr pthread_condattr_default
794 #ifdef GLOBAL_PTHREAD_PRIVATE
795 = { COND_TYPE_FAST, 0 };
796 #else
797 ;
798 #endif
799
800 /*
801  * Standard I/O file descriptors need special flag treatment since
802  * setting one to non-blocking does all on *BSD. Sigh. This array
803  * is used to store the initial flag settings.
804  */
805 SCLASS int      _pthread_stdio_flags[3];
806
807 /* File table information: */
808 SCLASS struct fd_table_entry **_thread_fd_table
809 #ifdef GLOBAL_PTHREAD_PRIVATE
810 = NULL;
811 #else
812 ;
813 #endif
814
815 /* Table for polling file descriptors: */
816 SCLASS struct pollfd *_thread_pfd_table
817 #ifdef GLOBAL_PTHREAD_PRIVATE
818 = NULL;
819 #else
820 ;
821 #endif
822
823 SCLASS const int dtablecount
824 #ifdef GLOBAL_PTHREAD_PRIVATE
825 = 4096/sizeof(struct fd_table_entry);
826 #else
827 ;
828 #endif
829 SCLASS int    _thread_dtablesize        /* Descriptor table size.           */
830 #ifdef GLOBAL_PTHREAD_PRIVATE
831 = 1024;
832 #else
833 ;
834 #endif
835
836 SCLASS int    _clock_res_nsec           /* Clock resolution in nsec.    */
837 #ifdef GLOBAL_PTHREAD_PRIVATE
838 = CLOCK_RES_NSEC;
839 #else
840 ;
841 #endif
842
843 /* Garbage collector mutex and condition variable. */
844 SCLASS  pthread_mutex_t _gc_mutex
845 #ifdef GLOBAL_PTHREAD_PRIVATE
846 = NULL
847 #endif
848 ;
849 SCLASS  pthread_cond_t  _gc_cond
850 #ifdef GLOBAL_PTHREAD_PRIVATE
851 = NULL
852 #endif
853 ;
854
855 /*
856  * Array of signal actions for this process.
857  */
858 struct  sigaction _thread_sigact[NSIG];
859
860 /*
861  * Scheduling queues:
862  */
863 SCLASS pq_queue_t               _readyq;
864 SCLASS TAILQ_HEAD(, pthread)    _waitingq;
865
866 /*
867  * Work queue:
868  */
869 SCLASS TAILQ_HEAD(, pthread)    _workq;
870
871 /* Tracks the number of threads blocked while waiting for a spinlock. */
872 SCLASS  volatile int    _spinblock_count
873 #ifdef GLOBAL_PTHREAD_PRIVATE
874 = 0
875 #endif
876 ;
877
878 /* Indicates that the signal queue needs to be checked. */
879 SCLASS  volatile int    _sigq_check_reqd
880 #ifdef GLOBAL_PTHREAD_PRIVATE
881 = 0
882 #endif
883 ;
884
885 /* Thread switch hook. */
886 SCLASS pthread_switch_routine_t _sched_switch_hook
887 #ifdef GLOBAL_PTHREAD_PRIVATE
888 = NULL
889 #endif
890 ;
891
892 #ifdef _PTHREAD_GSTACK
893 /* Spare stack queue.  Stacks of default size are cached in order to reduce
894  * thread creation time.  Spare stacks are used in LIFO order to increase cache
895  * locality. */
896 SCLASS SLIST_HEAD(, stack)      _stackq;
897
898 /* Base address of next unallocated default-size stack.  Stacks are allocated
899  * contiguously, starting below the beginning of the main stack.  When a new
900  * stack is created, a guard page is created just above it in order to (usually)
901  * detect attempts by the adjacent stack to trounce the next thread stack. */
902 SCLASS void *   _next_stack
903 #ifdef GLOBAL_PTHREAD_PRIVATE
904 /* main stack top   - main stack size       - stack size            - (red zone + main stack red zone) */
905 = (void *) PTHREAD_STACK_TOP - PTHREAD_STACK_INITIAL - PTHREAD_STACK_DEFAULT - (2 * PTHREAD_STACK_GUARD)
906 #endif
907 ;
908 #endif
909
910 /* Used for _PTHREADS_INVARIANTS checking. */
911 SCLASS int      _thread_kern_new_state
912 #ifdef GLOBAL_PTHREAD_PRIVATE
913 = 0
914 #endif
915 ;
916
917 /* Undefine the storage class specifier: */
918 #undef  SCLASS
919
920 #ifdef  _LOCK_DEBUG
921 #define _FD_LOCK(_fd,_type,_ts)         _thread_fd_lock_debug(_fd, _type, \
922                                                 _ts, __FILE__, __LINE__)
923 #define _FD_UNLOCK(_fd,_type)           _thread_fd_unlock_debug(_fd, _type, \
924                                                 __FILE__, __LINE__)
925 #else
926 #define _FD_LOCK(_fd,_type,_ts)         _thread_fd_lock(_fd, _type, _ts)
927 #define _FD_UNLOCK(_fd,_type)           _thread_fd_unlock(_fd, _type)
928 #endif
929
930 /*
931  * Function prototype definitions.
932  */
933 __BEGIN_DECLS
934 char    *__ttyname_basic(int);
935 char    *__ttyname_r_basic(int, char *, size_t);
936 char    *ttyname_r(int, char *, size_t);
937 int     _find_dead_thread(pthread_t);
938 int     _find_thread(pthread_t);
939 int     _thread_create(pthread_t *,const pthread_attr_t *,void *(*start_routine)(void *),void *,pthread_t);
940 int     _thread_fd_lock(int, int, struct timespec *);
941 int     _thread_fd_lock_debug(int, int, struct timespec *,char *fname,int lineno);
942 void    _dispatch_signals(void);
943 void    _thread_signal(pthread_t, int);
944 int     _mutex_cv_lock(pthread_mutex_t *);
945 int     _mutex_cv_unlock(pthread_mutex_t *);
946 int     _mutex_reinit(pthread_mutex_t *);
947 void    _mutex_notify_priochange(struct pthread *);
948 int     _cond_reinit(pthread_cond_t *);
949 int     _pq_alloc(struct pq_queue *, int, int);
950 int     _pq_init(struct pq_queue *);
951 void    _pq_remove(struct pq_queue *pq, struct pthread *);
952 void    _pq_insert_head(struct pq_queue *pq, struct pthread *);
953 void    _pq_insert_tail(struct pq_queue *pq, struct pthread *);
954 struct pthread *_pq_first(struct pq_queue *pq);
955 #if defined(_PTHREADS_INVARIANTS)
956 void    _waitq_insert(pthread_t pthread);
957 void    _waitq_remove(pthread_t pthread);
958 void    _waitq_setactive(void);
959 void    _waitq_clearactive(void);
960 #endif
961 void    _thread_exit(char *, int, char *);
962 void    _thread_fd_unlock(int, int);
963 void    _thread_fd_unlock_debug(int, int, char *, int);
964 void    *_thread_cleanup(pthread_t);
965 void    _thread_cleanupspecific(void);
966 void    _thread_dump_info(void);
967 void    _thread_init(void);
968 void    _thread_kern_sched(struct sigcontext *);
969 void    _thread_kern_sched_state(enum pthread_state,char *fname,int lineno);
970 void    _thread_kern_sched_state_unlock(enum pthread_state state,
971             spinlock_t *lock, char *fname, int lineno);
972 void    _thread_kern_set_timeout(struct timespec *);
973 void    _thread_kern_sig_defer(void);
974 void    _thread_kern_sig_undefer(void);
975 void    _thread_sig_handler(int, int, struct sigcontext *);
976 void    _thread_sig_handle(int, struct sigcontext *);
977 void    _thread_sig_init(void);
978 void    _thread_start(void);
979 void    _thread_start_sig_handler(void);
980 void    _thread_seterrno(pthread_t,int);
981 int     _thread_fd_table_init(int fd);
982 pthread_addr_t _thread_gc(pthread_addr_t);
983
984 /* #include <signal.h> */
985 int     _thread_sys_sigaction(int, const struct sigaction *, struct sigaction *);
986 int     _thread_sys_sigpending(sigset_t *);
987 int     _thread_sys_sigprocmask(int, const sigset_t *, sigset_t *);
988 int     _thread_sys_sigsuspend(const sigset_t *);
989 int     _thread_sys_siginterrupt(int, int);
990 int     _thread_sys_sigpause(int);
991 int     _thread_sys_sigreturn(struct sigcontext *);
992 int     _thread_sys_sigstack(const struct sigstack *, struct sigstack *);
993 int     _thread_sys_sigvec(int, struct sigvec *, struct sigvec *);
994 void    _thread_sys_psignal(unsigned int, const char *);
995 void    (*_thread_sys_signal(int, void (*)(int)))(int);
996
997 /* #include <sys/stat.h> */
998 #ifdef  _SYS_STAT_H_
999 int     _thread_sys_fchmod(int, mode_t);
1000 int     _thread_sys_fstat(int, struct stat *);
1001 int     _thread_sys_fchflags(int, u_long);
1002 #endif
1003
1004 /* #include <sys/mount.h> */
1005 #ifdef  _SYS_MOUNT_H_
1006 int     _thread_sys_fstatfs(int, struct statfs *);
1007 #endif
1008 int     _thread_sys_pipe(int *);
1009
1010 /* #include <sys/socket.h> */
1011 #ifdef  _SYS_SOCKET_H_
1012 int     _thread_sys_accept(int, struct sockaddr *, int *);
1013 int     _thread_sys_bind(int, const struct sockaddr *, int);
1014 int     _thread_sys_connect(int, const struct sockaddr *, int);
1015 int     _thread_sys_getpeername(int, struct sockaddr *, int *);
1016 int     _thread_sys_getsockname(int, struct sockaddr *, int *);
1017 int     _thread_sys_getsockopt(int, int, int, void *, int *);
1018 int     _thread_sys_listen(int, int);
1019 int     _thread_sys_setsockopt(int, int, int, const void *, int);
1020 int     _thread_sys_shutdown(int, int);
1021 int     _thread_sys_socket(int, int, int);
1022 int     _thread_sys_socketpair(int, int, int, int *);
1023 ssize_t _thread_sys_recv(int, void *, size_t, int);
1024 ssize_t _thread_sys_recvfrom(int, void *, size_t, int, struct sockaddr *, int *);
1025 ssize_t _thread_sys_recvmsg(int, struct msghdr *, int);
1026 ssize_t _thread_sys_send(int, const void *, size_t, int);
1027 ssize_t _thread_sys_sendmsg(int, const struct msghdr *, int);
1028 ssize_t _thread_sys_sendto(int, const void *,size_t, int, const struct sockaddr *, int);
1029 #endif
1030
1031 /* #include <stdio.h> */
1032 #ifdef  _STDIO_H_
1033 FILE    *_thread_sys_fdopen(int, const char *);
1034 FILE    *_thread_sys_fopen(const char *, const char *);
1035 FILE    *_thread_sys_freopen(const char *, const char *, FILE *);
1036 FILE    *_thread_sys_popen(const char *, const char *);
1037 FILE    *_thread_sys_tmpfile(void);
1038 char    *_thread_sys_ctermid(char *);
1039 char    *_thread_sys_cuserid(char *);
1040 char    *_thread_sys_fgetln(FILE *, size_t *);
1041 char    *_thread_sys_fgets(char *, int, FILE *);
1042 char    *_thread_sys_gets(char *);
1043 char    *_thread_sys_tempnam(const char *, const char *);
1044 char    *_thread_sys_tmpnam(char *);
1045 int     _thread_sys_fclose(FILE *);
1046 int     _thread_sys_feof(FILE *);
1047 int     _thread_sys_ferror(FILE *);
1048 int     _thread_sys_fflush(FILE *);
1049 int     _thread_sys_fgetc(FILE *);
1050 int     _thread_sys_fgetpos(FILE *, fpos_t *);
1051 int     _thread_sys_fileno(FILE *);
1052 int     _thread_sys_fprintf(FILE *, const char *, ...);
1053 int     _thread_sys_fpurge(FILE *);
1054 int     _thread_sys_fputc(int, FILE *);
1055 int     _thread_sys_fputs(const char *, FILE *);
1056 int     _thread_sys_fscanf(FILE *, const char *, ...);
1057 int     _thread_sys_fseek(FILE *, long, int);
1058 int     _thread_sys_fsetpos(FILE *, const fpos_t *);
1059 int     _thread_sys_getc(FILE *);
1060 int     _thread_sys_getchar(void);
1061 int     _thread_sys_getw(FILE *);
1062 int     _thread_sys_pclose(FILE *);
1063 int     _thread_sys_printf(const char *, ...);
1064 int     _thread_sys_putc(int, FILE *);
1065 int     _thread_sys_putchar(int);
1066 int     _thread_sys_puts(const char *);
1067 int     _thread_sys_putw(int, FILE *);
1068 int     _thread_sys_remove(const char *);
1069 int     _thread_sys_rename (const char *, const char *);
1070 int     _thread_sys_scanf(const char *, ...);
1071 int     _thread_sys_setlinebuf(FILE *);
1072 int     _thread_sys_setvbuf(FILE *, char *, int, size_t);
1073 int     _thread_sys_snprintf(char *, size_t, const char *, ...);
1074 int     _thread_sys_sprintf(char *, const char *, ...);
1075 int     _thread_sys_sscanf(const char *, const char *, ...);
1076 int     _thread_sys_ungetc(int, FILE *);
1077 int     _thread_sys_vfprintf(FILE *, const char *, _BSD_VA_LIST_);
1078 int     _thread_sys_vprintf(const char *, _BSD_VA_LIST_);
1079 int     _thread_sys_vscanf(const char *, _BSD_VA_LIST_);
1080 int     _thread_sys_vsnprintf(char *, size_t, const char *, _BSD_VA_LIST_);
1081 int     _thread_sys_vsprintf(char *, const char *, _BSD_VA_LIST_);
1082 int     _thread_sys_vsscanf(const char *, const char *, _BSD_VA_LIST_);
1083 long    _thread_sys_ftell(FILE *);
1084 size_t  _thread_sys_fread(void *, size_t, size_t, FILE *);
1085 size_t  _thread_sys_fwrite(const void *, size_t, size_t, FILE *);
1086 void    _thread_sys_clearerr(FILE *);
1087 void    _thread_sys_perror(const char *);
1088 void    _thread_sys_rewind(FILE *);
1089 void    _thread_sys_setbuf(FILE *, char *);
1090 void    _thread_sys_setbuffer(FILE *, char *, int);
1091 #endif
1092
1093 /* #include <unistd.h> */
1094 #ifdef  _UNISTD_H_
1095 char    *_thread_sys_ttyname(int);
1096 int     _thread_sys_close(int);
1097 int     _thread_sys_dup(int);
1098 int     _thread_sys_dup2(int, int);
1099 int     _thread_sys_exect(const char *, char * const *, char * const *);
1100 int     _thread_sys_execve(const char *, char * const *, char * const *);
1101 int     _thread_sys_fchdir(int);
1102 int     _thread_sys_fchown(int, uid_t, gid_t);
1103 int     _thread_sys_fsync(int);
1104 int     _thread_sys_ftruncate(int, off_t);
1105 int     _thread_sys_pause(void);
1106 int     _thread_sys_pipe(int *);
1107 int     _thread_sys_select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
1108 off_t   _thread_sys_lseek(int, off_t, int);
1109 pid_t   _thread_sys_fork(void);
1110 pid_t   _thread_sys_tcgetpgrp(int);
1111 ssize_t _thread_sys_read(int, void *, size_t);
1112 ssize_t _thread_sys_write(int, const void *, size_t);
1113 void    _thread_sys__exit(int);
1114 #endif
1115
1116 /* #include <fcntl.h> */
1117 #ifdef  _SYS_FCNTL_H_
1118 int     _thread_sys_creat(const char *, mode_t);
1119 int     _thread_sys_fcntl(int, int, ...);
1120 int     _thread_sys_flock(int, int);
1121 int     _thread_sys_open(const char *, int, ...);
1122 #endif
1123
1124 /* #include <sys/ioctl.h> */
1125 #ifdef  _SYS_IOCTL_H_
1126 int     _thread_sys_ioctl(int, unsigned long, ...);
1127 #endif
1128
1129 /* #include <dirent.h> */
1130 #ifdef  _DIRENT_H_
1131 DIR     *___thread_sys_opendir2(const char *, int);
1132 DIR     *_thread_sys_opendir(const char *);
1133 int     _thread_sys_alphasort(const void *, const void *);
1134 int     _thread_sys_scandir(const char *, struct dirent ***,
1135         int (*)(struct dirent *), int (*)(const void *, const void *));
1136 int     _thread_sys_closedir(DIR *);
1137 int     _thread_sys_getdirentries(int, char *, int, long *);
1138 long    _thread_sys_telldir(const DIR *);
1139 struct  dirent *_thread_sys_readdir(DIR *);
1140 void    _thread_sys_rewinddir(DIR *);
1141 void    _thread_sys_seekdir(DIR *, long);
1142 #endif
1143
1144 /* #include <sys/uio.h> */
1145 #ifdef  _SYS_UIO_H_
1146 ssize_t _thread_sys_readv(int, const struct iovec *, int);
1147 ssize_t _thread_sys_writev(int, const struct iovec *, int);
1148 #endif
1149
1150 /* #include <sys/wait.h> */
1151 #ifdef  WNOHANG
1152 pid_t   _thread_sys_wait(int *);
1153 pid_t   _thread_sys_waitpid(pid_t, int *, int);
1154 pid_t   _thread_sys_wait3(int *, int, struct rusage *);
1155 pid_t   _thread_sys_wait4(pid_t, int *, int, struct rusage *);
1156 #endif
1157
1158 /* #include <poll.h> */
1159 #ifdef _SYS_POLL_H_
1160 int     _thread_sys_poll(struct pollfd *, unsigned, int);
1161 #endif
1162 __END_DECLS
1163
1164 #endif  /* !_PTHREAD_PRIVATE_H */