libmetal
condition.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, Linaro Limited. and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 /*
8  * @file zephyr/condition.h
9  * @brief Zephyr condition variable primitives for libmetal.
10  */
11 
12 #ifndef __METAL_CONDITION__H__
13 #error "Include metal/condition.h instead of metal/generic/condition.h"
14 #endif
15 
16 #ifndef __METAL_ZEPHYR_CONDITION__H__
17 #define __METAL_ZEPHYR_CONDITION__H__
18 
19 #include <errno.h>
20 #include <metal/atomic.h>
21 #include <stddef.h>
22 #include <stdint.h>
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 struct metal_condition {
29  metal_mutex_t *m;
35  atomic_int v;
36 };
37 
39 #define METAL_CONDITION_INIT { NULL, ATOMIC_VAR_INIT(0) }
40 
41 static inline void metal_condition_init(struct metal_condition *cv)
42 {
43  cv->m = NULL;
44  atomic_init(&cv->v, 0);
45 }
46 
47 static inline int metal_condition_signal(struct metal_condition *cv)
48 {
49  if (!cv)
50  return -EINVAL;
51 
53  atomic_fetch_add(&cv->v, 1);
54  return 0;
55 }
56 
57 static inline int metal_condition_broadcast(struct metal_condition *cv)
58 {
59  return metal_condition_signal(cv);
60 }
61 
62 
63 #ifdef __cplusplus
64 }
65 #endif
66 
67 #endif /* __METAL_ZEPHYR_CONDITION__H__ */
#define atomic_init(OBJ, VAL)
Definition: atomic.h:51
static int metal_condition_signal(struct metal_condition *cv)
Definition: condition.h:47
atomic_int v
Definition: condition.h:32
Definition: condition.h:25
static void metal_condition_init(struct metal_condition *cv)
Definition: condition.h:41
static int metal_condition_broadcast(struct metal_condition *cv)
Definition: condition.h:57
#define atomic_fetch_add(OBJ, VAL)
Definition: atomic.h:94
Definition: mutex.h:26
metal_mutex_t * m
Definition: condition.h:26
int atomic_int
Definition: atomic.h:24