]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/semlock.h
19July06
[bacula/bacula] / bacula / src / lib / semlock.h
1 /*
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.
6  *
7  *  Kern Sibbald, March MMIII
8  *
9  *   Derived from rwlock.h which was in turn derived from code in
10  *     "Programming with POSIX Threads" By David R. Butenhof
11  *
12  *   Version $Id$
13  */
14 /*
15    Copyright (C) 2000-2006 Kern Sibbald
16
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.
21
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.
26
27  */
28
29 #ifndef __SEMLOCK_H
30 #define __SEMLOCK_H 1
31
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 */
39 } semlock_t;
40
41 #define SEMLOCK_VALID  0xfacade
42
43 #define SEM_INIIALIZER \
44    {PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, \
45     PTHREAD_COND_INITIALIZER, SEMLOCK_VALID, 0, 0, 0, 0}
46
47 /*
48  * semaphore lock prototypes
49  */
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);
55
56 #endif /* __SEMLOCK_H */