dRonin  adbada4
dRonin firmware
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
pios_mutex.c
Go to the documentation of this file.
1 
11 /*
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20  * for more details.
21  *
22  * You should have received a copy of the GNU General Public License along
23  * with this program; if not, see <http://www.gnu.org/licenses/>
24  */
25 
26 #include "pios.h"
27 #include "pios_mutex.h"
28 
29 #if !defined(PIOS_INCLUDE_RTOS)
30 #error "pios_mutex.c requires an RTOS"
31 #endif
32 
33 #if defined(PIOS_INCLUDE_CHIBIOS)
34 struct pios_mutex
35 {
36  Mutex mtx;
37 };
38 
40 {
41  Mutex mtx;
42  uint32_t count;
43 };
44 
52 struct pios_mutex *PIOS_Mutex_Create(void)
53 {
54  struct pios_mutex *mtx = PIOS_malloc_no_dma(sizeof(struct pios_mutex));
55 
56  if (mtx == NULL)
57  return NULL;
58 
59  chMtxInit(&mtx->mtx);
60 
61  return mtx;
62 }
63 
74 bool PIOS_Mutex_Lock(struct pios_mutex *mtx, uint32_t timeout_ms)
75 {
76  PIOS_Assert(mtx != NULL);
77 
78  chMtxLock(&mtx->mtx);
79 
80  return true;
81 }
82 
92 bool PIOS_Mutex_Unlock(struct pios_mutex *mtx)
93 {
94  PIOS_Assert(mtx != NULL);
95 
96  chMtxUnlock();
97 
98  return true;
99 }
100 
109 {
110  struct pios_recursive_mutex *mtx = PIOS_malloc_no_dma(sizeof(struct pios_recursive_mutex));
111 
112  if (mtx == NULL)
113  return NULL;
114 
115  chMtxInit(&mtx->mtx);
116  mtx->count = 0;
117 
118  return mtx;
119 }
120 
131 bool PIOS_Recursive_Mutex_Lock(struct pios_recursive_mutex *mtx, uint32_t timeout_ms)
132 {
133  PIOS_Assert(mtx != NULL);
134 
135  chSysLock();
136 
137  if (chThdSelf() != mtx->mtx.m_owner)
138  chMtxLockS(&mtx->mtx);
139 
140  ++mtx->count;
141 
142  chSysUnlock();
143 
144  return true;
145 }
146 
157 {
158  PIOS_Assert(mtx != NULL);
159 
160  chSysLock();
161 
162  --mtx->count;
163 
164  if (mtx->count == 0)
165  chMtxUnlockS();
166 
167  chSysUnlock();
168 
169  return true;
170 }
171 
172 #endif
173 
Main PiOS header to include all the compiled in PiOS options.
const uint8_t count
Definition: panel.c:515
void * PIOS_malloc_no_dma(size_t size)
Definition: pios_heap.c:166
bool PIOS_Mutex_Unlock(struct pios_mutex *mtx)
Definition: pios_mutex.c:104
struct pios_mutex * PIOS_Mutex_Create(void)
Definition: pios_mutex.c:43
struct pios_recursive_mutex * PIOS_Recursive_Mutex_Create(void)
Definition: pios_mutex.c:115
bool PIOS_Recursive_Mutex_Lock(struct pios_recursive_mutex *mtx, uint32_t timeout_ms)
Definition: pios_mutex.c:139
#define PIOS_Assert(test)
Definition: pios_debug.h:52
bool PIOS_Recursive_Mutex_Unlock(struct pios_recursive_mutex *mtx)
Definition: pios_mutex.c:144
bool PIOS_Mutex_Lock(struct pios_mutex *mtx, uint32_t timeout_ms)
Definition: pios_mutex.c:66