19 #ifndef _COPPERPLATE_THREADOBJ_H
20 #define _COPPERPLATE_THREADOBJ_H
23 #include <semaphore.h>
27 #include <boilerplate/list.h>
28 #include <boilerplate/lock.h>
29 #include <boilerplate/sched.h>
30 #include <copperplate/clockobj.h>
31 #include <copperplate/heapobj.h>
32 #include <copperplate/debug.h>
34 #ifdef CONFIG_XENO_COBALT
36 #include <cobalt/uapi/kernel/types.h>
38 struct xnthread_user_window;
40 struct threadobj_corespec {
44 struct xnthread_user_window *u_window;
48 struct threadobj_stat {
69 #define SCHED_CORE SCHED_COBALT
72 void threadobj_save_timeout(
struct threadobj_corespec *corespec,
73 const struct timespec *timeout)
81 #ifdef CONFIG_XENO_PSHARED
83 static inline struct xnthread_user_window *
84 threadobj_get_window(
struct threadobj_corespec *corespec)
86 extern void *cobalt_umm_shared;
87 return (
struct xnthread_user_window *)
88 ((caddr_t)cobalt_umm_shared + corespec->u_winoff);
93 static inline struct xnthread_user_window *
94 threadobj_get_window(
struct threadobj_corespec *corespec)
96 return corespec->u_window;
103 #include <sys/time.h>
105 struct threadobj_corespec {
106 pthread_cond_t grant_sync;
108 struct sched_param_ex schedparam_unlocked;
111 struct timespec timeout;
112 #ifdef CONFIG_XENO_WORKAROUND_CONDVAR_PI
113 int policy_unboosted;
114 struct sched_param_ex schedparam_unboosted;
118 struct threadobj_stat {
129 #define SCHED_CORE SCHED_FIFO
132 void threadobj_save_timeout(
struct threadobj_corespec *corespec,
133 const struct timespec *timeout)
136 corespec->timeout = *timeout;
144 #define __THREAD_S_STARTED (1 << 0)
145 #define __THREAD_S_WARMUP (1 << 1)
146 #define __THREAD_S_ABORTED (1 << 2)
147 #define __THREAD_S_LOCKED (1 << 3)
148 #define __THREAD_S_ACTIVE (1 << 4)
149 #define __THREAD_S_SUSPENDED (1 << 5)
150 #define __THREAD_S_SAFE (1 << 6)
151 #define __THREAD_S_DEBUG (1 << 31)
156 #define __THREAD_S_RUNNING 0
157 #define __THREAD_S_DORMANT (1 << 16)
158 #define __THREAD_S_WAIT (1 << 17)
159 #define __THREAD_S_TIMEDWAIT (1 << 18)
160 #define __THREAD_S_DELAYED (1 << 19)
161 #define __THREAD_S_BREAK (__THREAD_S_DELAYED|(1 << 20))
164 #define __THREAD_M_LOCK (1 << 0)
165 #define __THREAD_M_WARNSW (1 << 1)
166 #define __THREAD_M_CONFORMING (1 << 2)
167 #define __THREAD_M_SPARE0 (1 << 16)
168 #define __THREAD_M_SPARE1 (1 << 17)
169 #define __THREAD_M_SPARE2 (1 << 18)
170 #define __THREAD_M_SPARE3 (1 << 19)
171 #define __THREAD_M_SPARE4 (1 << 20)
172 #define __THREAD_M_SPARE5 (1 << 21)
173 #define __THREAD_M_SPARE6 (1 << 22)
174 #define __THREAD_M_SPARE7 (1 << 23)
176 #define THREADOBJ_IRQCONTEXT ((struct threadobj *)-2UL)
184 pthread_mutex_t lock;
191 struct sched_param_ex schedparam;
197 void (*finalizer)(
struct threadobj *thobj);
201 struct syncobj *wait_sobj;
202 struct holder wait_link;
205 dref_type(
void *) wait_union;
207 timer_t periodic_timer;
209 struct threadobj_corespec core;
210 struct timespec tslice;
211 pthread_cond_t barrier;
212 struct traceobj *tracer;
214 struct sysgroup_memspec memspec;
215 struct agent_memspec agent;
216 struct backtrace_data btd;
219 struct threadobj_init_data {
223 struct sched_param_ex param_ex;
224 void (*finalizer)(
struct threadobj *thobj);
227 extern int threadobj_high_prio;
229 extern int threadobj_irq_prio;
231 extern pthread_key_t threadobj_tskey;
235 extern __thread __attribute__ ((tls_model (CONFIG_XENO_TLS_MODEL)))
236 struct threadobj *__threadobj_current;
238 static inline
void threadobj_set_current(struct threadobj *thobj)
240 __threadobj_current = thobj;
241 pthread_setspecific(threadobj_tskey, thobj);
244 static inline struct threadobj *__threadobj_get_current(
void)
246 return __threadobj_current;
251 static inline void threadobj_set_current(
struct threadobj *thobj)
253 pthread_setspecific(threadobj_tskey, thobj);
256 static inline struct threadobj *__threadobj_get_current(
void)
258 return (
struct threadobj *)pthread_getspecific(threadobj_tskey);
263 static inline struct threadobj *threadobj_current(
void)
265 struct threadobj *thobj = __threadobj_get_current();
266 return thobj == NULL || thobj == THREADOBJ_IRQCONTEXT ? NULL : thobj;
269 #ifdef CONFIG_XENO_DEBUG
271 static inline void __threadobj_tag_locked(
struct threadobj *thobj)
273 thobj->status |= __THREAD_S_LOCKED;
276 static inline void __threadobj_tag_unlocked(
struct threadobj *thobj)
278 assert(thobj->status & __THREAD_S_LOCKED);
279 thobj->status &= ~__THREAD_S_LOCKED;
282 static inline void __threadobj_check_locked(
struct threadobj *thobj)
284 assert(thobj->status & __THREAD_S_LOCKED);
289 static inline void __threadobj_tag_locked(
struct threadobj *thobj)
293 static inline void __threadobj_tag_unlocked(
struct threadobj *thobj)
297 static inline void __threadobj_check_locked(
struct threadobj *thobj)
307 void *__threadobj_alloc(
size_t tcb_struct_size,
308 size_t wait_union_size,
311 static inline void __threadobj_free(
void *p)
316 static inline void threadobj_free(
struct threadobj *thobj)
318 __threadobj_free((
unsigned char *)thobj - thobj->core_offset);
321 int threadobj_init(
struct threadobj *thobj,
322 struct threadobj_init_data *idata) __must_check;
324 int threadobj_start(
struct threadobj *thobj) __must_check;
326 int threadobj_shadow(
struct threadobj *thobj,
329 int threadobj_prologue(
struct threadobj *thobj,
332 void threadobj_wait_start(
void);
334 void threadobj_notify_entry(
void);
336 int threadobj_cancel(
struct threadobj *thobj);
338 void threadobj_uninit(
struct threadobj *thobj);
340 int threadobj_suspend(
struct threadobj *thobj);
342 int threadobj_resume(
struct threadobj *thobj);
344 int threadobj_unblock(
struct threadobj *thobj);
346 int __threadobj_lock_sched(
struct threadobj *current);
348 int threadobj_lock_sched(
void);
350 int __threadobj_unlock_sched(
struct threadobj *current);
352 int threadobj_unlock_sched(
void);
354 int threadobj_set_schedparam(
struct threadobj *thobj,
int policy,
355 const struct sched_param_ex *param_ex);
357 int threadobj_set_schedprio(
struct threadobj *thobj,
int priority);
359 int threadobj_set_mode(
int clrmask,
int setmask,
int *mode_r);
361 int threadobj_set_periodic(
struct threadobj *thobj,
362 const struct timespec *__restrict__ idate,
363 const struct timespec *__restrict__ period);
365 int threadobj_wait_period(
unsigned long *overruns_r) __must_check;
367 void threadobj_spin(ticks_t ns);
369 int threadobj_stat(
struct threadobj *thobj,
370 struct threadobj_stat *stat);
372 int threadobj_sleep(
const struct timespec *ts);
374 void threadobj_set_current_name(
const char *name);
376 #ifdef CONFIG_XENO_PSHARED
378 static inline int threadobj_local_p(
struct threadobj *thobj)
380 extern pid_t __node_id;
381 return thobj->cnode == __node_id;
386 static inline int threadobj_local_p(
struct threadobj *thobj)
393 void threadobj_init_key(
void);
395 int threadobj_pkg_init(
void);
401 #define threadobj_alloc(T, __mptr, W) \
404 __p = __threadobj_alloc(sizeof(T), sizeof(W), offsetof(T, __mptr)); \
408 static inline int threadobj_get_policy(
struct threadobj *thobj)
410 return thobj->policy;
413 static inline int threadobj_get_priority(
struct threadobj *thobj)
415 return thobj->schedparam.sched_priority;
418 static inline void threadobj_copy_schedparam(
struct sched_param_ex *param_ex,
419 const struct threadobj *thobj)
421 *param_ex = thobj->schedparam;
424 static inline int threadobj_lock(
struct threadobj *thobj)
428 ret = write_lock_safe(&thobj->lock, thobj->cancel_state);
432 __threadobj_tag_locked(thobj);
437 static inline int threadobj_trylock(
struct threadobj *thobj)
441 ret = write_trylock_safe(&thobj->lock, thobj->cancel_state);
445 __threadobj_tag_locked(thobj);
450 static inline int threadobj_unlock(
struct threadobj *thobj)
452 __threadobj_check_locked(thobj);
453 __threadobj_tag_unlocked(thobj);
454 return write_unlock_safe(&thobj->lock, thobj->cancel_state);
457 static inline int threadobj_irq_p(
void)
459 struct threadobj *current = __threadobj_get_current();
460 return current == THREADOBJ_IRQCONTEXT;
463 static inline int threadobj_current_p(
void)
465 return threadobj_current() != NULL;
468 static inline int __threadobj_lock_sched_once(
struct threadobj *current)
470 if (current->schedlock_depth == 0)
471 return __threadobj_lock_sched(current);
476 static inline int threadobj_lock_sched_once(
void)
478 struct threadobj *current = threadobj_current();
480 if (current->schedlock_depth == 0)
481 return threadobj_lock_sched();
486 static inline void threadobj_yield(
void)
491 static inline unsigned int threadobj_get_magic(
struct threadobj *thobj)
496 static inline void threadobj_set_magic(
struct threadobj *thobj,
499 thobj->magic = magic;
502 static inline int threadobj_get_lockdepth(
struct threadobj *thobj)
504 return thobj->schedlock_depth;
507 static inline int threadobj_get_status(
struct threadobj *thobj)
509 return thobj->status | thobj->run_state;
512 static inline int threadobj_get_errno(
struct threadobj *thobj)
514 return *thobj->errno_pointer;
517 #define threadobj_prepare_wait(T) \
519 struct threadobj *__thobj = threadobj_current(); \
520 assert(__thobj != NULL); \
521 assert(sizeof(typeof(T)) <= __thobj->wait_size); \
522 __mptr(__thobj->wait_union); \
525 #define threadobj_finish_wait() do { } while (0)
527 static inline void *threadobj_get_wait(
struct threadobj *thobj)
529 return __mptr(thobj->wait_union);
532 static inline const char *threadobj_get_name(
struct threadobj *thobj)
537 static inline pid_t threadobj_get_pid(
struct threadobj *thobj)
542 #ifdef CONFIG_XENO_WORKAROUND_CONDVAR_PI
544 int threadobj_cond_timedwait(pthread_cond_t *cond,
545 pthread_mutex_t *lock,
546 const struct timespec *timeout);
548 int threadobj_cond_wait(pthread_cond_t *cond,
549 pthread_mutex_t *lock);
551 int threadobj_cond_signal(pthread_cond_t *cond);
553 int threadobj_cond_broadcast(pthread_cond_t *cond);
558 int threadobj_cond_timedwait(pthread_cond_t *cond,
559 pthread_mutex_t *lock,
560 const struct timespec *timeout)
566 int threadobj_cond_wait(pthread_cond_t *cond,
567 pthread_mutex_t *lock)
573 int threadobj_cond_signal(pthread_cond_t *cond)
579 int threadobj_cond_broadcast(pthread_cond_t *cond)
int pthread_cond_signal(pthread_cond_t *cond)
Signal a condition variable.
Definition: cond.c:423
int pthread_cond_broadcast(pthread_cond_t *cond)
Broadcast a condition variable.
Definition: cond.c:483
int sched_yield(void)
Yield the processor.
Definition: thread.c:800
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Wait on a condition variable.
Definition: cond.c:250
int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)
Wait a bounded time on a condition variable.
Definition: cond.c:345