[PATCH 1/2] libstdthreads: Import from FreeBSD
Sebastian Huber
sebastian.huber at embedded-brains.de
Mon Sep 21 07:56:19 UTC 2015
---
cpukit/libstdthreads/call_once.c | 44 ++++++++++++++
cpukit/libstdthreads/cnd.c | 98 ++++++++++++++++++++++++++++++
cpukit/libstdthreads/mtx.c | 116 +++++++++++++++++++++++++++++++++++
cpukit/libstdthreads/thrd.c | 128 +++++++++++++++++++++++++++++++++++++++
cpukit/libstdthreads/tss.c | 69 +++++++++++++++++++++
5 files changed, 455 insertions(+)
create mode 100644 cpukit/libstdthreads/call_once.c
create mode 100644 cpukit/libstdthreads/cnd.c
create mode 100644 cpukit/libstdthreads/mtx.c
create mode 100644 cpukit/libstdthreads/thrd.c
create mode 100644 cpukit/libstdthreads/tss.c
diff --git a/cpukit/libstdthreads/call_once.c b/cpukit/libstdthreads/call_once.c
new file mode 100644
index 0000000..2d7d6ff
--- /dev/null
+++ b/cpukit/libstdthreads/call_once.c
@@ -0,0 +1,44 @@
+/*-
+ * Copyright (c) 2011 Ed Schouten <ed at FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD r228904 2011-12-26T21:51:53Z$
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <pthread.h>
+
+#include "threads.h"
+
+void
+call_once(once_flag *flag, void (*func)(void))
+{
+
+ (void)pthread_once((pthread_once_t *)flag, func);
+}
+
+_Static_assert(sizeof(once_flag) == sizeof(pthread_once_t),
+ "once_flag must be of the same size as pthread_once_t");
diff --git a/cpukit/libstdthreads/cnd.c b/cpukit/libstdthreads/cnd.c
new file mode 100644
index 0000000..cccf728
--- /dev/null
+++ b/cpukit/libstdthreads/cnd.c
@@ -0,0 +1,98 @@
+/*-
+ * Copyright (c) 2011 Ed Schouten <ed at FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD r228904 2011-12-26T21:51:53Z$
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <errno.h>
+#include <pthread.h>
+
+#include "threads.h"
+
+int
+cnd_broadcast(cnd_t *cond)
+{
+
+ if (pthread_cond_broadcast(cond) != 0)
+ return (thrd_error);
+ return (thrd_success);
+}
+
+void
+cnd_destroy(cnd_t *cond)
+{
+
+ (void)pthread_cond_destroy(cond);
+}
+
+int
+cnd_init(cnd_t *cond)
+{
+
+ switch (pthread_cond_init(cond, NULL)) {
+ case 0:
+ return (thrd_success);
+ case ENOMEM:
+ return (thrd_nomem);
+ default:
+ return (thrd_error);
+ }
+}
+
+int
+cnd_signal(cnd_t *cond)
+{
+
+ if (pthread_cond_signal(cond) != 0)
+ return (thrd_error);
+ return (thrd_success);
+}
+
+int
+cnd_timedwait(cnd_t *restrict cond, mtx_t *restrict mtx,
+ const struct timespec *restrict ts)
+{
+
+ switch (pthread_cond_timedwait(cond, mtx, ts)) {
+ case 0:
+ return (thrd_success);
+ case ETIMEDOUT:
+ return (thrd_timedout);
+ default:
+ return (thrd_error);
+ }
+}
+
+int
+cnd_wait(cnd_t *cond, mtx_t *mtx)
+{
+
+ if (pthread_cond_wait(cond, mtx) != 0)
+ return (thrd_error);
+ return (thrd_success);
+}
diff --git a/cpukit/libstdthreads/mtx.c b/cpukit/libstdthreads/mtx.c
new file mode 100644
index 0000000..2d42f8a
--- /dev/null
+++ b/cpukit/libstdthreads/mtx.c
@@ -0,0 +1,116 @@
+/*-
+ * Copyright (c) 2011 Ed Schouten <ed at FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD r279326 2015-02-26T16:39:57Z$
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <errno.h>
+#include <pthread.h>
+
+#include "threads.h"
+
+void
+mtx_destroy(mtx_t *mtx)
+{
+
+ (void)pthread_mutex_destroy(mtx);
+}
+
+int
+mtx_init(mtx_t *mtx, int type)
+{
+ pthread_mutexattr_t attr;
+ int mt;
+
+ switch (type) {
+ case mtx_plain:
+ case mtx_timed:
+ mt = PTHREAD_MUTEX_NORMAL;
+ break;
+ case mtx_plain | mtx_recursive:
+ case mtx_timed | mtx_recursive:
+ mt = PTHREAD_MUTEX_RECURSIVE;
+ break;
+ default:
+ return (thrd_error);
+ }
+
+ if (pthread_mutexattr_init(&attr) != 0)
+ return (thrd_error);
+ if (pthread_mutexattr_settype(&attr, mt) != 0)
+ return (thrd_error);
+ if (pthread_mutex_init(mtx, &attr) != 0)
+ return (thrd_error);
+ return (thrd_success);
+}
+
+int
+mtx_lock(mtx_t *mtx)
+{
+
+ if (pthread_mutex_lock(mtx) != 0)
+ return (thrd_error);
+ return (thrd_success);
+}
+
+int
+mtx_timedlock(mtx_t *restrict mtx, const struct timespec *restrict ts)
+{
+
+ switch (pthread_mutex_timedlock(mtx, ts)) {
+ case 0:
+ return (thrd_success);
+ case ETIMEDOUT:
+ return (thrd_timedout);
+ default:
+ return (thrd_error);
+ }
+}
+
+int
+mtx_trylock(mtx_t *mtx)
+{
+
+ switch (pthread_mutex_trylock(mtx)) {
+ case 0:
+ return (thrd_success);
+ case EBUSY:
+ return (thrd_busy);
+ default:
+ return (thrd_error);
+ }
+}
+
+int
+mtx_unlock(mtx_t *mtx)
+{
+
+ if (pthread_mutex_unlock(mtx) != 0)
+ return (thrd_error);
+ return (thrd_success);
+}
diff --git a/cpukit/libstdthreads/thrd.c b/cpukit/libstdthreads/thrd.c
new file mode 100644
index 0000000..562e806
--- /dev/null
+++ b/cpukit/libstdthreads/thrd.c
@@ -0,0 +1,128 @@
+/*-
+ * Copyright (c) 2011 Ed Schouten <ed at FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD r279318 2015-02-26T09:42:03Z$
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <pthread.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+#include "threads.h"
+
+struct thrd_param {
+ thrd_start_t func;
+ void *arg;
+};
+
+static void *
+thrd_entry(void *arg)
+{
+ struct thrd_param tp;
+
+ tp = *(struct thrd_param *)arg;
+ free(arg);
+ return ((void *)(intptr_t)tp.func(tp.arg));
+}
+
+int
+thrd_create(thrd_t *thr, thrd_start_t func, void *arg)
+{
+ struct thrd_param *tp;
+
+ /*
+ * Work around return type inconsistency. Wrap execution using
+ * a function conforming to pthread_create()'s start_routine.
+ */
+ tp = malloc(sizeof(*tp));
+ if (tp == NULL)
+ return (thrd_nomem);
+ tp->func = func;
+ tp->arg = arg;
+ if (pthread_create(thr, NULL, thrd_entry, tp) != 0) {
+ free(tp);
+ return (thrd_error);
+ }
+ return (thrd_success);
+}
+
+thrd_t
+thrd_current(void)
+{
+
+ return (pthread_self());
+}
+
+int
+thrd_detach(thrd_t thr)
+{
+
+ if (pthread_detach(thr) != 0)
+ return (thrd_error);
+ return (thrd_success);
+}
+
+int
+thrd_equal(thrd_t thr0, thrd_t thr1)
+{
+
+ return (pthread_equal(thr0, thr1));
+}
+
+_Noreturn void
+thrd_exit(int res)
+{
+
+ pthread_exit((void *)(intptr_t)res);
+}
+
+int
+thrd_join(thrd_t thr, int *res)
+{
+ void *value_ptr;
+
+ if (pthread_join(thr, &value_ptr) != 0)
+ return (thrd_error);
+ if (res != NULL)
+ *res = (intptr_t)value_ptr;
+ return (thrd_success);
+}
+
+int
+thrd_sleep(const struct timespec *duration, struct timespec *remaining)
+{
+
+ return (nanosleep(duration, remaining));
+}
+
+void
+thrd_yield(void)
+{
+
+ pthread_yield();
+}
diff --git a/cpukit/libstdthreads/tss.c b/cpukit/libstdthreads/tss.c
new file mode 100644
index 0000000..0d4eea7
--- /dev/null
+++ b/cpukit/libstdthreads/tss.c
@@ -0,0 +1,69 @@
+/*-
+ * Copyright (c) 2011 Ed Schouten <ed at FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD r228904 2011-12-26T21:51:53Z$
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <pthread.h>
+
+#include "threads.h"
+
+int
+tss_create(tss_t *key, tss_dtor_t dtor)
+{
+
+ if (pthread_key_create(key, dtor) != 0)
+ return (thrd_error);
+ return (thrd_success);
+}
+
+void
+tss_delete(tss_t key)
+{
+
+ (void)pthread_key_delete(key);
+}
+
+void *
+tss_get(tss_t key)
+{
+
+ return (pthread_getspecific(key));
+}
+
+int
+tss_set(tss_t key, void *val)
+{
+
+ if (pthread_setspecific(key, val) != 0)
+ return (thrd_error);
+ return (thrd_success);
+}
+
+_Static_assert(TSS_DTOR_ITERATIONS == PTHREAD_DESTRUCTOR_ITERATIONS,
+ "TSS_DTOR_ITERATIONS must be identical to PTHREAD_DESTRUCTOR_ITERATIONS");
--
1.8.4.5
More information about the devel
mailing list