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 
12 /*
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful, but
19  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  * for more details.
22  *
23  * You should have received a copy of the GNU General Public License along
24  * with this program; if not, see <http://www.gnu.org/licenses/>
25  */
26 
27 
28 #include <pthread.h>
29 #include <stdlib.h>
30 
31 #include <pios.h>
32 #include <pios_mutex.h>
33 
34 struct pios_mutex {
35  pthread_mutex_t mutex;
36 };
37 
39  /* Must consist of only this */
40  struct pios_mutex mutex;
41 };
42 
44 {
45  pthread_mutexattr_t attr;
46 
47  if (pthread_mutexattr_init(&attr)) {
48  abort();
49  }
50 
51  pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
52 
53  struct pios_mutex *p = malloc(sizeof(*p));
54 
55  if (p) {
56  if (pthread_mutex_init(&p->mutex, &attr)) {
57  free(p);
58 
59  return NULL;
60  }
61  }
62 
63  return p;
64 }
65 
66 bool PIOS_Mutex_Lock(struct pios_mutex *mtx, uint32_t timeout_ms)
67 {
68  int ret;
69 
70  if (timeout_ms >= PIOS_MUTEX_TIMEOUT_MAX) {
71  ret = pthread_mutex_lock(&mtx->mutex);
72 
73  PIOS_Assert(!ret);
74  } else {
75 #ifdef __linux__
76  struct timespec abstime;
77 
78  clock_gettime(CLOCK_REALTIME, &abstime);
79 
80  abstime.tv_nsec += (timeout_ms % 1000) * 1000000;
81  abstime.tv_sec += timeout_ms / 1000;
82 
83  if (abstime.tv_nsec > 1000000000) {
84  abstime.tv_nsec -= 1000000000;
85  abstime.tv_sec += 1;
86  }
87 
88  ret = pthread_mutex_timedlock(&mtx->mutex, &abstime);
89 #else
90  /* MacOSX does not have pthread_mutex_timedlock so these
91  * semantics are not possible.
92  */
93  if (timeout_ms == 0) {
94  ret = pthread_mutex_trylock(&mtx->mutex);
95  } else {
96  abort();
97  }
98 #endif
99  }
100 
101  return (ret == 0);
102 }
103 
104 bool PIOS_Mutex_Unlock(struct pios_mutex *mtx)
105 {
106  int ret;
107 
108  ret = pthread_mutex_unlock(&mtx->mutex);
109 
110  PIOS_Assert(!ret);
111 
112  return true;
113 }
114 
116 {
117  pthread_mutexattr_t attr;
118 
119  if (pthread_mutexattr_init(&attr)) {
120  abort();
121  }
122 
123  pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
124  pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
125 
126  struct pios_mutex *p = malloc(sizeof(*p));
127 
128  if (p) {
129  if (pthread_mutex_init(&p->mutex, &attr)) {
130  free(p);
131 
132  return NULL;
133  }
134  }
135 
136  return (struct pios_recursive_mutex *) p;
137 }
138 
139 bool PIOS_Recursive_Mutex_Lock(struct pios_recursive_mutex *mtx, uint32_t timeout_ms)
140 {
141  return PIOS_Mutex_Lock(&mtx->mutex, timeout_ms);
142 }
143 
145 {
146  return PIOS_Mutex_Unlock(&mtx->mutex);
147 }
148 
int clock_gettime(clockid_t clk_id, struct timespec *t)
Main PiOS header to include all the compiled in PiOS options.
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
pthread_mutex_t mutex
Definition: pios_mutex.c:35
bool PIOS_Recursive_Mutex_Lock(struct pios_recursive_mutex *mtx, uint32_t timeout_ms)
Definition: pios_mutex.c:139
struct pios_mutex mutex
Definition: pios_mutex.c:40
#define CLOCK_REALTIME
Definition: pios_posix.h:63
uint8_t p
Definition: msp_messages.h:96
#define PIOS_Assert(test)
Definition: pios_debug.h:52
bool PIOS_Recursive_Mutex_Unlock(struct pios_recursive_mutex *mtx)
Definition: pios_mutex.c:144
#define PIOS_MUTEX_TIMEOUT_MAX
Definition: pios_mutex.h:30
bool PIOS_Mutex_Lock(struct pios_mutex *mtx, uint32_t timeout_ms)
Definition: pios_mutex.c:66