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