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
This commit is contained in:
Alexander Boettcher 2014-04-09 14:35:56 +02:00 committed by Christian Helmuth
parent 03ce614c23
commit 0c08334b2c
4 changed files with 108 additions and 58 deletions

View File

@ -1,5 +1,5 @@
SRC_CC = semaphore.cc \
thread.cc
thread.cc thread_create.cc
LIBS += libc

View File

@ -21,6 +21,7 @@
#include <errno.h>
#include <pthread.h>
#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<STACK_SIZE>
{
pthread_attr_t _attr;
void *(*_start_routine) (void *);
void *_arg;
pthread(pthread_attr_t attr, void *(*start_routine) (void *), void *arg)
: Thread<STACK_SIZE>("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();

View File

@ -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 <pthread.h>
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_ */

View File

@ -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 <base/env.h>
#include <errno.h>
#include <pthread.h>
#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;
}
}