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 <sys/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, int timeout, void (*callback)(void *));
51
52/* Wake up N threads waiting on the given object. If cnt is <=0, then we
53 wake all threads. Returns the number of threads actually woken. */
54/** \brief Wake up a number of threads sleeping on an object.
55
56 This function wakes up the specified number of threads sleeping on the
57 object specified.
58
59 \param obj The object to wake threads that are sleeping on it
60 \param cnt The number of threads to wake, if <= 0, wake all
61 \param err The errno code to set as the errno value on the
62 woken threads. If this is 0 (EOK), then the thread's
63 errno will not be changed, and the thread will get a
64 return value of 0 from the genwait_wait(). If it is
65 non-zero, the thread will get a return value of -1
66 and errno will be set to this value for the woken
67 threads.
68 \return The number of threads woken
69*/
70int genwait_wake_cnt(void * obj, int cnt, int err);
71
72/** \brief Wake up all threads sleeping on an object.
73
74 This function simply calls genwait_wake_cnt(obj, -1, 0).
75
76 \param obj The object to wake threads that are sleeping on it
77 \see genwait_wake_cnt()
78*/
79void genwait_wake_all(void * obj);
80
81/** \brief Wake up one thread sleeping on an object.
82
83 This function simply calls genwait_wake_cnt(obj, 1, 0).
84
85 \param obj The object to wake threads that are sleeping on it
86 \see genwait_wake_cnt()
87*/
88void genwait_wake_one(void * obj);
89
90/** \brief Wake up all threads sleeping on an object, with an error.
91
92 This function simply calls genwait_wake_cnt(obj, -1, err).
93
94 \param obj The object to wake threads that are sleeping on it
95 \param err The value to set in the threads' errno values
96 \see genwait_wake_cnt()
97*/
98void genwait_wake_all_err(void *obj, int err);
99
100/** \brief Wake up one thread sleeping on an object, with an error.
101
102 This function simply calls genwait_wake_cnt(obj, 1, err).
103
104 \param obj The object to wake threads that are sleeping on it
105 \param err The value to set in the threads' errno values
106 \see genwait_wake_cnt()
107*/
108void genwait_wake_one_err(void *obj, int err);
109
110/** \brief Wake up a specific thread that is sleeping on an object.
111
112 This function wakes up the specified thread, assuming it is sleeping on the
113 specified object.
114
115 \param obj The object to wake the thread from
116 \param thd The specific thread to wake
117 \param err The errno code to set as the errno value on the
118 woken thread. If this is 0 (EOK), then the thread's
119 errno will not be changed, and the thread will get a
120 return value of 0 from the genwait_wait(). If it is
121 non-zero, the thread will get a return value of -1
122 and errno will be set to this value for the woken
123 threads.
124 \return The number of threads woken, which should be 1 on
125 success.
126*/
127int genwait_wake_thd(void *obj, kthread_t *thd, int err);
128
129/** \brief Look for timed out genwait_wait() calls.
130
131 There should be no reason you need to call this function, it is called
132 internally by the scheduler for you.
133
134 \param now The current system time, in milliseconds since boot
135*/
136void genwait_check_timeouts(uint64_t now);
137
138/** \brief Look for the next timeout event time.
139
140 This function looks up when the next genwait_wait() call will timeout. This
141 function is for the internal use of the scheduler, and should not be called
142 from user code.
143
144 \return The next timeout time in milliseconds since boot, or
145 0 if there are no pending genwait_wait() calls
146*/
147uint64_t genwait_next_timeout(void);
148
149/** \cond */
150/* Initialize the genwait system */
151int genwait_init(void);
152
153/* Shut down the genwait system */
154void genwait_shutdown(void);
155/** \endcond */
156
157
158__END_DECLS
159
160#endif /* __KOS_GENWAIT_H */
161
void genwait_wake_one(void *obj)
Wake up one thread sleeping on an object.
void genwait_wake_one_err(void *obj, int err)
Wake up one thread sleeping on an object, with an error.
void genwait_wake_all(void *obj)
Wake up all threads sleeping on an object.
int genwait_wait(void *obj, const char *mesg, int timeout, void(*callback)(void *))
Sleep on an object.
int genwait_wake_thd(void *obj, kthread_t *thd, int err)
Wake up a specific thread that is sleeping on an object.
void genwait_wake_all_err(void *obj, int err)
Wake up all threads sleeping on an object, with an error.
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.
int genwait_wake_cnt(void *obj, int cnt, int err)
Wake up a number of threads sleeping on an object.
Structure describing one running thread.
Definition thread.h:161
Threading support.