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