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-2006 Kern Sibbald
17 This program is free software; you can redistribute it and/or
18 modify it under the terms of the GNU General Public License
19 version 2 as amended with additional clauses defined in the
20 file LICENSE in the main source directory.
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
25 the file LICENSE for additional details.
32 typedef struct s_semlock_tag {
33 pthread_mutex_t mutex; /* main lock */
34 pthread_cond_t wait; /* wait for available slot */
35 int valid; /* set when valid */
36 int waiting; /* number of callers waiting */
37 int max_active; /* maximum active callers */
38 int active; /* number of active callers */
41 #define SEMLOCK_VALID 0xfacade
43 #define SEM_INIIALIZER \
44 {PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, \
45 PTHREAD_COND_INITIALIZER, SEMLOCK_VALID, 0, 0, 0, 0}
48 * semaphore lock prototypes
50 extern int sem_init(semlock_t *sem, int max_active);
51 extern int sem_destroy(semlock_t *sem);
52 extern int sem_lock(semlock_t *sem);
53 extern int sem_trylock(semlock_t *sem);
54 extern int sem_unlock(semlock_t *sem);
56 #endif /* __SEMLOCK_H */