libmetal
condition.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, Xilinx Inc. and Contributors. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * 3. Neither the name of Xilinx nor the names of its contributors may be used
15  * to endorse or promote products derived from this software without
16  * specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
24 CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
30 IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 OTHERWISE)
33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
34 OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 /*
39  * @file linux/condition.h
40  * @brief Linux condition variable primitives for libmetal.
41  */
42 
43 #ifndef __METAL_CONDITION__H__
44 #error "Include metal/condition.h instead of metal/linux/condition.h"
45 #endif
46 
47 #ifndef __METAL_LINUX_CONDITION__H__
48 #define __METAL_LINUX_CONDITION__H__
49 
50 #include <unistd.h>
51 #include <sys/syscall.h>
52 #include <linux/futex.h>
53 #include "metal/atomic.h"
54 #include <stdint.h>
55 #include <limits.h>
56 #include <errno.h>
57 
58 #ifdef __cplusplus
59 extern "C" {
60 #endif
61 
62 struct metal_condition {
63  metal_mutex_t *m;
71 };
72 
74 #define METAL_CONDITION_INIT { NULL, ATOMIC_VAR_INIT(0), ATOMIC_VAR_INIT(0) }
75 
76 static inline void metal_condition_init(struct metal_condition *cv)
77 {
78  cv->m = NULL;
79  atomic_init(&cv->waiters, 0);
80  atomic_init(&cv->wakeups, 0);
81 }
82 
83 static inline int metal_condition_signal(struct metal_condition *cv)
84 {
85  if (!cv)
86  return -EINVAL;
87 
88  atomic_fetch_add(&cv->wakeups, 1);
89  if (atomic_load(&cv->waiters) > 0)
90  syscall(SYS_futex, &cv->wakeups, FUTEX_WAKE, 1, NULL, NULL, 0);
91  return 0;
92 }
93 
94 static inline int metal_condition_broadcast(struct metal_condition *cv)
95 {
96  if (!cv)
97  return -EINVAL;
98 
99  atomic_fetch_add(&cv->wakeups, 1);
100  if (atomic_load(&cv->waiters) > 0)
101  syscall(SYS_futex, &cv->wakeups, FUTEX_WAKE, INT_MAX, NULL, NULL, 0);
102  return 0;
103 }
104 
105 #endif /* __METAL_LINUX_CONDITION__H__ */
#define atomic_init(OBJ, VAL)
Definition: atomic.h:75
atomic_int wakeups
Definition: condition.h:70
Definition: condition.h:49
static int metal_condition_signal(struct metal_condition *cv)
Definition: condition.h:83
atomic_int waiters
Definition: condition.h:69
static void metal_condition_init(struct metal_condition *cv)
Definition: condition.h:76
#define atomic_fetch_add(OBJ, VAL)
Definition: atomic.h:118
#define atomic_load(OBJ)
Definition: atomic.h:83
static int metal_condition_broadcast(struct metal_condition *cv)
Definition: condition.h:94
Definition: mutex.h:52
metal_mutex_t * m
Definition: condition.h:50
int atomic_int
Definition: atomic.h:48