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
262static inline void mutex_init_locked(mutex_t *mutex)
263{
264 *mutex = (mutex_t)MUTEX_INIT_LOCKED;
265}
266
278{
279 mutex_cancel_t result = { mutex, thread_get_active(), 0 };
280
281 return result;
282}
283
296static inline int mutex_trylock(mutex_t *mutex)
297{
298 return mutex_lock_internal(mutex, false);
299}
300
312static inline void mutex_lock(mutex_t *mutex)
313{
314#if (MAXTHREADS > 1)
315 mutex_lock_internal(mutex, true);
316#else
317 /* dummy implementation for when no scheduler is used */
318 /* (ab)use next pointer as lock variable */
319 volatile uintptr_t *lock = (void *)&mutex->queue.next;
320
321 /* spin until lock is released (e.g. by interrupt).
322 *
323 * Note: since only the numbers 0 and 1 are ever stored in lock, this
324 * read does not need to be atomic here - even while a concurrent write
325 * is performed on lock, a read will still either yield 0 or 1 (so the old
326 * or new value, which both is fine), even if the lock is read out byte-wise
327 * (e.g. on AVR).
328 */
329 while (*lock) {}
330
331 /* set lock variable */
332 *lock = 1;
333#endif
334}
335
359
369#if (MAXTHREADS > 1) || DOXYGEN
371#else
375static inline void mutex_unlock(mutex_t *mutex)
376{
377 /* (ab)use next pointer as lock variable */
378 mutex->queue.next = NULL;
379}
380#endif
381
390
453
454#ifdef __cplusplus
455}
456#endif
457
458#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:277
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:262
#define MUTEX_INIT_LOCKED
Static initializer for mutex_t with a locked mutex.
Definition mutex.h:229
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:312
static int mutex_trylock(mutex_t *mutex)
Tries to get a mutex, non-blocking.
Definition mutex.h:296
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: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.