19#ifndef _COBALT_KERNEL_THREAD_H
20#define _COBALT_KERNEL_THREAD_H
22#include <linux/irq_work.h>
23#include <linux/wait.h>
24#include <linux/sched.h>
25#include <linux/sched/rt.h>
26#include <pipeline/thread.h>
27#include <cobalt/kernel/list.h>
28#include <cobalt/kernel/stat.h>
29#include <cobalt/kernel/timer.h>
30#include <cobalt/kernel/registry.h>
31#include <cobalt/kernel/schedparam.h>
32#include <cobalt/kernel/trace.h>
33#include <cobalt/kernel/synch.h>
34#include <cobalt/uapi/kernel/thread.h>
35#include <cobalt/uapi/signal.h>
36#include <asm/xenomai/machine.h>
37#include <asm/xenomai/thread.h>
43#define XNTHREAD_BLOCK_BITS (XNSUSP|XNPEND|XNDELAY|XNDORMANT|XNRELAX|XNHELD|XNDBGSTOP)
44#define XNTHREAD_MODE_BITS (XNRRB|XNWARN|XNTRAPLB)
46#define XNTHREAD_SIGDEBUG 0
47#define XNTHREAD_SIGSHADOW_HARDEN 1
48#define XNTHREAD_SIGSHADOW_BACKTRACE 2
49#define XNTHREAD_SIGSHADOW_HOME 3
50#define XNTHREAD_SIGTERM 4
51#define XNTHREAD_MAX_SIGNALS 5
58struct xnthread_personality;
61struct lostage_signal {
62 struct irq_work inband_work;
63 struct task_struct *task;
67struct lostage_wakeup {
68 struct irq_work inband_work;
69 struct task_struct *task;
72struct xnthread_init_attr {
73 struct xnthread_personality *personality;
79struct xnthread_start_attr {
81 void (*entry)(
void *cookie);
85struct xnthread_wait_context {
89struct xnthread_personality {
95 void *(*attach_process)(void);
96 void (*detach_process)(
void *arg);
97 void (*map_thread)(
struct xnthread *thread);
98 struct xnthread_personality *(*relax_thread)(
struct xnthread *thread);
99 struct xnthread_personality *(*harden_thread)(
struct xnthread *thread);
100 struct xnthread_personality *(*move_thread)(
struct xnthread *thread,
102 struct xnthread_personality *(*exit_thread)(
struct xnthread *thread);
103 struct xnthread_personality *(*finalize_thread)(
struct xnthread *thread);
105 struct module *
module;
109 struct xnarchtcb tcb;
116 struct xnsched_class *sched_class;
117 struct xnsched_class *base_class;
119#ifdef CONFIG_XENO_OPT_SCHED_TP
120 struct xnsched_tpslot *tps;
121 struct list_head tp_link;
123#ifdef CONFIG_XENO_OPT_SCHED_SPORADIC
124 struct xnsched_sporadic_data *pss;
126#ifdef CONFIG_XENO_OPT_SCHED_QUOTA
127 struct xnsched_quota_group *quota;
128 struct list_head quota_expired;
129 struct list_head quota_next;
150 struct list_head rlink;
156 struct list_head plink;
159 struct list_head glink;
173 struct list_head boosters;
175 struct xnsynch *wchan;
177 struct xnsynch *wwake;
181 struct xntimer rtimer;
183 struct xntimer ptimer;
187 struct xnthread_wait_context *wcontext;
190 xnstat_counter_t ssw;
191 xnstat_counter_t csw;
192 xnstat_counter_t xsc;
194 xnstat_exectime_t account;
195 xnstat_exectime_t lastperiod;
198 struct xnselector *selector;
202 char name[XNOBJECT_NAME_LEN];
204 void (*entry)(
void *cookie);
211 struct xnthread_user_window *u_window;
213 struct xnthread_personality *personality;
215 struct completion exited;
217#ifdef CONFIG_XENO_OPT_DEBUG
218 const char *exe_path;
221 struct lostage_signal sigarray[XNTHREAD_MAX_SIGNALS];
222 struct lostage_wakeup relax_work;
225static inline int xnthread_get_state(
const struct xnthread *thread)
227 return thread->state;
230static inline int xnthread_test_state(
struct xnthread *thread,
int bits)
232 return thread->state & bits;
235static inline void xnthread_set_state(
struct xnthread *thread,
int bits)
237 thread->state |= bits;
240static inline void xnthread_clear_state(
struct xnthread *thread,
int bits)
242 thread->state &= ~bits;
245static inline int xnthread_test_info(
struct xnthread *thread,
int bits)
247 return thread->info & bits;
250static inline void xnthread_set_info(
struct xnthread *thread,
int bits)
252 thread->info |= bits;
255static inline void xnthread_clear_info(
struct xnthread *thread,
int bits)
257 thread->info &= ~bits;
260static inline int xnthread_test_localinfo(
struct xnthread *curr,
int bits)
262 return curr->local_info & bits;
265static inline void xnthread_set_localinfo(
struct xnthread *curr,
int bits)
267 curr->local_info |= bits;
270static inline void xnthread_clear_localinfo(
struct xnthread *curr,
int bits)
272 curr->local_info &= ~bits;
275static inline struct xnarchtcb *xnthread_archtcb(
struct xnthread *thread)
280static inline int xnthread_base_priority(
const struct xnthread *thread)
282 return thread->bprio;
285static inline int xnthread_current_priority(
const struct xnthread *thread)
287 return thread->cprio;
290static inline struct task_struct *xnthread_host_task(
struct xnthread *thread)
292 return xnarch_host_task(xnthread_archtcb(thread));
295#define xnthread_for_each_booster(__pos, __thread) \
296 list_for_each_entry(__pos, &(__thread)->boosters, next)
298#define xnthread_for_each_booster_safe(__pos, __tmp, __thread) \
299 list_for_each_entry_safe(__pos, __tmp, &(__thread)->boosters, next)
301#define xnthread_run_handler(__t, __h, __a...) \
303 struct xnthread_personality *__p__ = (__t)->personality; \
304 if ((__p__)->ops.__h) \
305 (__p__)->ops.__h(__t, ##__a); \
308#define xnthread_run_handler_stack(__t, __h, __a...) \
310 struct xnthread_personality *__p__ = (__t)->personality; \
312 if ((__p__)->ops.__h == NULL) \
314 __p__ = (__p__)->ops.__h(__t, ##__a); \
319struct xnthread_wait_context *xnthread_get_wait_context(
struct xnthread *thread)
321 return thread->wcontext;
325int xnthread_register(
struct xnthread *thread,
const char *name)
331struct xnthread *xnthread_lookup(xnhandle_t threadh)
334 return thread && thread->handle == xnhandle_get_index(threadh) ? thread : NULL;
337static inline void xnthread_sync_window(
struct xnthread *thread)
339 if (thread->u_window) {
340 thread->u_window->state = thread->state;
341 thread->u_window->info = thread->info;
346void xnthread_clear_sync_window(
struct xnthread *thread,
int state_bits)
348 if (thread->u_window) {
349 thread->u_window->state = thread->state & ~state_bits;
350 thread->u_window->info = thread->info;
355void xnthread_set_sync_window(
struct xnthread *thread,
int state_bits)
357 if (thread->u_window) {
358 thread->u_window->state = thread->state | state_bits;
359 thread->u_window->info = thread->info;
363static inline int normalize_priority(
int prio)
365 return prio < MAX_RT_PRIO ? prio : MAX_RT_PRIO - 1;
368int __xnthread_init(
struct xnthread *thread,
369 const struct xnthread_init_attr *attr,
371 struct xnsched_class *sched_class,
372 const union xnsched_policy_param *sched_param);
374void __xnthread_test_cancel(
struct xnthread *curr);
376void __xnthread_cleanup(
struct xnthread *curr);
378void __xnthread_discard(
struct xnthread *thread);
397 return pipeline_current()->thread;
413 return pipeline_thread_from_task(p);
429 if (curr && xnthread_test_info(curr,
XNCANCELD))
430 __xnthread_test_cancel(curr);
434void xnthread_complete_wait(
struct xnthread_wait_context *wc)
440int xnthread_wait_complete_p(
struct xnthread_wait_context *wc)
445#ifdef CONFIG_XENO_ARCH_FPU
446void xnthread_switch_fpu(
struct xnsched *sched);
448static inline void xnthread_switch_fpu(
struct xnsched *sched) { }
451void xnthread_deregister(
struct xnthread *thread);
453char *xnthread_format_status(
unsigned long status,
454 char *buf,
int size);
456pid_t xnthread_host_pid(
struct xnthread *thread);
458int xnthread_set_clock(
struct xnthread *thread,
459 struct xnclock *newclock);
461xnticks_t xnthread_get_timeout(
struct xnthread *thread,
464xnticks_t xnthread_get_period(
struct xnthread *thread);
466void xnthread_prepare_wait(
struct xnthread_wait_context *wc);
469 const struct xnthread_init_attr *attr,
470 struct xnsched_class *sched_class,
471 const union xnsched_policy_param *sched_param);
474 const struct xnthread_start_attr *attr);
482 xntmode_t timeout_mode,
483 struct xnsynch *wchan);
492 xntmode_t timeout_mode,
502int xnthread_join(
struct xnthread *thread,
bool uninterruptible);
508void __xnthread_kick(
struct xnthread *thread);
510void xnthread_kick(
struct xnthread *thread);
512void __xnthread_demote(
struct xnthread *thread);
514void xnthread_demote(
struct xnthread *thread);
516void __xnthread_signal(
struct xnthread *thread,
int sig,
int arg);
518void xnthread_signal(
struct xnthread *thread,
int sig,
int arg);
520void xnthread_pin_initial(
struct xnthread *thread);
522void xnthread_call_mayday(
struct xnthread *thread,
int reason);
524static inline void xnthread_get_resource(
struct xnthread *curr)
530static inline int xnthread_put_resource(
struct xnthread *curr)
532 if (xnthread_test_state(curr,
XNWEAK) ||
533 IS_ENABLED(CONFIG_XENO_OPT_DEBUG_MUTEX_SLEEP)) {
534 if (unlikely(curr->res_count == 0)) {
535 if (xnthread_test_state(curr,
XNWARN))
536 xnthread_signal(curr, SIGDEBUG,
537 SIGDEBUG_RESCNT_IMBALANCE);
546static inline void xnthread_commit_ceiling(
struct xnthread *curr)
548 if (curr->u_window->pp_pending)
549 xnsynch_commit_ceiling(curr);
554void xnthread_migrate_passive(
struct xnthread *thread,
558static inline void xnthread_migrate_passive(
struct xnthread *thread,
564int __xnthread_set_schedparam(
struct xnthread *thread,
565 struct xnsched_class *sched_class,
566 const union xnsched_policy_param *sched_param);
569 struct xnsched_class *sched_class,
570 const union xnsched_policy_param *sched_param);
572int xnthread_killall(
int grace,
int mask);
574void __xnthread_propagate_schedparam(
struct xnthread *curr);
576static inline void xnthread_propagate_schedparam(
struct xnthread *curr)
578 if (xnthread_test_info(curr,
XNSCHEDP))
579 __xnthread_propagate_schedparam(curr);
582extern struct xnthread_personality xenomai_personality;
int xnregistry_enter(const char *key, void *objaddr, xnhandle_t *phandle, struct xnpnode *pnode)
Register a real-time object.
Definition registry.c:634
static void * xnregistry_lookup(xnhandle_t handle, unsigned long *cstamp_r)
Find a real-time object into the registry.
Definition registry.h:176
#define XNSCHEDP
schedparam propagation is pending
Definition thread.h:74
#define XNCANCELD
Cancellation request is pending.
Definition thread.h:72
#define XNWARN
Issue SIGDEBUG on error detection.
Definition thread.h:45
#define XNDEBUG
User-level debugging enabled.
Definition thread.h:52
#define XNWEAK
Non real-time shadow (from the WEAK class)
Definition thread.h:48
int xnthread_wait_period(unsigned long *overruns_r)
Wait for the next periodic release point.
Definition thread.c:1394
static struct xnthread * xnthread_current(void)
Retrieve the current Cobalt core TCB.
Definition thread.h:395
int xnthread_set_mode(int clrmask, int setmask)
Change control mode of the current thread.
Definition thread.c:783
int xnthread_init(struct xnthread *thread, const struct xnthread_init_attr *attr, struct xnsched_class *sched_class, const union xnsched_policy_param *sched_param)
Initialize a new thread.
Definition thread.c:619
int xnthread_unblock(struct xnthread *thread)
Unblock a thread.
Definition thread.c:1212
int xnthread_set_slice(struct xnthread *thread, xnticks_t quantum)
Set thread time-slicing information.
Definition thread.c:1470
void xnthread_resume(struct xnthread *thread, int mask)
Resume a thread.
Definition thread.c:1094
void xnthread_relax(int notify, int reason)
Switch a shadow thread back to the Linux domain.
Definition thread.c:2045
void xnthread_suspend(struct xnthread *thread, int mask, xnticks_t timeout, xntmode_t timeout_mode, struct xnsynch *wchan)
Suspend a thread.
Definition thread.c:877
static void xnthread_test_cancel(void)
Introduce a thread cancellation point.
Definition thread.h:425
void xnthread_cancel(struct xnthread *thread)
Cancel a thread.
Definition thread.c:1527
static struct xnthread * xnthread_from_task(struct task_struct *p)
Retrieve the Cobalt core TCB attached to a Linux task.
Definition thread.h:411
int xnthread_start(struct xnthread *thread, const struct xnthread_start_attr *attr)
Start a newly created thread.
Definition thread.c:692
int xnthread_join(struct xnthread *thread, bool uninterruptible)
Join with a terminated thread.
Definition thread.c:1656
int xnthread_set_periodic(struct xnthread *thread, xnticks_t idate, xntmode_t timeout_mode, xnticks_t period)
Make a thread periodic.
Definition thread.c:1304
int xnthread_harden(void)
Migrate a Linux task to the Xenomai domain.
Definition thread.c:1925
int xnthread_set_schedparam(struct xnthread *thread, struct xnsched_class *sched_class, const union xnsched_policy_param *sched_param)
Change the base scheduling parameters of a thread.
Definition thread.c:1830
Copyright © 2011 Gilles Chanteperdrix gilles.chanteperdrix@xenomai.org.
Definition atomic.h:24
Scheduling information structure.
Definition sched.h:64