All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
mutex.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015 Kaspar Schleiser <kaspar@schleiser.de>
3 * 2013, 2014 Freie Universität Berlin
4 *
5 * This file is subject to the terms and conditions of the GNU Lesser
6 * General Public License v2.1. See the file LICENSE in the top level
7 * directory for more details.
8 */
9
21#ifndef MUTEX_H
22#define MUTEX_H
23
24#include <stddef.h>
25#include <stdint.h>
26#include <stdbool.h>
27
28#include "architecture.h"
29#include "kernel_defines.h"
30#include "list.h"
31#include "thread.h"
32
33#ifdef __cplusplus
34extern "C" {
35#endif
36
40typedef struct {
47#if defined(DOXYGEN) || defined(MODULE_CORE_MUTEX_PRIORITY_INHERITANCE) \
48 || defined(MODULE_CORE_MUTEX_DEBUG)
59#endif
60#if defined(DOXYGEN) || defined(MODULE_CORE_MUTEX_DEBUG)
69#endif
70#if defined(DOXYGEN) || defined(MODULE_CORE_MUTEX_PRIORITY_INHERITANCE)
77#endif
78} mutex_t;
79
99bool mutex_lock_internal(mutex_t *mutex, bool block);
100
112
113#ifndef __cplusplus
118# define MUTEX_INIT { .queue = { .next = NULL } }
119
123# define MUTEX_INIT_LOCKED { .queue = { .next = MUTEX_LOCKED } }
124#else
125# define MUTEX_INIT {}
126# define MUTEX_INIT_LOCKED { { MUTEX_LOCKED } }
127#endif /* __cplusplus */
128
134#define MUTEX_LOCKED ((list_node_t *)-1)
145static inline void mutex_init(mutex_t *mutex)
146{
147 mutex->queue.next = NULL;
148}
149
156static inline void mutex_init_locked(mutex_t *mutex)
157{
158 *mutex = (mutex_t)MUTEX_INIT_LOCKED;
159}
160
172{
173 mutex_cancel_t result = { mutex, thread_get_active(), 0 };
174
175 return result;
176}
177
190static inline int mutex_trylock(mutex_t *mutex)
191{
192 return mutex_lock_internal(mutex, false);
193}
194
206static inline void mutex_lock(mutex_t *mutex)
207{
208#if (MAXTHREADS > 1)
209 mutex_lock_internal(mutex, true);
210#else
211 /* dummy implementation for when no scheduler is used */
212 /* (ab)use next pointer as lock variable */
213 volatile uintptr_t *lock = (void *)&mutex->queue.next;
214
215 /* spin until lock is released (e.g. by interrupt).
216 *
217 * Note: since only the numbers 0 and 1 are ever stored in lock, this
218 * read does not need to be atomic here - even while a concurrent write
219 * is performed on lock, a read will still either yield 0 or 1 (so the old
220 * or new value, which both is fine), even if the lock is read out byte-wise
221 * (e.g. on AVR).
222 */
223 while (*lock) {}
224
225 /* set lock variable */
226 *lock = 1;
227#endif
228}
229
253
263#if (MAXTHREADS > 1) || DOXYGEN
265#else
269static inline void mutex_unlock(mutex_t *mutex)
270{
271 /* (ab)use next pointer as lock variable */
272 mutex->queue.next = NULL;
273}
274#endif
275
284
347
348#ifdef __cplusplus
349}
350#endif
351
352#endif /* MUTEX_H */
Platform-independent access to architecture details.
Threading API.
int16_t kernel_pid_t
Unique process identifier.
Definition sched.h:139
void mutex_unlock(mutex_t *mutex)
Unlocks the mutex.
void mutex_cancel(mutex_cancel_t *mc)
Cancels a call to mutex_lock_cancelable.
static mutex_cancel_t mutex_cancel_init(mutex_t *mutex)
Initialize a mutex cancellation structure.
Definition mutex.h:171
int mutex_lock_cancelable(mutex_cancel_t *mc)
Locks a mutex, blocking.
static void mutex_init_locked(mutex_t *mutex)
Initializes a mutex object in a locked state.
Definition mutex.h:156
#define MUTEX_INIT_LOCKED
Static initializer for mutex_t with a locked mutex.
Definition mutex.h:123
static void mutex_init(mutex_t *mutex)
Initializes a mutex object.
Definition mutex.h:145
void mutex_unlock_and_sleep(mutex_t *mutex)
Unlocks the mutex and sends the current thread to sleep.
static void mutex_lock(mutex_t *mutex)
Locks a mutex, blocking.
Definition mutex.h:206
static int mutex_trylock(mutex_t *mutex)
Tries to get a mutex, non-blocking.
Definition mutex.h:190
bool mutex_lock_internal(mutex_t *mutex, bool block)
Internal function implementing mutex_lock and mutex_trylock.
static thread_t * thread_get_active(void)
Returns a pointer to the Thread Control Block of the currently running thread.
Definition thread.h:407
uintptr_t uinttxtptr_t
Pointer type to point anywhere in the .text section.
Common macros and compiler attributes/pragmas configuration.
Intrusive linked list.
thread_t holds thread's context data.
Definition thread.h:168
List node structure.
Definition list.h:40
struct list_node * next
pointer to next list entry
Definition list.h:41
A cancellation structure for use with mutex_lock_cancelable and mutex_cancel.
Definition mutex.h:107
thread_t * thread
The thread trying to lock the mutex.
Definition mutex.h:109
uint8_t cancelled
Flag whether the mutex has been cancelled.
Definition mutex.h:110
mutex_t * mutex
The mutex to lock.
Definition mutex.h:108
Mutex structure.
Definition mutex.h:40
uint8_t owner_original_priority
Original priority of the owner.
Definition mutex.h:76
kernel_pid_t owner
The current owner of the mutex or NULL
Definition mutex.h:58
list_node_t queue
The process waiting queue of the mutex.
Definition mutex.h:46
uinttxtptr_t owner_calling_pc
Program counter of the call to mutex_lock that most recently acquired this mutex.
Definition mutex.h:68