mirror of
https://github.com/genodelabs/genode.git
synced 2025-06-14 05:08:19 +00:00
Thread API cleanup
This patch cleans up the thread API and comes with the following noteworthy changes: - Introduced Cpu_session::Weight type that replaces a formerly used plain integer value to prevent the accidental mix-up of arguments. - The enum definition of Cpu_session::DEFAULT_WEIGHT moved to Cpu_session::Weight::DEFAULT_WEIGHT - New Thread constructor that takes a 'Env &' as first argument. The original constructors are now marked as deprecated. For the common use case where the default 'Weight' and 'Affinity' are used, a shortcut is provided. In the long term, those two constructors should be the only ones to remain. - The former 'Thread<>' class template has been renamed to 'Thread_deprecated'. - The former 'Thread_base' class is now called 'Thread'. - The new 'name()' accessor returns the thread's name as 'Name' object as centrally defined via 'Cpu_session::Name'. It is meant to replace the old-fashioned 'name' method that takes a buffer and size as arguments. - Adaptation of the thread test to the new API Issue #1954
This commit is contained in:
committed by
Christian Helmuth
parent
7b73d1d823
commit
fd401bdf53
@ -160,7 +160,7 @@ namespace Genode {
|
||||
|
||||
Socket_pair server_socket_pair()
|
||||
{
|
||||
return create_server_socket_pair(Thread_base::myself()->native_thread().tid);
|
||||
return create_server_socket_pair(Thread::myself()->native_thread().tid);
|
||||
}
|
||||
|
||||
void destroy_server_socket_pair(Socket_pair socket_pair)
|
||||
|
@ -28,7 +28,7 @@ static void empty_signal_handler(int) { }
|
||||
|
||||
static char signal_stack[0x2000] __attribute__((aligned(0x1000)));
|
||||
|
||||
void Thread_base::_thread_start()
|
||||
void Thread::_thread_start()
|
||||
{
|
||||
lx_sigaltstack(signal_stack, sizeof(signal_stack));
|
||||
|
||||
@ -46,23 +46,23 @@ void Thread_base::_thread_start()
|
||||
*/
|
||||
lx_sigsetmask(LX_SIGCHLD, false);
|
||||
|
||||
Thread_base::myself()->entry();
|
||||
Thread_base::myself()->_join_lock.unlock();
|
||||
Thread::myself()->entry();
|
||||
Thread::myself()->_join_lock.unlock();
|
||||
sleep_forever();
|
||||
}
|
||||
|
||||
|
||||
void Thread_base::_init_platform_thread(size_t, Type) { }
|
||||
void Thread::_init_platform_thread(size_t, Type) { }
|
||||
|
||||
|
||||
void Thread_base::_deinit_platform_thread() { }
|
||||
void Thread::_deinit_platform_thread() { }
|
||||
|
||||
|
||||
void Thread_base::start()
|
||||
void Thread::start()
|
||||
{
|
||||
native_thread().tid = lx_create_thread(Thread_base::_thread_start, stack_top(), this);
|
||||
native_thread().tid = lx_create_thread(Thread::_thread_start, stack_top(), this);
|
||||
native_thread().pid = lx_getpid();
|
||||
}
|
||||
|
||||
|
||||
void Thread_base::cancel_blocking() { }
|
||||
void Thread::cancel_blocking() { }
|
||||
|
@ -38,7 +38,7 @@ static inline void thread_yield()
|
||||
}
|
||||
|
||||
|
||||
static inline bool thread_check_stopped_and_restart(Genode::Thread_base *thread_base)
|
||||
static inline bool thread_check_stopped_and_restart(Genode::Thread *thread_base)
|
||||
{
|
||||
const int *futex_counter_ptr = thread_base ?
|
||||
&thread_base->native_thread().futex_counter :
|
||||
@ -47,7 +47,7 @@ static inline bool thread_check_stopped_and_restart(Genode::Thread_base *thread_
|
||||
}
|
||||
|
||||
|
||||
static inline void thread_switch_to(Genode::Thread_base *thread_base)
|
||||
static inline void thread_switch_to(Genode::Thread *thread_base)
|
||||
{
|
||||
thread_yield();
|
||||
}
|
||||
@ -60,7 +60,7 @@ static inline void thread_stop_myself()
|
||||
* 'thread_check_stopped_and_restart()' function will get called
|
||||
* repeatedly until this thread has actually executed the syscall.
|
||||
*/
|
||||
Genode::Thread_base *myself = Genode::Thread_base::myself();
|
||||
Genode::Thread *myself = Genode::Thread::myself();
|
||||
const int *futex_counter_ptr = myself ?
|
||||
&myself->native_thread().futex_counter :
|
||||
&main_thread_futex_counter;
|
||||
|
@ -37,7 +37,7 @@ Child::Process::Initial_thread::Initial_thread(Cpu_session &cpu,
|
||||
char const *name)
|
||||
:
|
||||
cpu(cpu),
|
||||
cap(cpu.create_thread(pd, Cpu_session::DEFAULT_WEIGHT, name))
|
||||
cap(cpu.create_thread(pd, name, Affinity::Location(), Cpu_session::Weight()))
|
||||
{ }
|
||||
|
||||
|
||||
|
@ -431,7 +431,7 @@ Genode::Rpc_request Genode::ipc_reply_wait(Reply_capability const &last_caller,
|
||||
* Block infinitely if called from the main thread. This may happen if the
|
||||
* main thread calls 'sleep_forever()'.
|
||||
*/
|
||||
if (!Thread_base::myself()) {
|
||||
if (!Thread::myself()) {
|
||||
struct timespec ts = { 1000, 0 };
|
||||
for (;;) lx_nanosleep(&ts, 0);
|
||||
}
|
||||
@ -443,7 +443,7 @@ Genode::Rpc_request Genode::ipc_reply_wait(Reply_capability const &last_caller,
|
||||
|
||||
msg.accept_sockets(Message::MAX_SDS_PER_MSG);
|
||||
|
||||
Native_thread &native_thread = Thread_base::myself()->native_thread();
|
||||
Native_thread &native_thread = Thread::myself()->native_thread();
|
||||
|
||||
request_msg.reset();
|
||||
int const ret = lx_recvmsg(native_thread.socket_pair.server_sd, msg.msg(), 0);
|
||||
@ -479,10 +479,10 @@ Ipc_server::Ipc_server()
|
||||
* definition, main is never an RPC entrypoint. However, the main thread
|
||||
* may call 'sleep_forever()', which instantiates 'Ipc_server'.
|
||||
*/
|
||||
if (!Thread_base::myself())
|
||||
if (!Thread::myself())
|
||||
return;
|
||||
|
||||
Native_thread &native_thread = Thread_base::myself()->native_thread();
|
||||
Native_thread &native_thread = Thread::myself()->native_thread();
|
||||
|
||||
if (native_thread.is_ipc_server) {
|
||||
PRAW("[%d] unexpected multiple instantiation of Ipc_server by one thread",
|
||||
@ -504,14 +504,14 @@ Ipc_server::Ipc_server()
|
||||
|
||||
Ipc_server::~Ipc_server()
|
||||
{
|
||||
if (!Thread_base::myself())
|
||||
if (!Thread::myself())
|
||||
return;
|
||||
|
||||
/*
|
||||
* Reset thread role to non-server such that we can enter 'sleep_forever'
|
||||
* without getting a warning.
|
||||
*/
|
||||
Native_thread &native_thread = Thread_base::myself()->native_thread();
|
||||
Native_thread &native_thread = Thread::myself()->native_thread();
|
||||
|
||||
Genode::ep_sd_registry()->disassociate(native_thread.socket_pair.client_sd);
|
||||
native_thread.is_ipc_server = false;
|
||||
|
@ -175,7 +175,7 @@ namespace Genode {
|
||||
|
||||
Socket_pair socket_pair;
|
||||
|
||||
Thread_base *thread = Thread_base::myself();
|
||||
Thread *thread = Thread::myself();
|
||||
if (thread) {
|
||||
socket_pair.server_sd = native_cpu.server_sd(thread->cap()).dst().socket;
|
||||
socket_pair.client_sd = native_cpu.client_sd(thread->cap()).dst().socket;
|
||||
|
@ -47,7 +47,7 @@ static void thread_exit_signal_handler(int) { lx_exit(0); }
|
||||
|
||||
static char signal_stack[0x2000] __attribute__((aligned(0x1000)));
|
||||
|
||||
void Thread_base::_thread_start()
|
||||
void Thread::_thread_start()
|
||||
{
|
||||
lx_sigaltstack(signal_stack, sizeof(signal_stack));
|
||||
|
||||
@ -57,7 +57,7 @@ void Thread_base::_thread_start()
|
||||
*/
|
||||
lx_sigaction(LX_SIGUSR1, empty_signal_handler);
|
||||
|
||||
Thread_base * const thread = Thread_base::myself();
|
||||
Thread * const thread = Thread::myself();
|
||||
|
||||
/* inform core about the new thread and process ID of the new thread */
|
||||
Linux_native_cpu_client native_cpu(thread->_cpu_session->native_cpu());
|
||||
@ -75,7 +75,7 @@ void Thread_base::_thread_start()
|
||||
}
|
||||
|
||||
|
||||
void Thread_base::_init_platform_thread(size_t weight, Type type)
|
||||
void Thread::_init_platform_thread(size_t weight, Type type)
|
||||
{
|
||||
/* if no cpu session is given, use it from the environment */
|
||||
if (!_cpu_session)
|
||||
@ -84,7 +84,9 @@ void Thread_base::_init_platform_thread(size_t weight, Type type)
|
||||
/* for normal threads create an object at the CPU session */
|
||||
if (type == NORMAL) {
|
||||
_thread_cap = _cpu_session->create_thread(env()->pd_session_cap(),
|
||||
weight, _stack->name().string());
|
||||
_stack->name().string(),
|
||||
Affinity::Location(),
|
||||
Weight());
|
||||
return;
|
||||
}
|
||||
/* adjust initial object state for main threads */
|
||||
@ -93,7 +95,7 @@ void Thread_base::_init_platform_thread(size_t weight, Type type)
|
||||
}
|
||||
|
||||
|
||||
void Thread_base::_deinit_platform_thread()
|
||||
void Thread::_deinit_platform_thread()
|
||||
{
|
||||
/*
|
||||
* Kill thread until it is really really dead
|
||||
@ -124,7 +126,7 @@ void Thread_base::_deinit_platform_thread()
|
||||
}
|
||||
|
||||
|
||||
void Thread_base::start()
|
||||
void Thread::start()
|
||||
{
|
||||
/* synchronize calls of the 'start' function */
|
||||
static Lock lock;
|
||||
@ -142,7 +144,7 @@ void Thread_base::start()
|
||||
threadlib_initialized = true;
|
||||
}
|
||||
|
||||
native_thread().tid = lx_create_thread(Thread_base::_thread_start, stack_top(), this);
|
||||
native_thread().tid = lx_create_thread(Thread::_thread_start, stack_top(), this);
|
||||
native_thread().pid = lx_getpid();
|
||||
|
||||
/* wait until the 'thread_start' function got entered */
|
||||
@ -150,7 +152,7 @@ void Thread_base::start()
|
||||
}
|
||||
|
||||
|
||||
void Thread_base::cancel_blocking()
|
||||
void Thread::cancel_blocking()
|
||||
{
|
||||
_cpu_session->cancel_blocking(_thread_cap);
|
||||
}
|
||||
|
@ -194,7 +194,7 @@ namespace Genode {
|
||||
* Filled out by 'thread_start' function in the stack of the new
|
||||
* thread
|
||||
*/
|
||||
Thread_base * const thread_base;
|
||||
Thread * const thread_base;
|
||||
|
||||
/**
|
||||
* POSIX thread handle
|
||||
@ -204,9 +204,9 @@ namespace Genode {
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* \param thread associated 'Thread_base' object
|
||||
* \param thread associated 'Thread' object
|
||||
*/
|
||||
Meta_data(Thread_base *thread) : thread_base(thread)
|
||||
Meta_data(Thread *thread) : thread_base(thread)
|
||||
{
|
||||
native_thread.meta_data = this;
|
||||
}
|
||||
@ -261,7 +261,7 @@ namespace Genode {
|
||||
|
||||
public:
|
||||
|
||||
Thread_meta_data_created(Thread_base *thread)
|
||||
Thread_meta_data_created(Thread *thread)
|
||||
: Native_thread::Meta_data(thread) { }
|
||||
|
||||
void wait_for_construction()
|
||||
@ -302,7 +302,7 @@ namespace Genode {
|
||||
{
|
||||
public:
|
||||
|
||||
Thread_meta_data_adopted(Thread_base *thread)
|
||||
Thread_meta_data_adopted(Thread *thread)
|
||||
: Native_thread::Meta_data(thread) { }
|
||||
|
||||
void wait_for_construction()
|
||||
@ -374,13 +374,13 @@ static void *thread_start(void *arg)
|
||||
|
||||
adopt_thread(meta_data);
|
||||
|
||||
/* unblock 'Thread_base' constructor */
|
||||
/* unblock 'Thread' constructor */
|
||||
meta_data->constructed();
|
||||
|
||||
/* block until the 'Thread_base::start' gets called */
|
||||
/* block until the 'Thread::start' gets called */
|
||||
meta_data->wait_for_start();
|
||||
|
||||
Thread_base::myself()->entry();
|
||||
Thread::myself()->entry();
|
||||
|
||||
meta_data->joined();
|
||||
return 0;
|
||||
@ -390,7 +390,7 @@ static void *thread_start(void *arg)
|
||||
extern "C" void *malloc(::size_t size);
|
||||
|
||||
|
||||
Thread_base *Thread_base::myself()
|
||||
Thread *Thread::myself()
|
||||
{
|
||||
void * const tls = pthread_getspecific(tls_key());
|
||||
|
||||
@ -406,26 +406,26 @@ Thread_base *Thread_base::myself()
|
||||
* Genode's thread API. This may happen if a native Linux library creates
|
||||
* threads via the pthread library. If such a thread calls Genode code,
|
||||
* which then tries to perform IPC, the program fails because there exists
|
||||
* no 'Thread_base' object. We recover from this unfortunate situation by
|
||||
* creating a dummy 'Thread_base' object and associate it with the calling
|
||||
* no 'Thread' object. We recover from this unfortunate situation by
|
||||
* creating a dummy 'Thread' object and associate it with the calling
|
||||
* thread.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Create dummy 'Thread_base' object but suppress the execution of its
|
||||
* Create dummy 'Thread' object but suppress the execution of its
|
||||
* constructor. If we called the constructor, we would create a new Genode
|
||||
* thread, which is not what we want. For the allocation, we use glibc
|
||||
* malloc because 'Genode::env()->heap()->alloc()' uses IPC.
|
||||
*
|
||||
* XXX Both the 'Thread_base' and 'Native_thread::Meta_data' objects are
|
||||
* XXX Both the 'Thread' and 'Native_thread::Meta_data' objects are
|
||||
* never freed.
|
||||
*/
|
||||
Thread_base *thread = (Thread_base *)malloc(sizeof(Thread_base));
|
||||
Thread *thread = (Thread *)malloc(sizeof(Thread));
|
||||
memset(thread, 0, sizeof(*thread));
|
||||
Native_thread::Meta_data *meta_data = new Thread_meta_data_adopted(thread);
|
||||
|
||||
/*
|
||||
* Initialize 'Thread_base::_native_thread' to point to the default-
|
||||
* Initialize 'Thread::_native_thread' to point to the default-
|
||||
* constructed 'Native_thread' (part of 'Meta_data').
|
||||
*/
|
||||
meta_data->thread_base->_native_thread = &meta_data->native_thread;
|
||||
@ -435,7 +435,7 @@ Thread_base *Thread_base::myself()
|
||||
}
|
||||
|
||||
|
||||
void Thread_base::start()
|
||||
void Thread::start()
|
||||
{
|
||||
/*
|
||||
* Unblock thread that is supposed to slumber in 'thread_start'.
|
||||
@ -444,17 +444,17 @@ void Thread_base::start()
|
||||
}
|
||||
|
||||
|
||||
void Thread_base::join()
|
||||
void Thread::join()
|
||||
{
|
||||
native_thread().meta_data->wait_for_join();
|
||||
}
|
||||
|
||||
|
||||
Native_thread &Thread_base::native_thread() { return *_native_thread; }
|
||||
Native_thread &Thread::native_thread() { return *_native_thread; }
|
||||
|
||||
|
||||
Thread_base::Thread_base(size_t weight, const char *name, size_t stack_size,
|
||||
Type type, Cpu_session * cpu_sess, Affinity::Location)
|
||||
Thread::Thread(size_t weight, const char *name, size_t stack_size,
|
||||
Type type, Cpu_session * cpu_sess, Affinity::Location)
|
||||
: _cpu_session(cpu_sess)
|
||||
{
|
||||
Native_thread::Meta_data *meta_data =
|
||||
@ -472,18 +472,31 @@ Thread_base::Thread_base(size_t weight, const char *name, size_t stack_size,
|
||||
|
||||
native_thread().meta_data->wait_for_construction();
|
||||
|
||||
_thread_cap = _cpu_session->create_thread(env()->pd_session_cap(), weight, name);
|
||||
_thread_cap = _cpu_session->create_thread(env()->pd_session_cap(), name,
|
||||
Location(), Weight(weight));
|
||||
|
||||
Linux_native_cpu_client native_cpu(_cpu_session->native_cpu());
|
||||
native_cpu.thread_id(_thread_cap, native_thread().pid, native_thread().tid);
|
||||
}
|
||||
|
||||
|
||||
Thread_base::Thread_base(size_t weight, const char *name, size_t stack_size,
|
||||
Type type, Affinity::Location)
|
||||
: Thread_base(weight, name, stack_size, type, env()->cpu_session()) { }
|
||||
Thread::Thread(size_t weight, const char *name, size_t stack_size,
|
||||
Type type, Affinity::Location)
|
||||
: Thread(weight, name, stack_size, type, env()->cpu_session()) { }
|
||||
|
||||
void Thread_base::cancel_blocking()
|
||||
|
||||
Thread::Thread(Env &env, Name const &name, size_t stack_size, Location location,
|
||||
Weight weight, Cpu_session &cpu)
|
||||
: Thread(weight.value, name.string(), stack_size, NORMAL,
|
||||
&cpu, location)
|
||||
{ }
|
||||
|
||||
|
||||
Thread::Thread(Env &env, Name const &name, size_t stack_size)
|
||||
: Thread(env, name, stack_size, Location(), Weight(), env.cpu()) { }
|
||||
|
||||
|
||||
void Thread::cancel_blocking()
|
||||
{
|
||||
/*
|
||||
* XXX implement interaction with CPU session
|
||||
@ -491,7 +504,7 @@ void Thread_base::cancel_blocking()
|
||||
}
|
||||
|
||||
|
||||
Thread_base::~Thread_base()
|
||||
Thread::~Thread()
|
||||
{
|
||||
bool const needs_join = (pthread_cancel(native_thread().meta_data->pt) == 0);
|
||||
|
||||
|
@ -17,12 +17,12 @@
|
||||
|
||||
enum { STACK_SIZE = 4096 };
|
||||
|
||||
struct Thread : Genode::Thread<STACK_SIZE>
|
||||
struct Thread : Genode::Thread_deprecated<STACK_SIZE>
|
||||
{
|
||||
Genode::Lock &_barrier;
|
||||
|
||||
Thread(Genode::Lock &barrier)
|
||||
: Genode::Thread<STACK_SIZE>("stat"), _barrier(barrier) { start(); }
|
||||
: Genode::Thread_deprecated<STACK_SIZE>("stat"), _barrier(barrier) { start(); }
|
||||
|
||||
void entry()
|
||||
{
|
||||
|
@ -34,11 +34,11 @@ static void *pthread_entry(void *)
|
||||
PINF("first message");
|
||||
|
||||
/*
|
||||
* Without the lazy initialization of 'Thread_base' objects for threads
|
||||
* Without the lazy initialization of 'Thread' objects for threads
|
||||
* created w/o Genode's Thread API, the printing of the first message will
|
||||
* never return because the IPC reply could not be delivered.
|
||||
*
|
||||
* With the on-demand creation of 'Thread_base' objects, the second message
|
||||
* With the on-demand creation of 'Thread' objects, the second message
|
||||
* will appear in the LOG output.
|
||||
*/
|
||||
|
||||
|
@ -44,7 +44,7 @@ int main()
|
||||
using namespace Genode;
|
||||
|
||||
/* activate for early printf in Rm_session_mmap::attach() etc. */
|
||||
if (0) Thread_base::trace("FOO");
|
||||
if (0) Thread::trace("FOO");
|
||||
|
||||
/* induce initial heap expansion to remove RM noise */
|
||||
if (1) {
|
||||
|
Reference in New Issue
Block a user