cf0b974ba20ff57848f7730ae5c0084ee2d09cb2
[debian/amanda] / common-src / amsemaphore.h
1 /*
2  * Copyright (c) 2008,2009 Zmanda, Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 as published
6  * by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11  * for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16  *
17  * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18  * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
19  */
20
21 /* GLib does not provide semaphores, which are useful in queue.c.
22    So, we implement it here. */
23
24 #include <glib.h>
25
26 #ifndef SEMAPHORE_H
27
28 typedef struct {
29     int value;
30     GMutex *mutex;
31     GCond * decrement_cond;
32     GCond * zero_cond;
33 } amsemaphore_t;
34
35 /* Create a new semaphore object with the given value.
36  *
37  * @param value: new value
38  * @returns: newly allocated amsemaphore_t
39  */
40 amsemaphore_t* amsemaphore_new_with_value(int value);
41
42 /* Shortcut to make a new semaphore with value 1.
43  */
44 #define amsemaphore_new() amsemaphore_new_with_value(1)
45
46 /* Free a semaphore allocated by amsemaphore_with_new_value().  Be sure the
47  * semaphore is no longer in use by any threads.
48  *
49  * @param sem: the semaphore to free
50  */
51 void amsemaphore_free(amsemaphore_t *sem);
52
53 /* Increment the value of the semaphore by incr.  This corresponds to
54  * Dijkstra's V(), or the typical semaphore's release().
55  *
56  * This function will not block, but may wake other threads waiting
57  * on amsemaphore_decrement().
58  *
59  * @param sem: the semaphore
60  * @param incr: added to the semaphore's value
61  */
62 void amsemaphore_increment(amsemaphore_t *sem, unsigned int incr);
63
64 /* Shortcut to increment the semaphore by 1.
65  */
66 #define amsemaphore_up(semaphore) amsemaphore_increment(semaphore,1)
67
68 /* Decrement the value of the semaphore by incr.  If this operation
69  * would make the semaphore negative, block until the semaphore
70  * value is large enough, then perform the decerement operation. Threads
71  * waiting on amsemaphore_wait_empty() may be awakened if the value
72  * reaches 0.
73  *
74  * @param sem: the semaphore
75  * @param decr: subtracted from the semaphore's value
76  */
77 void amsemaphore_decrement(amsemaphore_t *sem, unsigned int decr);
78
79 /* Shortcut to decrement the semaphore by 1.
80  */
81 #define amsemaphore_down(semaphore) amsemaphore_decrement(semaphore, 1)
82
83 /* Increment or decrement (with a negative incr) the value without
84  * blocking.  Threads waiting on amsemaphore_decrement() or
85  * amsemaphore_wait_empty() will be awakened if necessary.
86  *
87  * @param sem: the semaphore
88  * @param incr: added to the semaphore's value
89  */
90 void amsemaphore_force_adjust(amsemaphore_t *sem, int incr);
91
92 /* Set the semaphore to a given value without blocking.  Threads
93  * waiting on amsemaphore_decrement() or amsemaphore_wait_empty()
94  * will be awakened if necessary.
95  *
96  * @param sem: the semaphore
97  * @param value: the new value
98  */
99 void amsemaphore_force_set(amsemaphore_t *sem, int value);
100
101 /* Block until the semaphore's value is zero.
102  *
103  * @param sem: the semaphore
104  */
105 void amsemaphore_wait_empty(amsemaphore_t *sem);
106
107 #endif /* SEMAPHORE_H */