]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/semlock.h
Win32 BackupRead/Write, begin adding Base, Jmsg knows about console, autochanger...
[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-2003 Kern Sibbald and John Walker
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 as
19    published by the Free Software Foundation; either version 2 of
20    the License, or (at your option) any later version.
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 GNU
25    General Public License for more details.
26
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,
30    MA 02111-1307, USA.
31
32  */
33
34 #ifndef __SEMLOCK_H 
35 #define __SEMLOCK_H 1
36
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 */
44 } semlock_t;
45
46 #define SEMLOCK_VALID  0xfacade
47
48 #define SEM_INIIALIZER \
49    {PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, \
50     PTHREAD_COND_INITIALIZER, SEMLOCK_VALID, 0, 0, 0, 0}
51
52 /* 
53  * semaphore lock prototypes
54  */
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);
60
61 #endif /* __SEMLOCK_H */