libmetal
Loading...
Searching...
No Matches
condition.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2016, Xilinx Inc. and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7/*
8 * @file generic/condition.h
9 * @brief Generic 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_GENERIC_CONDITION__H__
17#define __METAL_GENERIC_CONDITION__H__
18
19#include <unistd.h>
20#include <metal/atomic.h>
21#include <stdint.h>
22#include <limits.h>
23#include <errno.h>
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
29struct metal_condition {
38};
39
41#define METAL_CONDITION_INIT { ATOMIC_VAR_INIT(0), ATOMIC_VAR_INIT(0) }
42
43static inline void metal_condition_init(struct metal_condition *cv)
44{
45 atomic_init(&cv->mptr, 0);
46 atomic_init(&cv->v, 0);
47}
48
49static inline int metal_condition_signal(struct metal_condition *cv)
50{
51 if (!cv)
52 return -EINVAL;
53
55 atomic_fetch_add(&cv->v, 1);
56 return 0;
57}
58
59static inline int metal_condition_broadcast(struct metal_condition *cv)
60{
61 return metal_condition_signal(cv);
62}
63
64
65#ifdef __cplusplus
66}
67#endif
68
69#endif /* __METAL_GENERIC_CONDITION__H__ */
#define atomic_fetch_add(OBJ, VAL)
Definition: atomic.h:95
int atomic_int
Definition: atomic.h:24
#define atomic_init(OBJ, VAL)
Definition: atomic.h:52
atomic_uint atomic_uintptr_t
Definition: atomic.h:26
static int metal_condition_signal(struct metal_condition *cv)
Notify one waiter. Before calling this function, the caller should have acquired the mutex.
static int metal_condition_broadcast(struct metal_condition *cv)
Notify all waiters. Before calling this function, the caller should have acquired the mutex.
static void metal_condition_init(struct metal_condition *cv)
Initialize a libmetal condition variable.
Definition: condition.h:25
atomic_int v
Definition: condition.h:32
atomic_uintptr_t mptr
Definition: condition.h:30