KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
genwait.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 include/kos/genwait.h
4 Copyright (C) 2003 Megan Potter
5 Copyright (C) 2012 Lawrence Sebald
6
7*/
8
9/** \file kos/genwait.h
10 \brief Generic wait system.
11 \ingroup kthreads
12
13 The generic wait system in KOS, like many other portions of KOS, is based on
14 an idea from the BSD kernel. It allows you to sleep on any random object and
15 later wake up any threads that happen to be sleeping on that object. All of
16 KOS' sync primitives (other than spinlocks) are based on this concept, and
17 it can be used for some fairly useful things.
18
19 \author Megan Potter
20 \author Lawrence Sebald
21*/
22
23#ifndef __KOS_GENWAIT_H
24#define __KOS_GENWAIT_H
25
26#include <kos/cdefs.h>
27__BEGIN_DECLS
28
29#include <kos/thread.h>
30#include <stdint.h>
31
32/** \brief Sleep on an object.
33
34 This function sleeps on the specified object. You are not allowed to call
35 this function inside an interrupt.
36
37 \param obj The object to sleep on
38 \param mesg A message to show in the status
39 \param timeout If not woken before this many milliseconds have
40 passed, wake up anyway
41 \param callback If non-NULL, call this function with obj as its
42 argument if the wait times out (but before the
43 calling thread has been woken back up)
44 \retval 0 On successfully being woken up (not by timeout)
45 \retval -1 On error or being woken by timeout
46
47 \par Error Conditions:
48 \em EAGAIN - on timeout
49*/
50int genwait_wait(void *obj, const char *mesg, unsigned int timeout,
51 void (*callback)(void *));
52
53/* Wake up N threads waiting on the given object. If cnt is <=0, then we
54 wake all threads. Returns the number of threads actually woken. */
55/** \brief Wake up a number of threads sleeping on an object.
56
57 This function wakes up the specified number of threads sleeping on the
58 object specified.
59
60 \param obj The object to wake threads that are sleeping on it
61 \param cnt The number of threads to wake, if <= 0, wake all
62 \param err The errno code to set as the errno value on the
63 woken threads. If this is 0 (EOK), then the thread's
64 errno will not be changed, and the thread will get a
65 return value of 0 from the genwait_wait(). If it is
66 non-zero, the thread will get a return value of -1
67 and errno will be set to this value for the woken
68 threads.
69 \return The number of threads woken
70*/
71int genwait_wake_cnt(const void *obj, int cnt, int err);
72
73/** \brief Wake up all threads sleeping on an object.
74
75 This function simply calls genwait_wake_cnt(obj, -1, 0).
76
77 \param obj The object to wake threads that are sleeping on it
78 \see genwait_wake_cnt()
79*/
80void genwait_wake_all(const void *obj);
81
82/** \brief Wake up one thread sleeping on an object.
83
84 This function simply calls genwait_wake_cnt(obj, 1, 0).
85
86 \param obj The object to wake threads that are sleeping on it
87 \see genwait_wake_cnt()
88*/
89void genwait_wake_one(const void *obj);
90
91/** \brief Wake up all threads sleeping on an object, with an error.
92
93 This function simply calls genwait_wake_cnt(obj, -1, err).
94
95 \param obj The object to wake threads that are sleeping on it
96 \param err The value to set in the threads' errno values
97 \see genwait_wake_cnt()
98*/
99void genwait_wake_all_err(const void *obj, int err);
100
101/** \brief Wake up one thread sleeping on an object, with an error.
102
103 This function simply calls genwait_wake_cnt(obj, 1, err).
104
105 \param obj The object to wake threads that are sleeping on it
106 \param err The value to set in the threads' errno values
107 \see genwait_wake_cnt()
108*/
109void genwait_wake_one_err(const void *obj, int err);
110
111/** \brief Wake up a specific thread that is sleeping on an object.
112
113 This function wakes up the specified thread, assuming it is sleeping on the
114 specified object.
115
116 \param obj The object to wake the thread from
117 \param thd The specific thread to wake (non-null).
118 \param err The errno code to set as the errno value on the
119 woken thread. If this is 0 (EOK), then the thread's
120 errno will not be changed, and the thread will get a
121 return value of 0 from the genwait_wait(). If it is
122 non-zero, the thread will get a return value of -1
123 and errno will be set to this value for the woken
124 threads.
125 \return The number of threads woken, which should be 1 on
126 success.
127*/
128int genwait_wake_thd(const void *obj, kthread_t *thd, int err) __nonnull((2));
129
130/** \brief Look for timed out genwait_wait() calls.
131
132 There should be no reason you need to call this function, it is called
133 internally by the scheduler for you.
134
135 \param now The current system time, in milliseconds since boot
136*/
137void genwait_check_timeouts(uint64_t now);
138
139/** \brief Look for the next timeout event time.
140
141 This function looks up when the next genwait_wait() call will timeout. This
142 function is for the internal use of the scheduler, and should not be called
143 from user code.
144
145 \return The next timeout time in milliseconds since boot, or
146 0 if there are no pending genwait_wait() calls
147*/
148uint64_t genwait_next_timeout(void);
149
150/** \cond */
151/* Initialize the genwait system */
152int genwait_init(void);
153
154/* Shut down the genwait system */
155void genwait_shutdown(void);
156/** \endcond */
157
158
159__END_DECLS
160
161#endif /* __KOS_GENWAIT_H */
162
Various common macros used throughout the codebase.
int genwait_wait(void *obj, const char *mesg, unsigned int timeout, void(*callback)(void *))
Sleep on an object.
void genwait_wake_all_err(const void *obj, int err)
Wake up all threads sleeping on an object, with an error.
int genwait_wake_cnt(const void *obj, int cnt, int err)
Wake up a number of threads sleeping on an object.
int genwait_wake_thd(const void *obj, kthread_t *thd, int err) __nonnull((2))
Wake up a specific thread that is sleeping on an object.
void genwait_wake_one_err(const void *obj, int err)
Wake up one thread sleeping on an object, with an error.
void genwait_wake_one(const void *obj)
Wake up one thread sleeping on an object.
void genwait_check_timeouts(uint64_t now)
Look for timed out genwait_wait() calls.
uint64_t genwait_next_timeout(void)
Look for the next timeout event time.
void genwait_wake_all(const void *obj)
Wake up all threads sleeping on an object.
Structure describing one running thread.
Definition thread.h:164
Threading support.