• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

include/nucleus/thread.h

00001 /*
00002  * @note Copyright (C) 2001,2002,2003 Philippe Gerum <rpm@xenomai.org>.
00003  *
00004  * Xenomai is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published
00006  * by the Free Software Foundation; either version 2 of the License,
00007  * or (at your option) any later version.
00008  *
00009  * Xenomai is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with Xenomai; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00017  * 02111-1307, USA.
00018  *
00019  * \ingroup thread
00020  */
00021 
00022 #ifndef _XENO_NUCLEUS_THREAD_H
00023 #define _XENO_NUCLEUS_THREAD_H
00024 
00025 #include <nucleus/types.h>
00026 
00033 /* State flags */
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  * Some skins may depend on the following fields to live in the high
00057  * 16-bit word, in order to be combined with the emulated RTOS flags
00058  * which use the low one, so don't change them carelessly.
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  /* Ends doxygen comment group: nucleus_state_flags */
00068 
00069 /*
00070   Must follow the declaration order of the above bits. Status symbols
00071   are defined as follows:
00072   'S' -> Forcibly suspended.
00073   'w'/'W' -> Waiting for a resource, with or without timeout.
00074   'D' -> Delayed (without any other wait condition).
00075   'R' -> Runnable.
00076   'U' -> Unstarted or dormant.
00077   'X' -> Relaxed shadow.
00078   'H' -> Held in emergency.
00079   'b' -> Priority boost undergoing.
00080   'T' -> Ptraced and stopped.
00081   'l' -> Locks scheduler.
00082   'r' -> Undergoes round-robin.
00083   's' -> Interrupt shield enabled.
00084   't' -> Mode switches trapped.
00085   'o' -> Priority coupling off.
00086   'f' -> FPU enabled (for kernel threads).
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 /* These state flags are available to the real-time interfaces */
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 /* Information flags */
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 /* These information flags are available to the real-time interfaces */
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  /* Ends doxygen comment group: nucleus_info_flags */
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 /* Pseudo-status (must not conflict with other bits) */
00165 #define XNRUNNING  XNTHREAD_STATE_SPARE0
00166 #define XNDELETED  XNTHREAD_STATE_SPARE1
00167 #endif /* __XENO_SIM__ */
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;                /* Architecture-dependent block -- Must be first */
00210 
00211         xnflags_t state;                /* Thread state flags */
00212 
00213         xnflags_t info;                 /* Thread information flags */
00214 
00215         struct xnsched *sched;          /* Thread scheduler */
00216 
00217         struct xnsched_class *sched_class; /* Current scheduling class */
00218 
00219         struct xnsched_class *base_class; /* Base scheduling class */
00220 
00221 #ifdef CONFIG_XENO_OPT_SCHED_TP
00222         struct xnsched_tpslot *tps;     /* Current partition slot for TP scheduling */
00223         struct xnholder tp_link;        /* Link in per-sched TP thread queue */
00224 #endif
00225 #ifdef CONFIG_XENO_OPT_SCHED_SPORADIC
00226         struct xnsched_sporadic_data *pss; /* Sporadic scheduling data. */
00227 #endif
00228 
00229         unsigned idtag;                 /* Unique ID tag */
00230 
00231         xnarch_cpumask_t affinity;      /* Processor affinity. */
00232 
00233         int bprio;                      /* Base priority (before PIP boost) */
00234 
00235         int cprio;                      /* Current priority */
00236 
00237         u_long schedlck;                
00239         xnpholder_t rlink;              /* Thread holder in ready queue */
00240 
00241         xnpholder_t plink;              /* Thread holder in synchronization queue(s) */
00242 
00243 #ifdef CONFIG_XENO_OPT_PRIOCPL
00244         xnpholder_t xlink;              /* Thread holder in the RPI queue (shadow only) */
00245 
00246         struct xnsched *rpi;            /* Backlink pointer to the RPI slot (shadow only) */
00247 #endif /* CONFIG_XENO_OPT_PRIOCPL */
00248 
00249         xnholder_t glink;               /* Thread holder in global queue */
00250 
00251 #define link2thread(ln, fld)    container_of(ln, struct xnthread, fld)
00252 
00253         xnpqueue_t claimq;              /* Owned resources claimed by others (PIP) */
00254 
00255         struct xnsynch *wchan;          /* Resource the thread pends on */
00256 
00257         struct xnsynch *wwake;          /* Wait channel the thread was resumed from */
00258 
00259         int hrescnt;                    /* Held resources count */
00260 
00261         xntimer_t rtimer;               /* Resource timer */
00262 
00263         xntimer_t ptimer;               /* Periodic timer */
00264 
00265         xnsigmask_t signals;            /* Pending core signals */
00266 
00267         xnticks_t rrperiod;             /* Allotted round-robin period (ticks) */
00268 
00269         xnticks_t rrcredit;             /* Remaining round-robin time credit (ticks) */
00270 
00271         union {
00272                 struct {
00273                         /*
00274                          * XXX: the buffer struct should disappear as
00275                          * soon as all IPCs are converted to use
00276                          * buffer descriptors instead (bufd).
00277                          */
00278                         void *ptr;
00279                         size_t size;
00280                 } buffer;
00281                 struct xnbufd *bufd;
00282                 size_t size;
00283         } wait_u;
00284 
00285         /* Active wait context - Obsoletes wait_u. */
00286         struct xnthread_wait_context *wcontext;
00287 
00288         struct {
00289                 xnstat_counter_t ssw;   /* Primary -> secondary mode switch count */
00290                 xnstat_counter_t csw;   /* Context switches (includes secondary -> primary switches) */
00291                 xnstat_counter_t pf;    /* Number of page faults */
00292                 xnstat_exectime_t account; /* Execution time accounting entity */
00293                 xnstat_exectime_t lastperiod; /* Interval marker for execution time reports */
00294         } stat;
00295 
00296 #ifdef CONFIG_XENO_OPT_SELECT
00297         struct xnselector *selector;    /* For select. */
00298 #endif /* CONFIG_XENO_OPT_SELECT */
00299 
00300         int errcode;                    /* Local errno */
00301 
00302         xnasr_t asr;                    /* Asynchronous service routine */
00303 
00304         xnflags_t asrmode;              /* Thread's mode for ASR */
00305 
00306         int asrimask;                   /* Thread's interrupt mask for ASR */
00307 
00308         unsigned asrlevel;              /* ASR execution level (ASRs are reentrant) */
00309 
00310         int imask;                      /* Initial interrupt mask */
00311 
00312         int imode;                      /* Initial mode */
00313 
00314         struct xnsched_class *init_class; /* Initial scheduling class */
00315 
00316         union xnsched_policy_param init_schedparam; /* Initial scheduling parameters */
00317 
00318         struct {
00319                 xnhandle_t handle;      /* Handle in registry */
00320                 const char *waitkey;    /* Pended key */
00321         } registry;
00322 
00323         struct xnthread_operations *ops; /* Thread class operations. */
00324 
00325         char name[XNOBJECT_NAME_LEN]; /* Symbolic name of thread */
00326 
00327         void (*entry)(void *cookie); /* Thread entry routine */
00328 
00329         void *cookie;           /* Cookie to pass to the entry routine */
00330 
00331 #ifdef CONFIG_XENO_OPT_PERVASIVE
00332         unsigned long *u_mode;  /* Thread mode variable shared with userland. */
00333 #endif /* CONFIG_XENO_OPT_PERVASIVE */
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 /* !CONFIG_XENO_OPT_TIMING_PERIODIC */
00380 #define xnthread_time_base(thread)         (&nktbase)
00381 #endif /* !CONFIG_XENO_OPT_TIMING_PERIODIC */
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 /* !CONFIG_XENO_OPT_PERVASIVE */
00397 #define xnthread_inc_rescnt(thread)        do { } while (0)
00398 #define xnthread_dec_rescnt(thread)        do { } while (0)
00399 #endif /* !CONFIG_XENO_OPT_PERVASIVE */
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 /* !CONFIG_XENO_OPT_WATCHDOG */
00404 #define xnthread_amok_p(thread)            ({ (void)(thread); 0; })
00405 #define xnthread_clear_amok(thread)        do { (void)(thread); } while (0)
00406 #endif /* !CONFIG_XENO_OPT_WATCHDOG */
00407 
00408 /* Class-level operations for threads. */
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 /* __KERNEL__ || __XENO_SIM__ */
00469 
00470 #endif /* !_XENO_NUCLEUS_THREAD_H */

Generated on Tue Jul 10 2012 20:41:22 for Xenomai API by  doxygen 1.7.1