2 * Copyright (c) 2005 Zmanda, Inc. All Rights Reserved.
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License version 2.1 as
6 * published by the Free Software Foundation.
8 * This library 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 Lesser General Public
11 * License for more details.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this library; if not, write to the Free Software Foundation,
15 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17 * Contact information: Zmanda Inc., 505 N Mathlida Ave, Suite 120
18 * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
21 /* GLib does not provide semaphores, which are useful in queue.c.
22 So, we implement it here. */
31 GCond * decrement_cond;
35 /* Create a new semaphore object with the given value.
37 * @param value: new value
38 * @returns: newly allocated semaphore_t
40 semaphore_t* semaphore_new_with_value(int value);
42 /* Shortcut to make a new semaphore with value 1.
44 #define semaphore_new() semaphore_new_with_value(1)
46 /* Free a semaphore allocated by semaphore_with_new_value(). Be sure the
47 * semaphore is no longer in use by any threads.
49 * @param sem: the semaphore to free
51 void semaphore_free(semaphore_t *sem);
53 /* Increment the value of the semaphore by incr. This corresponds to
54 * Dijkstra's V(), or the typical semaphore's release().
56 * This function will not block, but may wake other threads waiting
57 * on semaphore_decrement().
59 * @param sem: the semaphore
60 * @param incr: added to the semaphore's value
62 void semaphore_increment(semaphore_t *sem, unsigned int incr);
64 /* Shortcut to increment the semaphore by 1.
66 #define semaphore_up(semaphore) semphore_increment(semaphore,1)
68 /* Decrement the value of the semaphore by incr. If this operation
69 * would make the semaphore zero or less, block until the semaphore
70 * value is large enough, then perform the decerement operation. Threads
71 * waiting on semaphore_wait_empty() may be awakened if the value
74 * @param sem: the semaphore
75 * @param decr: subtracted from the semaphore's value
77 void semaphore_decrement(semaphore_t *sem, unsigned int decr);
79 /* Shortcut to decrement the semaphore by 1.
81 #define semaphore_down(semaphore) semaphore_decrement(semaphore, 1)
83 /* Increment or decrement (with a negative incr) the value without
84 * blocking. Threads waiting on semaphore_decrement() or
85 * semaphore_wait_empty() will be awakened if necessary.
87 * @param sem: the semaphore
88 * @param incr: added to the semaphore's value
90 void semaphore_force_adjust(semaphore_t *sem, int incr);
92 /* Set the semaphore to a given value without blocking. Threads
93 * waiting on semaphore_decrement() or semaphore_wait_empty()
94 * will be awakened if necessary.
96 * @param sem: the semaphore
97 * @param value: the new value
99 void semaphore_force_set(semaphore_t *sem, int value);
101 /* Block until the semaphore's value is zero.
103 * @param sem: the semaphore
105 void semaphore_wait_empty(semaphore_t *sem);
107 #endif /* SEMAPHORE_H */