KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
sem.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 include/kos/sem.h
4 Copyright (C) 2001, 2003 Megan Potter
5 Copyright (C) 2012 Lawrence Sebald
6
7*/
8
9/** \file kos/sem.h
10 \brief Semaphores.
11 \ingroup kthreads
12
13 This file defines semaphores. A semaphore is a synchronization primitive
14 that allows a specified number of threads to be in its critical section at a
15 single point of time. Another way to think of it is that you have a
16 predetermined number of resources available, and the semaphore maintains the
17 resources.
18
19 \author Megan Potter
20 \see kos/mutex.h
21*/
22
23#ifndef __KOS_SEM_H
24#define __KOS_SEM_H
25
26#include <kos/cdefs.h>
27
28__BEGIN_DECLS
29
30/** \brief Semaphore type.
31
32 This structure defines a semaphore. There are no public members of this
33 structure for you to actually do anything with in your code, so don't try.
34
35 \headerfile kos/sem.h
36*/
37typedef struct semaphore {
38 int initialized; /**< \brief Are we initialized? */
39 int count; /**< \brief The semaphore count */
41
42/** \brief Initializer for a transient semaphore.
43 \param value The initial count of the semaphore. */
44#define SEM_INITIALIZER(value) { 1, value }
45
46/** \brief Allocate a new semaphore.
47
48 This function allocates and initializes a new semaphore for use.
49
50 \deprecated
51 This function is formally deprecated. Please update your code to use
52 sem_init() or static initialization with SEM_INITIALIZER instead.
53
54 \param value The initial count of the semaphore (the number of
55 threads to allow in the critical section at a time)
56
57 \return The created semaphore on success. NULL is returned
58 on failure and errno is set as appropriate.
59
60 \par Error Conditions:
61 \em ENOMEM - out of memory \n
62 \em EINVAL - the semaphore's value is invalid (less than 0)
63*/
64semaphore_t *sem_create(int value) __depr("Use sem_init or SEM_INITIALIZER.");
65
66/** \brief Initialize a semaphore for use.
67
68 This function initializes the semaphore passed in with the starting count
69 value specified.
70
71 \param sm The semaphore to initialize
72 \param count The initial count of the semaphore
73 \retval 0 On success
74 \retval -1 On error, errno will be set as appropriate
75
76 \par Error Conditions:
77 \em EINVAL - the semaphore's value is invalid (less than 0)
78*/
79int sem_init(semaphore_t *sm, int count);
80
81/** \brief Destroy a semaphore.
82
83 This function frees a semaphore, releasing any memory associated with it. If
84 there are any threads currently waiting on the semaphore, they will be woken
85 with an ENOTRECOVERABLE error.
86
87 \param sem The semaphore to destroy
88 \retval 0 On success (no error conditions currently defined)
89*/
91
92/** \brief Wait on a semaphore.
93
94 This function will decrement the semaphore's count and return, if resources
95 are available. Otherwise, the function will block until the resources become
96 available.
97
98 This function does not protect you against doing things that will cause a
99 deadlock. This function is not safe to call in an interrupt. See
100 sem_trywait() for a safe function to call in an interrupt.
101
102 \param sem The semaphore to wait on
103 \retval 0 On success
104 \retval -1 On error, sets errno as appropriate
105
106 \par Error Conditions:
107 \em EPERM - called inside an interrupt \n
108 \em EINVAL - the semaphore was not initialized
109*/
111
112/** \brief Wait on a semaphore (with a timeout).
113
114 This function will decrement the semaphore's count and return, if resources
115 are available. Otherwise, the function will block until the resources become
116 available or the timeout expires.
117
118 This function does not protect you against doing things that will cause a
119 deadlock. This function is not safe to call in an interrupt. See
120 sem_trywait() for a safe function to call in an interrupt.
121
122 \param sem The semaphore to wait on
123 \param timeout The maximum number of milliseconds to block (a value
124 of 0 here will block indefinitely)
125 \retval 0 On success
126 \retval -1 On error, sets errno as appropriate
127
128 \par Error Conditions:
129 \em EPERM - called inside an interrupt \n
130 \em EINVAL - the semaphore was not initialized \n
131 \em EINVAL - the timeout value was invalid (less than 0) \n
132 \em ETIMEDOUT - timed out while blocking
133 */
134int sem_wait_timed(semaphore_t *sem, int timeout);
135
136/** \brief "Wait" on a semaphore without blocking.
137
138 This function will decrement the semaphore's count and return, if resources
139 are available. Otherwise, it will return an error.
140
141 This function does not protect you against doing things that will cause a
142 deadlock. This function, unlike the other waiting functions is safe to call
143 inside an interrupt.
144
145 \param sem The semaphore to "wait" on
146 \retval 0 On success
147 \retval -1 On error, sets errno as appropriate
148
149 \par Error Conditions:
150 \em EWOULDBLOCK - a call to sem_wait() would block \n
151 \em EINVAL - the semaphore was not initialized
152*/
154
155/** \brief Signal a semaphore.
156
157 This function will release resources associated with a semaphore, signalling
158 a waiting thread to continue on, if any are waiting. It is your
159 responsibility to make sure you only release resources you have.
160
161 \param sem The semaphore to signal
162 \retval 0 On success
163 \retval -1 On error, sets errno as appropriate
164
165 \par Error Conditions:
166 \em EINVAL - the semaphore was not initialized
167*/
169
170/** \brief Retrieve the number of available resources.
171
172 This function will retrieve the count of available resources for a
173 semaphore. This is not a thread-safe way to make sure resources will be
174 available when you get around to waiting, so don't use it as such.
175
176 \param sem The semaphore to check
177 \return The count of the semaphore (the number of resources
178 currently available)
179*/
181
182__END_DECLS
183
184#endif /* __KOS_SEM_H */
Definitions for builtin attributes and compiler directives.
#define __depr(m)
Mark something as deprecated, with an informative message.
Definition cdefs.h:119
int sem_count(semaphore_t *sem)
Retrieve the number of available resources.
semaphore_t * sem_create(int value) __depr("Use sem_init or SEM_INITIALIZER.")
Allocate a new semaphore.
int sem_wait_timed(semaphore_t *sem, int timeout)
Wait on a semaphore (with a timeout).
int sem_wait(semaphore_t *sem)
Wait on a semaphore.
int sem_trywait(semaphore_t *sem)
"Wait" on a semaphore without blocking.
int sem_signal(semaphore_t *sem)
Signal a semaphore.
int sem_destroy(semaphore_t *sem)
Destroy a semaphore.
int sem_init(semaphore_t *sm, int count)
Initialize a semaphore for use.
Semaphore type.
Definition sem.h:37
int initialized
Are we initialized?
Definition sem.h:38
int count
The semaphore count.
Definition sem.h:39