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 Initialize a semaphore for use.
47
48 This function initializes the semaphore passed in with the starting count
49 value specified.
50
51 \param sm The semaphore to initialize
52 \param count The initial count of the semaphore
53 \retval 0 On success
54 \retval -1 On error, errno will be set as appropriate
55
56 \par Error Conditions:
57 \em EINVAL - the semaphore's value is invalid (less than 0)
58*/
59int sem_init(semaphore_t *sm, int count);
60
61/** \brief Destroy a semaphore.
62
63 This function destroys a semaphore, leaving it uninitialized. If there
64 are any threads currently waiting on the semaphore, they will be woken
65 with an ENOTRECOVERABLE error.
66
67 \param sm The semaphore to destroy
68 \retval 0 On success (no error conditions currently defined)
69*/
70int sem_destroy(semaphore_t *sm) __nonnull_all;
71
72/** \brief Wait on a semaphore.
73
74 This function will decrement the semaphore's count and return, if resources
75 are available. Otherwise, the function will block until the resources become
76 available.
77
78 This function does not protect you against doing things that will cause a
79 deadlock. This function is not safe to call in an interrupt. See
80 sem_trywait() for a safe function to call in an interrupt.
81
82 \param sm The semaphore to wait on
83 \retval 0 On success
84 \retval -1 On error, sets errno as appropriate
85
86 \par Error Conditions:
87 \em EPERM - called inside an interrupt \n
88 \em EINVAL - the semaphore was not initialized
89*/
90int sem_wait(semaphore_t *sm) __nonnull_all;
91
92/** \brief Wait on a semaphore (with a timeout).
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 or the timeout expires.
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 sm The semaphore to wait on
103 \param timeout The maximum number of milliseconds to block (a value
104 of 0 here will block indefinitely)
105 \retval 0 On success
106 \retval -1 On error, sets errno as appropriate
107
108 \par Error Conditions:
109 \em EPERM - called inside an interrupt \n
110 \em EINVAL - the semaphore was not initialized \n
111 \em EINVAL - the timeout value was invalid (less than 0) \n
112 \em ETIMEDOUT - timed out while blocking
113 */
114int sem_wait_timed(semaphore_t *sm, int timeout) __nonnull_all;
115
116/** \brief "Wait" on a semaphore without blocking.
117
118 This function will decrement the semaphore's count and return, if resources
119 are available. Otherwise, it will return an error.
120
121 This function does not protect you against doing things that will cause a
122 deadlock. This function, unlike the other waiting functions is safe to call
123 inside an interrupt.
124
125 \param sm The semaphore to "wait" on
126 \retval 0 On success
127 \retval -1 On error, sets errno as appropriate
128
129 \par Error Conditions:
130 \em EWOULDBLOCK - a call to sem_wait() would block \n
131 \em EINVAL - the semaphore was not initialized
132*/
133int sem_trywait(semaphore_t *sm) __nonnull_all;
134
135/** \brief Signal a semaphore.
136
137 This function will release resources associated with a semaphore, signalling
138 a waiting thread to continue on, if any are waiting. It is your
139 responsibility to make sure you only release resources you have.
140
141 \param sm The semaphore to signal
142 \retval 0 On success
143 \retval -1 On error, sets errno as appropriate
144
145 \par Error Conditions:
146 \em EINVAL - the semaphore was not initialized
147*/
148int sem_signal(semaphore_t *sm) __nonnull_all;
149
150/** \brief Retrieve the number of available resources.
151
152 This function will retrieve the count of available resources for a
153 semaphore. This is not a thread-safe way to make sure resources will be
154 available when you get around to waiting, so don't use it as such.
155
156 \param sm The semaphore to check
157 \return The count of the semaphore (the number of resources
158 currently available)
159*/
160int sem_count(const semaphore_t *sm) __nonnull_all;
161
162__END_DECLS
163
164#endif /* __KOS_SEM_H */
Various common macros used throughout the codebase.
int sem_wait(semaphore_t *sm) __nonnull_all
Wait on a semaphore.
int sem_count(const semaphore_t *sm) __nonnull_all
Retrieve the number of available resources.
int sem_destroy(semaphore_t *sm) __nonnull_all
Destroy a semaphore.
int sem_init(semaphore_t *sm, int count)
Initialize a semaphore for use.
int sem_trywait(semaphore_t *sm) __nonnull_all
"Wait" on a semaphore without blocking.
int sem_wait_timed(semaphore_t *sm, int timeout) __nonnull_all
Wait on a semaphore (with a timeout).
int sem_signal(semaphore_t *sm) __nonnull_all
Signal a semaphore.
Semaphore type.
Definition sem.h:37
int initialized
Are we initialized?
Definition sem.h:38
int count
The semaphore count.
Definition sem.h:39