2014-04-09 14:35:56 +02:00
|
|
|
/*
|
|
|
|
* \brief POSIX thread header
|
|
|
|
* \author Christian Prochaska
|
|
|
|
* \author Alexander Boettcher
|
|
|
|
* \date 2012-03-12
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-02-20 13:23:52 +01:00
|
|
|
* Copyright (C) 2012-2017 Genode Labs GmbH
|
2014-04-09 14:35:56 +02:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
2017-02-20 13:23:52 +01:00
|
|
|
* under the terms of the GNU Affero General Public License version 3.
|
2014-04-09 14:35:56 +02:00
|
|
|
*/
|
|
|
|
|
libc: split task.cc into multiple files
This patch is the first step of re-organizing the internal structure of
the libc. The original version involved many direct calls of global
functions (often with side effects) across compilation units, which
made the control flow (e.g., the initialization sequence) hard to
follow.
The new version replaces those ad-hoc interactions with dedicated
interfaces (like suspend.h, resume.h, select.h, current_time.h). The
underlying facilities are provided by the central Libc::Kernel and
selectively propagated to the various compilation units. The latter is
done by a sequence of 'init_*' calls, which eventually will be replaced
by constructor calls.
The addition of new headers increases the chance for name clashes with
existing (public) headers. To disambiguate libc-internal header files
from public headers, this patch moves the former into a new 'internal/'
subdirectory. This makes the include directives easier to follow and the
libc's source-tree structure more tidy.
There are still a few legacies left, which cannot easily be removed
right now (e.g., because noux relies on them). However, the patch moves
those bad apples to legacy.h and legacy.cc, which highlights the
deprecation of those functions.
Issue #3497
2019-09-18 20:19:10 +02:00
|
|
|
#ifndef _LIBC__INTERNAL__PTHREAD_H_
|
|
|
|
#define _LIBC__INTERNAL__PTHREAD_H_
|
2014-04-09 14:35:56 +02:00
|
|
|
|
2018-01-04 14:47:16 +01:00
|
|
|
/* Genode includes */
|
2018-06-12 15:21:08 +02:00
|
|
|
#include <libc/component.h>
|
2018-01-04 14:47:16 +01:00
|
|
|
#include <util/reconstructible.h>
|
|
|
|
|
libc: split task.cc into multiple files
This patch is the first step of re-organizing the internal structure of
the libc. The original version involved many direct calls of global
functions (often with side effects) across compilation units, which
made the control flow (e.g., the initialization sequence) hard to
follow.
The new version replaces those ad-hoc interactions with dedicated
interfaces (like suspend.h, resume.h, select.h, current_time.h). The
underlying facilities are provided by the central Libc::Kernel and
selectively propagated to the various compilation units. The latter is
done by a sequence of 'init_*' calls, which eventually will be replaced
by constructor calls.
The addition of new headers increases the chance for name clashes with
existing (public) headers. To disambiguate libc-internal header files
from public headers, this patch moves the former into a new 'internal/'
subdirectory. This makes the include directives easier to follow and the
libc's source-tree structure more tidy.
There are still a few legacies left, which cannot easily be removed
right now (e.g., because noux relies on them). However, the patch moves
those bad apples to legacy.h and legacy.cc, which highlights the
deprecation of those functions.
Issue #3497
2019-09-18 20:19:10 +02:00
|
|
|
/* libc includes */
|
2014-04-09 14:35:56 +02:00
|
|
|
#include <pthread.h>
|
|
|
|
|
2019-09-19 20:37:17 +02:00
|
|
|
/* libc-internal includes */
|
|
|
|
#include <internal/types.h>
|
|
|
|
|
|
|
|
namespace Libc {
|
|
|
|
|
|
|
|
struct Pthread;
|
|
|
|
struct Pthread_registry;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-25 16:05:25 +02:00
|
|
|
/*
|
|
|
|
* Used by 'pthread_self()' to find out if the current thread is an alien
|
|
|
|
* thread.
|
|
|
|
*/
|
2019-09-19 20:37:17 +02:00
|
|
|
class Libc::Pthread_registry
|
2016-08-25 16:05:25 +02:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
|
|
|
enum { MAX_NUM_PTHREADS = 128 };
|
|
|
|
|
2019-09-19 20:37:17 +02:00
|
|
|
Pthread *_array[MAX_NUM_PTHREADS] = { 0 };
|
2016-08-25 16:05:25 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2019-09-19 20:37:17 +02:00
|
|
|
void insert(Pthread &thread);
|
2016-08-25 16:05:25 +02:00
|
|
|
|
2019-09-19 20:37:17 +02:00
|
|
|
void remove(Pthread &thread);
|
2016-08-25 16:05:25 +02:00
|
|
|
|
2019-09-19 20:37:17 +02:00
|
|
|
bool contains(Pthread &thread);
|
2016-08-25 16:05:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-09-19 20:37:17 +02:00
|
|
|
Libc::Pthread_registry &pthread_registry();
|
2016-08-25 16:05:25 +02:00
|
|
|
|
|
|
|
|
2014-04-09 14:35:56 +02:00
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
struct pthread_attr
|
|
|
|
{
|
2018-06-12 15:21:08 +02:00
|
|
|
void *stack_addr { nullptr };
|
|
|
|
size_t stack_size { Libc::Component::stack_size() };
|
2014-04-09 14:35:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This class is named 'struct pthread' because the 'pthread_t' type is
|
|
|
|
* defined as 'struct pthread*' in '_pthreadtypes.h'
|
|
|
|
*/
|
2018-01-04 14:47:16 +01:00
|
|
|
struct pthread;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct Genode::Thread::Tls::Base
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Register thread-local-storage object at Genode thread
|
|
|
|
*/
|
2019-09-19 20:37:17 +02:00
|
|
|
static void tls(Thread &thread, Tls::Base &tls)
|
2014-04-09 14:35:56 +02:00
|
|
|
{
|
2018-01-04 14:47:16 +01:00
|
|
|
thread._tls = Tls { &tls };
|
|
|
|
}
|
2014-04-09 14:35:56 +02:00
|
|
|
|
2018-01-04 14:47:16 +01:00
|
|
|
struct Undefined : Exception { };
|
2015-03-27 14:05:55 +01:00
|
|
|
|
2018-01-04 14:47:16 +01:00
|
|
|
/**
|
|
|
|
* Obtain thread-local-storage object for the calling thread
|
|
|
|
*
|
|
|
|
* \throw Undefined
|
|
|
|
*/
|
|
|
|
static Tls::Base &tls()
|
|
|
|
{
|
|
|
|
Thread &myself = *Thread::myself();
|
|
|
|
|
|
|
|
if (!myself._tls.ptr)
|
|
|
|
throw Undefined();
|
|
|
|
|
|
|
|
return *myself._tls.ptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-09-19 20:37:17 +02:00
|
|
|
struct Libc::Pthread : Noncopyable, Thread::Tls::Base
|
2018-01-04 14:47:16 +01:00
|
|
|
{
|
|
|
|
typedef void *(*start_routine_t) (void *);
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2019-09-19 20:37:17 +02:00
|
|
|
struct Thread_object : Thread
|
2018-01-04 14:47:16 +01:00
|
|
|
{
|
|
|
|
start_routine_t _start_routine;
|
|
|
|
void *_arg;
|
|
|
|
|
2018-06-12 15:21:08 +02:00
|
|
|
void *&_stack_addr;
|
|
|
|
size_t &_stack_size;
|
|
|
|
|
2019-09-19 20:37:17 +02:00
|
|
|
enum { WEIGHT = Cpu_session::Weight::DEFAULT_WEIGHT };
|
2018-01-04 14:47:16 +01:00
|
|
|
|
2018-06-12 15:21:08 +02:00
|
|
|
/* 'stack_addr_out' and 'stack_size_out' are written when the thread starts */
|
2018-01-04 14:47:16 +01:00
|
|
|
Thread_object(char const *name, size_t stack_size,
|
2019-09-19 20:37:17 +02:00
|
|
|
Cpu_session *cpu,
|
|
|
|
Affinity::Location location,
|
2018-06-12 15:21:08 +02:00
|
|
|
start_routine_t start_routine, void *arg,
|
|
|
|
void *&stack_addr_out, size_t &stack_size_out)
|
2018-01-04 14:47:16 +01:00
|
|
|
:
|
2019-09-19 20:37:17 +02:00
|
|
|
Thread(WEIGHT, name, stack_size, Type::NORMAL, cpu, location),
|
2018-06-12 15:21:08 +02:00
|
|
|
_start_routine(start_routine), _arg(arg),
|
|
|
|
_stack_addr(stack_addr_out), _stack_size(stack_size_out)
|
2018-01-04 14:47:16 +01:00
|
|
|
{ }
|
|
|
|
|
2018-01-11 18:24:59 +01:00
|
|
|
void entry() override;
|
2018-01-04 14:47:16 +01:00
|
|
|
};
|
|
|
|
|
2019-09-19 20:37:17 +02:00
|
|
|
Constructible<Thread_object> _thread_object;
|
2018-01-04 14:47:16 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Helper to construct '_thread_object' in class initializer
|
|
|
|
*/
|
|
|
|
template <typename... ARGS>
|
2019-09-19 20:37:17 +02:00
|
|
|
Thread &_construct_thread_object(ARGS &&... args)
|
2018-01-04 14:47:16 +01:00
|
|
|
{
|
|
|
|
_thread_object.construct(args...);
|
|
|
|
return *_thread_object;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-09-19 20:37:17 +02:00
|
|
|
* Refers to '_thread_object' or an external 'Thread' object
|
2018-01-04 14:47:16 +01:00
|
|
|
*/
|
2019-09-19 20:37:17 +02:00
|
|
|
Thread &_thread;
|
2018-01-04 14:47:16 +01:00
|
|
|
|
|
|
|
void _associate_thread_with_pthread()
|
2014-04-09 14:35:56 +02:00
|
|
|
{
|
2019-09-19 20:37:17 +02:00
|
|
|
Thread::Tls::Base::tls(_thread, *this);
|
|
|
|
pthread_registry().insert(*this);
|
2014-04-09 14:35:56 +02:00
|
|
|
}
|
|
|
|
|
2018-06-28 17:43:39 +02:00
|
|
|
bool _exiting = false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The join lock is needed because 'Libc::resume_all()' uses a
|
|
|
|
* 'Signal_transmitter' which holds a reference to a signal context
|
|
|
|
* capability, which needs to be released before the thread can be
|
|
|
|
* destroyed.
|
|
|
|
*
|
2019-09-19 20:37:17 +02:00
|
|
|
* Also, we cannot use 'Thread::join()', because it only
|
2018-06-28 17:43:39 +02:00
|
|
|
* returns when the thread entry function returns, which does not
|
|
|
|
* happen with 'pthread_cancel()'.
|
|
|
|
*/
|
2019-09-19 20:37:17 +02:00
|
|
|
Lock _join_lock { Lock::LOCKED };
|
2018-06-28 17:43:39 +02:00
|
|
|
|
|
|
|
/* return value for 'pthread_join()' */
|
|
|
|
void *_retval = PTHREAD_CANCELED;
|
|
|
|
|
2018-06-12 15:21:08 +02:00
|
|
|
/* attributes for 'pthread_attr_get_np()' */
|
|
|
|
void *_stack_addr = nullptr;
|
|
|
|
size_t _stack_size = 0;
|
|
|
|
|
2018-01-04 14:47:16 +01:00
|
|
|
public:
|
|
|
|
|
2019-12-11 15:42:46 +01:00
|
|
|
int thread_local_errno = 0;
|
|
|
|
|
2014-04-09 15:47:26 +02:00
|
|
|
/**
|
2018-01-04 14:47:16 +01:00
|
|
|
* Constructor for threads created via 'pthread_create'
|
2014-04-09 15:47:26 +02:00
|
|
|
*/
|
2019-09-19 20:37:17 +02:00
|
|
|
Pthread(start_routine_t start_routine,
|
2018-01-04 14:47:16 +01:00
|
|
|
void *arg, size_t stack_size, char const * name,
|
2019-09-19 20:37:17 +02:00
|
|
|
Cpu_session * cpu, Affinity::Location location)
|
2018-01-04 14:47:16 +01:00
|
|
|
:
|
|
|
|
_thread(_construct_thread_object(name, stack_size, cpu, location,
|
2018-06-12 15:21:08 +02:00
|
|
|
start_routine, arg,
|
|
|
|
_stack_addr, _stack_size))
|
2014-04-09 15:47:26 +02:00
|
|
|
{
|
2018-01-04 14:47:16 +01:00
|
|
|
_associate_thread_with_pthread();
|
2016-08-25 16:05:25 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 14:47:16 +01:00
|
|
|
/**
|
|
|
|
* Constructor to create pthread object out of existing thread,
|
|
|
|
* i.e., the main thread
|
|
|
|
*/
|
2019-09-19 20:37:17 +02:00
|
|
|
Pthread(Thread &existing_thread)
|
2018-01-04 14:47:16 +01:00
|
|
|
:
|
2018-01-11 18:24:59 +01:00
|
|
|
_thread(existing_thread)
|
2016-08-25 16:05:25 +02:00
|
|
|
{
|
2018-06-12 15:21:08 +02:00
|
|
|
/* obtain stack attributes of main thread */
|
2019-09-19 20:37:17 +02:00
|
|
|
Thread::Stack_info info = Thread::mystack();
|
2018-06-12 15:21:08 +02:00
|
|
|
_stack_addr = (void *)info.base;
|
|
|
|
_stack_size = info.top - info.base;
|
|
|
|
|
2018-01-04 14:47:16 +01:00
|
|
|
_associate_thread_with_pthread();
|
2014-04-09 15:47:26 +02:00
|
|
|
}
|
|
|
|
|
2019-09-19 20:37:17 +02:00
|
|
|
~Pthread()
|
2014-04-09 14:35:56 +02:00
|
|
|
{
|
2019-09-19 20:37:17 +02:00
|
|
|
pthread_registry().remove(*this);
|
2014-04-09 14:35:56 +02:00
|
|
|
}
|
2015-09-23 12:12:20 +02:00
|
|
|
|
2018-01-04 14:47:16 +01:00
|
|
|
void start() { _thread.start(); }
|
2018-06-28 17:43:39 +02:00
|
|
|
|
|
|
|
void join(void **retval);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Inform the thread calling 'pthread_join()' that this thread can be
|
|
|
|
* destroyed.
|
|
|
|
*/
|
|
|
|
void cancel();
|
|
|
|
|
|
|
|
void exit(void *retval)
|
2017-08-08 14:10:18 +02:00
|
|
|
{
|
2018-06-28 17:43:39 +02:00
|
|
|
_retval = retval;
|
|
|
|
cancel();
|
2017-08-08 14:10:18 +02:00
|
|
|
}
|
|
|
|
|
2018-06-12 15:21:08 +02:00
|
|
|
void *stack_addr() const { return _stack_addr; }
|
|
|
|
size_t stack_size() const { return _stack_size; }
|
2018-01-04 14:47:16 +01:00
|
|
|
};
|
2014-04-09 14:35:56 +02:00
|
|
|
|
2019-01-18 11:40:23 +01:00
|
|
|
|
2019-09-19 20:37:17 +02:00
|
|
|
struct pthread : Libc::Pthread
|
|
|
|
{
|
|
|
|
using Libc::Pthread::Pthread;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-01-18 11:40:23 +01:00
|
|
|
namespace Libc { void init_pthread_support(Env &env); }
|
|
|
|
|
libc: split task.cc into multiple files
This patch is the first step of re-organizing the internal structure of
the libc. The original version involved many direct calls of global
functions (often with side effects) across compilation units, which
made the control flow (e.g., the initialization sequence) hard to
follow.
The new version replaces those ad-hoc interactions with dedicated
interfaces (like suspend.h, resume.h, select.h, current_time.h). The
underlying facilities are provided by the central Libc::Kernel and
selectively propagated to the various compilation units. The latter is
done by a sequence of 'init_*' calls, which eventually will be replaced
by constructor calls.
The addition of new headers increases the chance for name clashes with
existing (public) headers. To disambiguate libc-internal header files
from public headers, this patch moves the former into a new 'internal/'
subdirectory. This makes the include directives easier to follow and the
libc's source-tree structure more tidy.
There are still a few legacies left, which cannot easily be removed
right now (e.g., because noux relies on them). However, the patch moves
those bad apples to legacy.h and legacy.cc, which highlights the
deprecation of those functions.
Issue #3497
2019-09-18 20:19:10 +02:00
|
|
|
#endif /* _LIBC__INTERNAL__PTHREAD_H_ */
|