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 XNOTHER 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
00120 #define XNSWREP 0x00001000
00122
00123 #define XNTHREAD_INFO_SPARE0 0x10000000
00124 #define XNTHREAD_INFO_SPARE1 0x20000000
00125 #define XNTHREAD_INFO_SPARE2 0x40000000
00126 #define XNTHREAD_INFO_SPARE3 0x80000000
00127 #define XNTHREAD_INFO_SPARES 0xf0000000
00128
00130
00134 typedef struct xnthread_info {
00135
00136 unsigned long state;
00138 int bprio;
00139 int cprio;
00141 int cpu;
00142 unsigned long affinity;
00144 unsigned long long relpoint;
00146 unsigned long long exectime;
00148 unsigned long modeswitches;
00149 unsigned long ctxswitches;
00150 unsigned long pagefaults;
00152 char name[XNOBJECT_NAME_LEN];
00154 } xnthread_info_t;
00155
00156 #if defined(__KERNEL__) || defined(__XENO_SIM__)
00157
00158 #include <nucleus/stat.h>
00159 #include <nucleus/timer.h>
00160 #include <nucleus/registry.h>
00161 #include <nucleus/schedparam.h>
00162
00163 #ifdef __XENO_SIM__
00164
00165 #define XNRUNNING XNTHREAD_STATE_SPARE0
00166 #define XNDELETED XNTHREAD_STATE_SPARE1
00167 #endif
00168
00169 #define XNTHREAD_INVALID_ASR ((void (*)(xnsigmask_t))0)
00170
00171 struct xnthread;
00172 struct xnsynch;
00173 struct xnsched;
00174 struct xnselector;
00175 struct xnsched_class;
00176 struct xnsched_tpslot;
00177 union xnsched_policy_param;
00178 struct xnbufd;
00179
00180 struct xnthread_operations {
00181 int (*get_denormalized_prio)(struct xnthread *, int coreprio);
00182 unsigned (*get_magic)(void);
00183 };
00184
00185 struct xnthread_init_attr {
00186 struct xntbase *tbase;
00187 struct xnthread_operations *ops;
00188 xnflags_t flags;
00189 unsigned int stacksize;
00190 const char *name;
00191 };
00192
00193 struct xnthread_start_attr {
00194 xnflags_t mode;
00195 int imask;
00196 xnarch_cpumask_t affinity;
00197 void (*entry)(void *cookie);
00198 void *cookie;
00199 };
00200
00201 struct xnthread_wait_context {
00202 unsigned long oldstate;
00203 };
00204
00205 typedef void (*xnasr_t)(xnsigmask_t sigs);
00206
00207 typedef struct xnthread {
00208
00209 xnarchtcb_t tcb;
00210
00211 xnflags_t state;
00212
00213 xnflags_t info;
00214
00215 struct xnsched *sched;
00216
00217 struct xnsched_class *sched_class;
00218
00219 struct xnsched_class *base_class;
00220
00221 #ifdef CONFIG_XENO_OPT_SCHED_TP
00222 struct xnsched_tpslot *tps;
00223 struct xnholder tp_link;
00224 #endif
00225 #ifdef CONFIG_XENO_OPT_SCHED_SPORADIC
00226 struct xnsched_sporadic_data *pss;
00227 #endif
00228
00229 unsigned idtag;
00230
00231 xnarch_cpumask_t affinity;
00232
00233 int bprio;
00234
00235 int cprio;
00236
00237 u_long schedlck;
00239 xnpholder_t rlink;
00240
00241 xnpholder_t plink;
00242
00243 #ifdef CONFIG_XENO_OPT_PRIOCPL
00244 xnpholder_t xlink;
00245
00246 struct xnsched *rpi;
00247 #endif
00248
00249 xnholder_t glink;
00250
00251 #define link2thread(ln, fld) container_of(ln, struct xnthread, fld)
00252
00253 xnpqueue_t claimq;
00254
00255 struct xnsynch *wchan;
00256
00257 struct xnsynch *wwake;
00258
00259 int hrescnt;
00260
00261 xntimer_t rtimer;
00262
00263 xntimer_t ptimer;
00264
00265 xnsigmask_t signals;
00266
00267 xnticks_t rrperiod;
00268
00269 xnticks_t rrcredit;
00270
00271 union {
00272 struct {
00273
00274
00275
00276
00277
00278 void *ptr;
00279 size_t size;
00280 } buffer;
00281 struct xnbufd *bufd;
00282 size_t size;
00283 } wait_u;
00284
00285
00286 struct xnthread_wait_context *wcontext;
00287
00288 struct {
00289 xnstat_counter_t ssw;
00290 xnstat_counter_t csw;
00291 xnstat_counter_t pf;
00292 xnstat_exectime_t account;
00293 xnstat_exectime_t lastperiod;
00294 } stat;
00295
00296 #ifdef CONFIG_XENO_OPT_SELECT
00297 struct xnselector *selector;
00298 #endif
00299
00300 int errcode;
00301
00302 xnasr_t asr;
00303
00304 xnflags_t asrmode;
00305
00306 int asrimask;
00307
00308 unsigned asrlevel;
00309
00310 int imask;
00311
00312 int imode;
00313
00314 struct xnsched_class *init_class;
00315
00316 union xnsched_policy_param init_schedparam;
00317
00318 struct {
00319 xnhandle_t handle;
00320 const char *waitkey;
00321 } registry;
00322
00323 struct xnthread_operations *ops;
00324
00325 char name[XNOBJECT_NAME_LEN];
00326
00327 void (*entry)(void *cookie);
00328
00329 void *cookie;
00330
00331 #ifdef CONFIG_XENO_OPT_PERVASIVE
00332 unsigned long *u_mode;
00333 #endif
00334
00335 XNARCH_DECL_DISPLAY_CONTEXT();
00336
00337 } xnthread_t;
00338
00339 #define XNHOOK_THREAD_START 1
00340 #define XNHOOK_THREAD_SWITCH 2
00341 #define XNHOOK_THREAD_DELETE 3
00342
00343 typedef struct xnhook {
00344 xnholder_t link;
00345 #define link2hook(ln) container_of(ln, xnhook_t, link)
00346 void (*routine)(struct xnthread *thread);
00347 } xnhook_t;
00348
00349 #define xnthread_name(thread) ((thread)->name)
00350 #define xnthread_clear_name(thread) do { *(thread)->name = 0; } while(0)
00351 #define xnthread_sched(thread) ((thread)->sched)
00352 #define xnthread_start_time(thread) ((thread)->stime)
00353 #define xnthread_state_flags(thread) ((thread)->state)
00354 #define xnthread_test_state(thread,flags) testbits((thread)->state,flags)
00355 #define xnthread_set_state(thread,flags) __setbits((thread)->state,flags)
00356 #define xnthread_clear_state(thread,flags) __clrbits((thread)->state,flags)
00357 #define xnthread_test_info(thread,flags) testbits((thread)->info,flags)
00358 #define xnthread_set_info(thread,flags) __setbits((thread)->info,flags)
00359 #define xnthread_clear_info(thread,flags) __clrbits((thread)->info,flags)
00360 #define xnthread_lock_count(thread) ((thread)->schedlck)
00361 #define xnthread_init_schedparam(thread) ((thread)->init_schedparam)
00362 #define xnthread_base_priority(thread) ((thread)->bprio)
00363 #define xnthread_current_priority(thread) ((thread)->cprio)
00364 #define xnthread_init_class(thread) ((thread)->init_class)
00365 #define xnthread_base_class(thread) ((thread)->base_class)
00366 #define xnthread_sched_class(thread) ((thread)->sched_class)
00367 #define xnthread_time_slice(thread) ((thread)->rrperiod)
00368 #define xnthread_time_credit(thread) ((thread)->rrcredit)
00369 #define xnthread_archtcb(thread) (&((thread)->tcb))
00370 #define xnthread_asr_level(thread) ((thread)->asrlevel)
00371 #define xnthread_pending_signals(thread) ((thread)->signals)
00372 #define xnthread_timeout(thread) xntimer_get_timeout(&(thread)->rtimer)
00373 #define xnthread_stack_size(thread) xnarch_stack_size(xnthread_archtcb(thread))
00374 #define xnthread_stack_base(thread) xnarch_stack_base(xnthread_archtcb(thread))
00375 #define xnthread_stack_end(thread) xnarch_stack_end(xnthread_archtcb(thread))
00376 #define xnthread_handle(thread) ((thread)->registry.handle)
00377 #ifdef CONFIG_XENO_OPT_TIMING_PERIODIC
00378 #define xnthread_time_base(thread) ((thread)->rtimer.base)
00379 #else
00380 #define xnthread_time_base(thread) (&nktbase)
00381 #endif
00382 #define xnthread_signaled_p(thread) ((thread)->signals != 0)
00383 #define xnthread_timed_p(thread) (!!testbits(xnthread_time_base(thread)->status, XNTBRUN))
00384 #define xnthread_user_task(thread) xnarch_user_task(xnthread_archtcb(thread))
00385 #define xnthread_user_pid(thread) \
00386 (xnthread_test_state((thread),XNROOT) || !xnthread_user_task(thread) ? \
00387 0 : xnarch_user_pid(xnthread_archtcb(thread)))
00388 #define xnthread_affinity(thread) ((thread)->affinity)
00389 #define xnthread_affine_p(thread, cpu) xnarch_cpu_isset(cpu, (thread)->affinity)
00390 #define xnthread_get_exectime(thread) xnstat_exectime_get_total(&(thread)->stat.account)
00391 #define xnthread_get_lastswitch(thread) xnstat_exectime_get_last_switch((thread)->sched)
00392 #ifdef CONFIG_XENO_OPT_PERVASIVE
00393 #define xnthread_inc_rescnt(thread) ({ (thread)->hrescnt++; })
00394 #define xnthread_dec_rescnt(thread) ({ --(thread)->hrescnt; })
00395 #define xnthread_get_rescnt(thread) ((thread)->hrescnt)
00396 #else
00397 #define xnthread_inc_rescnt(thread) do { } while (0)
00398 #define xnthread_dec_rescnt(thread) do { } while (0)
00399 #endif
00400 #ifdef CONFIG_XENO_OPT_WATCHDOG
00401 #define xnthread_amok_p(thread) xnthread_test_info(thread, XNAMOK)
00402 #define xnthread_clear_amok(thread) xnthread_clear_info(thread, XNAMOK)
00403 #else
00404 #define xnthread_amok_p(thread) ({ (void)(thread); 0; })
00405 #define xnthread_clear_amok(thread) do { (void)(thread); } while (0)
00406 #endif
00407
00408
00409 static inline int xnthread_get_denormalized_prio(struct xnthread *t, int coreprio)
00410 {
00411 return t->ops && t->ops->get_denormalized_prio
00412 ? t->ops->get_denormalized_prio(t, coreprio) : coreprio;
00413 }
00414
00415 static inline unsigned xnthread_get_magic(struct xnthread *t)
00416 {
00417 return t->ops ? t->ops->get_magic() : 0;
00418 }
00419
00420 static inline
00421 struct xnthread_wait_context *xnthread_get_wait_context(struct xnthread *thread)
00422 {
00423 return thread->wcontext;
00424 }
00425
00426 static inline
00427 int xnthread_register(struct xnthread *thread, const char *name)
00428 {
00429 return xnregistry_enter(name, thread, &xnthread_handle(thread), NULL);
00430 }
00431
00432 static inline
00433 struct xnthread *xnthread_lookup(xnhandle_t threadh)
00434 {
00435 struct xnthread *thread = (struct xnthread *)xnregistry_lookup(threadh);
00436 return (thread && xnthread_handle(thread) == threadh) ? thread : NULL;
00437 }
00438
00439 #ifdef __cplusplus
00440 extern "C" {
00441 #endif
00442
00443 int xnthread_init(struct xnthread *thread,
00444 const struct xnthread_init_attr *attr,
00445 struct xnsched *sched,
00446 struct xnsched_class *sched_class,
00447 const union xnsched_policy_param *sched_param);
00448
00449 void xnthread_cleanup_tcb(struct xnthread *thread);
00450
00451 char *xnthread_format_status(xnflags_t status, char *buf, int size);
00452
00453 int *xnthread_get_errno_location(struct xnthread *thread);
00454
00455 xnticks_t xnthread_get_timeout(struct xnthread *thread, xnticks_t tsc_ns);
00456
00457 xnticks_t xnthread_get_period(struct xnthread *thread);
00458
00459 void xnthread_prepare_wait(struct xnthread_wait_context *wc);
00460
00461 void xnthread_finish_wait(struct xnthread_wait_context *wc,
00462 void (*cleanup)(struct xnthread_wait_context *wc));
00463
00464 #ifdef __cplusplus
00465 }
00466 #endif
00467
00468 #endif
00469
00470 #endif