]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blobdiff - rpp/include/rpp/mutex.h
Make the RPP layer thread safe
[pes-rpp/rpp-lib.git] / rpp / include / rpp / mutex.h
diff --git a/rpp/include/rpp/mutex.h b/rpp/include/rpp/mutex.h
new file mode 100644 (file)
index 0000000..6c30c50
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2015 Czech Technical University in Prague
+ *
+ * Authors:
+ *     - Michal Sojka <sojkam1@fel.cvut.cz>
+ *
+ * This document contains proprietary information belonging to Czech
+ * Technical University in Prague. Passing on and copying of this
+ * document, and communication of its contents is not permitted
+ * without prior written authorization.
+ *
+ */
+
+#ifndef RPP_MUTEX_H
+#define RPP_MUTEX_H
+
+#ifndef RPP_THREADSAFE
+#define RPP_THREADSAFE 1
+#endif
+
+#if RPP_THREADSAFE
+
+#include <os/semphr.h>
+
+#define RPP_MUTEX_DEFINE(mutex) static xSemaphoreHandle mutex
+#define RPP_MUTEX_INIT(mutex)   (((mutex) = xSemaphoreCreateMutex()) != NULL)
+#define RPP_MUTEX_LOCK(mutex)   xSemaphoreTake((mutex), portMAX_DELAY)
+#define RPP_MUTEX_UNLOCK(mutex) xSemaphoreGive(mutex)
+
+#else
+
+#define RPP_MUTEX_DEFINE(mutex)
+#define RPP_MUTEX_INIT(mutex) true
+#define RPP_MUTEX_LOCK(mutex)
+#define RPP_MUTEX_UNLOCK(mutex)
+
+#endif
+
+#endif