thread.h

00001 /*
00002  * Copyright (C) 2001,2002,2003 Philippe Gerum <rpm@xenomai.org>.
00003  *
00004  * RTAI/fusion 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  * RTAI/fusion 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 RTAI/fusion; 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 _RTAI_NUCLEUS_THREAD_H
00021 #define _RTAI_NUCLEUS_THREAD_H
00022 
00023 #include <nucleus/timer.h>
00024 
00025 /* Status flags */
00026 #define XNSUSP    0x00000001    /* Suspended */
00027 #define XNPEND    0x00000002    /* Sleep-wait for a resource */
00028 #define XNDELAY   0x00000004    /* Delayed */
00029 #define XNREADY   0x00000008    /* Linked to the ready queue */
00030 #define XNDORMANT 0x00000010    /* Not started yet or killed */
00031 #define XNZOMBIE  0x00000020    /* Zombie thread in deletion process */
00032 #define XNRESTART 0x00000040    /* Restarting thread */
00033 #define XNSTARTED 0x00000080    /* Could be restarted */
00034 #define XNRELAX   0x00000100    /* Relaxed shadow thread (blocking bit) */
00035 
00036 #define XNTIMEO   0x00000200    /* Woken up due to a timeout condition */
00037 #define XNRMID    0x00000400    /* Pending on a removed resource */
00038 #define XNBREAK   0x00000800    /* Forcibly awaken from a wait state */
00039 #define XNKICKED  0x00001000    /* Kicked upon signal (shadow only) */
00040 #define XNBOOST   0x00002000    /* Undergoes regular PIP boost */
00041 #define XNDEBUG   0x00004000    /* Hit debugger breakpoint (shadow only) */
00042 
00043 /* Mode flags. */
00044 #define XNLOCK    0x00008000    /* Not preemptible */
00045 #define XNRRB     0x00010000    /* Undergoes a round-robin scheduling */
00046 #define XNASDI    0x00020000    /* ASR are disabled */
00047 #define XNSHIELD  0x00040000    /* IRQ shield is enabled (shadow only) */
00048 #define XNTRAPSW  0x00080000    /* Trap execution mode switches */
00049 
00050 #define XNFPU     0x00100000    /* Thread uses FPU */
00051 #define XNSHADOW  0x00200000    /* Shadow thread */
00052 #define XNROOT    0x00400000    /* Root thread (i.e. Linux/IDLE) */
00053 
00054 /*
00055   Must follow the declaration order of the above bits. Status symbols
00056   are defined as follows:
00057   'S' -> forcibly suspended.
00058   'w'/'W' -> waiting for a resource, with or without timeout.
00059   'D' -> delayed (without any other wait condition).
00060   'R' -> runnable.
00061   'U' -> unstarted or dormant.
00062   'X' -> Relaxed shadow.
00063   'b' -> Priority boost undergoing.
00064   'T' -> ptraced and stopped.
00065   'l' -> locks scheduler.
00066   'r' -> undergoes round-robin .
00067   's' -> interrupt shield enabled.
00068   't' -> mode switches trapped.
00069   'f' -> FPU enabled (for kernel threads).
00070 */
00071 #define XNTHREAD_SLABEL_INIT { \
00072   'S', 'W', 'D', 'R', 'U', \
00073   '.', '.', '.', 'X', '.', \
00074   '.', '.', '.', 'b', 'T', \
00075   'l', 'r', '.', 's', 't', \
00076   'f', '.', '.' \
00077 }
00078 
00079 #define XNTHREAD_BLOCK_BITS   (XNSUSP|XNPEND|XNDELAY|XNDORMANT|XNRELAX)
00080 #define XNTHREAD_MODE_BITS    (XNLOCK|XNRRB|XNASDI|XNSHIELD|XNTRAPSW)
00081 #define XNTHREAD_SYSTEM_BITS  (XNROOT)
00082 
00083 /* These flags are available to the real-time interfaces */
00084 #define XNTHREAD_SPARE0  0x10000000
00085 #define XNTHREAD_SPARE1  0x20000000
00086 #define XNTHREAD_SPARE2  0x40000000
00087 #define XNTHREAD_SPARE3  0x80000000
00088 #define XNTHREAD_SPARES  0xf0000000
00089 
00090 #if defined(__KERNEL__) || defined(__RTAI_UVM__) || defined(__RTAI_SIM__)
00091 
00092 #ifdef __RTAI_SIM__
00093 #define XNRUNNING  XNTHREAD_SPARE0      /* Pseudo-status (must not conflict with system bits) */
00094 #define XNDELETED  XNTHREAD_SPARE1      /* idem. */
00095 #endif /* __RTAI_SIM__ */
00096 
00097 #define XNTHREAD_INVALID_ASR  ((void (*)(xnsigmask_t))0)
00098 
00099 struct xnsched;
00100 struct xnsynch;
00101 
00102 typedef void (*xnasr_t)(xnsigmask_t sigs);
00103 
00104 typedef struct xnthread {
00105 
00106     xnarchtcb_t tcb;            /* Architecture-dependent block -- Must be first */
00107 
00108     xnflags_t status;           /* Thread status flags */
00109 
00110     struct xnsched *sched;      /* Thread scheduler */
00111 
00112     xnarch_cpumask_t affinity;  /* Processor affinity. */
00113 
00114     int bprio;                  /* Base priority (before PIP boost) */
00115 
00116     int cprio;                  /* Current priority */
00117 
00118     xnholder_t slink;           /* Thread holder in suspend queue */
00119 
00120     xnpholder_t rlink;          /* Thread holder in ready queue */
00121 
00122     xnpholder_t plink;          /* Thread holder in synchronization queue(s) */
00123 
00124     xnholder_t glink;           /* Thread holder in global queue */
00125 
00126 /* We don't want side-effects on laddr here! */
00127 #define link2thread(laddr,link) \
00128 ((xnthread_t *)(((char *)laddr) - (int)(&((xnthread_t *)0)->link)))
00129 
00130     xnpqueue_t claimq;          /* Owned resources claimed by others (PIP) */
00131 
00132     struct xnsynch *wchan;      /* Resource the thread pends on */
00133 
00134     xntimer_t rtimer;           /* Resource timer */
00135 
00136     xntimer_t ptimer;           /* Periodic timer */
00137 
00138     int poverrun;               /* Periodic timer overrun. */
00139 
00140     xnsigmask_t signals;        /* Pending signals */
00141 
00142     xnticks_t rrperiod;         /* Allotted round-robin period (ticks) */
00143 
00144     xnticks_t rrcredit;         /* Remaining round-robin time credit (ticks) */
00145 
00146 #ifdef CONFIG_RTAI_OPT_STATS
00147     struct {
00148         unsigned long psw;      /* Primary mode switch count */
00149         unsigned long ssw;      /* Secondary mode switch count */
00150         unsigned long csw;      /* Context switches (includes
00151                                    secondary -> primary switches) */
00152         unsigned long pf;       /* Number of page faults */
00153     } stat;
00154 #endif /* CONFIG_RTAI_OPT_STATS */
00155 
00156     xnasr_t asr;                /* Asynchronous service routine */
00157 
00158     xnflags_t asrmode;          /* Thread's mode for ASR */
00159 
00160     int asrimask;               /* Thread's interrupt mask for ASR */
00161 
00162     unsigned asrlevel;          /* ASR execution level (ASRs are reentrant) */
00163 
00164     int imask;                  /* Initial interrupt mask */
00165 
00166     int imode;                  /* Initial mode */
00167 
00168     int iprio;                  /* Initial priority */
00169 
00170     unsigned magic;             /* Skin magic. */
00171 
00172     char name[XNOBJECT_NAME_LEN]; /* Symbolic name of thread */
00173 
00174     xnticks_t stime;            /* Start time */
00175 
00176     void (*entry)(void *cookie); /* Thread entry routine */
00177 
00178     void *cookie;               /* Cookie to pass to the entry routine */
00179 
00180     void *extinfo;              /* Extended information -- user-defined */
00181 
00182     XNARCH_DECL_DISPLAY_CONTEXT();
00183 
00184 } xnthread_t;
00185 
00186 #define XNHOOK_THREAD_START  1
00187 #define XNHOOK_THREAD_SWITCH 2
00188 #define XNHOOK_THREAD_DELETE 3
00189 
00190 typedef struct xnhook {
00191 
00192     xnholder_t link;
00193 
00194 #define link2hook(laddr) \
00195 ((xnhook_t *)(((char *)laddr) - (int)(&((xnhook_t *)0)->link)))
00196 
00197     void (*routine)(xnthread_t *thread);
00198 
00199 } xnhook_t;
00200 
00201 #define xnthread_name(thread)              ((thread)->name)
00202 #define xnthread_sched(thread)             ((thread)->sched)
00203 #define xnthread_start_time(thread)        ((thread)->stime)
00204 #define xnthread_status_flags(thread)      ((thread)->status)
00205 #define xnthread_test_flags(thread,flags)  testbits((thread)->status,flags)
00206 #define xnthread_set_flags(thread,flags)   __setbits((thread)->status,flags)
00207 #define xnthread_clear_flags(thread,flags) __clrbits((thread)->status,flags)
00208 #define xnthread_initial_priority(thread)  ((thread)->iprio)
00209 #define xnthread_base_priority(thread)     ((thread)->bprio)
00210 #define xnthread_current_priority(thread)  ((thread)->cprio)
00211 #define xnthread_time_slice(thread)        ((thread)->rrperiod)
00212 #define xnthread_time_credit(thread)       ((thread)->rrcredit)
00213 #define xnthread_archtcb(thread)           (&((thread)->tcb))
00214 #define xnthread_asr_level(thread)         ((thread)->asrlevel)
00215 #define xnthread_pending_signals(thread)   ((thread)->signals)
00216 #define xnthread_timeout(thread)           xntimer_get_timeout(&(thread)->rtimer)
00217 #define xnthread_stack_size(thread)        xnarch_stack_size(xnthread_archtcb(thread))
00218 #define xnthread_extended_info(thread)     ((thread)->extinfo)
00219 #define xnthread_set_magic(thread,m)       do { (thread)->magic = (m); } while(0)
00220 #define xnthread_get_magic(thread)         ((thread)->magic)
00221 #define xnthread_signaled_p(thread)        ((thread)->signals != 0)
00222 #define xnthread_user_task(thread)         xnarch_user_task(xnthread_archtcb(thread))
00223 #define xnthread_user_pid(thread) \
00224     (testbits((thread)->status,XNROOT) || !xnthread_user_task(thread) ? \
00225     0 : xnarch_user_pid(xnthread_archtcb(thread)))
00226 
00227 #ifdef CONFIG_RTAI_OPT_STATS
00228 #define xnthread_inc_psw(thread)     ++(thread)->stat.psw
00229 #define xnthread_inc_ssw(thread)     ++(thread)->stat.ssw
00230 #define xnthread_inc_csw(thread)     ++(thread)->stat.csw
00231 #define xnthread_inc_pf(thread)      ++(thread)->stat.pf
00232 #else /* CONFIG_RTAI_OPT_STATS */
00233 #define xnthread_inc_psw(thread)     do { } while(0)
00234 #define xnthread_inc_ssw(thread)     do { } while(0)
00235 #define xnthread_inc_csw(thread)     do { } while(0)
00236 #define xnthread_inc_pf(thread)      do { } while(0)
00237 #endif /* CONFIG_RTAI_OPT_STATS */
00238 
00239 #ifdef __cplusplus
00240 extern "C" {
00241 #endif
00242 
00243 int xnthread_init(xnthread_t *thread,
00244                   const char *name,
00245                   int prio,
00246                   xnflags_t flags,
00247                   unsigned stacksize);
00248 
00249 void xnthread_cleanup_tcb(xnthread_t *thread);
00250 
00251 char *xnthread_symbolic_status(xnflags_t status, char *buf, int size);
00252 
00253 static inline xnticks_t xnthread_get_timeout(xnthread_t *thread, xnticks_t now)
00254 {
00255     xnticks_t timeout;
00256 
00257     if (!testbits(thread->status,XNDELAY))
00258         return 0LL;
00259 
00260     timeout = (xntimer_get_date(&thread->rtimer) ? : xntimer_get_date(&thread->ptimer));
00261 
00262     if (timeout <= now)
00263         return 1;
00264 
00265     return timeout - now;
00266 }
00267 
00268 #ifdef __cplusplus
00269 }
00270 #endif
00271 
00272 #endif /* __KERNEL__ || __RTAI_UVM__ || __RTAI_SIM__ */
00273 
00274 #endif /* !_RTAI_NUCLEUS_THREAD_H */

Generated on Sat Sep 3 12:32:47 2005 for RTAI Fusion API by  doxygen 1.4.2