00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef _XENO_NUCLEUS_THREAD_H
00023 #define _XENO_NUCLEUS_THREAD_H
00024
00031
00032
00033 #define XNSUSP 0x00000001
00034 #define XNPEND 0x00000002
00035 #define XNDELAY 0x00000004
00036 #define XNREADY 0x00000008
00037 #define XNDORMANT 0x00000010
00038 #define XNZOMBIE 0x00000020
00039 #define XNRESTART 0x00000040
00040 #define XNSTARTED 0x00000080
00041 #define XNMAPPED 0x00000100
00042 #define XNRELAX 0x00000200
00043 #define XNHELD 0x00000400
00045 #define XNBOOST 0x00000800
00046 #define XNDEBUG 0x00001000
00047 #define XNLOCK 0x00002000
00048 #define XNRRB 0x00004000
00049 #define XNASDI 0x00008000
00051
00052
00053
00054
00055 #define XNSHIELD 0x00010000
00056 #define XNTRAPSW 0x00020000
00057 #define XNRPIOFF 0x00040000
00059 #define XNFPU 0x00100000
00060 #define XNSHADOW 0x00200000
00061 #define XNROOT 0x00400000
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 #define XNTHREAD_STATE_LABELS { \
00085 'S', 'W', 'D', 'R', 'U', \
00086 '.', '.', '.', '.', 'X', \
00087 'H', 'b', 'T', 'l', 'r', \
00088 '.', 's', 't', 'o', '.', \
00089 'f', '.', '.', \
00090 }
00091
00092 #define XNTHREAD_BLOCK_BITS (XNSUSP|XNPEND|XNDELAY|XNDORMANT|XNRELAX|XNHELD)
00093 #define XNTHREAD_MODE_BITS (XNLOCK|XNRRB|XNASDI|XNSHIELD|XNTRAPSW|XNRPIOFF)
00094
00095
00096 #define XNTHREAD_STATE_SPARE0 0x10000000
00097 #define XNTHREAD_STATE_SPARE1 0x20000000
00098 #define XNTHREAD_STATE_SPARE2 0x40000000
00099 #define XNTHREAD_STATE_SPARE3 0x80000000
00100 #define XNTHREAD_STATE_SPARES 0xf0000000
00101
00108
00109
00110 #define XNTIMEO 0x00000001
00111 #define XNRMID 0x00000002
00112 #define XNBREAK 0x00000004
00113 #define XNKICKED 0x00000008
00114 #define XNWAKEN 0x00000010
00115 #define XNROBBED 0x00000020
00116 #define XNATOMIC 0x00000040
00117 #define XNAFFSET 0x00000080
00119
00120 #define XNTHREAD_INFO_SPARE0 0x10000000
00121 #define XNTHREAD_INFO_SPARE1 0x20000000
00122 #define XNTHREAD_INFO_SPARE2 0x40000000
00123 #define XNTHREAD_INFO_SPARE3 0x80000000
00124 #define XNTHREAD_INFO_SPARES 0xf0000000
00125
00127
00128 #if defined(__KERNEL__) || defined(__XENO_SIM__)
00129
00130 #include <nucleus/stat.h>
00131 #include <nucleus/timer.h>
00132
00133 #ifdef __XENO_SIM__
00134
00135 #define XNRUNNING XNTHREAD_STATE_SPARE0
00136 #define XNDELETED XNTHREAD_STATE_SPARE1
00137 #endif
00138
00139 #define XNTHREAD_INVALID_ASR ((void (*)(xnsigmask_t))0)
00140
00141 struct xnthread;
00142 struct xnsched;
00143 struct xnsynch;
00144 struct xnrpi;
00145
00146 typedef struct xnthrops {
00147
00148 int (*get_denormalized_prio)(struct xnthread *, int coreprio);
00149 unsigned (*get_magic)(void);
00150
00151 } xnthrops_t;
00152
00153 typedef void (*xnasr_t)(xnsigmask_t sigs);
00154
00155 typedef struct xnthread {
00156
00157 xnarchtcb_t tcb;
00158
00159 xnflags_t state;
00160
00161 xnflags_t info;
00162
00163 struct xnsched *sched;
00164
00165 xnarch_cpumask_t affinity;
00166
00167 int bprio;
00168
00169 int cprio;
00170
00171 u_long schedlck;
00173 xnpholder_t rlink;
00174
00175 xnpholder_t plink;
00176
00177 #ifdef CONFIG_XENO_OPT_PRIOCPL
00178 xnpholder_t xlink;
00179
00180 struct xnrpi *rpi;
00181 #endif
00182
00183 xnholder_t glink;
00184
00185 #define link2thread(ln, fld) container_of(ln, xnthread_t, fld)
00186
00187 xnpqueue_t claimq;
00188
00189 struct xnsynch *wchan;
00190
00191 struct xnsynch *wwake;
00192
00193 xntimer_t rtimer;
00194
00195 xntimer_t ptimer;
00196
00197 xnsigmask_t signals;
00198
00199 xnticks_t rrperiod;
00200
00201 xnticks_t rrcredit;
00202
00203 struct {
00204 xnstat_counter_t ssw;
00205 xnstat_counter_t csw;
00206 xnstat_counter_t pf;
00207 xnstat_exectime_t account;
00208 xnstat_exectime_t lastperiod;
00209 } stat;
00210
00211 int errcode;
00212
00213 xnasr_t asr;
00214
00215 xnflags_t asrmode;
00216
00217 int asrimask;
00218
00219 unsigned asrlevel;
00220
00221 int imask;
00222
00223 int imode;
00224
00225 int iprio;
00226
00227 #ifdef CONFIG_XENO_OPT_REGISTRY
00228 struct {
00229 xnhandle_t handle;
00230 const char *waitkey;
00231 } registry;
00232 #endif
00233
00234 xnthrops_t *ops;
00235
00236 char name[XNOBJECT_NAME_LEN];
00237
00238 void (*entry)(void *cookie);
00239
00240 void *cookie;
00241
00242 XNARCH_DECL_DISPLAY_CONTEXT();
00243
00244 } xnthread_t;
00245
00246 #define XNHOOK_THREAD_START 1
00247 #define XNHOOK_THREAD_SWITCH 2
00248 #define XNHOOK_THREAD_DELETE 3
00249
00250 typedef struct xnhook {
00251
00252 xnholder_t link;
00253 #define link2hook(ln) container_of(ln, xnhook_t, link)
00254
00255 void (*routine)(xnthread_t *thread);
00256
00257 } xnhook_t;
00258
00259 #define xnthread_name(thread) ((thread)->name)
00260 #define xnthread_clear_name(thread) do { *(thread)->name = 0; } while(0)
00261 #define xnthread_sched(thread) ((thread)->sched)
00262 #define xnthread_start_time(thread) ((thread)->stime)
00263 #define xnthread_state_flags(thread) ((thread)->state)
00264 #define xnthread_test_state(thread,flags) testbits((thread)->state,flags)
00265 #define xnthread_set_state(thread,flags) __setbits((thread)->state,flags)
00266 #define xnthread_clear_state(thread,flags) __clrbits((thread)->state,flags)
00267 #define xnthread_test_info(thread,flags) testbits((thread)->info,flags)
00268 #define xnthread_set_info(thread,flags) __setbits((thread)->info,flags)
00269 #define xnthread_clear_info(thread,flags) __clrbits((thread)->info,flags)
00270 #define xnthread_lock_count(thread) ((thread)->schedlck)
00271 #define xnthread_initial_priority(thread) ((thread)->iprio)
00272 #define xnthread_base_priority(thread) ((thread)->bprio)
00273 #define xnthread_current_priority(thread) ((thread)->cprio)
00274 #define xnthread_time_slice(thread) ((thread)->rrperiod)
00275 #define xnthread_time_credit(thread) ((thread)->rrcredit)
00276 #define xnthread_archtcb(thread) (&((thread)->tcb))
00277 #define xnthread_asr_level(thread) ((thread)->asrlevel)
00278 #define xnthread_pending_signals(thread) ((thread)->signals)
00279 #define xnthread_timeout(thread) xntimer_get_timeout(&(thread)->rtimer)
00280 #define xnthread_stack_size(thread) xnarch_stack_size(xnthread_archtcb(thread))
00281 #define xnthread_stack_base(thread) xnarch_stack_base(xnthread_archtcb(thread))
00282 #define xnthread_stack_end(thread) xnarch_stack_end(xnthread_archtcb(thread))
00283 #define xnthread_handle(thread) ((thread)->registry.handle)
00284 #ifdef CONFIG_XENO_OPT_TIMING_PERIODIC
00285 #define xnthread_time_base(thread) ((thread)->rtimer.base)
00286 #else
00287 #define xnthread_time_base(thread) (&nktbase)
00288 #endif
00289 #define xnthread_signaled_p(thread) ((thread)->signals != 0)
00290 #define xnthread_timed_p(thread) (!!testbits(xnthread_time_base(thread)->status, XNTBRUN))
00291 #define xnthread_user_task(thread) xnarch_user_task(xnthread_archtcb(thread))
00292 #define xnthread_user_pid(thread) \
00293 (xnthread_test_state((thread),XNROOT) || !xnthread_user_task(thread) ? \
00294 0 : xnarch_user_pid(xnthread_archtcb(thread)))
00295 #define xnthread_affinity(thread) ((thread)->affinity)
00296 #define xnthread_affine_p(thread, cpu) xnarch_cpu_isset(cpu, (thread)->affinity)
00297 #define xnthread_get_exectime(thread) xnstat_exectime_get_total(&(thread)->stat.account)
00298 #define xnthread_get_lastswitch(thread) xnstat_exectime_get_last_switch((thread)->sched)
00299
00300
00301 static inline int xnthread_get_denormalized_prio(xnthread_t *t, int coreprio)
00302 {
00303 return t->ops && t->ops->get_denormalized_prio ?
00304 t->ops->get_denormalized_prio(t, coreprio) : coreprio;
00305 }
00306
00307 static inline unsigned xnthread_get_magic(xnthread_t *t)
00308 {
00309 return t->ops ? t->ops->get_magic() : 0;
00310 }
00311
00312 #ifdef __cplusplus
00313 extern "C" {
00314 #endif
00315
00316 int xnthread_init(xnthread_t *thread,
00317 xntbase_t *tbase,
00318 const char *name,
00319 int prio,
00320 xnflags_t flags,
00321 unsigned stacksize,
00322 xnthrops_t *ops);
00323
00324 void xnthread_cleanup_tcb(xnthread_t *thread);
00325
00326 char *xnthread_symbolic_status(xnflags_t status, char *buf, int size);
00327
00328 int *xnthread_get_errno_location(xnthread_t *thread);
00329
00330 static inline xnticks_t xnthread_get_timeout(xnthread_t *thread, xnticks_t tsc_ns)
00331 {
00332 xnticks_t timeout;
00333 xntimer_t *timer;
00334
00335 if (!xnthread_test_state(thread,XNDELAY))
00336 return 0LL;
00337
00338 if (xntimer_running_p(&thread->rtimer))
00339 timer = &thread->rtimer;
00340 else if (xntimer_running_p(&thread->ptimer))
00341 timer = &thread->ptimer;
00342 else
00343 return 0LL;
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357 if (xntbase_periodic_p(xnthread_time_base(thread)))
00358 return xntimer_get_timeout(timer);
00359
00360 timeout = xntimer_get_date(timer);
00361
00362 if (timeout <= tsc_ns)
00363 return 1;
00364
00365 return timeout - tsc_ns;
00366 }
00367
00368 static inline xnticks_t xnthread_get_period(xnthread_t *thread)
00369 {
00370 xnticks_t period = 0;
00371
00372
00373
00374
00375
00376
00377
00378
00379 if (xntimer_running_p(&thread->ptimer))
00380 period = xntimer_get_interval(&thread->ptimer);
00381 else if (xnthread_test_state(thread,XNRRB))
00382 period = xnthread_time_slice(thread);
00383
00384 return period;
00385 }
00386
00387 #ifdef __cplusplus
00388 }
00389 #endif
00390
00391 #endif
00392
00393 #endif