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 
102 #ifndef MUTEX_H
103 #define MUTEX_H
104 
105 #include <stddef.h>
106 #include <stdint.h>
107 
108 #include "kernel_defines.h"
109 #include "list.h"
110 #include "thread.h"
111 
112 #ifndef __cplusplus
113 #include "irq.h"
114 #endif
115 
116 #ifdef __cplusplus
117 extern "C" {
118 #endif
119 
123 typedef struct {
130 } mutex_t;
131 
138 typedef struct {
141  uint8_t cancelled;
143 
148 #define MUTEX_INIT { { NULL } }
149 
153 #define MUTEX_INIT_LOCKED { { MUTEX_LOCKED } }
154 
160 #define MUTEX_LOCKED ((list_node_t *)-1)
161 
171 static inline void mutex_init(mutex_t *mutex)
172 {
173  mutex->queue.next = NULL;
174 }
175 
187 {
188  mutex_cancel_t result = { mutex, thread_get_active(), 0 };
189 
190  return result;
191 }
192 
210 int mutex_trylock_ffi(mutex_t *mutex);
211 
224 static inline int mutex_trylock(mutex_t *mutex)
225 {
226 #ifdef __cplusplus
227  return mutex_trylock_ffi(mutex);
228 #else
229  unsigned irq_state = irq_disable();
230  int retval = 0;
231 
232  if (mutex->queue.next == NULL) {
233  mutex->queue.next = MUTEX_LOCKED;
234  retval = 1;
235  }
236  irq_restore(irq_state);
237  return retval;
238 #endif
239 }
240 
252 void mutex_lock(mutex_t *mutex);
253 
277 
287 void mutex_unlock(mutex_t *mutex);
288 
296 void mutex_unlock_and_sleep(mutex_t *mutex);
297 
359 void mutex_cancel(mutex_cancel_t *mc);
360 
361 #ifdef __cplusplus
362 }
363 #endif
364 
365 #endif /* MUTEX_H */
366 
thread_t * thread
The thread trying to lock the mutex.
Definition: mutex.h:140
Threading API.
MAYBE_INLINE unsigned irq_disable(void)
This function sets the IRQ disable bit in the status register.
void mutex_unlock_and_sleep(mutex_t *mutex)
Unlocks the mutex and sends the current thread to sleep.
MAYBE_INLINE void irq_restore(unsigned state)
This function restores the IRQ disable bit in the status register to the value contained within passe...
int mutex_trylock_ffi(mutex_t *mutex)
Tries to get a mutex, non-blocking.
mutex_t * mutex
The mutex to lock.
Definition: mutex.h:139
A cancellation structure for use with mutex_lock_cancelable and mutex_cancel.
Definition: mutex.h:138
thread_t holds thread&#39;s context data.
Definition: thread.h:168
list_node_t queue
The process waiting queue of the mutex.
Definition: mutex.h:129
static thread_t * thread_get_active(void)
Returns a pointer to the Thread Control Block of the currently running thread.
Definition: thread.h:500
void mutex_unlock(mutex_t *mutex)
Unlocks the mutex.
int mutex_lock_cancelable(mutex_cancel_t *mc)
Locks a mutex, blocking.
Intrusive linked list.
static int mutex_trylock(mutex_t *mutex)
Tries to get a mutex, non-blocking.
Definition: mutex.h:224
List node structure.
Definition: list.h:40
uint8_t cancelled
Flag whether the mutex has been cancelled.
Definition: mutex.h:141
Common macros and compiler attributes/pragmas configuration.
void mutex_lock(mutex_t *mutex)
Locks a mutex, blocking.
struct list_node * next
pointer to next list entry
Definition: list.h:41
void mutex_cancel(mutex_cancel_t *mc)
Cancels a call to mutex_lock_cancelable.
Mutex structure.
Definition: mutex.h:123
IRQ driver interface.
static void mutex_init(mutex_t *mutex)
Initializes a mutex object.
Definition: mutex.h:171
static mutex_cancel_t mutex_cancel_init(mutex_t *mutex)
Initialize a mutex cancellation structure.
Definition: mutex.h:186