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', '.',        \
00085         'f', '.', '.', '.',             \
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 
00112 #define XNATOMIC  0x00000040 
00114 /* These information flags are available to the real-time interfaces */
00115 #define XNTHREAD_INFO_SPARE0  0x10000000
00116 #define XNTHREAD_INFO_SPARE1  0x20000000
00117 #define XNTHREAD_INFO_SPARE2  0x40000000
00118 #define XNTHREAD_INFO_SPARE3  0x80000000
00119 #define XNTHREAD_INFO_SPARES  0xf0000000
00120  /* Ends doxygen comment group: nucleus_info_flags */
00122 
00123 #if defined(__KERNEL__) || defined(__XENO_SIM__)
00124 
00125 #include <nucleus/stat.h>
00126 
00127 #ifdef __XENO_SIM__
00128 /* Pseudo-status (must not conflict with other bits) */
00129 #define XNRUNNING  XNTHREAD_STATE_SPARE0
00130 #define XNDELETED  XNTHREAD_STATE_SPARE1
00131 #endif /* __XENO_SIM__ */
00132 
00133 #define XNTHREAD_INVALID_ASR  ((void (*)(xnsigmask_t))0)
00134 
00135 struct xnsched;
00136 struct xnsynch;
00137 struct xnrpi;
00138 
00139 typedef void (*xnasr_t)(xnsigmask_t sigs);
00140 
00141 typedef struct xnthread {
00142 
00143     xnarchtcb_t tcb;            /* Architecture-dependent block -- Must be first */
00144 
00145     xnflags_t state;            /* Thread state flags */
00146 
00147     xnflags_t info;             /* Thread information flags */
00148 
00149     struct xnsched *sched;      /* Thread scheduler */
00150 
00151     xnarch_cpumask_t affinity;  /* Processor affinity. */
00152 
00153     int bprio;                  /* Base priority (before PIP boost) */
00154 
00155     int cprio;                  /* Current priority */
00156 
00157     u_long schedlck;            
00159     xnpholder_t rlink;          /* Thread holder in ready queue */
00160 
00161     xnpholder_t plink;          /* Thread holder in synchronization queue(s) */
00162 
00163 #if !defined(CONFIG_XENO_OPT_RPIDISABLE) && defined(CONFIG_XENO_OPT_PERVASIVE)
00164     xnpholder_t xlink;          /* Thread holder in the RPI queue (shadow only) */
00165 
00166     struct xnrpi *rpi;          /* Backlink pointer to the RPI slot (shadow only) */
00167 #endif /* !CONFIG_XENO_OPT_RPIDISABLE && CONFIG_XENO_OPT_PERVASIVE */
00168 
00169     xnholder_t glink;           /* Thread holder in global queue */
00170 
00171 #define link2thread(ln, fld)    container_of(ln, xnthread_t, fld)
00172 
00173     xnpqueue_t claimq;          /* Owned resources claimed by others (PIP) */
00174 
00175     struct xnsynch *wchan;      /* Resource the thread pends on */
00176 
00177     xntimer_t rtimer;           /* Resource timer */
00178 
00179     xntimer_t ptimer;           /* Periodic timer */
00180 
00181     xnticks_t pexpect;          /* Date of next periodic release point (raw ticks). */
00182 
00183     xnsigmask_t signals;        /* Pending core signals */
00184 
00185     xnticks_t rrperiod;         /* Allotted round-robin period (ticks) */
00186 
00187     xnticks_t rrcredit;         /* Remaining round-robin time credit (ticks) */
00188 
00189     struct {
00190         xnstat_counter_t ssw;   /* Primary -> secondary mode switch count */
00191         xnstat_counter_t csw;   /* Context switches (includes secondary -> primary switches) */
00192         xnstat_counter_t pf;    /* Number of page faults */
00193         xnstat_runtime_t account; /* Runtime accounting entity */
00194     } stat;
00195 
00196     int errcode;                /* Local errno */
00197 
00198     xnasr_t asr;                /* Asynchronous service routine */
00199 
00200     xnflags_t asrmode;          /* Thread's mode for ASR */
00201 
00202     int asrimask;               /* Thread's interrupt mask for ASR */
00203 
00204     unsigned asrlevel;          /* ASR execution level (ASRs are reentrant) */
00205 
00206     int imask;                  /* Initial interrupt mask */
00207 
00208     int imode;                  /* Initial mode */
00209 
00210     int iprio;                  /* Initial priority */
00211 
00212 #ifdef CONFIG_XENO_OPT_REGISTRY
00213     struct {
00214         xnhandle_t handle;      /* Handle in registry */
00215         const char *waitkey;    /* Pended key */
00216     } registry;
00217 #endif /* CONFIG_XENO_OPT_REGISTRY */
00218 
00219     unsigned magic;             /* Skin magic. */
00220 
00221     char name[XNOBJECT_NAME_LEN]; /* Symbolic name of thread */
00222 
00223     xnticks_t stime;            /* Start time */
00224 
00225     void (*entry)(void *cookie); /* Thread entry routine */
00226 
00227     void *cookie;               /* Cookie to pass to the entry routine */
00228 
00229     XNARCH_DECL_DISPLAY_CONTEXT();
00230 
00231 } xnthread_t;
00232 
00233 #define XNHOOK_THREAD_START  1
00234 #define XNHOOK_THREAD_SWITCH 2
00235 #define XNHOOK_THREAD_DELETE 3
00236 
00237 typedef struct xnhook {
00238 
00239     xnholder_t link;
00240 
00241 #define link2hook(ln)           container_of(ln, xnhook_t, link)
00242 
00243     void (*routine)(xnthread_t *thread);
00244 
00245 } xnhook_t;
00246 
00247 #define xnthread_name(thread)               ((thread)->name)
00248 #define xnthread_clear_name(thread)        do { *(thread)->name = 0; } while(0)
00249 #define xnthread_sched(thread)             ((thread)->sched)
00250 #define xnthread_start_time(thread)        ((thread)->stime)
00251 #define xnthread_state_flags(thread)       ((thread)->state)
00252 #define xnthread_test_state(thread,flags)  testbits((thread)->state,flags)
00253 #define xnthread_set_state(thread,flags)   __setbits((thread)->state,flags)
00254 #define xnthread_clear_state(thread,flags) __clrbits((thread)->state,flags)
00255 #define xnthread_test_info(thread,flags)   testbits((thread)->info,flags)
00256 #define xnthread_set_info(thread,flags)    __setbits((thread)->info,flags)
00257 #define xnthread_clear_info(thread,flags)  __clrbits((thread)->info,flags)
00258 #define xnthread_lock_count(thread)        ((thread)->schedlck)
00259 #define xnthread_initial_priority(thread) ((thread)->iprio)
00260 #define xnthread_base_priority(thread)     ((thread)->bprio)
00261 #define xnthread_current_priority(thread) ((thread)->cprio)
00262 #define xnthread_time_slice(thread)        ((thread)->rrperiod)
00263 #define xnthread_time_credit(thread)       ((thread)->rrcredit)
00264 #define xnthread_archtcb(thread)           (&((thread)->tcb))
00265 #define xnthread_asr_level(thread)         ((thread)->asrlevel)
00266 #define xnthread_pending_signals(thread)  ((thread)->signals)
00267 #define xnthread_timeout(thread)           xntimer_get_timeout(&(thread)->rtimer)
00268 #define xnthread_stack_size(thread)        xnarch_stack_size(xnthread_archtcb(thread))
00269 #define xnthread_handle(thread)            ((thread)->registry.handle)
00270 #define xnthread_set_magic(thread,m)       do { (thread)->magic = (m); } while(0)
00271 #define xnthread_get_magic(thread)         ((thread)->magic)
00272 #define xnthread_signaled_p(thread)        ((thread)->signals != 0)
00273 #define xnthread_user_task(thread)         xnarch_user_task(xnthread_archtcb(thread))
00274 #define xnthread_user_pid(thread) \
00275     (xnthread_test_state((thread),XNROOT) || !xnthread_user_task(thread) ? \
00276     0 : xnarch_user_pid(xnthread_archtcb(thread)))
00277 
00278 #ifdef __cplusplus
00279 extern "C" {
00280 #endif
00281 
00282 int xnthread_init(xnthread_t *thread,
00283                   const char *name,
00284                   int prio,
00285                   xnflags_t flags,
00286                   unsigned stacksize);
00287 
00288 void xnthread_cleanup_tcb(xnthread_t *thread);
00289 
00290 char *xnthread_symbolic_status(xnflags_t status, char *buf, int size);
00291 
00292 int *xnthread_get_errno_location(void);
00293 
00294 static inline xnticks_t xnthread_get_timeout(xnthread_t *thread, xnticks_t now)
00295 {
00296     xnticks_t timeout;
00297 
00298     if (!xnthread_test_state(thread,XNDELAY))
00299         return 0LL;
00300 
00301     timeout = (xntimer_get_date(&thread->rtimer) ? : xntimer_get_date(&thread->ptimer));
00302 
00303     if (timeout <= now)
00304         return 1;
00305 
00306     return timeout - now;
00307 }
00308 
00309 static inline xnticks_t xnthread_get_period(xnthread_t *thread)
00310 {
00311     xnticks_t period = 0;
00312 
00313     /*
00314      * The current thread period might be:
00315      * - the value of the timer interval for periodic threads (ns/ticks)
00316      * - or, the value of the alloted round-robin quantum (ticks)
00317      * - or zero, meaning "no periodic activity".
00318      */
00319        
00320     if (xntimer_running_p(&thread->ptimer))
00321         period = xntimer_get_interval(&thread->ptimer);
00322     else if (xnthread_test_state(thread,XNRRB))
00323         period = xnthread_time_slice(thread);
00324 
00325     return period;
00326 }
00327 
00328 #ifdef __cplusplus
00329 }
00330 #endif
00331 
00332 #endif /* __KERNEL__ || __XENO_SIM__ */
00333 
00334 #endif /* !_XENO_NUCLEUS_THREAD_H */

Generated on Sun Dec 9 10:37:17 2007 for Xenomai API by  doxygen 1.5.3