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
00025 #include <nucleus/types.h>
00026
00033
00034
00035 #define XNSUSP 0x00000001
00036 #define XNPEND 0x00000002
00037 #define XNDELAY 0x00000004
00038 #define XNREADY 0x00000008
00039 #define XNDORMANT 0x00000010
00040 #define XNZOMBIE 0x00000020
00041 #define XNRESTART 0x00000040
00042 #define XNSTARTED 0x00000080
00043 #define XNMAPPED 0x00000100
00044 #define XNRELAX 0x00000200
00045 #define XNMIGRATE 0x00000400
00046 #define XNHELD 0x00000800
00048 #define XNBOOST 0x00001000
00049 #define XNDEBUG 0x00002000
00050 #define XNLOCK 0x00004000
00051 #define XNRRB 0x00008000
00052 #define XNASDI 0x00010000
00053 #define XNDEFCAN 0x00020000
00055
00056
00057
00058
00059
00060 #define XNTRAPSW 0x00040000
00061 #define XNRPIOFF 0x00080000
00062 #define XNFPU 0x00100000
00063 #define XNSHADOW 0x00200000
00064 #define XNROOT 0x00400000
00065 #define XNSWREP 0x00800000
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088 #define XNTHREAD_STATE_LABELS "SWDRU....X.HbTlr..tof.."
00089
00090 #define XNTHREAD_BLOCK_BITS (XNSUSP|XNPEND|XNDELAY|XNDORMANT|XNRELAX|XNMIGRATE|XNHELD)
00091 #define XNTHREAD_MODE_BITS (XNLOCK|XNRRB|XNASDI|XNTRAPSW|XNRPIOFF)
00092
00093
00094 #define XNTHREAD_STATE_SPARE0 0x10000000
00095 #define XNTHREAD_STATE_SPARE1 0x20000000
00096 #define XNTHREAD_STATE_SPARE2 0x40000000
00097 #define XNTHREAD_STATE_SPARE3 0x80000000
00098 #define XNTHREAD_STATE_SPARES 0xf0000000
00099
00106
00107
00108 #define XNTIMEO 0x00000001
00109 #define XNRMID 0x00000002
00110 #define XNBREAK 0x00000004
00111 #define XNKICKED 0x00000008
00112 #define XNWAKEN 0x00000010
00113 #define XNROBBED 0x00000020
00114 #define XNATOMIC 0x00000040
00115 #define XNAFFSET 0x00000080
00116 #define XNPRIOSET 0x00000100
00117 #define XNABORT 0x00000200
00118 #define XNCANPND 0x00000400
00119 #define XNAMOK 0x00000800
00121
00122 #define XNTHREAD_INFO_SPARE0 0x10000000
00123 #define XNTHREAD_INFO_SPARE1 0x20000000
00124 #define XNTHREAD_INFO_SPARE2 0x40000000
00125 #define XNTHREAD_INFO_SPARE3 0x80000000
00126 #define XNTHREAD_INFO_SPARES 0xf0000000
00127
00129
00133 typedef struct xnthread_info {
00134
00135 unsigned long state;
00137 int bprio;
00138 int cprio;
00140 int cpu;
00141 unsigned long affinity;
00143 unsigned long long relpoint;
00145 unsigned long long exectime;
00147 unsigned long modeswitches;
00148 unsigned long ctxswitches;
00149 unsigned long pagefaults;
00151 char name[XNOBJECT_NAME_LEN];
00153 } xnthread_info_t;
00154
00155 #if defined(__KERNEL__) || defined(__XENO_SIM__)
00156
00157 #include <nucleus/stat.h>
00158 #include <nucleus/timer.h>
00159 #include <nucleus/registry.h>
00160 #include <nucleus/schedparam.h>
00161
00162 #ifdef __XENO_SIM__
00163
00164 #define XNRUNNING XNTHREAD_STATE_SPARE0
00165 #define XNDELETED XNTHREAD_STATE_SPARE1
00166 #endif
00167
00168 #define XNTHREAD_INVALID_ASR ((void (*)(xnsigmask_t))0)
00169
00170 struct xnthread;
00171 struct xnsynch;
00172 struct xnsched;
00173 struct xnselector;
00174 struct xnsched_class;
00175 struct xnsched_tpslot;
00176 union xnsched_policy_param;
00177 struct xnbufd;
00178
00179 struct xnthread_operations {
00180 int (*get_denormalized_prio)(struct xnthread *, int coreprio);
00181 unsigned (*get_magic)(void);
00182 };
00183
00184 struct xnthread_init_attr {
00185 struct xntbase *tbase;
00186 struct xnthread_operations *ops;
00187 xnflags_t flags;
00188 unsigned int stacksize;
00189 const char *name;
00190 };
00191
00192 struct xnthread_start_attr {
00193 xnflags_t mode;
00194 int imask;
00195 xnarch_cpumask_t affinity;
00196 void (*entry)(void *cookie);
00197 void *cookie;
00198 };
00199
00200 struct xnthread_wait_context {
00201 unsigned long oldstate;
00202 };
00203
00204 typedef void (*xnasr_t)(xnsigmask_t sigs);
00205
00206 typedef struct xnthread {
00207
00208 xnarchtcb_t tcb;
00209
00210 xnflags_t state;
00211
00212 xnflags_t info;
00213
00214 struct xnsched *sched;
00215
00216 struct xnsched_class *sched_class;
00217
00218 struct xnsched_class *base_class;
00219
00220 #ifdef CONFIG_XENO_OPT_SCHED_TP
00221 struct xnsched_tpslot *tps;
00222 struct xnholder tp_link;
00223 #endif
00224 #ifdef CONFIG_XENO_OPT_SCHED_SPORADIC
00225 struct xnsched_sporadic_data *pss;
00226 #endif
00227
00228 unsigned idtag;
00229
00230 xnarch_cpumask_t affinity;
00231
00232 int bprio;
00233
00234 int cprio;
00235
00236 u_long schedlck;
00238 xnpholder_t rlink;
00239
00240 xnpholder_t plink;
00241
00242 #ifdef CONFIG_XENO_OPT_PRIOCPL
00243 xnpholder_t xlink;
00244
00245 struct xnsched *rpi;
00246 #endif
00247
00248 xnholder_t glink;
00249
00250 #define link2thread(ln, fld) container_of(ln, struct xnthread, fld)
00251
00252 xnpqueue_t claimq;
00253
00254 struct xnsynch *wchan;
00255
00256 struct xnsynch *wwake;
00257
00258 xntimer_t rtimer;
00259
00260 xntimer_t ptimer;
00261
00262 xnsigmask_t signals;
00263
00264 xnticks_t rrperiod;
00265
00266 xnticks_t rrcredit;
00267
00268 union {
00269 struct {
00270
00271
00272
00273
00274
00275 void *ptr;
00276 size_t size;
00277 } buffer;
00278 struct xnbufd *bufd;
00279 size_t size;
00280 } wait_u;
00281
00282
00283 struct xnthread_wait_context *wcontext;
00284
00285 struct {
00286 xnstat_counter_t ssw;
00287 xnstat_counter_t csw;
00288 xnstat_counter_t pf;
00289 xnstat_exectime_t account;
00290 xnstat_exectime_t lastperiod;
00291 } stat;
00292
00293 #ifdef CONFIG_XENO_OPT_SELECT
00294 struct xnselector *selector;
00295 #endif
00296
00297 int errcode;
00298
00299 xnasr_t asr;
00300
00301 xnflags_t asrmode;
00302
00303 int asrimask;
00304
00305 unsigned asrlevel;
00306
00307 int imask;
00308
00309 int imode;
00310
00311 struct xnsched_class *init_class;
00312
00313 union xnsched_policy_param init_schedparam;
00314
00315 struct {
00316 xnhandle_t handle;
00317 const char *waitkey;
00318 } registry;
00319
00320 struct xnthread_operations *ops;
00321
00322 char name[XNOBJECT_NAME_LEN];
00323
00324 void (*entry)(void *cookie);
00325
00326 void *cookie;
00327
00328 #ifdef CONFIG_XENO_OPT_PERVASIVE
00329 unsigned long __user *u_mode;
00330
00331 unsigned u_sigpending;
00332 #endif
00333
00334 XNARCH_DECL_DISPLAY_CONTEXT();
00335
00336 } xnthread_t;
00337
00338 #define XNHOOK_THREAD_START 1
00339 #define XNHOOK_THREAD_SWITCH 2
00340 #define XNHOOK_THREAD_DELETE 3
00341
00342 typedef struct xnhook {
00343 xnholder_t link;
00344 #define link2hook(ln) container_of(ln, xnhook_t, link)
00345 void (*routine)(struct xnthread *thread);
00346 } xnhook_t;
00347
00348 #define xnthread_name(thread) ((thread)->name)
00349 #define xnthread_clear_name(thread) do { *(thread)->name = 0; } while(0)
00350 #define xnthread_sched(thread) ((thread)->sched)
00351 #define xnthread_start_time(thread) ((thread)->stime)
00352 #define xnthread_state_flags(thread) ((thread)->state)
00353 #define xnthread_test_state(thread,flags) testbits((thread)->state,flags)
00354 #define xnthread_set_state(thread,flags) __setbits((thread)->state,flags)
00355 #define xnthread_clear_state(thread,flags) __clrbits((thread)->state,flags)
00356 #define xnthread_test_info(thread,flags) testbits((thread)->info,flags)
00357 #define xnthread_set_info(thread,flags) __setbits((thread)->info,flags)
00358 #define xnthread_clear_info(thread,flags) __clrbits((thread)->info,flags)
00359 #define xnthread_lock_count(thread) ((thread)->schedlck)
00360 #define xnthread_init_schedparam(thread) ((thread)->init_schedparam)
00361 #define xnthread_base_priority(thread) ((thread)->bprio)
00362 #define xnthread_current_priority(thread) ((thread)->cprio)
00363 #define xnthread_init_class(thread) ((thread)->init_class)
00364 #define xnthread_base_class(thread) ((thread)->base_class)
00365 #define xnthread_sched_class(thread) ((thread)->sched_class)
00366 #define xnthread_time_slice(thread) ((thread)->rrperiod)
00367 #define xnthread_time_credit(thread) ((thread)->rrcredit)
00368 #define xnthread_archtcb(thread) (&((thread)->tcb))
00369 #define xnthread_asr_level(thread) ((thread)->asrlevel)
00370 #define xnthread_pending_signals(thread) ((thread)->signals)
00371 #define xnthread_timeout(thread) xntimer_get_timeout(&(thread)->rtimer)
00372 #define xnthread_stack_size(thread) xnarch_stack_size(xnthread_archtcb(thread))
00373 #define xnthread_stack_base(thread) xnarch_stack_base(xnthread_archtcb(thread))
00374 #define xnthread_stack_end(thread) xnarch_stack_end(xnthread_archtcb(thread))
00375 #define xnthread_handle(thread) ((thread)->registry.handle)
00376 #ifdef CONFIG_XENO_OPT_TIMING_PERIODIC
00377 #define xnthread_time_base(thread) ((thread)->rtimer.base)
00378 #else
00379 #define xnthread_time_base(thread) (&nktbase)
00380 #endif
00381 #define xnthread_signaled_p(thread) ((thread)->signals != 0)
00382 #define xnthread_timed_p(thread) (!!testbits(xnthread_time_base(thread)->status, XNTBRUN))
00383 #define xnthread_user_task(thread) xnarch_user_task(xnthread_archtcb(thread))
00384 #define xnthread_user_pid(thread) \
00385 (xnthread_test_state((thread),XNROOT) || !xnthread_user_task(thread) ? \
00386 0 : xnarch_user_pid(xnthread_archtcb(thread)))
00387 #define xnthread_affinity(thread) ((thread)->affinity)
00388 #define xnthread_affine_p(thread, cpu) xnarch_cpu_isset(cpu, (thread)->affinity)
00389 #define xnthread_get_exectime(thread) xnstat_exectime_get_total(&(thread)->stat.account)
00390 #define xnthread_get_lastswitch(thread) xnstat_exectime_get_last_switch((thread)->sched)
00391 #ifdef CONFIG_XENO_OPT_PERVASIVE
00392 #define xnthread_sigpending(thread) ((thread)->u_sigpending)
00393 #define xnthread_set_sigpending(thread, pending) \
00394 ((thread)->u_sigpending = (pending))
00395 #endif
00396 #ifdef CONFIG_XENO_OPT_WATCHDOG
00397 #define xnthread_amok_p(thread) xnthread_test_info(thread, XNAMOK)
00398 #define xnthread_clear_amok(thread) xnthread_clear_info(thread, XNAMOK)
00399 #else
00400 #define xnthread_amok_p(thread) (0)
00401 #define xnthread_clear_amok(thread) do { } while (0)
00402 #endif
00403
00404
00405 static inline int xnthread_get_denormalized_prio(struct xnthread *t, int coreprio)
00406 {
00407 return t->ops && t->ops->get_denormalized_prio
00408 ? t->ops->get_denormalized_prio(t, coreprio) : coreprio;
00409 }
00410
00411 static inline unsigned xnthread_get_magic(struct xnthread *t)
00412 {
00413 return t->ops ? t->ops->get_magic() : 0;
00414 }
00415
00416 static inline
00417 struct xnthread_wait_context *xnthread_get_wait_context(struct xnthread *thread)
00418 {
00419 return thread->wcontext;
00420 }
00421
00422 static inline
00423 int xnthread_register(struct xnthread *thread, const char *name)
00424 {
00425 return xnregistry_enter(name, thread, &xnthread_handle(thread), NULL);
00426 }
00427
00428 static inline
00429 struct xnthread *xnthread_lookup(xnhandle_t threadh)
00430 {
00431 struct xnthread *thread = (struct xnthread *)xnregistry_lookup(threadh);
00432 return (thread && xnthread_handle(thread) == threadh) ? thread : NULL;
00433 }
00434
00435 #ifdef __cplusplus
00436 extern "C" {
00437 #endif
00438
00439 int xnthread_init(struct xnthread *thread,
00440 const struct xnthread_init_attr *attr,
00441 struct xnsched *sched,
00442 struct xnsched_class *sched_class,
00443 const union xnsched_policy_param *sched_param);
00444
00445 void xnthread_cleanup_tcb(struct xnthread *thread);
00446
00447 char *xnthread_format_status(xnflags_t status, char *buf, int size);
00448
00449 int *xnthread_get_errno_location(struct xnthread *thread);
00450
00451 xnticks_t xnthread_get_timeout(struct xnthread *thread, xnticks_t tsc_ns);
00452
00453 xnticks_t xnthread_get_period(struct xnthread *thread);
00454
00455 void xnthread_prepare_wait(struct xnthread_wait_context *wc);
00456
00457 void xnthread_finish_wait(struct xnthread_wait_context *wc,
00458 void (*cleanup)(struct xnthread_wait_context *wc));
00459
00460 #ifdef __cplusplus
00461 }
00462 #endif
00463
00464 #endif
00465
00466 #endif