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> 33 #ifdef CONFIG_XENO_COBALT 35 #include <cobalt/uapi/kernel/types.h> 37 struct xnthread_user_window;
39 struct threadobj_corespec {
43 struct xnthread_user_window *u_window;
47 struct threadobj_stat {
68 #define SCHED_CORE SCHED_COBALT 71 void threadobj_save_timeout(
struct threadobj_corespec *corespec,
72 const struct timespec *timeout)
80 #ifdef CONFIG_XENO_PSHARED 82 static inline struct xnthread_user_window *
83 threadobj_get_window(
struct threadobj_corespec *corespec)
85 extern void *cobalt_umm_shared;
86 return (
struct xnthread_user_window *)
87 ((caddr_t)cobalt_umm_shared + corespec->u_winoff);
92 static inline struct xnthread_user_window *
93 threadobj_get_window(
struct threadobj_corespec *corespec)
95 return corespec->u_window;
102 #include <sys/time.h> 104 struct threadobj_corespec {
105 pthread_cond_t grant_sync;
107 struct sched_param_ex schedparam_unlocked;
110 struct timespec timeout;
111 #ifdef CONFIG_XENO_WORKAROUND_CONDVAR_PI 112 int policy_unboosted;
113 struct sched_param_ex schedparam_unboosted;
117 struct threadobj_stat {
128 #define SCHED_CORE SCHED_FIFO 131 void threadobj_save_timeout(
struct threadobj_corespec *corespec,
132 const struct timespec *timeout)
135 corespec->timeout = *timeout;
143 #define __THREAD_S_STARTED (1 << 0) 144 #define __THREAD_S_WARMUP (1 << 1) 145 #define __THREAD_S_ABORTED (1 << 2) 146 #define __THREAD_S_LOCKED (1 << 3) 147 #define __THREAD_S_ACTIVE (1 << 4) 148 #define __THREAD_S_SUSPENDED (1 << 5) 149 #define __THREAD_S_SAFE (1 << 6) 150 #define __THREAD_S_PERIODIC (1 << 7) 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 backtrace_data btd;
218 struct threadobj_init_data {
222 struct sched_param_ex param_ex;
223 void (*finalizer)(
struct threadobj *thobj);
226 extern int threadobj_high_prio;
228 extern int threadobj_irq_prio;
230 extern pthread_key_t threadobj_tskey;
234 extern __thread
__attribute__ ((tls_model (CONFIG_XENO_TLS_MODEL)))
235 struct threadobj *__threadobj_current;
237 static inline void threadobj_set_current(
struct threadobj *thobj)
239 __threadobj_current = thobj;
240 pthread_setspecific(threadobj_tskey, thobj);
243 static inline struct threadobj *__threadobj_get_current(
void)
245 return __threadobj_current;
250 static inline void threadobj_set_current(
struct threadobj *thobj)
252 pthread_setspecific(threadobj_tskey, thobj);
255 static inline struct threadobj *__threadobj_get_current(
void)
257 return (
struct threadobj *)pthread_getspecific(threadobj_tskey);
262 static inline struct threadobj *threadobj_current(
void)
264 struct threadobj *thobj = __threadobj_get_current();
265 return thobj == NULL || thobj == THREADOBJ_IRQCONTEXT ? NULL : thobj;
268 #ifdef CONFIG_XENO_DEBUG 270 static inline void __threadobj_tag_locked(
struct threadobj *thobj)
272 thobj->status |= __THREAD_S_LOCKED;
275 static inline void __threadobj_tag_unlocked(
struct threadobj *thobj)
277 assert(thobj->status & __THREAD_S_LOCKED);
278 thobj->status &= ~__THREAD_S_LOCKED;
281 static inline void __threadobj_check_locked(
struct threadobj *thobj)
283 assert(thobj->status & __THREAD_S_LOCKED);
288 static inline void __threadobj_tag_locked(
struct threadobj *thobj)
292 static inline void __threadobj_tag_unlocked(
struct threadobj *thobj)
296 static inline void __threadobj_check_locked(
struct threadobj *thobj)
306 void *__threadobj_alloc(
size_t tcb_struct_size,
307 size_t wait_union_size,
310 static inline void __threadobj_free(
void *p)
315 static inline void threadobj_free(
struct threadobj *thobj)
317 __threadobj_free((
unsigned char *)thobj - thobj->core_offset);
320 int threadobj_init(
struct threadobj *thobj,
321 struct threadobj_init_data *idata) __must_check;
323 int threadobj_start(
struct threadobj *thobj) __must_check;
325 int threadobj_shadow(
struct threadobj *thobj,
328 int threadobj_prologue(
struct threadobj *thobj,
331 void threadobj_wait_start(
void);
333 void threadobj_notify_entry(
void);
335 int threadobj_cancel(
struct threadobj *thobj);
337 void threadobj_uninit(
struct threadobj *thobj);
339 int threadobj_suspend(
struct threadobj *thobj);
341 int threadobj_resume(
struct threadobj *thobj);
343 int threadobj_unblock(
struct threadobj *thobj);
345 int __threadobj_lock_sched(
struct threadobj *current);
347 int threadobj_lock_sched(
void);
349 int __threadobj_unlock_sched(
struct threadobj *current);
351 int threadobj_unlock_sched(
void);
353 int threadobj_set_schedparam(
struct threadobj *thobj,
int policy,
354 const struct sched_param_ex *param_ex);
356 int threadobj_set_schedprio(
struct threadobj *thobj,
int priority);
358 int threadobj_set_mode(
int clrmask,
int setmask,
int *mode_r);
360 int threadobj_set_periodic(
struct threadobj *thobj,
361 const struct timespec *__restrict__ idate,
362 const struct timespec *__restrict__ period);
364 int threadobj_wait_period(
unsigned long *overruns_r) __must_check;
366 void threadobj_spin(ticks_t ns);
368 int threadobj_stat(
struct threadobj *thobj,
369 struct threadobj_stat *stat);
371 int threadobj_sleep(
const struct timespec *ts);
373 void threadobj_set_current_name(
const char *name);
375 #ifdef CONFIG_XENO_PSHARED 377 static inline int threadobj_local_p(
struct threadobj *thobj)
379 extern pid_t __node_id;
380 return thobj->cnode == __node_id;
385 static inline int threadobj_local_p(
struct threadobj *thobj)
392 void threadobj_init_key(
void);
394 int threadobj_pkg_init(
int anon_session);
400 #define threadobj_alloc(T, __mptr, W) \ 403 __p = __threadobj_alloc(sizeof(T), sizeof(W), offsetof(T, __mptr)); \ 407 static inline int threadobj_get_policy(
struct threadobj *thobj)
409 return thobj->policy;
412 static inline int threadobj_get_priority(
struct threadobj *thobj)
414 return thobj->schedparam.sched_priority;
417 static inline void threadobj_copy_schedparam(
struct sched_param_ex *param_ex,
418 const struct threadobj *thobj)
420 *param_ex = thobj->schedparam;
423 static inline int threadobj_lock(
struct threadobj *thobj)
427 ret = write_lock_safe(&thobj->lock, thobj->cancel_state);
431 __threadobj_tag_locked(thobj);
436 static inline int threadobj_trylock(
struct threadobj *thobj)
440 ret = write_trylock_safe(&thobj->lock, thobj->cancel_state);
444 __threadobj_tag_locked(thobj);
449 static inline int threadobj_unlock(
struct threadobj *thobj)
451 __threadobj_check_locked(thobj);
452 __threadobj_tag_unlocked(thobj);
453 return write_unlock_safe(&thobj->lock, thobj->cancel_state);
456 static inline int threadobj_irq_p(
void)
458 struct threadobj *current = __threadobj_get_current();
459 return current == THREADOBJ_IRQCONTEXT;
462 static inline int threadobj_current_p(
void)
464 return threadobj_current() != NULL;
467 static inline int __threadobj_lock_sched_once(
struct threadobj *current)
469 if (current->schedlock_depth == 0)
470 return __threadobj_lock_sched(current);
475 static inline int threadobj_lock_sched_once(
void)
477 struct threadobj *current = threadobj_current();
479 if (current->schedlock_depth == 0)
480 return threadobj_lock_sched();
485 static inline void threadobj_yield(
void)
490 static inline unsigned int threadobj_get_magic(
struct threadobj *thobj)
495 static inline void threadobj_set_magic(
struct threadobj *thobj,
498 thobj->magic = magic;
501 static inline int threadobj_get_lockdepth(
struct threadobj *thobj)
503 return thobj->schedlock_depth;
506 static inline int threadobj_get_status(
struct threadobj *thobj)
508 return thobj->status | thobj->run_state;
511 static inline int threadobj_get_errno(
struct threadobj *thobj)
513 return *thobj->errno_pointer;
516 #define threadobj_prepare_wait(T) \ 518 struct threadobj *__thobj = threadobj_current(); \ 519 assert(__thobj != NULL); \ 520 assert(sizeof(typeof(T)) <= __thobj->wait_size); \ 521 __mptr(__thobj->wait_union); \ 524 #define threadobj_finish_wait() do { } while (0) 526 static inline void *threadobj_get_wait(
struct threadobj *thobj)
528 return __mptr(thobj->wait_union);
531 static inline const char *threadobj_get_name(
struct threadobj *thobj)
536 static inline pid_t threadobj_get_pid(
struct threadobj *thobj)
541 #ifdef CONFIG_XENO_WORKAROUND_CONDVAR_PI 543 int threadobj_cond_timedwait(pthread_cond_t *cond,
544 pthread_mutex_t *lock,
545 const struct timespec *timeout);
547 int threadobj_cond_wait(pthread_cond_t *cond,
548 pthread_mutex_t *lock);
550 int threadobj_cond_signal(pthread_cond_t *cond);
552 int threadobj_cond_broadcast(pthread_cond_t *cond);
557 int threadobj_cond_timedwait(pthread_cond_t *cond,
558 pthread_mutex_t *lock,
559 const struct timespec *timeout)
565 int threadobj_cond_wait(pthread_cond_t *cond,
566 pthread_mutex_t *lock)
572 int threadobj_cond_signal(pthread_cond_t *cond)
578 int threadobj_cond_broadcast(pthread_cond_t *cond)
int pthread_cond_signal(pthread_cond_t *cond)
Signal a condition variable.
Definition: cond.c:445
int pthread_cond_broadcast(pthread_cond_t *cond)
Broadcast a condition variable.
Definition: cond.c:501
int sched_yield(void)
Yield the processor.
Definition: sched.c:58
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Wait on a condition variable.
Definition: cond.c:280
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:371
static int __attribute__((cold))
Test if a mutex structure contains a valid autoinitializer.
Definition: mutex.c:177