From 0c08334b2c2526fa3cc5a6e39fc34b14262bba99 Mon Sep 17 00:00:00 2001 From: Alexander Boettcher Date: Wed, 9 Apr 2014 14:35:56 +0200 Subject: [PATCH] pthread: separate structure and create function With the commit an application may create a modified version of pthread_create. Will be used by Virtualbox port. Issue #1114 --- libports/lib/mk/pthread.mk | 2 +- libports/src/lib/pthread/thread.cc | 60 ++-------------------- libports/src/lib/pthread/thread.h | 61 +++++++++++++++++++++++ libports/src/lib/pthread/thread_create.cc | 43 ++++++++++++++++ 4 files changed, 108 insertions(+), 58 deletions(-) create mode 100644 libports/src/lib/pthread/thread.h create mode 100644 libports/src/lib/pthread/thread_create.cc diff --git a/libports/lib/mk/pthread.mk b/libports/lib/mk/pthread.mk index 543c1821a..2854ff7ba 100644 --- a/libports/lib/mk/pthread.mk +++ b/libports/lib/mk/pthread.mk @@ -1,5 +1,5 @@ SRC_CC = semaphore.cc \ - thread.cc + thread.cc thread_create.cc LIBS += libc diff --git a/libports/src/lib/pthread/thread.cc b/libports/src/lib/pthread/thread.cc index 083a103a6..f83e70ba3 100644 --- a/libports/src/lib/pthread/thread.cc +++ b/libports/src/lib/pthread/thread.cc @@ -21,6 +21,7 @@ #include #include +#include "thread.h" using namespace Genode; @@ -31,17 +32,6 @@ extern "C" { /* Thread */ - enum { STACK_SIZE=64*1024 }; - - - struct pthread_attr - { - pthread_t pthread; - - pthread_attr() : pthread(0) { } - }; - - int pthread_attr_init(pthread_attr_t *attr) { if (!attr) @@ -65,51 +55,6 @@ extern "C" { } - /* - * This class is named 'struct pthread' because the 'pthread_t' type is - * defined as 'struct pthread*' in '_pthreadtypes.h' - */ - struct pthread : Thread - { - pthread_attr_t _attr; - void *(*_start_routine) (void *); - void *_arg; - - pthread(pthread_attr_t attr, void *(*start_routine) (void *), void *arg) - : Thread("pthread"), - _attr(attr), - _start_routine(start_routine), - _arg(arg) - { - if (_attr) - _attr->pthread = this; - } - - void entry() - { - void *exit_status = _start_routine(_arg); - pthread_exit(exit_status); - } - }; - - - int pthread_create(pthread_t *thread, const pthread_attr_t *attr, - void *(*start_routine) (void *), void *arg) - { - pthread_t thread_obj = new (env()->heap()) - pthread(attr ? *attr : 0, start_routine, arg); - - if (!thread_obj) - return EAGAIN; - - *thread = thread_obj; - - thread_obj->start(); - - return 0; - } - - int pthread_cancel(pthread_t thread) { destroy(env()->heap(), thread); @@ -127,7 +72,8 @@ extern "C" { pthread_t pthread_self(void) { static struct pthread_attr main_thread_attr; - static struct pthread main_thread(&main_thread_attr, 0, 0); + static struct pthread main_thread(&main_thread_attr, 0, 0, 64*1024, + "main", nullptr); Thread_base *myself = Thread_base::myself(); diff --git a/libports/src/lib/pthread/thread.h b/libports/src/lib/pthread/thread.h new file mode 100644 index 000000000..cb19830f5 --- /dev/null +++ b/libports/src/lib/pthread/thread.h @@ -0,0 +1,61 @@ +/* + * \brief POSIX thread header + * \author Christian Prochaska + * \author Alexander Boettcher + * \date 2012-03-12 + * + */ + +/* + * Copyright (C) 2012-2014 Genode Labs GmbH + * + * This file is part of the Genode OS framework, which is distributed + * under the terms of the GNU General Public License version 2. + */ + +#ifndef _INCLUDE__SRC_LIB_PTHREAD_THREAD_H_ +#define _INCLUDE__SRC_LIB_PTHREAD_THREAD_H_ + +#include + +extern "C" { + + struct pthread_attr + { + pthread_t pthread; + + pthread_attr() : pthread(0) { } + }; + + + /* + * This class is named 'struct pthread' because the 'pthread_t' type is + * defined as 'struct pthread*' in '_pthreadtypes.h' + */ + struct pthread : Genode::Thread_base + { + pthread_attr_t _attr; + void *(*_start_routine) (void *); + void *_arg; + + pthread(pthread_attr_t attr, void *(*start_routine) (void *), + void *arg, size_t stack_size, char const * name, + Genode::Cpu_session * cpu) + : Thread_base(name, stack_size, Type::NORMAL, cpu), + _attr(attr), + _start_routine(start_routine), + _arg(arg) + { + if (_attr) + _attr->pthread = this; + } + + void entry() + { + void *exit_status = _start_routine(_arg); + pthread_exit(exit_status); + } + }; +} + +#endif /* _INCLUDE__SRC_LIB_PTHREAD_THREAD_H_ */ diff --git a/libports/src/lib/pthread/thread_create.cc b/libports/src/lib/pthread/thread_create.cc new file mode 100644 index 000000000..90d8284cc --- /dev/null +++ b/libports/src/lib/pthread/thread_create.cc @@ -0,0 +1,43 @@ +/* + * \brief pthread_create implementation + * \author Christian Prochaska + * \date 2012-03-12 + * + * Purpose of a single file for pthread_create is that other application may + * easily replace this implementation with another one. + */ + +/* + * Copyright (C) 2012-2014 Genode Labs GmbH + * + * This file is part of the Genode OS framework, which is distributed + * under the terms of the GNU General Public License version 2. + */ + +#include + +#include +#include +#include "thread.h" + +extern "C" +{ + enum { STACK_SIZE=64*1024 }; + + int pthread_create(pthread_t *thread, const pthread_attr_t *attr, + void *(*start_routine) (void *), void *arg) + { + pthread_t thread_obj = new (Genode::env()->heap()) + pthread(attr ? *attr : 0, start_routine, + arg, STACK_SIZE, "pthread", nullptr); + + if (!thread_obj) + return EAGAIN; + + *thread = thread_obj; + + thread_obj->start(); + + return 0; + } +}