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