Home | History | Annotate | Line # | Download | only in libpthread
pthread_int.h revision 1.1.2.2
      1 /* $Id: pthread_int.h,v 1.1.2.2 2001/07/13 02:09:31 nathanw Exp $ */
      2 /* Copyright */
      3 
      4 #ifndef _LIB_PTHREAD_INT_H
      5 #define _LIB_PTHREAD_INT_H
      6 
      7 #include <sa.h>
      8 #include <signal.h>
      9 
     10 #define PTHREAD__DEBUG
     11 
     12 #include "pthread_types.h"
     13 
     14 
     15 struct	pthread_st {
     16 	unsigned int	pt_magic;
     17 
     18 	int	pt_type;	/* normal, upcall, or idle */
     19 	int	pt_state;	/* running, blocked, etc. */
     20 	int	pt_flags;
     21 	int	pt_spinlocks;	/* Number of spinlocks held. */
     22 
     23 	/* Entry on the run queue */
     24 	PTQ_ENTRY(pthread_st)	pt_runq;
     25 	/* Entry on the list of all threads */
     26 	PTQ_ENTRY(pthread_st)	pt_allq;
     27 	/* Entry on the sleep queue (xxx should be same as run queue?) */
     28 	PTQ_ENTRY(pthread_st)	pt_sleep;
     29 
     30 	stack_t		pt_stack;	/* Our stack */
     31 	ucontext_t	*pt_uc;		/* Saved context when we're stopped */
     32 
     33 	sigset_t	pt_sigmask;	/* Signals we won't take. */
     34 	sigset_t	pt_siglist;	/* Signals pending for us. */
     35 	pt_spin_t	pt_siglock;	/* Lock on above */
     36 
     37 	void *		pt_exitval;	/* Read by pthread_join() */
     38 
     39 	/* Other threads trying to pthread_join() us. */
     40 	struct pt_queue_t	pt_joiners;
     41 	/* Lock for above, and for changing pt_state to ZOMBIE or DEAD,
     42 	 * and for setting the DETACHED flag
     43 	 */
     44 	pt_spin_t 		pt_join_lock;
     45 
     46 	/* Thread we were going to switch to before we were preempted
     47 	 * ourselves. Will be used by the upcall that's continuing us.
     48 	 */
     49 	pthread_t	pt_switchto;
     50 	ucontext_t*	pt_switchtouc;
     51 
     52 	/* Threads that are preempted with spinlocks held will be
     53 	 * continued until they unlock their spinlock. When they do
     54 	 * so, they should jump ship to the thread pointed to by
     55 	 * pt_next.
     56 	 */
     57 	pthread_t	pt_next;
     58 
     59 	/* The upcall that is continuing this thread */
     60 	pthread_t	pt_parent;
     61 
     62 	/* A queue lock that this thread held while trying to
     63 	 * context switch to another process.
     64 	 */
     65 	pt_spin_t*	pt_heldlock;
     66 
     67 #ifdef PTHREAD__DEBUG
     68 	int	blocks;
     69 	int	preempts;
     70 	int	rescheds;
     71 #endif
     72 };
     73 
     74 
     75 
     76 /* Thread types */
     77 #define PT_THREAD_NORMAL	1
     78 #define PT_THREAD_UPCALL	2
     79 #define PT_THREAD_IDLE		3
     80 
     81 /* Thread states */
     82 #define PT_STATE_RUNNABLE	1
     83 #define PT_STATE_BLOCKED	2
     84 #define PT_STATE_ZOMBIE		3
     85 #define PT_STATE_DEAD		4
     86 #define PT_STATE_RECYCLABLE	5
     87 
     88 /* Flag values */
     89 
     90 #define PT_FLAG_DETACHED	0x0001
     91 #define PT_FLAG_IDLED		0x0002
     92 
     93 #define PT_MAGIC	0xBABCAAAA
     94 #define PT_DEAD		0xDEADBEEF
     95 
     96 #define PT_ATTR_MAGIC	0x5555FACE
     97 #define PT_ATTR_DEAD	0xFACEDEAD
     98 
     99 #define PT_STACKSIZE	(1<<20)
    100 #define PT_STACKMASK	(PT_STACKSIZE-1)
    101 
    102 #define PT_UPCALLSTACKS	16
    103 
    104 #define IDLESPINS	1000
    105 
    106 /* Utility functions */
    107 
    108 void*	pthread__malloc(size_t size);
    109 void	pthread__free(void *ptr);
    110 
    111 /* Set up/clean up a thread's basic state. */
    112 void pthread__initthread(pthread_t t);
    113 
    114 /* Go do something else. Don't go back on the run queue */
    115 void	pthread__block(pthread_t self, pt_spin_t* queuelock);
    116 /* Put a thread back on the run queue */
    117 void	pthread__sched(pthread_t self, pthread_t thread);
    118 void	pthread__sched_idle(pthread_t self, pthread_t thread);
    119 void	pthread__sched_idle2(pthread_t self);
    120 
    121 void	pthread__sched_bulk(pthread_t self, pthread_t qhead);
    122 
    123 void	pthread__idle(void);
    124 
    125 /* Get the next thread */
    126 pthread_t pthread__next(pthread_t self);
    127 
    128 int	pthread__stackalloc(pthread_t *t);
    129 void	pthread__initmain(pthread_t *t);
    130 
    131 void	pthread__sa_start(void);
    132 void	pthread__sa_recycle(pthread_t old, pthread_t new);
    133 
    134 #include "pthread_md.h"
    135 
    136 /* Stack location of pointer to a particular thread */
    137 #define pthread__id(sp) \
    138 	((pthread_t) (((vaddr_t)(sp)) & ~PT_STACKMASK))
    139 
    140 #define pthread__self() (pthread__id(pthread__sp()))
    141 
    142 void	pthread__upcall_switch(pthread_t self, pthread_t next);
    143 void	pthread__switch(pthread_t self, pthread_t next, int locks);
    144 void	pthread__locked_switch(pthread_t self, pthread_t next,
    145     pt_spin_t *lock);
    146 
    147 void	pthread_lockinit(pt_spin_t *lock);
    148 void	pthread_spinlock(pthread_t thread, pt_spin_t *lock);
    149 int	pthread_spintrylock(pthread_t thread, pt_spin_t *lock);
    150 void	pthread_spinunlock(pthread_t thread, pt_spin_t *lock);
    151 
    152 void pthread__signal(pthread_t t, int sig, int code);
    153 
    154 
    155 
    156 #define PTHREADD_CREATE		0
    157 #define PTHREADD_IDLE		1
    158 #define PTHREADD_UPCALLS	2
    159 #define PTHREADD_UP_BLOCK       3
    160 #define PTHREADD_UP_NEW		4
    161 #define PTHREADD_UP_PREEMPT	5
    162 #define PTHREADD_UP_UNBLOCK	6
    163 #define PTHREADD_UP_SIGNAL	7
    164 #define PTHREADD_SPINLOCKS	8
    165 #define PTHREADD_SPINUNLOCKS	9
    166 #define PTHREADD_SPINPREEMPT	10
    167 #define PTHREADD_RESOLVELOCKS	11
    168 #define PTHREADD_SWITCHTO	12
    169 #define PTHREADD_NCOUNTERS	13
    170 
    171 #ifdef PTHREAD__DEBUG
    172 
    173 extern int pthread__debug_counters[PTHREADD_NCOUNTERS];
    174 
    175 #define PTHREADD_ADD(x) (pthread__debug_counters[(x)]++)
    176 
    177 #else /* PTHREAD_DEBUG */
    178 
    179 #define PTHREADD_ADD(x)
    180 
    181 #endif /* PTHREAD_DEBUG */
    182 
    183 
    184 
    185 
    186 #endif /* _LIB_PTHREAD_INT_H */
    187 
    188