libc: implement '_spinlock()' and '_spinunlock()'

FreeBSD libc code uses only a single spinlock instance and, thus there
is no obvious reason why it would need to be implemented as an actual
spinlock. _spinlock() and _spinunlock() functions are implemented with a
static pthread mutex.

Issue #725
This commit is contained in:
Christian Prochaska 2023-10-23 09:58:20 +02:00 committed by Christian Helmuth
parent 94bbdbb71d
commit 9aa0de24af
3 changed files with 46 additions and 3 deletions

View File

@ -19,7 +19,7 @@ SRC_CC = atexit.cc dummies.cc rlimit.cc sysctl.cc \
vfs_plugin.cc dynamic_linker.cc signal.cc \
socket_operations.cc socket_fs_plugin.cc syscall.cc \
getpwent.cc getrandom.cc fork.cc execve.cc kernel.cc component.cc \
genode.cc
genode.cc spinlock.cc
#
# Pthreads

View File

@ -163,8 +163,6 @@ __SYS_DUMMY(int , -1, ptrace, (int, pid_t, caddr_t, int));
__SYS_DUMMY(ssize_t, -1, sendmsg, (int s, const struct msghdr*, int));
__SYS_DUMMY(int , -1, setcontext, (const ucontext_t *ucp));
__SYS_DUMMY(void , , spinlock_stub, (spinlock_t *));
__SYS_DUMMY(void , , spinlock, (spinlock_t *));
__SYS_DUMMY(void , , spinunlock, (spinlock_t *));
__SYS_DUMMY(void , , spinunlock_stub, (spinlock_t *));
__SYS_DUMMY(int, -1, swapcontext, (ucontext_t *, const ucontext_t *));
__SYS_DUMMY(int, -1, system, (const char *string));

View File

@ -0,0 +1,45 @@
/*
* \brief libc-internal spinlock implementation
* \author Christian Prochaska
* \date 2023-10-23
*/
/*
* Copyright (C) 2023 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU Affero General Public License version 3.
*/
#include <pthread.h>
#include <spinlock.h>
extern "C" {
/*
* Only one spinlock ('__stdio_thread_lock') is
* used in the FreeBSD libc code.
*/
static pthread_mutex_t *stdio_thread_lock_mutex()
{
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
return &mutex;
}
void _spinlock(spinlock_t *)
{
pthread_mutex_lock(stdio_thread_lock_mutex());
}
typeof(_spinlock) __sys_spinlock
__attribute__((alias("_spinlock")));
void _spinunlock(spinlock_t *)
{
pthread_mutex_unlock(stdio_thread_lock_mutex());
}
typeof(_spinunlock) __sys_spinunlock
__attribute__((alias("_spinunlock")));
}