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 {
42 struct xnthread_user_window *u_window;
45 struct threadobj_stat {
66 #define SCHED_CORE SCHED_COBALT
69 void threadobj_save_timeout(
struct threadobj_corespec *corespec,
70 const struct timespec *timeout)
82 struct threadobj_corespec {
83 pthread_cond_t grant_sync;
85 struct sched_param_ex schedparam_unlocked;
88 struct timespec timeout;
89 #ifdef CONFIG_XENO_WORKAROUND_CONDVAR_PI
91 struct sched_param_ex schedparam_unboosted;
95 struct threadobj_stat {
106 #define SCHED_CORE SCHED_FIFO
109 void threadobj_save_timeout(
struct threadobj_corespec *corespec,
110 const struct timespec *timeout)
113 corespec->timeout = *timeout;
121 #define __THREAD_S_STARTED (1 << 0)
122 #define __THREAD_S_WARMUP (1 << 1)
123 #define __THREAD_S_ABORTED (1 << 2)
124 #define __THREAD_S_LOCKED (1 << 3)
125 #define __THREAD_S_ACTIVE (1 << 4)
126 #define __THREAD_S_SUSPENDED (1 << 5)
127 #define __THREAD_S_SAFE (1 << 6)
128 #define __THREAD_S_DEBUG (1 << 31)
133 #define __THREAD_S_RUNNING 0
134 #define __THREAD_S_DORMANT (1 << 16)
135 #define __THREAD_S_WAIT (1 << 17)
136 #define __THREAD_S_TIMEDWAIT (1 << 18)
137 #define __THREAD_S_DELAYED (1 << 19)
138 #define __THREAD_S_BREAK (__THREAD_S_DELAYED|(1 << 20))
141 #define __THREAD_M_LOCK (1 << 0)
142 #define __THREAD_M_WARNSW (1 << 1)
143 #define __THREAD_M_CONFORMING (1 << 2)
144 #define __THREAD_M_SPARE0 (1 << 16)
145 #define __THREAD_M_SPARE1 (1 << 17)
146 #define __THREAD_M_SPARE2 (1 << 18)
147 #define __THREAD_M_SPARE3 (1 << 19)
148 #define __THREAD_M_SPARE4 (1 << 20)
149 #define __THREAD_M_SPARE5 (1 << 21)
150 #define __THREAD_M_SPARE6 (1 << 22)
151 #define __THREAD_M_SPARE7 (1 << 23)
153 #define THREADOBJ_IRQCONTEXT ((struct threadobj *)-2UL)
161 pthread_mutex_t lock;
168 struct sched_param_ex schedparam;
174 void (*finalizer)(
struct threadobj *thobj);
178 struct syncobj *wait_sobj;
179 struct holder wait_link;
184 timer_t periodic_timer;
186 struct threadobj_corespec core;
187 struct timespec tslice;
188 pthread_cond_t barrier;
189 struct traceobj *tracer;
191 struct sysgroup_memspec memspec;
192 struct agent_memspec agent;
193 struct backtrace_data btd;
196 struct threadobj_init_data {
200 struct sched_param_ex param_ex;
201 void (*finalizer)(
struct threadobj *thobj);
204 extern int threadobj_high_prio;
206 extern int threadobj_irq_prio;
208 extern pthread_key_t threadobj_tskey;
212 extern __thread __attribute__ ((tls_model (CONFIG_XENO_TLS_MODEL)))
213 struct threadobj *__threadobj_current;
215 static inline
void threadobj_set_current(struct threadobj *thobj)
217 __threadobj_current = thobj;
218 pthread_setspecific(threadobj_tskey, thobj);
221 static inline struct threadobj *__threadobj_get_current(
void)
223 return __threadobj_current;
228 static inline void threadobj_set_current(
struct threadobj *thobj)
230 pthread_setspecific(threadobj_tskey, thobj);
233 static inline struct threadobj *__threadobj_get_current(
void)
235 return (
struct threadobj *)pthread_getspecific(threadobj_tskey);
240 static inline struct threadobj *threadobj_current(
void)
242 struct threadobj *thobj = __threadobj_get_current();
243 return thobj == NULL || thobj == THREADOBJ_IRQCONTEXT ? NULL : thobj;
246 #ifdef CONFIG_XENO_DEBUG
248 static inline void __threadobj_tag_locked(
struct threadobj *thobj)
250 thobj->status |= __THREAD_S_LOCKED;
253 static inline void __threadobj_tag_unlocked(
struct threadobj *thobj)
255 assert(thobj->status & __THREAD_S_LOCKED);
256 thobj->status &= ~__THREAD_S_LOCKED;
259 static inline void __threadobj_check_locked(
struct threadobj *thobj)
261 assert(thobj->status & __THREAD_S_LOCKED);
266 static inline void __threadobj_tag_locked(
struct threadobj *thobj)
270 static inline void __threadobj_tag_unlocked(
struct threadobj *thobj)
274 static inline void __threadobj_check_locked(
struct threadobj *thobj)
284 void *__threadobj_alloc(
size_t tcb_struct_size,
285 size_t wait_union_size,
288 static inline void __threadobj_free(
void *p)
293 static inline void threadobj_free(
struct threadobj *thobj)
295 __threadobj_free((
unsigned char *)thobj - thobj->core_offset);
298 int threadobj_init(
struct threadobj *thobj,
299 struct threadobj_init_data *idata) __must_check;
301 int threadobj_start(
struct threadobj *thobj) __must_check;
303 int threadobj_shadow(
struct threadobj *thobj,
306 int threadobj_prologue(
struct threadobj *thobj,
309 void threadobj_wait_start(
void);
311 void threadobj_notify_entry(
void);
313 int threadobj_cancel(
struct threadobj *thobj);
315 void threadobj_uninit(
struct threadobj *thobj);
317 int threadobj_suspend(
struct threadobj *thobj);
319 int threadobj_resume(
struct threadobj *thobj);
321 int threadobj_unblock(
struct threadobj *thobj);
323 int __threadobj_lock_sched(
struct threadobj *current);
325 int threadobj_lock_sched(
void);
327 int __threadobj_unlock_sched(
struct threadobj *current);
329 int threadobj_unlock_sched(
void);
331 int threadobj_set_schedparam(
struct threadobj *thobj,
int policy,
332 const struct sched_param_ex *param_ex);
334 int threadobj_set_schedprio(
struct threadobj *thobj,
int priority);
336 int threadobj_set_mode(
int clrmask,
int setmask,
int *mode_r);
338 int threadobj_set_periodic(
struct threadobj *thobj,
339 const struct timespec *__restrict__ idate,
340 const struct timespec *__restrict__ period);
342 int threadobj_wait_period(
unsigned long *overruns_r) __must_check;
344 void threadobj_spin(ticks_t ns);
346 int threadobj_stat(
struct threadobj *thobj,
347 struct threadobj_stat *stat);
349 int threadobj_sleep(
const struct timespec *ts);
351 void threadobj_set_current_name(
const char *name);
353 #ifdef CONFIG_XENO_PSHARED
355 static inline int threadobj_local_p(
struct threadobj *thobj)
357 extern pid_t __node_id;
358 return thobj->cnode == __node_id;
363 static inline int threadobj_local_p(
struct threadobj *thobj)
370 void threadobj_init_key(
void);
372 int threadobj_pkg_init(
void);
378 #define threadobj_alloc(T, __mptr, W) \
381 __p = __threadobj_alloc(sizeof(T), sizeof(W), offsetof(T, __mptr)); \
385 static inline int threadobj_get_policy(
struct threadobj *thobj)
387 return thobj->policy;
390 static inline int threadobj_get_priority(
struct threadobj *thobj)
392 return thobj->schedparam.sched_priority;
395 static inline void threadobj_copy_schedparam(
struct sched_param_ex *param_ex,
396 const struct threadobj *thobj)
398 *param_ex = thobj->schedparam;
401 static inline int threadobj_lock(
struct threadobj *thobj)
405 ret = write_lock_safe(&thobj->lock, thobj->cancel_state);
409 __threadobj_tag_locked(thobj);
414 static inline int threadobj_trylock(
struct threadobj *thobj)
418 ret = write_trylock_safe(&thobj->lock, thobj->cancel_state);
422 __threadobj_tag_locked(thobj);
427 static inline int threadobj_unlock(
struct threadobj *thobj)
429 __threadobj_check_locked(thobj);
430 __threadobj_tag_unlocked(thobj);
431 return write_unlock_safe(&thobj->lock, thobj->cancel_state);
434 static inline int threadobj_irq_p(
void)
436 struct threadobj *current = __threadobj_get_current();
437 return current == THREADOBJ_IRQCONTEXT;
440 static inline int threadobj_current_p(
void)
442 return threadobj_current() != NULL;
445 static inline int __threadobj_lock_sched_once(
struct threadobj *current)
447 if (current->schedlock_depth == 0)
448 return __threadobj_lock_sched(current);
453 static inline int threadobj_lock_sched_once(
void)
455 struct threadobj *current = threadobj_current();
457 if (current->schedlock_depth == 0)
458 return threadobj_lock_sched();
463 static inline void threadobj_yield(
void)
468 static inline unsigned int threadobj_get_magic(
struct threadobj *thobj)
473 static inline void threadobj_set_magic(
struct threadobj *thobj,
476 thobj->magic = magic;
479 static inline int threadobj_get_lockdepth(
struct threadobj *thobj)
481 return thobj->schedlock_depth;
484 static inline int threadobj_get_status(
struct threadobj *thobj)
486 return thobj->status | thobj->run_state;
489 static inline int threadobj_get_errno(
struct threadobj *thobj)
491 return *thobj->errno_pointer;
494 #define threadobj_prepare_wait(T) \
496 struct threadobj *__thobj = threadobj_current(); \
497 assert(__thobj != NULL); \
498 assert(sizeof(typeof(T)) <= __thobj->wait_size); \
499 __thobj->wait_union; \
502 #define threadobj_finish_wait() do { } while (0)
504 static inline void *threadobj_get_wait(
struct threadobj *thobj)
506 return thobj->wait_union;
509 static inline const char *threadobj_get_name(
struct threadobj *thobj)
514 static inline pid_t threadobj_get_pid(
struct threadobj *thobj)
519 #ifdef CONFIG_XENO_WORKAROUND_CONDVAR_PI
521 int threadobj_cond_timedwait(pthread_cond_t *cond,
522 pthread_mutex_t *lock,
523 const struct timespec *timeout);
525 int threadobj_cond_wait(pthread_cond_t *cond,
526 pthread_mutex_t *lock);
528 int threadobj_cond_signal(pthread_cond_t *cond);
530 int threadobj_cond_broadcast(pthread_cond_t *cond);
535 int threadobj_cond_timedwait(pthread_cond_t *cond,
536 pthread_mutex_t *lock,
537 const struct timespec *timeout)
543 int threadobj_cond_wait(pthread_cond_t *cond,
544 pthread_mutex_t *lock)
550 int threadobj_cond_signal(pthread_cond_t *cond)
556 int threadobj_cond_broadcast(pthread_cond_t *cond)
int pthread_cond_signal(pthread_cond_t *cond)
Signal a condition variable.
Definition: cond.c:393
int pthread_cond_broadcast(pthread_cond_t *cond)
Broadcast a condition variable.
Definition: cond.c:445
int sched_yield(void)
Yield the processor.
Definition: thread.c:772
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Wait on a condition variable.
Definition: cond.c:238
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:324