1 #ifndef _DDEKIT_THREAD_H
2 #define _DDEKIT_THREAD_H
4 /** \defgroup DDEKit_threads */
5 #include <ddekit/ddekit.h>
6 #include <ddekit/lock.h>
9 typedef struct ddekit_thread ddekit_thread_t
;
13 * \ingroup DDEKit_threads
15 * Create a new thread running the specified thread function with the specified
16 * arguments. The thread is assigned the given internal name.
18 * Additionally, DDEKit threads possess a thread-local storage area where they
19 * may store arbitrary data.
21 * \param fun thread function
22 * \param arg optional argument to thread function, set to NULL if not needed
23 * \param name internal thread name
25 ddekit_thread_t
*ddekit_thread_create(void (*fun
)(void *), void *arg
,
28 /** Reference to own DDEKit thread id.
30 * \ingroup DDEKit_threads
32 ddekit_thread_t
*ddekit_thread_myself(void);
34 /** Initialize thread with given name.
36 * \ingroup DDEKit_threads
38 * This function may be used by threads that were not created using
39 * \ref ddekit_thread_create. This enables such threads to be handled as if they
40 * were DDEKit threads.
42 ddekit_thread_t
*ddekit_thread_setup_myself(const char *name
);
44 /** Get TLS data for a specific thread.
46 * \ingroup DDEKit_threads
48 * \return Pointer to TLS data of this thread.
50 void *ddekit_thread_get_data(ddekit_thread_t
*thread
);
52 /** Get TLS data for current thread.
54 * \ingroup DDEKit_threads
56 * Same as calling \ref ddekit_thread_get_data with \ref ddekit_thread_myself
59 * \return Pointer to TLS data of current thread.
61 void *ddekit_thread_get_my_data(void);
63 /** Set TLS data for specific thread.
65 * \ingroup DDEKit_threads
67 * \param thread DDEKit thread
68 * \param data pointer to thread data
70 void ddekit_thread_set_data(ddekit_thread_t
*thread
, void *data
);
72 /** Set TLS data for current thread.
74 * \ingroup DDEKit_threads
76 * \param data pointer to thread data
78 void ddekit_thread_set_my_data(void *data
);
80 /** Sleep for some miliseconds.
82 * \ingroup DDEKit_threads
84 * \param msecs time to sleep in ms.
86 void ddekit_thread_msleep(unsigned long msecs
);
88 /** Sleep for some microseconds.
90 * \ingroup DDEKit_threads
92 * \param usecs time to sleep in µs.
94 void ddekit_thread_usleep(unsigned long usecs
);
96 /** Sleep for some nanoseconds.
98 * \ingroup DDEKit_threads
100 * \param usecs time to sleep in ns.
102 void ddekit_thread_nsleep(unsigned long nsecs
);
104 /** Sleep until a lock becomes unlocked.
106 * \ingroup DDEKit_threads
108 void ddekit_thread_sleep(ddekit_lock_t
*lock
);
110 /** Wakeup a waiting thread.
112 * \ingroup DDEKit_threads
114 void ddekit_thread_wakeup(ddekit_thread_t
*thread
);
116 /** Terminate a thread
118 * \ingroup DDEKit_threads
120 void ddekit_thread_exit(void) __attribute__((noreturn
));
122 /** Terminate a thread
124 * \ingroup DDEKit_threads
126 void ddekit_thread_terminate(ddekit_thread_t
*thread
);
128 /** Get the name, a thread registered with DDEKit.
130 * \ingroup DDEKit_threads
132 const char *ddekit_thread_get_name(ddekit_thread_t
*thread
);
134 /** Get unique ID of a DDEKit thread.
136 * \ingroup DDEKit_threads
138 * DDEKit does not allow direct access to the thread data
139 * structure, since this struct contains L4-specific data types.
140 * However, applications might want to get some kind of ID related
141 * to a ddekit_thread, for instance to use it as a Linux-like PID.
143 int ddekit_thread_get_id(ddekit_thread_t
*thread
);
145 /** Hint that this thread is done and may be scheduled somehow.
147 * \ingroup DDEKit_threads
149 void ddekit_thread_schedule(void);
151 /** Hint that this thread is done and may be scheduled somehow.
153 * \ingroup DDEKit_threads
155 void ddekit_yield(void);
157 /** Initialize DDEKit thread subsystem.
159 * \ingroup DDEKit_threads
161 void ddekit_init_threads(void);