summaryrefslogtreecommitdiffstats
path: root/src/semaphore.h
blob: 4ca4f652f599b520d759f656f42aa782c4ba9b38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
 * Authors: Martin Nagy <mnagy@redhat.com>
 *
 * Copyright (C) 2008  Red Hat
 * see file 'COPYING' for use and warranty information
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; version 2 or later
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#ifndef _LD_SEMAPHORE_H_
#define _LD_SEMAPHORE_H_

#include <isc/condition.h>
#include <isc/mutex.h>

/*
 * Semaphore can be "acquired" multiple times. However, it has a maximum
 * number of times someone can acquire him. If a semaphore is already acquired
 * more times than allowed, it will block until other thread release its,
 */
struct semaphore {
	int value;		/* Maximum number of times you can LOCK()) */
	isc_mutex_t mutex;	/* Mutex protecting this whole struct.     */
	isc_condition_t cond;	/* Condition used for waiting on release.  */
};

typedef struct semaphore	semaphore_t;

/* Public functions. */
isc_result_t	semaphore_init(semaphore_t *sem, int value);
void		semaphore_destroy(semaphore_t *sem);
void		semaphore_wait(semaphore_t *sem);
void		semaphore_signal(semaphore_t *sem);

#endif /* !_LD_SEMAPHORE_H_ */