1: /*
2: * Copyright (C) 2011 Tobias Brunner
3: * HSR Hochschule fuer Technik Rapperswil
4: *
5: * This program is free software; you can redistribute it and/or modify it
6: * under the terms of the GNU General Public License as published by the
7: * Free Software Foundation; either version 2 of the License, or (at your
8: * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9: *
10: * This program is distributed in the hope that it will be useful, but
11: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13: * for more details.
14: */
15:
16: /**
17: * @defgroup semaphore semaphore
18: * @{ @ingroup threading
19: */
20:
21: #ifndef THREADING_SEMAPHORE_H_
22: #define THREADING_SEMAPHORE_H_
23:
24: #include <utils/utils.h>
25:
26: typedef struct semaphore_t semaphore_t;
27:
28: /**
29: * A semaphore is basically an integer whose value is never allowed to be
30: * lower than 0. Two operations can be performed on it: increment the
31: * value by one, and decrement the value by one. If the value is currently
32: * zero, then the decrement operation will block until the value becomes
33: * greater than zero.
34: */
35: struct semaphore_t {
36:
37: /**
38: * Decrease the value by one, if it is greater than zero. Otherwise the
39: * current thread is blocked and it waits until the value increases.
40: */
41: void (*wait)(semaphore_t *this);
42:
43: /**
44: * Decrease the value by one, if it is greater than zero. Otherwise the
45: * current thread is blocked and it waits until the value increases, or the
46: * call times out.
47: *
48: * @param timeout timeout im ms
49: * @return TRUE if timed out, FALSE otherwise
50: */
51: bool (*timed_wait)(semaphore_t *this, u_int timeout);
52:
53: /**
54: * Decrease the value by one, if it is greater than zero. Otherwise the
55: * current thread is blocked and it waits until the value increases, or the
56: * call times out.
57: *
58: * The passed timeval should be calculated based on the time_monotonic()
59: * function.
60: *
61: * @param tv absolute time until timeout
62: * @return TRUE if timed out, FALSE otherwise
63: */
64: bool (*timed_wait_abs)(semaphore_t *this, timeval_t tv);
65:
66: /**
67: * Increase the value by one. If the value becomes greater than zero, then
68: * another thread waiting will be woken up.
69: */
70: void (*post)(semaphore_t *this);
71:
72: /**
73: * Destroy a semaphore and free its resources.
74: */
75: void (*destroy)(semaphore_t *this);
76: };
77:
78: /**
79: * Create a semaphore instance.
80: *
81: * @param value initial value (typically 0)
82: * @return semaphore instance
83: */
84: semaphore_t *semaphore_create(u_int value);
85:
86: #endif /** THREADING_SEMAPHORE_H_ @} */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>