-
Notifications
You must be signed in to change notification settings - Fork 0
/
sem.h
84 lines (71 loc) · 2.47 KB
/
sem.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
*
* This file is part of GarbageCollector.
*
* GarbageCollector is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GarbageCollector is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GarbageCollector. If not, see <http://www.gnu.org/licenses/>.
*
*/
/**
* @file sem.h
* @brief Semaphore implementation for the synchronization of POSIX threads.
*
* This module implements counting P/V semaphores suitable for the
* synchronization of POSIX threads. POSIX mutexes and condition variables are
* utilized to implement the semaphor operations.
*/
#ifndef SEM_H
#define SEM_H
/** Opaque type of a semaphore. */
typedef struct SEM SEM;
/**
* @brief Creates a new semaphore.
*
* This function creates a new semaphore. If an error occurs during the
* initialization, the implementation frees all resources already allocated by
* then and sets @c errno to an appropriate value.
*
* It is legal to initialize the semaphore with a negative value. If this is the
* case, in order to reset the semaphore counter to zero, the V-operation must be
* performed @c (-initVal) times.
*
* @param initVal The initial value of the semaphore.
* @return Handle for the created semaphore, or @c NULL if an error occurred.
*/
SEM *semCreate(int initVal);
/**
* @brief Destroys a semaphore and frees all associated resources.
* @param sem Handle of the semaphore to destroy. If a @c NULL pointer is
* passed, the implementation does nothing.
*/
void semDestroy(SEM *sem);
/**
* @brief P-operation.
*
* Attempts to decrement the semaphore value by 1. If the semaphore value is not a
* positive number, the operation blocks until a V-operation increments the value
* and the P-operation succeeds.
*
* @param sem Handle of the semaphore to decrement.
*/
void P(SEM *sem);
/**
* @brief V-operation.
*
* Increments the semaphore value by 1 and notifies P-operations that are
* blocked on the semaphore of the change.
*
* @param sem Handle of the semaphore to increment.
*/
void V(SEM *sem);
#endif /* SEM_H */