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