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
127#ifndef MUTEX_H
128#define MUTEX_H
129
130#include <stddef.h>
131#include <stdint.h>
132#include <stdbool.h>
133
134#include "architecture.h"
135#include "kernel_defines.h"
136#include "list.h"
137#include "thread.h"
138
139#ifdef __cplusplus
140extern "C" {
141#endif
142
146typedef struct {
153#if defined(DOXYGEN) || defined(MODULE_CORE_MUTEX_PRIORITY_INHERITANCE) \
154 || defined(MODULE_CORE_MUTEX_DEBUG)
165#endif
166#if defined(DOXYGEN) || defined(MODULE_CORE_MUTEX_DEBUG)
175#endif
176#if defined(DOXYGEN) || defined(MODULE_CORE_MUTEX_PRIORITY_INHERITANCE)
183#endif
184} mutex_t;
185
205bool mutex_lock_internal(mutex_t *mutex, bool block);
206
218
219#ifndef __cplusplus
224# define MUTEX_INIT { .queue = { .next = NULL } }
225
229# define MUTEX_INIT_LOCKED { .queue = { .next = MUTEX_LOCKED } }
230#else
231# define MUTEX_INIT {}
232# define MUTEX_INIT_LOCKED { { MUTEX_LOCKED } }
233#endif /* __cplusplus */
234
240#define MUTEX_LOCKED ((list_node_t *)-1)
251static inline void mutex_init(mutex_t *mutex)
252{
253 mutex->queue.next = NULL;
254}
255
267{
268 mutex_cancel_t result = { mutex, thread_get_active(), 0 };
269
270 return result;
271}
272
285static inline int mutex_trylock(mutex_t *mutex)
286{
287 return mutex_lock_internal(mutex, false);
288}
289
301static inline void mutex_lock(mutex_t *mutex)
302{
303#if (MAXTHREADS > 1)
304 mutex_lock_internal(mutex, true);
305#else
306 /* dummy implementation for when no scheduler is used */
307 /* (ab)use next pointer as lock variable */
308 volatile uintptr_t *lock = (void *)&mutex->queue.next;
309
310 /* spin until lock is released (e.g. by interrupt).
311 *
312 * Note: since only the numbers 0 and 1 are ever stored in lock, this
313 * read does not need to be atomic here - even while a concurrent write
314 * is performed on lock, a read will still either yield 0 or 1 (so the old
315 * or new value, which both is fine), even if the lock is read out byte-wise
316 * (e.g. on AVR).
317 */
318 while (*lock) {}
319
320 /* set lock variable */
321 *lock = 1;
322#endif
323}
324
348
358#if (MAXTHREADS > 1) || DOXYGEN
360#else
364static inline void mutex_unlock(mutex_t *mutex)
365{
366 /* (ab)use next pointer as lock variable */
367 mutex->queue.next = NULL;
368}
369#endif
370
379
442
443#ifdef __cplusplus
444}
445#endif
446
447#endif /* MUTEX_H */
Platform-independent access to architecture details.
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:266
int mutex_lock_cancelable(mutex_cancel_t *mc)
Locks a mutex, blocking.
static void mutex_init(mutex_t *mutex)
Initializes a mutex object.
Definition mutex.h:251
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:301
static int mutex_trylock(mutex_t *mutex)
Tries to get a mutex, non-blocking.
Definition mutex.h:285
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:397
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:213
thread_t * thread
The thread trying to lock the mutex.
Definition mutex.h:215
uint8_t cancelled
Flag whether the mutex has been cancelled.
Definition mutex.h:216
mutex_t * mutex
The mutex to lock.
Definition mutex.h:214
Mutex structure.
Definition mutex.h:146
uint8_t owner_original_priority
Original priority of the owner.
Definition mutex.h:182
kernel_pid_t owner
The current owner of the mutex or NULL
Definition mutex.h:164
list_node_t queue
The process waiting queue of the mutex.
Definition mutex.h:152
uinttxtptr_t owner_calling_pc
Program counter of the call to mutex_lock that most recently acquired this mutex.
Definition mutex.h:174
Provides utility functions for event handler threads.