summaryrefslogtreecommitdiff
path: root/kern/condition.h
blob: f6d1fd65879e0e4a20900fc63f4ed5fb7aa40ff9 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
 * Copyright (c) 2013-2018 Richard Braun.
 *
 * 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, either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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, see <http://www.gnu.org/licenses/>.
 *
 *
 * Condition variables.
 *
 * A condition variable is a synchronization primitive used to wait
 * until a predicate becomes true. Multiple threads can be waiting
 * for this condition. In order to synchronize changes on the predicate
 * with waiting and signalling, a condition variable must be associated
 * with a mutex.
 */

#ifndef KERN_CONDITION_H
#define KERN_CONDITION_H

#include <stdint.h>

#include <kern/condition_types.h>
#include <kern/mutex_types.h>

struct condition;

/*
 * Initialize a condition variable.
 */
#define condition_init(c) ((void)(c))

/*
 * Wait for a signal on the given condition variable.
 *
 * The associated mutex must be locked when calling this function.
 * It is unlocked before waiting and relocked before returning.
 *
 * When bounding the duration of the wait, the caller must pass an absolute
 * time in ticks, and ETIMEDOUT is returned if that time is reached before
 * the sleep queue is signalled.
 */
void condition_wait(struct condition *condition, struct mutex *mutex);
int condition_timedwait(struct condition *condition,
                        struct mutex *mutex, uint64_t ticks);

/*
 * Wake up one (signal) or all (broadcast) threads waiting on a
 * condition variable, if any.
 *
 * Although it is not necessary to hold the mutex associated to the
 * condition variable when calling these functions, doing so guarantees
 * that a wake-up done when changing the predicate cannot be missed by
 * waiting threads.
 */
void condition_signal(struct condition *condition);
void condition_broadcast(struct condition *condition);

#endif /* KERN_CONDITION_H */