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/timer.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 XNHELD    0x00000400 
00047 #define XNBOOST   0x00000800 
00048 #define XNDEBUG   0x00001000 
00049 #define XNLOCK    0x00002000 
00050 #define XNRRB     0x00004000 
00051 #define XNASDI    0x00008000 
00053 /* Some skins may depend on the following fields to live in the high
00054    16-bit word, in order to be combined with the emulated RTOS flags
00055    which use the low one, so don't change them carelessly. */
00056 
00057 #define XNSHIELD  0x00010000 
00058 #define XNTRAPSW  0x00020000 
00059 #define XNRPIOFF  0x00040000 
00061 #define XNFPU     0x00100000 
00062 #define XNSHADOW  0x00200000 
00063 #define XNROOT    0x00400000  /* Ends doxygen comment group: nucleus_state_flags */
00066 
00067 /*
00068   Must follow the declaration order of the above bits. Status symbols
00069   are defined as follows:
00070   'S' -> Forcibly suspended.
00071   'w'/'W' -> Waiting for a resource, with or without timeout.
00072   'D' -> Delayed (without any other wait condition).
00073   'R' -> Runnable.
00074   'U' -> Unstarted or dormant.
00075   'X' -> Relaxed shadow.
00076   'H' -> Held thread.
00077   'b' -> Priority boost undergoing.
00078   'T' -> Ptraced and stopped.
00079   'l' -> Locks scheduler.
00080   'r' -> Undergoes round-robin.
00081   's' -> Interrupt shield enabled.
00082   't' -> Mode switches trapped.
00083   'o' -> Priority coupling off.
00084   'f' -> FPU enabled (for kernel threads).
00085 */
00086 #define XNTHREAD_STATE_LABELS  {        \
00087         'S', 'W', 'D', 'R', 'U',        \
00088         '.', '.', '.', '.', 'X',        \
00089         'H', 'b', 'T', 'l', 'r',        \
00090         '.', 's', 't', 'o', '.',        \
00091         'f', '.', '.',                  \
00092 }
00093 
00094 #define XNTHREAD_BLOCK_BITS   (XNSUSP|XNPEND|XNDELAY|XNDORMANT|XNRELAX|XNHELD)
00095 #define XNTHREAD_MODE_BITS    (XNLOCK|XNRRB|XNASDI|XNSHIELD|XNTRAPSW|XNRPIOFF)
00096 
00097 /* These state flags are available to the real-time interfaces */
00098 #define XNTHREAD_STATE_SPARE0  0x10000000
00099 #define XNTHREAD_STATE_SPARE1  0x20000000
00100 #define XNTHREAD_STATE_SPARE2  0x40000000
00101 #define XNTHREAD_STATE_SPARE3  0x80000000
00102 #define XNTHREAD_STATE_SPARES  0xf0000000
00103 
00110 /* Information flags */
00111 
00112 #define XNTIMEO   0x00000001 
00113 #define XNRMID    0x00000002 
00114 #define XNBREAK   0x00000004 
00115 #define XNKICKED  0x00000008 
00116 #define XNWAKEN   0x00000010 
00117 #define XNROBBED  0x00000020 
00118 #define XNATOMIC  0x00000040 
00119 #define XNAFFSET  0x00000080 
00121 /* These information flags are available to the real-time interfaces */
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  /* Ends doxygen comment group: nucleus_info_flags */
00129 
00130 #if defined(__KERNEL__) || defined(__XENO_SIM__)
00131 
00132 #include <nucleus/stat.h>
00133 
00134 #ifdef __XENO_SIM__
00135 /* Pseudo-status (must not conflict with other bits) */
00136 #define XNRUNNING  XNTHREAD_STATE_SPARE0
00137 #define XNDELETED  XNTHREAD_STATE_SPARE1
00138 #endif /* __XENO_SIM__ */
00139 
00140 #define XNTHREAD_INVALID_ASR  ((void (*)(xnsigmask_t))0)
00141 
00142 struct xnthread;
00143 struct xnsched;
00144 struct xnsynch;
00145 struct xnrpi;
00146 
00147 typedef struct xnthrops {
00148 
00149         int (*get_denormalized_prio)(struct xnthread *);
00150         unsigned (*get_magic)(void);
00151 
00152 } xnthrops_t;
00153 
00154 typedef void (*xnasr_t)(xnsigmask_t sigs);
00155 
00156 typedef struct xnthread {
00157 
00158     xnarchtcb_t tcb;            /* Architecture-dependent block -- Must be first */
00159 
00160     xnflags_t state;            /* Thread state flags */
00161 
00162     xnflags_t info;             /* Thread information flags */
00163 
00164     struct xnsched *sched;      /* Thread scheduler */
00165 
00166     xnarch_cpumask_t affinity;  /* Processor affinity. */
00167 
00168     int bprio;                  /* Base priority (before PIP boost) */
00169 
00170     int cprio;                  /* Current priority */
00171 
00172     u_long schedlck;            
00174     xnpholder_t rlink;          /* Thread holder in ready queue */
00175 
00176     xnpholder_t plink;          /* Thread holder in synchronization queue(s) */
00177 
00178 #ifdef CONFIG_XENO_OPT_PRIOCPL
00179     xnpholder_t xlink;          /* Thread holder in the RPI queue (shadow only) */
00180 
00181     struct xnrpi *rpi;          /* Backlink pointer to the RPI slot (shadow only) */
00182 #endif /* CONFIG_XENO_OPT_PRIOCPL */
00183 
00184     xnholder_t glink;           /* Thread holder in global queue */
00185 
00186 #define link2thread(ln, fld)    container_of(ln, xnthread_t, fld)
00187 
00188     xnpqueue_t claimq;          /* Owned resources claimed by others (PIP) */
00189 
00190     struct xnsynch *wchan;      /* Resource the thread pends on */
00191 
00192     xntimer_t rtimer;           /* Resource timer */
00193 
00194     xntimer_t ptimer;           /* Periodic timer */
00195 
00196     xnsigmask_t signals;        /* Pending core signals */
00197 
00198     xnticks_t rrperiod;         /* Allotted round-robin period (ticks) */
00199 
00200     xnticks_t rrcredit;         /* Remaining round-robin time credit (ticks) */
00201 
00202     struct {
00203         xnstat_counter_t ssw;   /* Primary -> secondary mode switch count */
00204         xnstat_counter_t csw;   /* Context switches (includes secondary -> primary switches) */
00205         xnstat_counter_t pf;    /* Number of page faults */
00206         xnstat_exectime_t account; /* Execution time accounting entity */
00207         xnstat_exectime_t lastperiod; /* Interval marker for execution time reports */
00208     } stat;
00209 
00210     int errcode;                /* Local errno */
00211 
00212     xnasr_t asr;                /* Asynchronous service routine */
00213 
00214     xnflags_t asrmode;          /* Thread's mode for ASR */
00215 
00216     int asrimask;               /* Thread's interrupt mask for ASR */
00217 
00218     unsigned asrlevel;          /* ASR execution level (ASRs are reentrant) */
00219 
00220     int imask;                  /* Initial interrupt mask */
00221 
00222     int imode;                  /* Initial mode */
00223 
00224     int iprio;                  /* Initial priority */
00225 
00226 #ifdef CONFIG_XENO_OPT_REGISTRY
00227     struct {
00228         xnhandle_t handle;      /* Handle in registry */
00229         const char *waitkey;    /* Pended key */
00230     } registry;
00231 #endif /* CONFIG_XENO_OPT_REGISTRY */
00232 
00233     xnthrops_t *ops;            /* Thread class operations. */
00234 
00235     char name[XNOBJECT_NAME_LEN]; /* Symbolic name of thread */
00236 
00237     void (*entry)(void *cookie); /* Thread entry routine */
00238 
00239     void *cookie;               /* Cookie to pass to the entry routine */
00240 
00241     XNARCH_DECL_DISPLAY_CONTEXT();
00242 
00243 } xnthread_t;
00244 
00245 #define XNHOOK_THREAD_START  1
00246 #define XNHOOK_THREAD_SWITCH 2
00247 #define XNHOOK_THREAD_DELETE 3
00248 
00249 typedef struct xnhook {
00250 
00251         xnholder_t link;
00252 #define link2hook(ln)           container_of(ln, xnhook_t, link)
00253 
00254         void (*routine)(xnthread_t *thread);
00255 
00256 } xnhook_t;
00257 
00258 #define xnthread_name(thread)               ((thread)->name)
00259 #define xnthread_clear_name(thread)        do { *(thread)->name = 0; } while(0)
00260 #define xnthread_sched(thread)             ((thread)->sched)
00261 #define xnthread_start_time(thread)        ((thread)->stime)
00262 #define xnthread_state_flags(thread)       ((thread)->state)
00263 #define xnthread_test_state(thread,flags)  testbits((thread)->state,flags)
00264 #define xnthread_set_state(thread,flags)   __setbits((thread)->state,flags)
00265 #define xnthread_clear_state(thread,flags) __clrbits((thread)->state,flags)
00266 #define xnthread_test_info(thread,flags)   testbits((thread)->info,flags)
00267 #define xnthread_set_info(thread,flags)    __setbits((thread)->info,flags)
00268 #define xnthread_clear_info(thread,flags)  __clrbits((thread)->info,flags)
00269 #define xnthread_lock_count(thread)        ((thread)->schedlck)
00270 #define xnthread_initial_priority(thread) ((thread)->iprio)
00271 #define xnthread_base_priority(thread)     ((thread)->bprio)
00272 #define xnthread_current_priority(thread) ((thread)->cprio)
00273 #define xnthread_time_slice(thread)        ((thread)->rrperiod)
00274 #define xnthread_time_credit(thread)       ((thread)->rrcredit)
00275 #define xnthread_archtcb(thread)           (&((thread)->tcb))
00276 #define xnthread_asr_level(thread)         ((thread)->asrlevel)
00277 #define xnthread_pending_signals(thread)  ((thread)->signals)
00278 #define xnthread_timeout(thread)           xntimer_get_timeout(&(thread)->rtimer)
00279 #define xnthread_stack_size(thread)        xnarch_stack_size(xnthread_archtcb(thread))
00280 #define xnthread_handle(thread)            ((thread)->registry.handle)
00281 #ifdef CONFIG_XENO_OPT_TIMING_PERIODIC
00282 #define xnthread_time_base(thread)         ((thread)->rtimer.base)
00283 #else /* !CONFIG_XENO_OPT_TIMING_PERIODIC */
00284 #define xnthread_time_base(thread)         (&nktbase)
00285 #endif /* !CONFIG_XENO_OPT_TIMING_PERIODIC */
00286 #define xnthread_signaled_p(thread)        ((thread)->signals != 0)
00287 #define xnthread_timed_p(thread)              (!!testbits(xnthread_time_base(thread)->status, XNTBRUN))
00288 #define xnthread_user_task(thread)         xnarch_user_task(xnthread_archtcb(thread))
00289 #define xnthread_user_pid(thread) \
00290     (xnthread_test_state((thread),XNROOT) || !xnthread_user_task(thread) ? \
00291     0 : xnarch_user_pid(xnthread_archtcb(thread)))
00292 #define xnthread_affinity(thread)          ((thread)->affinity)
00293 #define xnthread_affine_p(thread, cpu)     xnarch_cpu_isset(cpu, (thread)->affinity)
00294 #define xnthread_get_exectime(thread)      xnstat_exectime_get_total(&(thread)->stat.account)
00295 #define xnthread_get_lastswitch(thread)    xnstat_exectime_get_last_switch((thread)->sched)
00296 
00297 /* Class-level operations for threads. */
00298 static inline int xnthread_get_denormalized_prio(xnthread_t *t)
00299 {
00300         return t->ops ? t->ops->get_denormalized_prio(t) : xnthread_current_priority(t);
00301 }
00302 
00303 static inline unsigned xnthread_get_magic(xnthread_t *t)
00304 {
00305         return t->ops ? t->ops->get_magic() : 0;
00306 }
00307 
00308 #ifdef __cplusplus
00309 extern "C" {
00310 #endif
00311 
00312 int xnthread_init(xnthread_t *thread,
00313                   xntbase_t *tbase,
00314                   const char *name,
00315                   int prio,
00316                   xnflags_t flags,
00317                   unsigned stacksize,
00318                   xnthrops_t *ops);
00319 
00320 void xnthread_cleanup_tcb(xnthread_t *thread);
00321 
00322 char *xnthread_symbolic_status(xnflags_t status, char *buf, int size);
00323 
00324 int *xnthread_get_errno_location(void);
00325 
00326 static inline xnticks_t xnthread_get_timeout(xnthread_t *thread, xnticks_t tsc_ns)
00327 {
00328         xnticks_t timeout;
00329         xntimer_t *timer;
00330 
00331         if (!xnthread_test_state(thread,XNDELAY))
00332                 return 0LL;
00333 
00334         if (xntimer_running_p(&thread->rtimer))
00335                 timer = &thread->rtimer;
00336         else if (xntimer_running_p(&thread->ptimer))
00337                 timer = &thread->ptimer;
00338         else
00339                 return 0LL;
00340 
00341         /*
00342          * The caller should have masked IRQs while collecting the
00343          * timeout(s), so no tick could be announced in the meantime,
00344          * and all timeouts would always use the same epoch
00345          * value. Obviously, this can't be a valid assumption for
00346          * aperiodic timers, which values are based on the hardware
00347          * TSC, and as such the current time will change regardless of
00348          * the interrupt state; for this reason, we use the "tsc_ns"
00349          * input parameter (TSC converted to nanoseconds) the caller
00350          * has passed us as the epoch value instead.
00351          */
00352 
00353         if (xntbase_periodic_p(xnthread_time_base(thread)))
00354                 return xntimer_get_timeout(timer);
00355 
00356         timeout = xntimer_get_date(timer);
00357 
00358         if (timeout <= tsc_ns)
00359                 return 1;
00360 
00361         return timeout - tsc_ns;
00362 }
00363 
00364 static inline xnticks_t xnthread_get_period(xnthread_t *thread)
00365 {
00366         xnticks_t period = 0;
00367 
00368         /*
00369          * The current thread period might be:
00370          * - the value of the timer interval for periodic threads (ns/ticks)
00371          * - or, the value of the alloted round-robin quantum (ticks)
00372          * - or zero, meaning "no periodic activity".
00373          */
00374        
00375         if (xntimer_running_p(&thread->ptimer))
00376                 period = xntimer_get_interval(&thread->ptimer);
00377         else if (xnthread_test_state(thread,XNRRB))
00378                 period = xnthread_time_slice(thread);
00379 
00380         return period;
00381 }
00382 
00383 #ifdef __cplusplus
00384 }
00385 #endif
00386 
00387 #endif /* __KERNEL__ || __XENO_SIM__ */
00388 
00389 #endif /* !_XENO_NUCLEUS_THREAD_H */

Generated on Mon Mar 24 18:02:40 2008 for Xenomai API by  doxygen 1.5.3