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 
10 #pragma once
11 
23 #include <stddef.h>
24 #include <stdint.h>
25 #include <stdbool.h>
26 
27 #include "architecture.h"
28 #include "kernel_defines.h"
29 #include "list.h"
30 #include "thread.h"
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
39 typedef struct {
46 #if defined(DOXYGEN) || defined(MODULE_CORE_MUTEX_PRIORITY_INHERITANCE) \
47  || defined(MODULE_CORE_MUTEX_DEBUG)
58 #endif
59 #if defined(DOXYGEN) || defined(MODULE_CORE_MUTEX_DEBUG)
68 #endif
69 #if defined(DOXYGEN) || defined(MODULE_CORE_MUTEX_PRIORITY_INHERITANCE)
76 #endif
77 } mutex_t;
78 
98 bool mutex_lock_internal(mutex_t *mutex, bool block);
99 
106 typedef struct {
109  uint8_t cancelled;
111 
112 #ifndef __cplusplus
117 # define MUTEX_INIT { .queue = { .next = NULL } }
118 
122 # define MUTEX_INIT_LOCKED { .queue = { .next = MUTEX_LOCKED } }
123 #else
124 # define MUTEX_INIT {}
125 # define MUTEX_INIT_LOCKED { { MUTEX_LOCKED } }
126 #endif /* __cplusplus */
127 
133 #define MUTEX_LOCKED ((list_node_t *)-1)
144 static inline void mutex_init(mutex_t *mutex)
145 {
146  mutex->queue.next = NULL;
147 }
148 
155 static inline void mutex_init_locked(mutex_t *mutex)
156 {
157  *mutex = (mutex_t)MUTEX_INIT_LOCKED;
158 }
159 
171 {
172  mutex_cancel_t result = { mutex, thread_get_active(), 0 };
173 
174  return result;
175 }
176 
189 static inline int mutex_trylock(mutex_t *mutex)
190 {
191  return mutex_lock_internal(mutex, false);
192 }
193 
205 static inline void mutex_lock(mutex_t *mutex)
206 {
207 #if (MAXTHREADS > 1)
208  mutex_lock_internal(mutex, true);
209 #else
210  /* dummy implementation for when no scheduler is used */
211  /* (ab)use next pointer as lock variable */
212  volatile uintptr_t *lock = (void *)&mutex->queue.next;
213 
214  /* spin until lock is released (e.g. by interrupt).
215  *
216  * Note: since only the numbers 0 and 1 are ever stored in lock, this
217  * read does not need to be atomic here - even while a concurrent write
218  * is performed on lock, a read will still either yield 0 or 1 (so the old
219  * or new value, which both is fine), even if the lock is read out byte-wise
220  * (e.g. on AVR).
221  */
222  while (*lock) {}
223 
224  /* set lock variable */
225  *lock = 1;
226 #endif
227 }
228 
252 
262 #if (MAXTHREADS > 1) || DOXYGEN
263 void mutex_unlock(mutex_t *mutex);
264 #else
268 static inline void mutex_unlock(mutex_t *mutex)
269 {
270  /* (ab)use next pointer as lock variable */
271  mutex->queue.next = NULL;
272 }
273 #endif
274 
283 
346 
347 #ifdef __cplusplus
348 }
349 #endif
350 
Platform-independent access to architecture details.
int16_t kernel_pid_t
Unique process identifier.
Definition: sched.h:138
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:170
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:155
#define MUTEX_INIT_LOCKED
Static initializer for mutex_t with a locked mutex.
Definition: mutex.h:122
static void mutex_init(mutex_t *mutex)
Initializes a mutex object.
Definition: mutex.h:144
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:205
static int mutex_trylock(mutex_t *mutex)
Tries to get a mutex, non-blocking.
Definition: mutex.h:189
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:410
uintptr_t uinttxtptr_t
Pointer type to point anywhere in the .text section.
Definition: architecture.h:136
Common macros and compiler attributes/pragmas configuration.
Intrusive linked list.
thread_t holds thread's context data.
Definition: thread.h:170
List node structure.
Definition: list.h:39
struct list_node * next
pointer to next list entry
Definition: list.h:40
A cancellation structure for use with mutex_lock_cancelable and mutex_cancel.
Definition: mutex.h:106
thread_t * thread
The thread trying to lock the mutex.
Definition: mutex.h:108
uint8_t cancelled
Flag whether the mutex has been cancelled.
Definition: mutex.h:109
mutex_t * mutex
The mutex to lock.
Definition: mutex.h:107
Mutex structure.
Definition: mutex.h:39
uint8_t owner_original_priority
Original priority of the owner.
Definition: mutex.h:75
kernel_pid_t owner
The current owner of the mutex or NULL
Definition: mutex.h:57
list_node_t queue
The process waiting queue of the mutex.
Definition: mutex.h:45
uinttxtptr_t owner_calling_pc
Program counter of the call to mutex_lock that most recently acquired this mutex.
Definition: mutex.h:67
Provides utility functions for event handler threads.