1 #ifndef PARLIB_UTHREAD_H
2 #define PARLIB_UTHREAD_H
4 #include <parlib/vcore.h>
5 #include <ros/syscall.h>
9 #define UTHREAD_DONT_MIGRATE 0x001 /* don't move to another vcore */
10 #define UTHREAD_SAVED 0x002 /* uthread's state is in utf */
11 #define UTHREAD_FPSAVED 0x004 /* uthread's FP state is in uth->as */
12 #define UTHREAD_IS_THREAD0 0x008 /* thread0: glibc's main() thread */
16 #define UT_NOT_RUNNING 2
18 /* Externally blocked thread reasons (for uthread_has_blocked()) */
19 #define UTH_EXT_BLK_MUTEX 1
20 #define UTH_EXT_BLK_EVENTQ 2
21 #define UTH_EXT_BLK_JUSTICE 3 /* whatever. might need more options */
23 /* Bare necessities of a user thread. 2LSs should allocate a bigger struct and
24 * cast their threads to uthreads when talking with vcore code. Vcore/default
25 * 2LS code won't touch udata or beyond. */
27 struct user_context u_ctx;
28 struct ancillary_state as;
32 int notif_disabled_depth;
33 struct syscall *sysc; /* syscall we're blocking on, if any */
34 struct syscall local_sysc; /* for when we don't want to use the stack */
35 void (*yield_func)(struct uthread*, void*);
38 char err_str[MAX_ERRSTR_LEN];
40 typedef struct uthread uthread_t;
41 extern __thread struct uthread *current_uthread;
43 /* 2L-Scheduler operations. Examples in pthread.c. */
45 /* Functions supporting thread ops */
46 void (*sched_entry)(void);
47 void (*thread_runnable)(struct uthread *);
48 void (*thread_paused)(struct uthread *);
49 void (*thread_blockon_sysc)(struct uthread *, void *);
50 void (*thread_has_blocked)(struct uthread *, int);
51 void (*thread_refl_fault)(struct uthread *, unsigned int, unsigned int,
53 /* Functions event handling wants */
54 void (*preempt_pending)(void);
56 extern struct schedule_ops *sched_ops;
58 /* Low-level _S code calls this for basic uthreading without a 2LS */
59 void uthread_lib_init(void);
60 /* Call this, passing it a uthread representing thread0, from your 2LS init
61 * routines. When it returns, you're in _M mode (thread0 on vcore0) */
62 void uthread_2ls_init(struct uthread *uthread, struct schedule_ops *ops);
63 /* Call this to become an mcp capable of worling with uthreads. */
64 void uthread_mcp_init(void);
66 /* Functions to make/manage uthreads. Can be called by functions such as
67 * pthread_create(), which can wrap these with their own stuff (like attrs,
70 /* uthread_init() does the uthread initialization of a uthread that the caller
71 * created. Call this whenever you are "starting over" with a thread. Pass in
72 * attr, if you want to override any defaults. */
73 struct uth_thread_attr {
74 bool want_tls; /* default, no */
76 void uthread_init(struct uthread *new_thread, struct uth_thread_attr *attr);
77 /* Call this when you are done with a uthread, forever, but before you free it */
78 void uthread_cleanup(struct uthread *uthread);
79 void uthread_runnable(struct uthread *uthread);
80 void uthread_yield(bool save_state, void (*yield_func)(struct uthread*, void*),
82 void uthread_sleep(unsigned int seconds);
83 void uthread_usleep(unsigned int usecs);
84 void uthread_has_blocked(struct uthread *uthread, int flags);
86 /* Utility functions */
87 bool __check_preempt_pending(uint32_t vcoreid); /* careful: check the code */
88 void uth_disable_notifs(void);
89 void uth_enable_notifs(void);
91 bool register_evq(struct syscall *sysc, struct event_queue *ev_q);
92 void deregister_evq(struct syscall *sysc);
94 /* Helpers, which sched_entry() can call */
95 void highjack_current_uthread(struct uthread *uthread);
96 void run_current_uthread(void);
97 void run_uthread(struct uthread *uthread);
99 /* Asking for trouble with this API, when we just want stacktop (or whatever
100 * the SP will be). */
101 static inline void init_uthread_ctx(struct uthread *uth, void (*entry)(void),
102 void *stack_bottom, uint32_t size)
104 init_user_ctx(&uth->u_ctx, (long)entry, (long)(stack_bottom) + size);
107 #define uthread_set_tls_var(uth, name, val) \
109 typeof(val) __val = val; \
110 begin_access_tls_vars(((struct uthread*)(uth))->tls_desc); \
112 end_access_tls_vars(); \
115 #define uthread_get_tls_var(uth, name) \
118 begin_access_tls_vars(((struct uthread*)(uth))->tls_desc); \
120 end_access_tls_vars(); \
126 #endif /* PARLIB_UTHREAD_H */