2010-09-21 14:22:38 +02:00
|
|
|
/* EXTERN should be extern, except for the allocate file */
|
|
|
|
#ifdef ALLOCATE
|
|
|
|
#undef EXTERN
|
|
|
|
#define EXTERN
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <assert.h>
|
2012-09-17 11:09:12 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/signal.h>
|
|
|
|
|
2012-12-11 20:53:08 +01:00
|
|
|
#define MTHREAD_RND_SCHED 0 /* Enable/disable random scheduling */
|
2010-09-30 15:44:13 +02:00
|
|
|
#define NO_THREADS 4
|
|
|
|
#define MAX_THREAD_POOL 1024
|
2010-09-21 14:22:38 +02:00
|
|
|
#define STACKSZ 4096
|
2011-04-14 13:54:43 +02:00
|
|
|
#define MAIN_THREAD (-1)
|
|
|
|
#define NO_THREAD (-2)
|
2010-09-30 15:44:13 +02:00
|
|
|
#define isokthreadid(i) (i == MAIN_THREAD || (i >= 0 && i < no_threads))
|
2011-03-18 11:29:54 +01:00
|
|
|
#define MTHREAD_INIT_MAGIC 0xca11ab1e
|
|
|
|
#define MTHREAD_NOT_INUSE 0xdefec7
|
2010-09-30 15:44:13 +02:00
|
|
|
|
|
|
|
typedef enum {
|
2012-02-13 13:02:12 +01:00
|
|
|
MS_CONDITION, MS_DEAD, MS_EXITING, MS_MUTEX, MS_RUNNABLE, MS_NEEDRESET
|
2010-09-30 15:44:13 +02:00
|
|
|
} mthread_state_t;
|
|
|
|
|
|
|
|
struct __mthread_tcb {
|
|
|
|
mthread_thread_t m_tid; /* My own ID */
|
|
|
|
mthread_state_t m_state; /* Thread state */
|
|
|
|
struct __mthread_attr m_attr; /* Thread attributes */
|
|
|
|
struct __mthread_cond *m_cond; /* Condition variable that this thread
|
|
|
|
* might be blocking on */
|
|
|
|
void *(*m_proc)(void *); /* Procedure to run */
|
|
|
|
void *m_arg; /* Argument passed to procedure */
|
|
|
|
void *m_result; /* Result after procedure returns */
|
|
|
|
mthread_cond_t m_exited; /* Condition variable signaling this
|
|
|
|
* thread has ended */
|
|
|
|
mthread_mutex_t m_exitm; /* Mutex to accompany exit condition */
|
|
|
|
ucontext_t m_context; /* Thread machine context */
|
|
|
|
struct __mthread_tcb *m_next; /* Next thread in linked list */
|
|
|
|
};
|
|
|
|
typedef struct __mthread_tcb mthread_tcb_t;
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
EXTERN mthread_thread_t current_thread;
|
|
|
|
EXTERN mthread_queue_t free_threads;
|
|
|
|
EXTERN mthread_queue_t run_queue; /* FIFO of runnable threads */
|
2010-09-30 15:44:13 +02:00
|
|
|
EXTERN mthread_tcb_t **threads;
|
2010-09-21 14:22:38 +02:00
|
|
|
EXTERN mthread_tcb_t mainthread;
|
|
|
|
EXTERN int no_threads;
|
|
|
|
EXTERN int used_threads;
|
2012-02-13 13:02:12 +01:00
|
|
|
EXTERN int need_reset;
|
2010-09-21 14:22:38 +02:00
|
|
|
EXTERN int running_main_thread;
|
|
|
|
|