include/nucleus/thread.h

00001 /* 
00002  * 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 
00020 #ifndef _XENO_NUCLEUS_THREAD_H
00021 #define _XENO_NUCLEUS_THREAD_H
00022 
00023 #include <nucleus/timer.h>
00024 
00031 /* State flags */
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 
00050 #define XNSHIELD  0x00010000 
00051 #define XNTRAPSW  0x00020000 
00052 #define XNRPIOFF  0x00040000 
00054 #define XNFPU     0x00100000 
00055 #define XNSHADOW  0x00200000 
00056 #define XNROOT    0x00400000 
00057 #define XNINVPS   0x00800000  /* Ends doxygen comment group: nucleus_state_flags */
00060 
00061 /*
00062   Must follow the declaration order of the above bits. Status symbols
00063   are defined as follows:
00064   'S' -> Forcibly suspended.
00065   'w'/'W' -> Waiting for a resource, with or without timeout.
00066   'D' -> Delayed (without any other wait condition).
00067   'R' -> Runnable.
00068   'U' -> Unstarted or dormant.
00069   'X' -> Relaxed shadow.
00070   'H' -> Held thread.
00071   'b' -> Priority boost undergoing.
00072   'T' -> Ptraced and stopped.
00073   'l' -> Locks scheduler.
00074   'r' -> Undergoes round-robin.
00075   's' -> Interrupt shield enabled.
00076   't' -> Mode switches trapped.
00077   'o' -> Priority coupling off.
00078   'f' -> FPU enabled (for kernel threads).
00079 */
00080 #define XNTHREAD_STATE_LABELS  {        \
00081         'S', 'W', 'D', 'R', 'U',        \
00082         '.', '.', '.', '.', 'X',        \
00083         'H', 'b', 'T', 'l', 'r',        \
00084         '.', 's', 't', 'o', 'f',        \
00085         '.', '.', '.',                  \
00086 }
00087 
00088 #define XNTHREAD_BLOCK_BITS   (XNSUSP|XNPEND|XNDELAY|XNDORMANT|XNRELAX|XNHELD)
00089 #define XNTHREAD_MODE_BITS    (XNLOCK|XNRRB|XNASDI|XNSHIELD|XNTRAPSW|XNRPIOFF)
00090 
00091 /* These state flags are available to the real-time interfaces */
00092 #define XNTHREAD_STATE_SPARE0  0x10000000
00093 #define XNTHREAD_STATE_SPARE1  0x20000000
00094 #define XNTHREAD_STATE_SPARE2  0x40000000
00095 #define XNTHREAD_STATE_SPARE3  0x80000000
00096 #define XNTHREAD_STATE_SPARES  0xf0000000
00097 
00104 /* Information flags */
00105 
00106 #define XNTIMEO   0x00000001 
00107 #define XNRMID    0x00000002 
00108 #define XNBREAK   0x00000004 
00109 #define XNKICKED  0x00000008 
00110 #define XNWAKEN   0x00000010 
00111 #define XNROBBED  0x00000020 
00113 /* These information flags are available to the real-time interfaces */
00114 #define XNTHREAD_INFO_SPARE0  0x10000000
00115 #define XNTHREAD_INFO_SPARE1  0x20000000
00116 #define XNTHREAD_INFO_SPARE2  0x40000000
00117 #define XNTHREAD_INFO_SPARE3  0x80000000
00118 #define XNTHREAD_INFO_SPARES  0xf0000000
00119  /* Ends doxygen comment group: nucleus_info_flags */
00121 
00122 #if defined(__KERNEL__) || defined(__XENO_SIM__)
00123 
00124 #include <nucleus/stat.h>
00125 
00126 #ifdef __XENO_SIM__
00127 /* Pseudo-status (must not conflict with other bits) */
00128 #define XNRUNNING  XNTHREAD_STATE_SPARE0
00129 #define XNDELETED  XNTHREAD_STATE_SPARE1
00130 #endif /* __XENO_SIM__ */
00131 
00132 #define XNTHREAD_INVALID_ASR  ((void (*)(xnsigmask_t))0)
00133 
00134 struct xnsched;
00135 struct xnsynch;
00136 
00137 typedef void (*xnasr_t)(xnsigmask_t sigs);
00138 
00139 typedef struct xnthread {
00140 
00141     xnarchtcb_t tcb;            /* Architecture-dependent block -- Must be first */
00142 
00143     xnflags_t state;            /* Thread state flags */
00144 
00145     xnflags_t info;             /* Thread information flags */
00146 
00147     struct xnsched *sched;      /* Thread scheduler */
00148 
00149     xnarch_cpumask_t affinity;  /* Processor affinity. */
00150 
00151     int bprio;                  /* Base priority (before PIP boost) */
00152 
00153     int cprio;                  /* Current priority */
00154 
00155     xnpholder_t rlink;          /* Thread holder in ready queue */
00156 
00157     xnpholder_t plink;          /* Thread holder in synchronization queue(s) */
00158 
00159     xnholder_t glink;           /* Thread holder in global queue */
00160 
00161 /* We don't want side-effects on laddr here! */
00162 #define link2thread(laddr,link) \
00163 ((xnthread_t *)(((char *)laddr) - (int)(&((xnthread_t *)0)->link)))
00164 
00165     xnpqueue_t claimq;          /* Owned resources claimed by others (PIP) */
00166 
00167     struct xnsynch *wchan;      /* Resource the thread pends on */
00168 
00169     xntimer_t rtimer;           /* Resource timer */
00170 
00171     xntimer_t ptimer;           /* Periodic timer */
00172 
00173     xnticks_t pexpect;          /* Date of next periodic release point (raw ticks). */
00174 
00175     xnsigmask_t signals;        /* Pending core signals */
00176 
00177     xnticks_t rrperiod;         /* Allotted round-robin period (ticks) */
00178 
00179     xnticks_t rrcredit;         /* Remaining round-robin time credit (ticks) */
00180 
00181     struct {
00182         xnstat_counter_t ssw;   /* Primary -> secondary mode switch count */
00183         xnstat_counter_t csw;   /* Context switches (includes secondary -> primary switches) */
00184         xnstat_counter_t pf;    /* Number of page faults */
00185         xnstat_runtime_t account; /* Runtime accounting entity */
00186     } stat;
00187 
00188     int errcode;                /* Local errno */
00189 
00190     xnasr_t asr;                /* Asynchronous service routine */
00191 
00192     xnflags_t asrmode;          /* Thread's mode for ASR */
00193 
00194     int asrimask;               /* Thread's interrupt mask for ASR */
00195 
00196     unsigned asrlevel;          /* ASR execution level (ASRs are reentrant) */
00197 
00198     int imask;                  /* Initial interrupt mask */
00199 
00200     int imode;                  /* Initial mode */
00201 
00202     int iprio;                  /* Initial priority */
00203 
00204 #ifdef CONFIG_XENO_OPT_REGISTRY
00205     struct {
00206         xnhandle_t handle;      /* Handle in registry */
00207         const char *waitkey;    /* Pended key */
00208     } registry;
00209 #endif /* CONFIG_XENO_OPT_REGISTRY */
00210 
00211     unsigned magic;             /* Skin magic. */
00212 
00213     char name[XNOBJECT_NAME_LEN]; /* Symbolic name of thread */
00214 
00215     xnticks_t stime;            /* Start time */
00216 
00217     void (*entry)(void *cookie); /* Thread entry routine */
00218 
00219     void *cookie;               /* Cookie to pass to the entry routine */
00220 
00221     XNARCH_DECL_DISPLAY_CONTEXT();
00222 
00223 } xnthread_t;
00224 
00225 #define XNHOOK_THREAD_START  1
00226 #define XNHOOK_THREAD_SWITCH 2
00227 #define XNHOOK_THREAD_DELETE 3
00228 
00229 typedef struct xnhook {
00230 
00231     xnholder_t link;
00232 
00233 #define link2hook(laddr) \
00234 ((xnhook_t *)(((char *)laddr) - (int)(&((xnhook_t *)0)->link)))
00235 
00236     void (*routine)(xnthread_t *thread);
00237 
00238 } xnhook_t;
00239 
00240 #define xnthread_name(thread)               ((thread)->name)
00241 #define xnthread_clear_name(thread)        do { *(thread)->name = 0; } while(0)
00242 #define xnthread_sched(thread)             ((thread)->sched)
00243 #define xnthread_start_time(thread)        ((thread)->stime)
00244 #define xnthread_state_flags(thread)       ((thread)->state)
00245 #define xnthread_test_state(thread,flags)  testbits((thread)->state,flags)
00246 #define xnthread_set_state(thread,flags)   __setbits((thread)->state,flags)
00247 #define xnthread_clear_state(thread,flags) __clrbits((thread)->state,flags)
00248 #define xnthread_test_info(thread,flags)   testbits((thread)->info,flags)
00249 #define xnthread_set_info(thread,flags)    __setbits((thread)->info,flags)
00250 #define xnthread_clear_info(thread,flags)  __clrbits((thread)->info,flags)
00251 #define xnthread_initial_priority(thread) ((thread)->iprio)
00252 #define xnthread_base_priority(thread)     ((thread)->bprio)
00253 #define xnthread_current_priority(thread) ((thread)->cprio)
00254 #define xnthread_time_slice(thread)        ((thread)->rrperiod)
00255 #define xnthread_time_credit(thread)       ((thread)->rrcredit)
00256 #define xnthread_archtcb(thread)           (&((thread)->tcb))
00257 #define xnthread_asr_level(thread)         ((thread)->asrlevel)
00258 #define xnthread_pending_signals(thread)  ((thread)->signals)
00259 #define xnthread_timeout(thread)           xntimer_get_timeout(&(thread)->rtimer)
00260 #define xnthread_stack_size(thread)        xnarch_stack_size(xnthread_archtcb(thread))
00261 #define xnthread_handle(thread)            ((thread)->registry.handle)
00262 #define xnthread_set_magic(thread,m)       do { (thread)->magic = (m); } while(0)
00263 #define xnthread_get_magic(thread)         ((thread)->magic)
00264 #define xnthread_signaled_p(thread)        ((thread)->signals != 0)
00265 #define xnthread_user_task(thread)         xnarch_user_task(xnthread_archtcb(thread))
00266 #define xnthread_user_pid(thread) \
00267     (xnthread_test_state((thread),XNROOT) || !xnthread_user_task(thread) ? \
00268     0 : xnarch_user_pid(xnthread_archtcb(thread)))
00269 
00270 #ifdef __cplusplus
00271 extern "C" {
00272 #endif
00273 
00274 int xnthread_init(xnthread_t *thread,
00275                   const char *name,
00276                   int prio,
00277                   xnflags_t flags,
00278                   unsigned stacksize);
00279 
00280 void xnthread_cleanup_tcb(xnthread_t *thread);
00281 
00282 char *xnthread_symbolic_status(xnflags_t status, char *buf, int size);
00283 
00284 int *xnthread_get_errno_location(void);
00285 
00286 static inline xnticks_t xnthread_get_timeout(xnthread_t *thread, xnticks_t now)
00287 {
00288     xnticks_t timeout;
00289 
00290     if (!xnthread_test_state(thread,XNDELAY))
00291         return 0LL;
00292 
00293     timeout = (xntimer_get_date(&thread->rtimer) ? : xntimer_get_date(&thread->ptimer));
00294 
00295     if (timeout <= now)
00296         return 1;
00297 
00298     return timeout - now;
00299 }
00300 
00301 static inline xnticks_t xnthread_get_period(xnthread_t *thread)
00302 {
00303     xnticks_t period = 0;
00304 
00305     /*
00306      * The current thread period might be:
00307      * - the value of the timer interval for periodic threads (ns/ticks)
00308      * - or, the value of the alloted round-robin quantum (ticks)
00309      * - or zero, meaning "no periodic activity".
00310      */
00311        
00312     if (xntimer_running_p(&thread->ptimer))
00313         period = xntimer_get_interval(&thread->ptimer);
00314     else if (xnthread_test_state(thread,XNRRB))
00315         period = xnthread_time_slice(thread);
00316 
00317     return period;
00318 }
00319 
00320 #ifdef __cplusplus
00321 }
00322 #endif
00323 
00324 #endif /* __KERNEL__ || __XENO_SIM__ */
00325 
00326 #endif /* !_XENO_NUCLEUS_THREAD_H */

Generated on Mon Dec 25 13:57:10 2006 for Xenomai API by  doxygen 1.4.6