4 * Implements the architecture independant portion of the LWKT
7 * $DragonFly: src/sys/sys/thread.h,v 1.4 2003/06/21 07:54:57 dillon Exp $
10 #ifndef _SYS_THREAD_H_
11 #define _SYS_THREAD_H_
25 typedef struct lwkt_queue *lwkt_queue_t;
26 typedef struct lwkt_token *lwkt_token_t;
27 typedef struct lwkt_wait *lwkt_wait_t;
28 typedef struct lwkt_msg *lwkt_msg_t;
29 typedef struct lwkt_port *lwkt_port_t;
30 typedef struct lwkt_cpu_msg *lwkt_cpu_msg_t;
31 typedef struct lwkt_cpu_port *lwkt_cpu_port_t;
32 typedef struct lwkt_rwlock *lwkt_rwlock_t;
33 typedef struct thread *thread_t;
35 typedef TAILQ_HEAD(lwkt_queue, thread) lwkt_queue;
36 typedef TAILQ_HEAD(lwkt_msg_queue, lwkt_msg) lwkt_msg_queue;
38 #include <machine/thread.h>
41 * Tokens arbitrate access to information. They are 'soft' arbitrators
42 * in that they are associated with cpus rather then threads, making the
43 * optimal aquisition case very fast if your cpu already happens to own the
44 * token you are requesting.
46 typedef struct lwkt_token {
47 int t_cpu; /* the current owner of the token */
48 int t_reqcpu; /* return ownership to this cpu on release */
50 int t_pri; /* raise thread priority to hold token */
55 * Wait structures deal with blocked threads. Due to the way remote cpus
56 * interact with these structures stable storage must be used.
58 typedef struct lwkt_wait {
59 lwkt_queue wa_waitq; /* list of waiting threads */
60 lwkt_token wa_token; /* who currently owns the list */
66 * The standarding message and port structure for communications between
69 typedef struct lwkt_msg {
70 TAILQ_ENTRY(lwkt_msg) ms_node;
71 lwkt_port_t ms_replyport;
77 #define MSGF_DONE 0x0001
78 #define MSGF_REPLY 0x0002
79 #define MSGF_QUEUED 0x0004
81 typedef struct lwkt_port {
82 lwkt_msg_queue mp_msgq;
86 #define mp_token mp_wait.wa_token
89 * The standard message and queue structure used for communications between
90 * cpus. Messages are typically queued via a machine-specific non-linked
91 * FIFO matrix allowing any cpu to send a message to any other cpu without
94 typedef struct lwkt_cpu_msg {
95 void (*cm_func)(lwkt_cpu_msg_t msg); /* primary dispatch function */
96 int cm_code; /* request code if applicable */
97 int cm_cpu; /* reply to cpu */
98 thread_t cm_originator; /* originating thread for wakeup */
104 typedef struct lwkt_rwlock {
110 #define rw_token rw_wait.wa_token
113 * Thread structure. Note that ownership of a thread structure is special
114 * cased and there is no 'token'. A thread is always owned by td_cpu and
115 * any manipulation of the thread by some other cpu must be done through
116 * cpu_*msg() functions. e.g. you could request ownership of a thread that
117 * way, or hand a thread off to another cpu by changing td_cpu and sending
118 * a schedule request to the other cpu.
121 TAILQ_ENTRY(thread) td_threadq;
122 struct proc *td_proc; /* (optional) associated process */
123 struct pcb *td_pcb; /* points to pcb and top of kstack */
124 int td_cpu; /* cpu owning the thread */
125 int td_pri; /* 0-31, 0=highest priority */
126 int td_flags; /* THF flags */
127 int td_gen; /* wait queue chasing generation number */
128 char *td_kstack; /* kernel stack */
129 char *td_sp; /* kernel stack pointer for LWKT restore */
130 void (*td_switch)(struct thread *ntd);
131 lwkt_wait_t td_wait; /* thread sitting on wait structure */
132 struct mi_thread td_mach;
136 * Thread flags. Note that the RUNNING state is independant from the
137 * RUNQ/WAITQ state. That is, a thread's queueing state can be manipulated
138 * while it is running. If a thread is preempted it will always be moved
139 * back to the RUNQ if it isn't on it.
141 #define TDF_RUNNING 0x0001 /* currently running */
142 #define TDF_RUNQ 0x0002 /* on run queue */
145 * Thread priorities. Typically only one thread from any given
146 * user process scheduling queue is on the LWKT run queue at a time.
147 * Remember that there is one LWKT run queue per cpu.
149 * Critical sections are handled by bumping td_pri above TDPRI_MAX, which
150 * causes interrupts to be masked as they occur. When this occurs
151 * mycpu->gd_reqpri will be raised (possibly just set to TDPRI_CRIT for
152 * interrupt masking).
154 #define TDPRI_IDLE_THREAD 0 /* the idle thread */
155 #define TDPRI_USER_IDLE 4 /* user scheduler idle */
156 #define TDPRI_USER_NORM 6 /* user scheduler normal */
157 #define TDPRI_USER_REAL 8 /* user scheduler real time */
158 #define TDPRI_KERN_USER 10 /* kernel / block in syscall */
159 #define TDPRI_SOFT_NORM 14 /* kernel / normal */
160 #define TDPRI_SOFT_TIMER 16 /* kernel / timer */
161 #define TDPRI_INT_SUPPORT 20 /* kernel / high priority support */
162 #define TDPRI_INT_LOW 27 /* low priority interrupt */
163 #define TDPRI_INT_MED 28 /* medium priority interrupt */
164 #define TDPRI_INT_HIGH 29 /* high priority interrupt */
167 #define TDPRI_MASK 31
168 #define TDPRI_CRIT 32 /* high bits of td_pri used for crit */
170 #define CACHE_NTHREADS 4
174 extern struct vm_zone *thread_zone;
176 extern void lwkt_gdinit(struct globaldata *gd);
177 extern void lwkt_switch(void);
178 extern void lwkt_preempt(void);
179 extern void lwkt_schedule(thread_t td);
180 extern void lwkt_schedule_self(void);
181 extern void lwkt_deschedule(thread_t td);
182 extern void lwkt_deschedule_self(void);
183 extern void lwkt_yield(void);
184 extern void lwkt_yield_quick(void);
186 extern void lwkt_block(lwkt_wait_t w);
187 extern void lwkt_signal(lwkt_wait_t w);
188 extern void lwkt_gettoken(lwkt_token_t tok);
189 extern void lwkt_reltoken(lwkt_token_t tok);
190 extern int lwkt_regettoken(lwkt_token_t tok);