1*433d6423SLionel Sambuc /* EXTERN should be extern, except for the allocate file */ 2*433d6423SLionel Sambuc #ifdef ALLOCATE 3*433d6423SLionel Sambuc #undef EXTERN 4*433d6423SLionel Sambuc #define EXTERN 5*433d6423SLionel Sambuc #endif 6*433d6423SLionel Sambuc 7*433d6423SLionel Sambuc #include <assert.h> 8*433d6423SLionel Sambuc #include <sys/types.h> 9*433d6423SLionel Sambuc #include <sys/signal.h> 10*433d6423SLionel Sambuc 11*433d6423SLionel Sambuc #define MTHREAD_RND_SCHED 0 /* Enable/disable random scheduling */ 12*433d6423SLionel Sambuc #define NO_THREADS 4 13*433d6423SLionel Sambuc #define MAX_THREAD_POOL 1024 14*433d6423SLionel Sambuc #define STACKSZ 4096 15*433d6423SLionel Sambuc #define MAIN_THREAD (-1) 16*433d6423SLionel Sambuc #define NO_THREAD (-2) 17*433d6423SLionel Sambuc #define isokthreadid(i) (i == MAIN_THREAD || (i >= 0 && i < no_threads)) 18*433d6423SLionel Sambuc #define MTHREAD_INIT_MAGIC 0xca11ab1e 19*433d6423SLionel Sambuc #define MTHREAD_NOT_INUSE 0xdefec7 20*433d6423SLionel Sambuc 21*433d6423SLionel Sambuc typedef enum { 22*433d6423SLionel Sambuc MS_CONDITION, MS_DEAD, MS_EXITING, MS_MUTEX, MS_RUNNABLE, MS_NEEDRESET 23*433d6423SLionel Sambuc } mthread_state_t; 24*433d6423SLionel Sambuc 25*433d6423SLionel Sambuc struct __mthread_tcb { 26*433d6423SLionel Sambuc mthread_thread_t m_tid; /* My own ID */ 27*433d6423SLionel Sambuc mthread_state_t m_state; /* Thread state */ 28*433d6423SLionel Sambuc struct __mthread_attr m_attr; /* Thread attributes */ 29*433d6423SLionel Sambuc struct __mthread_cond *m_cond; /* Condition variable that this thread 30*433d6423SLionel Sambuc * might be blocking on */ 31*433d6423SLionel Sambuc void *(*m_proc)(void *); /* Procedure to run */ 32*433d6423SLionel Sambuc void *m_arg; /* Argument passed to procedure */ 33*433d6423SLionel Sambuc void *m_result; /* Result after procedure returns */ 34*433d6423SLionel Sambuc mthread_cond_t m_exited; /* Condition variable signaling this 35*433d6423SLionel Sambuc * thread has ended */ 36*433d6423SLionel Sambuc mthread_mutex_t m_exitm; /* Mutex to accompany exit condition */ 37*433d6423SLionel Sambuc ucontext_t m_context; /* Thread machine context */ 38*433d6423SLionel Sambuc struct __mthread_tcb *m_next; /* Next thread in linked list */ 39*433d6423SLionel Sambuc }; 40*433d6423SLionel Sambuc typedef struct __mthread_tcb mthread_tcb_t; 41*433d6423SLionel Sambuc 42*433d6423SLionel Sambuc EXTERN mthread_thread_t current_thread; 43*433d6423SLionel Sambuc EXTERN mthread_queue_t free_threads; 44*433d6423SLionel Sambuc EXTERN mthread_queue_t run_queue; /* FIFO of runnable threads */ 45*433d6423SLionel Sambuc EXTERN mthread_tcb_t **threads; 46*433d6423SLionel Sambuc EXTERN mthread_tcb_t mainthread; 47*433d6423SLionel Sambuc EXTERN int no_threads; 48*433d6423SLionel Sambuc EXTERN int used_threads; 49*433d6423SLionel Sambuc EXTERN int need_reset; 50*433d6423SLionel Sambuc EXTERN int running_main_thread; 51*433d6423SLionel Sambuc 52