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 
22 #ifndef MUTEX_H
23 #define MUTEX_H
24 
25 #include <stddef.h>
26 #include <stdint.h>
27 
28 #include "list.h"
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
37 typedef struct {
44 } mutex_t;
45 
50 #define MUTEX_INIT { { NULL } }
51 
55 #define MUTEX_INIT_LOCKED { { MUTEX_LOCKED } }
56 
62 #define MUTEX_LOCKED ((list_node_t *)-1)
63 
73 static inline void mutex_init(mutex_t *mutex)
74 {
75  mutex->queue.next = NULL;
76 }
77 
91 int _mutex_lock(mutex_t *mutex, volatile uint8_t *blocking);
92 
102 static inline int mutex_trylock(mutex_t *mutex)
103 {
104  volatile uint8_t blocking = 0;
105 
106  return _mutex_lock(mutex, &blocking);
107 }
108 
114 static inline void mutex_lock(mutex_t *mutex)
115 {
116  volatile uint8_t blocking = 1;
117 
118  _mutex_lock(mutex, &blocking);
119 }
120 
126 void mutex_unlock(mutex_t *mutex);
127 
133 void mutex_unlock_and_sleep(mutex_t *mutex);
134 
135 #ifdef __cplusplus
136 }
137 #endif
138 
139 #endif /* MUTEX_H */
140 
static void mutex_lock(mutex_t *mutex)
Locks a mutex, blocking.
Definition: mutex.h:114
void mutex_unlock_and_sleep(mutex_t *mutex)
Unlocks the mutex and sends the current thread to sleep.
int _mutex_lock(mutex_t *mutex, volatile uint8_t *blocking)
Lock a mutex, blocking or non-blocking.
list_node_t queue
The process waiting queue of the mutex.
Definition: mutex.h:43
void mutex_unlock(mutex_t *mutex)
Unlocks the mutex.
Intrusive linked list.
static int mutex_trylock(mutex_t *mutex)
Tries to get a mutex, non-blocking.
Definition: mutex.h:102
List node structure.
Definition: list.h:40
struct list_node * next
pointer to next list entry
Definition: list.h:41
Mutex structure.
Definition: mutex.h:37
static void mutex_init(mutex_t *mutex)
Initializes a mutex object.
Definition: mutex.h:73