Xenomai API  2.6.5
thread.h
1 /*
2  * Written by Gilles Chanteperdrix <[email protected]>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18 
19 
20 #ifndef _POSIX_THREAD_H
21 #define _POSIX_THREAD_H
22 
23 #include <posix/internal.h>
24 
25 typedef unsigned long long pse51_sigset_t;
26 
27 struct mm_struct;
28 
29 struct pse51_hkey {
30 
31  unsigned long u_tid;
32  struct mm_struct *mm;
33 };
34 
35 typedef struct {
36  pse51_sigset_t mask;
37  xnpqueue_t list;
38 } pse51_sigqueue_t;
39 
40 struct pse51_thread {
41  unsigned magic;
42  xnthread_t threadbase;
43 
44 #define thread2pthread(taddr) ({ \
45  xnthread_t *_taddr = (taddr); \
46  (_taddr \
47  ? ((xnthread_get_magic(_taddr) == PSE51_SKIN_MAGIC) \
48  ? ((pthread_t)(((char *)_taddr)- offsetof(struct pse51_thread, \
49  threadbase))) \
50  : NULL) \
51  : NULL); \
52 })
53 
54 
55  xnholder_t link; /* Link in pse51_threadq */
56  xnqueue_t *container;
57 
58 #define link2pthread(laddr) \
59  ((pthread_t)(((char *)laddr) - offsetof(struct pse51_thread, link)))
60 
61 
62  pthread_attr_t attr; /* creation attributes */
63 
64  void *(*entry)(void *arg); /* start routine */
65  void *arg; /* start routine argument */
66 
67  /* For pthread_join */
68  void *exit_status;
69  xnsynch_t join_synch; /* synchronization object, used by other threads
70  waiting for this one to finish. */
71  int nrt_joiners;
72 
73  /* For pthread_cancel */
74  unsigned cancelstate : 2;
75  unsigned canceltype : 2;
76  unsigned cancel_request : 1;
77  xnqueue_t cleanup_handlers_q;
78 
79  /* errno value for this thread. */
80  int err;
81 
82  /* For signals handling. */
83  pse51_sigset_t sigmask; /* signals mask. */
84  pse51_sigqueue_t pending; /* Pending signals */
85  pse51_sigqueue_t blocked_received; /* Blocked signals received. */
86 
87  /* For thread specific data. */
88  const void *tsd [PTHREAD_KEYS_MAX];
89 
90  /* For timers. */
91  xnqueue_t timersq;
92 
93 #ifdef CONFIG_XENO_OPT_PERVASIVE
94  struct pse51_hkey hkey;
95 #endif /* CONFIG_XENO_OPT_PERVASIVE */
96 };
97 
98 #define PSE51_JOINED_DETACHED XNTHREAD_INFO_SPARE0
99 
100 #define pse51_current_thread() thread2pthread(xnpod_current_thread())
101 
102 static inline void thread_set_errno (int err)
103 {
104  *xnthread_get_errno_location(xnpod_current_thread()) = err;
105 }
106 
107 static inline int thread_get_errno (void)
108 {
109  return *xnthread_get_errno_location(xnpod_current_thread());
110 }
111 
112 #define thread_name(thread) ((thread)->attr.name)
113 
114 #define thread_exit_status(thread) ((thread)->exit_status)
115 
116 #define thread_getdetachstate(thread) ((thread)->attr.detachstate)
117 
118 #define thread_setdetachstate(thread, state) ((thread)->attr.detachstate=state)
119 
120 #define thread_getcancelstate(thread) ((thread)->cancelstate)
121 
122 #define thread_setcancelstate(thread, state) ((thread)->cancelstate=state)
123 
124 #define thread_setcanceltype(thread, type) ((thread)->canceltype=type)
125 
126 #define thread_getcanceltype(thread) ((thread)->canceltype)
127 
128 #define thread_clrcancel(thread) ((thread)->cancel_request = 0)
129 
130 #define thread_setcancel(thread) ((thread)->cancel_request = 1)
131 
132 #define thread_cleanups(thread) (&(thread)->cleanup_handlers_q)
133 
134 #define thread_gettsd(thread, key) ((thread)->tsd[key])
135 
136 #define thread_settsd(thread, key, value) ((thread)->tsd[key]=(value))
137 
138 void pse51_thread_abort(pthread_t thread, void *status);
139 
140 static inline void thread_cancellation_point (xnthread_t *thread)
141 {
142  pthread_t cur = thread2pthread(thread);
143 
144  if(cur && cur->cancel_request
145  && thread_getcancelstate(cur) == PTHREAD_CANCEL_ENABLE )
146  pse51_thread_abort(cur, PTHREAD_CANCELED);
147 }
148 
149 void pse51_threadq_cleanup(pse51_kqueues_t *q);
150 
151 void pse51_thread_pkg_init(u_long rrperiod);
152 
153 void pse51_thread_pkg_cleanup(void);
154 
155 /* round-robin period. */
156 extern xnticks_t pse51_time_slice;
157 
158 #endif /* !_POSIX_THREAD_H */