[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[tyndur-devel] [PATCH 4/4] libc: pthread
From: Patrick Pokatilo <shyxormz@xxxxxxxxxx>
+ libc: pthread-Wrapper um die nativen Syscalls von tyndur
Signed-off-by: Patrick Pokatilo <shyxormz@xxxxxxxxxx>
Signed-off-by: Kevin Wolf <kevin@xxxxxxxxxx>
---
src/modules/c/pthreads/Makefile.all | 9 +++
src/modules/c/pthreads/main.c | 85 ++++++++++++++++++++++++++
src/modules/include/pthread.h | 42 +++++++++++++
src/modules/include/sys/types.h | 3 +
src/modules/lib/posix/pthread.c | 112 +++++++++++++++++++++++++++++++++++
5 files changed, 251 insertions(+), 0 deletions(-)
create mode 100644 src/modules/c/pthreads/Makefile.all
create mode 100644 src/modules/c/pthreads/main.c
create mode 100644 src/modules/include/pthread.h
create mode 100644 src/modules/lib/posix/pthread.c
diff --git a/src/modules/c/pthreads/Makefile.all b/src/modules/c/pthreads/Makefile.all
new file mode 100644
index 0000000..d7579e0
--- /dev/null
+++ b/src/modules/c/pthreads/Makefile.all
@@ -0,0 +1,9 @@
+shopt -s extglob
+source $LOST_BUILDMK_ROOT/config.sh
+
+echo "LD $1/apps/pthreads"
+
+$LOST_TOOLS_LD -opthreads $LDSCRIPT *.o --start-group $2 --end-group
+
+
+mv pthreads $1/apps/
diff --git a/src/modules/c/pthreads/main.c b/src/modules/c/pthreads/main.c
new file mode 100644
index 0000000..4433bbf
--- /dev/null
+++ b/src/modules/c/pthreads/main.c
@@ -0,0 +1,85 @@
+/*
+ * Copyright (c) 2012 The tyndur Project. All rights reserved.
+ *
+ * This code is derived from software contributed to the tyndur Project
+ * by Patrick Pokatilo.
+ *
+ * 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 COPYRIGHT HOLDERS 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 COPYRIGHT HOLDERS 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.
+ */
+
+#include <pthread.h>
+#include <stdio.h>
+#include <stdbool.h>
+
+#include "sleep.h"
+
+struct param {
+ int a;
+ void* b;
+};
+
+void *test_a(void *p)
+{
+ while (true) {
+ printf("%d\n", ((struct param *)p)->a);
+ }
+
+ return p;
+}
+
+void *test_b(void *p)
+{
+ while (true) {
+ printf("%p\n", ((struct param *)p)->b);
+ }
+
+ return ((struct param *)p)->b;
+}
+
+void *test_c(void *p)
+{
+ while (true) {
+ printf("Hallo, Welt\n");
+ }
+
+ return p;
+}
+
+int main(int argc, char *argv[])
+{
+ pthread_t a, b, c;
+
+ struct param data = {
+ .a = 1337,
+ .b = (void *) 0xdeadc0de,
+ };
+
+ pthread_create(&a, NULL, test_a, &data);
+ pthread_create(&b, NULL, test_b, &data);
+ pthread_create(&c, NULL, test_c, &data);
+
+ msleep(1000);
+
+ printf("%d, %d, %d\n", a, b, c);
+
+ return 0;
+}
diff --git a/src/modules/include/pthread.h b/src/modules/include/pthread.h
new file mode 100644
index 0000000..45c0acb
--- /dev/null
+++ b/src/modules/include/pthread.h
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2012 The tyndur Project. All rights reserved.
+ *
+ * This code is derived from software contributed to the tyndur Project
+ * by Patrick Pokatilo.
+ *
+ * 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 COPYRIGHT HOLDERS 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 COPYRIGHT HOLDERS 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.
+ */
+
+#ifndef _PTHREAD_H_
+#define _PTHREAD_H_
+
+#include <sys/types.h>
+
+#define PTHREAD_THREADS_MAX 8192
+
+int pthread_create(pthread_t *thread,
+ __attribute__((unused)) const pthread_attr_t *attr,
+ void *(*start_routine)(void *), void *arg);
+
+pthread_t pthread_self(void);
+
+#endif
diff --git a/src/modules/include/sys/types.h b/src/modules/include/sys/types.h
index dcf0103..003da63 100644
--- a/src/modules/include/sys/types.h
+++ b/src/modules/include/sys/types.h
@@ -56,4 +56,7 @@ typedef uint64_t u_int64_t;
typedef unsigned int u_int;
typedef unsigned long u_long;
+
+typedef unsigned int pthread_t;
+typedef unsigned int pthread_attr_t;
#endif //ifndef _SYS_TYPES_H_
diff --git a/src/modules/lib/posix/pthread.c b/src/modules/lib/posix/pthread.c
new file mode 100644
index 0000000..9a3f84b
--- /dev/null
+++ b/src/modules/lib/posix/pthread.c
@@ -0,0 +1,112 @@
+/*
+ * Copyright (c) 2012 The tyndur Project. All rights reserved.
+ *
+ * This code is derived from software contributed to the tyndur Project
+ * by Patrick Pokatilo.
+ *
+ * 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 COPYRIGHT HOLDERS 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 COPYRIGHT HOLDERS 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.
+ */
+
+#include <pthread.h>
+#include <lock.h>
+#include <errno.h>
+#include <collections.h>
+#include <unistd.h>
+#include <syscall.h>
+#include <stdlib.h>
+
+typedef struct
+{
+ pid_t process;
+ tid_t thread;
+ pthread_t name;
+} __pthread_name_binding_t;
+
+list_t __pthread_name_bindings = { NULL, 0 };
+unsigned int __pthread_ids[PTHREAD_THREADS_MAX / 32];
+lock_t __pthread_ids_lock = LOCK_UNLOCKED;
+
+pthread_t pthread_self()
+{
+ pid_t pid = getpid();
+ tid_t tid = gettid();
+
+ if (tid == -1) {
+ return 0;
+ }
+
+ int i = 0;
+ __pthread_name_binding_t *binding = 0;
+
+ while ((binding = list_get_element_at(&__pthread_name_bindings, i))) {
+ if ((binding->process == pid) && (binding->thread == tid)) {
+ return binding->name;
+ }
+ }
+
+ return 0;
+}
+
+int pthread_create(pthread_t *thread,
+ __attribute__((unused)) const pthread_attr_t *attr,
+ void *(*start_routine)(void *), void *arg)
+{
+ lock(&__pthread_ids_lock);
+
+ unsigned int name = 0;
+ int i, j;
+
+ for (i = 0; i < PTHREAD_THREADS_MAX / 32; i++)
+ {
+ if (__pthread_ids[i] != 0xffffffff) {
+ for (j = 0; j < 32; j++) {
+ if ((__pthread_ids[i] & (1 << j)) == 0) {
+ __pthread_ids[i] |= 1 << j;
+ name = i * 32 + j;
+ goto found;
+ }
+ }
+ }
+ }
+ unlock(&__pthread_ids_lock);
+ return -EAGAIN;
+
+found:
+ unlock(&__pthread_ids_lock);
+
+ tid_t tid = create_thread((uint32_t)start_routine, arg);
+ if (tid == -1) {
+ return -EAGAIN;
+ }
+
+ __pthread_name_binding_t *binding = malloc(sizeof(__pthread_name_binding_t));
+
+ binding->process = getpid();
+ binding->thread = tid;
+ binding->name = name;
+
+ list_push(&__pthread_name_bindings, binding);
+
+ *thread = name;
+
+ return 0;
+}
--
1.7.7