genode/repos/base-linux/src/lib/base/thread_linux.cc

172 lines
4.6 KiB
C++
Raw Normal View History

2011-12-22 16:19:25 +01:00
/*
* \brief Implementation of the Thread API via Linux threads
* \author Norman Feske
* \author Martin Stein
2011-12-22 16:19:25 +01:00
* \date 2006-06-13
*/
/*
* Copyright (C) 2006-2017 Genode Labs GmbH
2011-12-22 16:19:25 +01:00
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU Affero General Public License version 3.
2011-12-22 16:19:25 +01:00
*/
/* Genode includes */
#include <base/env.h>
#include <base/thread.h>
#include <base/snprintf.h>
#include <base/sleep.h>
#include <base/log.h>
#include <linux_native_cpu/client.h>
#include <cpu_thread/client.h>
#include <deprecated/env.h>
2011-12-22 16:19:25 +01:00
/* base-internal includes */
#include <base/internal/stack.h>
#include <base/internal/globals.h>
2011-12-22 16:19:25 +01:00
/* Linux syscall bindings */
#include <linux_syscalls.h>
using namespace Genode;
extern int main_thread_futex_counter;
2011-12-22 16:19:25 +01:00
static void empty_signal_handler(int) { }
2020-02-18 15:29:47 +01:00
static Blockade &startup_lock()
{
2020-02-18 15:29:47 +01:00
static Blockade blockade;
return blockade;
}
2011-12-22 16:19:25 +01:00
/**
* Signal handler for killing the thread
*/
static void thread_exit_signal_handler(int) { lx_exit(0); }
void Thread::_thread_start()
2011-12-22 16:19:25 +01:00
{
Thread * const thread = Thread::myself();
/* use primary stack as alternate stack for fatal signals (exceptions) */
void *stack_base = (void *)thread->_stack->base();
size_t stack_size = thread->_stack->top() - thread->_stack->base();
lx_sigaltstack(stack_base, stack_size);
if (stack_size < 0x1000)
raw("small stack of ", stack_size, " bytes for \"", thread->name(),
"\" may break Linux signal handling");
2011-12-22 16:19:25 +01:00
/*
* Set signal handler such that canceled system calls get not
* transparently retried after a signal gets received.
*/
lx_sigaction(LX_SIGUSR1, empty_signal_handler, false);
/* inform core about the new thread and process ID of the new thread */
{
Linux_native_cpu_client native_cpu(thread->_cpu_session->native_cpu());
native_cpu.thread_id(thread->cap(), thread->native_thread().pid, thread->native_thread().tid);
}
/* wakeup 'start' function */
2020-02-18 15:29:47 +01:00
startup_lock().wakeup();
thread->entry();
/* unblock caller of 'join()' */
thread->_join.wakeup();
2011-12-22 16:19:25 +01:00
sleep_forever();
}
Follow practices suggested by "Effective C++" The patch adjust the code of the base, base-<kernel>, and os repository. To adapt existing components to fix violations of the best practices suggested by "Effective C++" as reported by the -Weffc++ compiler argument. The changes follow the patterns outlined below: * A class with virtual functions can no longer publicly inherit base classed without a vtable. The inherited object may either be moved to a member variable, or inherited privately. The latter would be used for classes that inherit 'List::Element' or 'Avl_node'. In order to enable the 'List' and 'Avl_tree' to access the meta data, the 'List' must become a friend. * Instead of adding a virtual destructor to abstract base classes, we inherit the new 'Interface' class, which contains a virtual destructor. This way, single-line abstract base classes can stay as compact as they are now. The 'Interface' utility resides in base/include/util/interface.h. * With the new warnings enabled, all member variables must be explicitly initialized. Basic types may be initialized with '='. All other types are initialized with braces '{ ... }' or as class initializers. If basic types and non-basic types appear in a row, it is nice to only use the brace syntax (also for basic types) and align the braces. * If a class contains pointers as members, it must now also provide a copy constructor and assignment operator. In the most cases, one would make them private, effectively disallowing the objects to be copied. Unfortunately, this warning cannot be fixed be inheriting our existing 'Noncopyable' class (the compiler fails to detect that the inheriting class cannot be copied and still gives the error). For now, we have to manually add declarations for both the copy constructor and assignment operator as private class members. Those declarations should be prepended with a comment like this: /* * Noncopyable */ Thread(Thread const &); Thread &operator = (Thread const &); In the future, we should revisit these places and try to replace the pointers with references. In the presence of at least one reference member, the compiler would no longer implicitly generate a copy constructor. So we could remove the manual declaration. Issue #465
2017-12-21 15:42:15 +01:00
void Thread::_init_platform_thread(size_t /* weight */, Type type)
{
/* if no cpu session is given, use it from the environment */
if (!_cpu_session)
_cpu_session = env_deprecated()->cpu_session();
/* for normal threads create an object at the CPU session */
if (type == NORMAL) {
_thread_cap = _cpu_session->create_thread(env_deprecated()->pd_session_cap(),
_stack->name().string(),
Affinity::Location(),
Weight());
return;
}
/* adjust initial object state for main threads */
native_thread().futex_counter = main_thread_futex_counter;
_thread_cap = main_thread_cap();
}
2011-12-22 16:19:25 +01:00
void Thread::_deinit_platform_thread()
2011-12-22 16:19:25 +01:00
{
/*
* Kill thread until it is really really dead
*
* We use the 'tgkill' system call to kill the thread. This system call
* returns immediately and just flags the corresponding signal at the
* targeted thread context. However, the thread still lives until the
* signal flags are evaluated. When leaving this function, however, we want
* to be sure that the thread is no more executing any code such that we
* an safely free and unmap the thread's stack. So we call 'tgkill' in a
* loop until we get an error indicating that the thread does not exists
* anymore.
*/
for (;;) {
/* destroy thread locally */
int ret = lx_tgkill(native_thread().pid, native_thread().tid, LX_SIGCANCEL);
2011-12-22 16:19:25 +01:00
if (ret < 0) break;
/* if thread still exists, wait a bit and try to kill it again */
struct timespec ts = { 0, 500 };
lx_nanosleep(&ts, 0);
}
/* inform core about the killed thread */
_cpu_session->kill_thread(_thread_cap);
2011-12-22 16:19:25 +01:00
}
void Thread::start()
2011-12-22 16:19:25 +01:00
{
/* synchronize calls of the 'start' function */
2020-02-18 15:29:47 +01:00
static Mutex mutex;
Mutex::Guard guard(mutex);
2018-12-04 16:46:38 +01:00
_init_cpu_session_and_trace_control();
2011-12-22 16:19:25 +01:00
/*
* The first time we enter this code path, the 'start' function is
* called by the main thread as there cannot exist other threads
* without executing this function. When first called, we initialize
* the thread lib here.
*/
static bool threadlib_initialized = false;
if (!threadlib_initialized) {
lx_sigaction(LX_SIGCANCEL, thread_exit_signal_handler, false);
2011-12-22 16:19:25 +01:00
threadlib_initialized = true;
}
native_thread().tid = lx_create_thread(Thread::_thread_start, stack_top(), this);
native_thread().pid = lx_getpid();
2011-12-22 16:19:25 +01:00
/* wait until the 'thread_start' function got entered */
2020-02-18 15:29:47 +01:00
startup_lock().block();
2011-12-22 16:19:25 +01:00
}
void Thread::cancel_blocking()
2011-12-22 16:19:25 +01:00
{
Cpu_thread_client(_thread_cap).cancel_blocking();
2011-12-22 16:19:25 +01:00
}