pthread_spin.h
2.92 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* Copyright (C) 2014 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup pthread
* @{
* @file
* @brief Spin locks.
* @note Do not include this header file directly, but pthread.h.
* @warning Spinlocks should be avoided.
* They will burn away the battery needlessly, and may not work because RIOT is tickless.
* Use irq_disable() and irq_restore() for shortterm locks instead.
*/
#ifndef PTHREAD_SPIN_H
#define PTHREAD_SPIN_H
#include <stdatomic.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief A spinlock.
* @warning Spinlocks should be avoided.
* They will burn away the battery needlessly, and may not work because RIOT is tickless.
* Use irq_disable() and irq_restore() for shortterm locks instead.
*/
typedef struct {
atomic_flag flag; /**< Current lock state */
} pthread_spinlock_t;
/**
* @brief Intializes a spinlock.
* @warning See the warning in pthread_spinlock_t.
* @details A zeroed out datum is initialized.
* @param[in,out] lock Datum to initialize.
* @param[in] pshared Unused.
* @returns `0` on success.
* `EINVAL` if `lock == NULL`.
*/
int pthread_spin_init(pthread_spinlock_t *lock, int pshared);
/**
* @brief Destroys a spinlock.
* @warning See the warning in pthread_spinlock_t.
* @details Destroying a spinlock while a thread is waiting for it causes undefined behavior.
* This is a no-op.
* @param[in,out] lock Datum to destroy.
* @returns `0` on success.
* `EINVAL` if `lock == NULL`.
*/
int pthread_spin_destroy(pthread_spinlock_t *lock);
/**
* @brief Lock a spinlock.
* @warning See the warning in pthread_spinlock_t.
* @param[in,out] lock Lock to acquire.
* @return `0` on success.
* `EINVAL` if `lock == NULL`.
*/
int pthread_spin_lock(pthread_spinlock_t *lock);
/**
* @brief Tries to lock a spinlock, returns immediately if already locked.
* @warning See the warning in pthread_spinlock_t.
* @param[in,out] lock Lock to acquire.
* @return `0` on success.
* `EBUSY` if the lock was already locked.
* `EINVAL` if `lock == NULL`.
*/
int pthread_spin_trylock(pthread_spinlock_t *lock);
/**
* @brief Releases a spinlock.
* @warning See the warning in pthread_spinlock_t.
* @param[in,out] lock Lock to release
* @return `0` on success.
* `EPERM` if the lock was not locked.
* `EINVAL` if `lock == NULL`.
*/
int pthread_spin_unlock(pthread_spinlock_t *lock);
#ifdef __cplusplus
}
#endif
#endif /* PTHREAD_SPIN_H */
/**
* @}
*/