changeset 59:af965108a9b3

common: sanity wrappers around pthread mutex & condition variable functions Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
author Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
date Tue, 07 Jul 2015 20:40:26 -0400
parents e0a328d71687
children 858975077f7c
files src/common/CMakeLists.txt src/common/include/nomad/mutex.h src/common/mutex.c
diffstat 3 files changed, 122 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/common/CMakeLists.txt	Tue Jul 07 20:39:36 2015 -0400
+++ b/src/common/CMakeLists.txt	Tue Jul 07 20:40:26 2015 -0400
@@ -21,6 +21,7 @@
 #
 
 add_library(nomad_common SHARED
+	mutex.c
 	node.c
 	oid.c
 	rand.c
@@ -40,6 +41,7 @@
 		include/nomad/config.h
 		include/nomad/error.h
 		include/nomad/malloc.h
+		include/nomad/mutex.h
 		include/nomad/rand.h
 		include/nomad/time.h
 		include/nomad/types.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/common/include/nomad/mutex.h	Tue Jul 07 20:40:26 2015 -0400
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2015 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef __NOMAD_MUTEX_H
+#define __NOMAD_MUTEX_H
+
+#include <pthread.h>
+
+extern void mxinit(pthread_mutex_t *m);
+extern void mxdestroy(pthread_mutex_t *m);
+extern void mxlock(pthread_mutex_t *m);
+extern void mxunlock(pthread_mutex_t *m);
+
+extern void condinit(pthread_cond_t *c);
+extern void conddestroy(pthread_cond_t *c);
+extern void condwait(pthread_cond_t *c, pthread_mutex_t *m);
+extern void condsig(pthread_cond_t *c);
+extern void condbcast(pthread_cond_t *c);
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/common/mutex.c	Tue Jul 07 20:40:26 2015 -0400
@@ -0,0 +1,81 @@
+/*
+ * Copyright (c) 2015 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <nomad/error.h>
+#include <nomad/mutex.h>
+
+static void mutex_assert0(int v)
+{
+	if (!v)
+		return;
+
+	fprintf(stderr, "MUTEX ASSERTION FAILURE: ret = %s\n", strerror(v));
+	abort();
+}
+
+void mxinit(pthread_mutex_t *m)
+{
+	mutex_assert0(pthread_mutex_init(m, NULL));
+}
+
+void mxdestroy(pthread_mutex_t *m)
+{
+	mutex_assert0(pthread_mutex_destroy(m));
+}
+
+void mxlock(pthread_mutex_t *m)
+{
+	mutex_assert0(pthread_mutex_lock(m));
+}
+
+void mxunlock(pthread_mutex_t *m)
+{
+	mutex_assert0(pthread_mutex_unlock(m));
+}
+
+void condinit(pthread_cond_t *c)
+{
+	mutex_assert0(pthread_cond_init(c, NULL));
+}
+
+void conddestroy(pthread_cond_t *c)
+{
+	mutex_assert0(pthread_cond_destroy(c));
+}
+
+void condwait(pthread_cond_t *c, pthread_mutex_t *m)
+{
+	mutex_assert0(pthread_cond_wait(c, m));
+}
+
+void condsig(pthread_cond_t *c)
+{
+	mutex_assert0(pthread_cond_signal(c));
+}
+
+void condbcast(pthread_cond_t *c)
+{
+	mutex_assert0(pthread_cond_broadcast(c));
+}