The friendly Operating System for the Internet of Things
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 
27 #include "list.h"
28 
29 #ifdef __cplusplus
30  extern "C" {
31 #endif
32 
36 typedef struct {
43 } mutex_t;
44 
49 #define MUTEX_INIT { { NULL } }
50 
54 #define MUTEX_INIT_LOCKED { { MUTEX_LOCKED } }
55 
61 #define MUTEX_LOCKED ((void *)-1)
62 
72 static inline void mutex_init(mutex_t *mutex)
73 {
74  mutex->queue.next = NULL;
75 }
76 
90 int _mutex_lock(mutex_t *mutex, int blocking);
91 
101 static inline int mutex_trylock(mutex_t *mutex)
102 {
103  return _mutex_lock(mutex, 0);
104 }
105 
111 static inline void mutex_lock(mutex_t *mutex)
112 {
113  _mutex_lock(mutex, 1);
114 }
115 
121 void mutex_unlock(mutex_t *mutex);
122 
128 void mutex_unlock_and_sleep(mutex_t *mutex);
129 
130 #ifdef __cplusplus
131 }
132 #endif
133 
134 #endif /* MUTEX_H */
135 
static void mutex_init(mutex_t *mutex)
Initializes a mutex object.
Definition: mutex.h:72
list_node_t queue
The process waiting queue of the mutex.
Definition: mutex.h:42
Intrusive linked list.
void mutex_unlock(mutex_t *mutex)
Unlocks the mutex.
static void mutex_lock(mutex_t *mutex)
Locks a mutex, blocking.
Definition: mutex.h:111
void mutex_unlock_and_sleep(mutex_t *mutex)
Unlocks the mutex and sends the current thread to sleep.
List node structure.
Definition: list.h:40
static int mutex_trylock(mutex_t *mutex)
Tries to get a mutex, non-blocking.
Definition: mutex.h:101
struct list_node * next
pointer to next list entry
Definition: list.h:41
Mutex structure.
Definition: mutex.h:36
int _mutex_lock(mutex_t *mutex, int blocking)
Lock a mutex, blocking or non-blocking.