2 * Bacula Semaphore code. This code permits setting up
3 * a semaphore that lets through a specified number
4 * of callers simultaneously. Once the number of callers
5 * exceed the limit, they block.
7 * Kern Sibbald, March MMIII
9 * Derived from rwlock.h which was in turn derived from code in
10 * "Programming with POSIX Threads" By David R. Butenhof
15 Copyright (C) 2000-2003 Kern Sibbald and John Walker
17 This program is free software; you can redistribute it and/or
18 modify it under the terms of the GNU General Public License as
19 published by the Free Software Foundation; either version 2 of
20 the License, or (at your option) any later version.
22 This program is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 General Public License for more details.
27 You should have received a copy of the GNU General Public
28 License along with this program; if not, write to the Free
29 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
37 typedef struct s_semlock_tag {
38 pthread_mutex_t mutex; /* main lock */
39 pthread_cond_t wait; /* wait for available slot */
40 int valid; /* set when valid */
41 int waiting; /* number of callers waiting */
42 int max_active; /* maximum active callers */
43 int active; /* number of active callers */
46 #define SEMLOCK_VALID 0xfacade
48 #define SEM_INIIALIZER \
49 {PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, \
50 PTHREAD_COND_INITIALIZER, SEMLOCK_VALID, 0, 0, 0, 0}
53 * semaphore lock prototypes
55 extern int sem_init(semlock_t *sem, int max_active);
56 extern int sem_destroy(semlock_t *sem);
57 extern int sem_lock(semlock_t *sem);
58 extern int sem_trylock(semlock_t *sem);
59 extern int sem_unlock(semlock_t *sem);
61 #endif /* __SEMLOCK_H */