123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897 |
- /*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
- #include <AK/Assertions.h>
- #include <AK/Atomic.h>
- #include <AK/Debug.h>
- #include <AK/Format.h>
- #include <AK/StdLibExtras.h>
- #include <Kernel/API/Syscall.h>
- #include <LibSystem/syscall.h>
- #include <bits/pthread_integration.h>
- #include <errno.h>
- #include <limits.h>
- #include <pthread.h>
- #include <serenity.h>
- #include <signal.h>
- #include <stdio.h>
- #include <string.h>
- #include <sys/mman.h>
- #include <syscall.h>
- #include <time.h>
- #include <unistd.h>
- namespace {
- using PthreadAttrImpl = Syscall::SC_create_thread_params;
- } // end anonymous namespace
- static constexpr size_t required_stack_alignment = 4 * MiB;
- static constexpr size_t highest_reasonable_guard_size = 32 * PAGE_SIZE;
- static constexpr size_t highest_reasonable_stack_size = 8 * MiB; // That's the default in Ubuntu?
- __thread void* s_stack_location;
- __thread size_t s_stack_size;
- #define __RETURN_PTHREAD_ERROR(rc) \
- return ((rc) < 0 ? -(rc) : 0)
- extern "C" {
- static void* pthread_create_helper(void* (*routine)(void*), void* argument, void* stack_location, size_t stack_size)
- {
- s_stack_location = stack_location;
- s_stack_size = stack_size;
- void* ret_val = routine(argument);
- pthread_exit(ret_val);
- return nullptr;
- }
- static int create_thread(pthread_t* thread, void* (*entry)(void*), void* argument, PthreadAttrImpl* thread_params)
- {
- void** stack = (void**)((uintptr_t)thread_params->m_stack_location + thread_params->m_stack_size);
- auto push_on_stack = [&](void* data) {
- stack--;
- *stack = data;
- thread_params->m_stack_size -= sizeof(void*);
- };
- // We set up the stack for pthread_create_helper.
- // Note that we need to align the stack to 16B, accounting for
- // the fact that we also push 16 bytes.
- while (((uintptr_t)stack - 16) % 16 != 0)
- push_on_stack(nullptr);
- push_on_stack((void*)thread_params->m_stack_size);
- push_on_stack(thread_params->m_stack_location);
- push_on_stack(argument);
- push_on_stack((void*)entry);
- VERIFY((uintptr_t)stack % 16 == 0);
- // Push a fake return address
- push_on_stack(nullptr);
- int rc = syscall(SC_create_thread, pthread_create_helper, thread_params);
- if (rc >= 0)
- *thread = rc;
- __RETURN_PTHREAD_ERROR(rc);
- }
- [[noreturn]] static void exit_thread(void* code, void* stack_location, size_t stack_size)
- {
- __pthread_key_destroy_for_current_thread();
- syscall(SC_exit_thread, code, stack_location, stack_size);
- VERIFY_NOT_REACHED();
- }
- int pthread_self()
- {
- return __pthread_self();
- }
- int pthread_create(pthread_t* thread, pthread_attr_t* attributes, void* (*start_routine)(void*), void* argument_to_start_routine)
- {
- if (!thread)
- return -EINVAL;
- PthreadAttrImpl default_attributes {};
- PthreadAttrImpl** arg_attributes = reinterpret_cast<PthreadAttrImpl**>(attributes);
- PthreadAttrImpl* used_attributes = arg_attributes ? *arg_attributes : &default_attributes;
- if (!used_attributes->m_stack_location) {
- // adjust stack size, user might have called setstacksize, which has no restrictions on size/alignment
- if (0 != (used_attributes->m_stack_size % required_stack_alignment))
- used_attributes->m_stack_size += required_stack_alignment - (used_attributes->m_stack_size % required_stack_alignment);
- used_attributes->m_stack_location = mmap_with_name(nullptr, used_attributes->m_stack_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, 0, 0, "Thread stack");
- if (!used_attributes->m_stack_location)
- return -1;
- }
- dbgln_if(PTHREAD_DEBUG, "pthread_create: Creating thread with attributes at {}, detach state {}, priority {}, guard page size {}, stack size {}, stack location {}",
- used_attributes,
- (PTHREAD_CREATE_JOINABLE == used_attributes->m_detach_state) ? "joinable" : "detached",
- used_attributes->m_schedule_priority,
- used_attributes->m_guard_page_size,
- used_attributes->m_stack_size,
- used_attributes->m_stack_location);
- return create_thread(thread, start_routine, argument_to_start_routine, used_attributes);
- }
- void pthread_exit(void* value_ptr)
- {
- exit_thread(value_ptr, s_stack_location, s_stack_size);
- }
- void pthread_cleanup_push([[maybe_unused]] void (*routine)(void*), [[maybe_unused]] void* arg)
- {
- TODO();
- }
- void pthread_cleanup_pop([[maybe_unused]] int execute)
- {
- TODO();
- }
- int pthread_join(pthread_t thread, void** exit_value_ptr)
- {
- int rc = syscall(SC_join_thread, thread, exit_value_ptr);
- __RETURN_PTHREAD_ERROR(rc);
- }
- int pthread_detach(pthread_t thread)
- {
- int rc = syscall(SC_detach_thread, thread);
- __RETURN_PTHREAD_ERROR(rc);
- }
- int pthread_sigmask(int how, const sigset_t* set, sigset_t* old_set)
- {
- if (sigprocmask(how, set, old_set))
- return errno;
- return 0;
- }
- int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attributes)
- {
- return __pthread_mutex_init(mutex, attributes);
- }
- int pthread_mutex_destroy(pthread_mutex_t*)
- {
- return 0;
- }
- int pthread_mutex_lock(pthread_mutex_t* mutex)
- {
- return __pthread_mutex_lock(mutex);
- }
- int pthread_mutex_trylock(pthread_mutex_t* mutex)
- {
- return __pthread_mutex_trylock(mutex);
- }
- int pthread_mutex_unlock(pthread_mutex_t* mutex)
- {
- return __pthread_mutex_unlock(mutex);
- }
- int pthread_mutexattr_init(pthread_mutexattr_t* attr)
- {
- attr->type = PTHREAD_MUTEX_NORMAL;
- return 0;
- }
- int pthread_mutexattr_destroy(pthread_mutexattr_t*)
- {
- return 0;
- }
- int pthread_mutexattr_settype(pthread_mutexattr_t* attr, int type)
- {
- if (!attr)
- return EINVAL;
- if (type != PTHREAD_MUTEX_NORMAL && type != PTHREAD_MUTEX_RECURSIVE)
- return EINVAL;
- attr->type = type;
- return 0;
- }
- int pthread_mutexattr_gettype(pthread_mutexattr_t* attr, int* type)
- {
- *type = attr->type;
- return 0;
- }
- int pthread_attr_init(pthread_attr_t* attributes)
- {
- auto* impl = new PthreadAttrImpl {};
- *attributes = impl;
- dbgln_if(PTHREAD_DEBUG, "pthread_attr_init: New thread attributes at {}, detach state {}, priority {}, guard page size {}, stack size {}, stack location {}",
- impl,
- (PTHREAD_CREATE_JOINABLE == impl->m_detach_state) ? "joinable" : "detached",
- impl->m_schedule_priority,
- impl->m_guard_page_size,
- impl->m_stack_size,
- impl->m_stack_location);
- return 0;
- }
- int pthread_attr_destroy(pthread_attr_t* attributes)
- {
- auto* attributes_impl = *(reinterpret_cast<PthreadAttrImpl**>(attributes));
- delete attributes_impl;
- return 0;
- }
- int pthread_attr_getdetachstate(const pthread_attr_t* attributes, int* p_detach_state)
- {
- auto* attributes_impl = *(reinterpret_cast<const PthreadAttrImpl* const*>(attributes));
- if (!attributes_impl || !p_detach_state)
- return EINVAL;
- *p_detach_state = attributes_impl->m_detach_state;
- return 0;
- }
- int pthread_attr_setdetachstate(pthread_attr_t* attributes, int detach_state)
- {
- auto* attributes_impl = *(reinterpret_cast<PthreadAttrImpl**>(attributes));
- if (!attributes_impl)
- return EINVAL;
- if (detach_state != PTHREAD_CREATE_JOINABLE && detach_state != PTHREAD_CREATE_DETACHED)
- return EINVAL;
- attributes_impl->m_detach_state = detach_state;
- dbgln_if(PTHREAD_DEBUG, "pthread_attr_setdetachstate: Thread attributes at {}, detach state {}, priority {}, guard page size {}, stack size {}, stack location {}",
- attributes_impl,
- (PTHREAD_CREATE_JOINABLE == attributes_impl->m_detach_state) ? "joinable" : "detached",
- attributes_impl->m_schedule_priority,
- attributes_impl->m_guard_page_size,
- attributes_impl->m_stack_size,
- attributes_impl->m_stack_location);
- return 0;
- }
- int pthread_attr_getguardsize(const pthread_attr_t* attributes, size_t* p_guard_size)
- {
- auto* attributes_impl = *(reinterpret_cast<const PthreadAttrImpl* const*>(attributes));
- if (!attributes_impl || !p_guard_size)
- return EINVAL;
- *p_guard_size = attributes_impl->m_reported_guard_page_size;
- return 0;
- }
- int pthread_attr_setguardsize(pthread_attr_t* attributes, size_t guard_size)
- {
- auto* attributes_impl = *(reinterpret_cast<PthreadAttrImpl**>(attributes));
- if (!attributes_impl)
- return EINVAL;
- size_t actual_guard_size = guard_size;
- // round up
- if (0 != (guard_size % PAGE_SIZE))
- actual_guard_size += PAGE_SIZE - (guard_size % PAGE_SIZE);
- // what is the user even doing?
- if (actual_guard_size > highest_reasonable_guard_size) {
- return EINVAL;
- }
- attributes_impl->m_guard_page_size = actual_guard_size;
- attributes_impl->m_reported_guard_page_size = guard_size; // POSIX, why?
- dbgln_if(PTHREAD_DEBUG, "pthread_attr_setguardsize: Thread attributes at {}, detach state {}, priority {}, guard page size {}, stack size {}, stack location {}",
- attributes_impl,
- (PTHREAD_CREATE_JOINABLE == attributes_impl->m_detach_state) ? "joinable" : "detached",
- attributes_impl->m_schedule_priority,
- attributes_impl->m_guard_page_size,
- attributes_impl->m_stack_size,
- attributes_impl->m_stack_location);
- return 0;
- }
- int pthread_attr_getschedparam(const pthread_attr_t* attributes, struct sched_param* p_sched_param)
- {
- auto* attributes_impl = *(reinterpret_cast<const PthreadAttrImpl* const*>(attributes));
- if (!attributes_impl || !p_sched_param)
- return EINVAL;
- p_sched_param->sched_priority = attributes_impl->m_schedule_priority;
- return 0;
- }
- int pthread_attr_setschedparam(pthread_attr_t* attributes, const struct sched_param* p_sched_param)
- {
- auto* attributes_impl = *(reinterpret_cast<PthreadAttrImpl**>(attributes));
- if (!attributes_impl || !p_sched_param)
- return EINVAL;
- if (p_sched_param->sched_priority < THREAD_PRIORITY_MIN || p_sched_param->sched_priority > THREAD_PRIORITY_MAX)
- return ENOTSUP;
- attributes_impl->m_schedule_priority = p_sched_param->sched_priority;
- dbgln_if(PTHREAD_DEBUG, "pthread_attr_setschedparam: Thread attributes at {}, detach state {}, priority {}, guard page size {}, stack size {}, stack location {}",
- attributes_impl,
- (PTHREAD_CREATE_JOINABLE == attributes_impl->m_detach_state) ? "joinable" : "detached",
- attributes_impl->m_schedule_priority,
- attributes_impl->m_guard_page_size,
- attributes_impl->m_stack_size,
- attributes_impl->m_stack_location);
- return 0;
- }
- int pthread_attr_getstack(const pthread_attr_t* attributes, void** p_stack_ptr, size_t* p_stack_size)
- {
- auto* attributes_impl = *(reinterpret_cast<const PthreadAttrImpl* const*>(attributes));
- if (!attributes_impl || !p_stack_ptr || !p_stack_size)
- return EINVAL;
- *p_stack_ptr = attributes_impl->m_stack_location;
- *p_stack_size = attributes_impl->m_stack_size;
- return 0;
- }
- int pthread_attr_setstack(pthread_attr_t* attributes, void* p_stack, size_t stack_size)
- {
- auto* attributes_impl = *(reinterpret_cast<PthreadAttrImpl**>(attributes));
- if (!attributes_impl || !p_stack)
- return EINVAL;
- // Check for required alignment on size
- if (0 != (stack_size % required_stack_alignment))
- return EINVAL;
- // FIXME: Check for required alignment on pointer?
- // FIXME: "[EACCES] The stack page(s) described by stackaddr and stacksize are not both readable and writable by the thread."
- // Have to check that the whole range is mapped to this process/thread? Can we defer this to create_thread?
- attributes_impl->m_stack_size = stack_size;
- attributes_impl->m_stack_location = p_stack;
- dbgln_if(PTHREAD_DEBUG, "pthread_attr_setstack: Thread attributes at {}, detach state {}, priority {}, guard page size {}, stack size {}, stack location {}",
- attributes_impl,
- (PTHREAD_CREATE_JOINABLE == attributes_impl->m_detach_state) ? "joinable" : "detached",
- attributes_impl->m_schedule_priority,
- attributes_impl->m_guard_page_size,
- attributes_impl->m_stack_size,
- attributes_impl->m_stack_location);
- return 0;
- }
- int pthread_attr_getstacksize(const pthread_attr_t* attributes, size_t* p_stack_size)
- {
- auto* attributes_impl = *(reinterpret_cast<const PthreadAttrImpl* const*>(attributes));
- if (!attributes_impl || !p_stack_size)
- return EINVAL;
- *p_stack_size = attributes_impl->m_stack_size;
- return 0;
- }
- int pthread_attr_setstacksize(pthread_attr_t* attributes, size_t stack_size)
- {
- auto* attributes_impl = *(reinterpret_cast<PthreadAttrImpl**>(attributes));
- if (!attributes_impl)
- return EINVAL;
- if ((stack_size < PTHREAD_STACK_MIN) || stack_size > highest_reasonable_stack_size)
- return EINVAL;
- attributes_impl->m_stack_size = stack_size;
- dbgln_if(PTHREAD_DEBUG, "pthread_attr_setstacksize: Thread attributes at {}, detach state {}, priority {}, guard page size {}, stack size {}, stack location {}",
- attributes_impl,
- (PTHREAD_CREATE_JOINABLE == attributes_impl->m_detach_state) ? "joinable" : "detached",
- attributes_impl->m_schedule_priority,
- attributes_impl->m_guard_page_size,
- attributes_impl->m_stack_size,
- attributes_impl->m_stack_location);
- return 0;
- }
- int pthread_attr_getscope([[maybe_unused]] const pthread_attr_t* attributes, [[maybe_unused]] int* contention_scope)
- {
- return 0;
- }
- int pthread_attr_setscope([[maybe_unused]] pthread_attr_t* attributes, [[maybe_unused]] int contention_scope)
- {
- return 0;
- }
- int pthread_getschedparam([[maybe_unused]] pthread_t thread, [[maybe_unused]] int* policy, [[maybe_unused]] struct sched_param* param)
- {
- return 0;
- }
- int pthread_setschedparam([[maybe_unused]] pthread_t thread, [[maybe_unused]] int policy, [[maybe_unused]] const struct sched_param* param)
- {
- return 0;
- }
- int pthread_cond_init(pthread_cond_t* cond, const pthread_condattr_t* attr)
- {
- cond->value = 0;
- cond->previous = 0;
- cond->clockid = attr ? attr->clockid : CLOCK_MONOTONIC_COARSE;
- return 0;
- }
- int pthread_cond_destroy(pthread_cond_t*)
- {
- return 0;
- }
- static int futex_wait(uint32_t& futex_addr, uint32_t value, const struct timespec* abstime)
- {
- int saved_errno = errno;
- // NOTE: FUTEX_WAIT takes a relative timeout, so use FUTEX_WAIT_BITSET instead!
- int rc = futex(&futex_addr, FUTEX_WAIT_BITSET | FUTEX_CLOCK_REALTIME, value, abstime, nullptr, FUTEX_BITSET_MATCH_ANY);
- if (rc < 0 && errno == EAGAIN) {
- // If we didn't wait, that's not an error
- errno = saved_errno;
- rc = 0;
- }
- return rc;
- }
- static int cond_wait(pthread_cond_t* cond, pthread_mutex_t* mutex, const struct timespec* abstime)
- {
- u32 value = cond->value;
- cond->previous = value;
- pthread_mutex_unlock(mutex);
- int rc = futex_wait(cond->value, value, abstime);
- pthread_mutex_lock(mutex);
- return rc;
- }
- int pthread_cond_wait(pthread_cond_t* cond, pthread_mutex_t* mutex)
- {
- int rc = cond_wait(cond, mutex, nullptr);
- VERIFY(rc == 0);
- return 0;
- }
- int pthread_condattr_init(pthread_condattr_t* attr)
- {
- attr->clockid = CLOCK_MONOTONIC_COARSE;
- return 0;
- }
- int pthread_condattr_destroy(pthread_condattr_t*)
- {
- return 0;
- }
- int pthread_condattr_setclock(pthread_condattr_t* attr, clockid_t clock)
- {
- attr->clockid = clock;
- return 0;
- }
- int pthread_cond_timedwait(pthread_cond_t* cond, pthread_mutex_t* mutex, const struct timespec* abstime)
- {
- return cond_wait(cond, mutex, abstime);
- }
- int pthread_cond_signal(pthread_cond_t* cond)
- {
- u32 value = cond->previous + 1;
- cond->value = value;
- int rc = futex(&cond->value, FUTEX_WAKE, 1, nullptr, nullptr, 0);
- VERIFY(rc >= 0);
- return 0;
- }
- int pthread_cond_broadcast(pthread_cond_t* cond)
- {
- u32 value = cond->previous + 1;
- cond->value = value;
- int rc = futex(&cond->value, FUTEX_WAKE, INT32_MAX, nullptr, nullptr, 0);
- VERIFY(rc >= 0);
- return 0;
- }
- // libgcc expects this function to exist in libpthread, even
- // if it is not implemented.
- int pthread_cancel(pthread_t)
- {
- TODO();
- }
- int pthread_key_create(pthread_key_t* key, KeyDestructor destructor)
- {
- return __pthread_key_create(key, destructor);
- }
- int pthread_key_delete(pthread_key_t key)
- {
- return __pthread_key_delete(key);
- }
- void* pthread_getspecific(pthread_key_t key)
- {
- return __pthread_getspecific(key);
- }
- int pthread_setspecific(pthread_key_t key, const void* value)
- {
- return __pthread_setspecific(key, value);
- }
- int pthread_setname_np(pthread_t thread, const char* name)
- {
- if (!name)
- return EFAULT;
- int rc = syscall(SC_set_thread_name, thread, name, strlen(name));
- __RETURN_PTHREAD_ERROR(rc);
- }
- int pthread_getname_np(pthread_t thread, char* buffer, size_t buffer_size)
- {
- int rc = syscall(SC_get_thread_name, thread, buffer, buffer_size);
- __RETURN_PTHREAD_ERROR(rc);
- }
- int pthread_setcancelstate(int state, int* oldstate)
- {
- if (oldstate)
- *oldstate = PTHREAD_CANCEL_DISABLE;
- dbgln("FIXME: Implement pthread_setcancelstate({}, ...)", state);
- if (state != PTHREAD_CANCEL_DISABLE)
- return EINVAL;
- return 0;
- }
- int pthread_setcanceltype(int type, int* oldtype)
- {
- if (oldtype)
- *oldtype = PTHREAD_CANCEL_DEFERRED;
- dbgln("FIXME: Implement pthread_setcanceltype({}, ...)", type);
- if (type != PTHREAD_CANCEL_DEFERRED)
- return EINVAL;
- return 0;
- }
- constexpr static pid_t spinlock_unlock_sentinel = 0;
- int pthread_spin_destroy(pthread_spinlock_t* lock)
- {
- auto current = AK::atomic_load(&lock->m_lock);
- if (current != spinlock_unlock_sentinel)
- return EBUSY;
- return 0;
- }
- int pthread_spin_init(pthread_spinlock_t* lock, [[maybe_unused]] int shared)
- {
- lock->m_lock = spinlock_unlock_sentinel;
- return 0;
- }
- int pthread_spin_lock(pthread_spinlock_t* lock)
- {
- const auto desired = gettid();
- while (true) {
- auto current = AK::atomic_load(&lock->m_lock);
- if (current == desired)
- return EDEADLK;
- if (AK::atomic_compare_exchange_strong(&lock->m_lock, current, desired, AK::MemoryOrder::memory_order_acquire))
- break;
- }
- return 0;
- }
- int pthread_spin_trylock(pthread_spinlock_t* lock)
- {
- // We expect the current value to be unlocked, as the specification
- // states that trylock should lock only if it is not held by ANY thread.
- auto current = spinlock_unlock_sentinel;
- auto desired = gettid();
- if (AK::atomic_compare_exchange_strong(&lock->m_lock, current, desired, AK::MemoryOrder::memory_order_acquire)) {
- return 0;
- } else {
- return EBUSY;
- }
- }
- int pthread_spin_unlock(pthread_spinlock_t* lock)
- {
- auto current = AK::atomic_load(&lock->m_lock);
- if (gettid() != current)
- return EPERM;
- AK::atomic_store(&lock->m_lock, spinlock_unlock_sentinel);
- return 0;
- }
- int pthread_equal(pthread_t t1, pthread_t t2)
- {
- return t1 == t2;
- }
- // FIXME: Use the fancy futex mechanism above to write an rw lock.
- // For the time being, let's just use a less-than-good lock to get things working.
- int pthread_rwlock_destroy(pthread_rwlock_t* rl)
- {
- if (!rl)
- return 0;
- return 0;
- }
- // In a very non-straightforward way, this value is composed of two 32-bit integers
- // the top 32 bits are reserved for the ID of write-locking thread (if any)
- // and the bottom 32 bits are:
- // top 2 bits (30,31): reader wake mask, writer wake mask
- // middle 16 bits: information
- // bit 16: someone is waiting to write
- // bit 17: locked for write
- // bottom 16 bits (0..15): reader count
- constexpr static u32 reader_wake_mask = 1 << 30;
- constexpr static u32 writer_wake_mask = 1 << 31;
- constexpr static u32 writer_locked_mask = 1 << 17;
- constexpr static u32 writer_intent_mask = 1 << 16;
- int pthread_rwlock_init(pthread_rwlock_t* __restrict lockp, const pthread_rwlockattr_t* __restrict attr)
- {
- // Just ignore the attributes. use defaults for now.
- (void)attr;
- // No readers, no writer, not locked at all.
- *lockp = 0;
- return 0;
- }
- // Note that this function does not care about the top 32 bits at all.
- static int rwlock_rdlock_maybe_timed(u32* lockp, const struct timespec* timeout = nullptr, bool only_once = false, int value_if_timeout = -1, int value_if_okay = -2)
- {
- auto current = AK::atomic_load(lockp);
- for (; !only_once;) {
- // First, see if this is locked for writing
- // if it's not, try to add to the counter.
- // If someone is waiting to write, and there is one or no other readers, let them have the lock.
- if (!(current & writer_locked_mask)) {
- auto count = (u16)current;
- if (!(current & writer_intent_mask) || count > 1) {
- ++count;
- auto desired = (current << 16) | count;
- auto did_exchange = AK::atomic_compare_exchange_strong(lockp, current, desired, AK::MemoryOrder::memory_order_acquire);
- if (!did_exchange)
- continue; // tough luck, try again.
- return value_if_okay;
- }
- }
- // If no one else is waiting for the read wake bit, set it.
- if (!(current & reader_wake_mask)) {
- auto desired = current | reader_wake_mask;
- auto did_exchange = AK::atomic_compare_exchange_strong(lockp, current, desired, AK::MemoryOrder::memory_order_acquire);
- if (!did_exchange)
- continue; // Something interesting happened!
- current = desired;
- }
- // Seems like someone is writing (or is interested in writing and we let them have the lock)
- // wait until they're done.
- auto rc = futex(lockp, FUTEX_WAIT_BITSET, current, timeout, nullptr, reader_wake_mask);
- if (rc < 0 && errno == ETIMEDOUT && timeout) {
- return value_if_timeout;
- }
- if (rc < 0 && errno != EAGAIN) {
- // Something broke. let's just bail out.
- return errno;
- }
- errno = 0;
- // Reload the 'current' value
- current = AK::atomic_load(lockp);
- }
- return value_if_timeout;
- }
- static int rwlock_wrlock_maybe_timed(pthread_rwlock_t* lockval_p, const struct timespec* timeout = nullptr, bool only_once = false, int value_if_timeout = -1, int value_if_okay = -2)
- {
- u32* lockp = reinterpret_cast<u32*>(lockval_p);
- auto current = AK::atomic_load(lockp);
- for (; !only_once;) {
- // First, see if this is locked for writing, and if there are any readers.
- // if not, lock it.
- // If someone is waiting to write, let them have the lock.
- if (!(current & writer_locked_mask) && ((u16)current) == 0) {
- if (!(current & writer_intent_mask)) {
- auto desired = current | writer_locked_mask | writer_intent_mask;
- auto did_exchange = AK::atomic_compare_exchange_strong(lockp, current, desired, AK::MemoryOrder::memory_order_acquire);
- if (!did_exchange)
- continue;
- // Now that we've locked the value, it's safe to set our thread ID.
- AK::atomic_store(reinterpret_cast<i32*>(lockval_p) + 1, pthread_self());
- return value_if_okay;
- }
- }
- // That didn't work, if no one else is waiting for the write bit, set it.
- if (!(current & writer_wake_mask)) {
- auto desired = current | writer_wake_mask | writer_intent_mask;
- auto did_exchange = AK::atomic_compare_exchange_strong(lockp, current, desired, AK::MemoryOrder::memory_order_acquire);
- if (!did_exchange)
- continue; // Something interesting happened!
- current = desired;
- }
- // Seems like someone is writing (or is interested in writing and we let them have the lock)
- // wait until they're done.
- auto rc = futex(lockp, FUTEX_WAIT_BITSET, current, timeout, nullptr, writer_wake_mask);
- if (rc < 0 && errno == ETIMEDOUT && timeout) {
- return value_if_timeout;
- }
- if (rc < 0 && errno != EAGAIN) {
- // Something broke. let's just bail out.
- return errno;
- }
- errno = 0;
- // Reload the 'current' value
- current = AK::atomic_load(lockp);
- }
- return value_if_timeout;
- }
- int pthread_rwlock_rdlock(pthread_rwlock_t* lockp)
- {
- if (!lockp)
- return EINVAL;
- return rwlock_rdlock_maybe_timed(reinterpret_cast<u32*>(lockp), nullptr, false, 0, 0);
- }
- int pthread_rwlock_timedrdlock(pthread_rwlock_t* __restrict lockp, const struct timespec* __restrict timespec)
- {
- if (!lockp)
- return EINVAL;
- auto rc = rwlock_rdlock_maybe_timed(reinterpret_cast<u32*>(lockp), timespec);
- if (rc == -1) // "ok"
- return 0;
- if (rc == -2) // "timed out"
- return 1;
- return rc;
- }
- int pthread_rwlock_timedwrlock(pthread_rwlock_t* __restrict lockp, const struct timespec* __restrict timespec)
- {
- if (!lockp)
- return EINVAL;
- auto rc = rwlock_wrlock_maybe_timed(lockp, timespec);
- if (rc == -1) // "ok"
- return 0;
- if (rc == -2) // "timed out"
- return 1;
- return rc;
- }
- int pthread_rwlock_tryrdlock(pthread_rwlock_t* lockp)
- {
- if (!lockp)
- return EINVAL;
- return rwlock_rdlock_maybe_timed(reinterpret_cast<u32*>(lockp), nullptr, true, EBUSY, 0);
- }
- int pthread_rwlock_trywrlock(pthread_rwlock_t* lockp)
- {
- if (!lockp)
- return EINVAL;
- return rwlock_wrlock_maybe_timed(lockp, nullptr, true, EBUSY, 0);
- }
- int pthread_rwlock_unlock(pthread_rwlock_t* lockval_p)
- {
- if (!lockval_p)
- return EINVAL;
- // This is a weird API, we don't really know whether we're unlocking write or read...
- auto lockp = reinterpret_cast<u32*>(lockval_p);
- auto current = AK::atomic_load(lockp, AK::MemoryOrder::memory_order_relaxed);
- if (current & writer_locked_mask) {
- // If this lock is locked for writing, its owner better be us!
- auto owner_id = AK::atomic_load(reinterpret_cast<i32*>(lockval_p) + 1);
- auto my_id = pthread_self();
- if (owner_id != my_id)
- return EINVAL; // you don't own this lock, silly.
- // Now just unlock it.
- auto desired = current & ~(writer_locked_mask | writer_intent_mask);
- AK::atomic_store(lockp, desired, AK::MemoryOrder::memory_order_release);
- // Then wake both readers and writers, if any.
- auto rc = futex(lockp, FUTEX_WAKE_BITSET, current, nullptr, nullptr, (current & writer_wake_mask) | reader_wake_mask);
- if (rc < 0)
- return errno;
- return 0;
- }
- for (;;) {
- auto count = (u16)current;
- if (!count) {
- // Are you crazy? this isn't even locked!
- return EINVAL;
- }
- --count;
- auto desired = (current << 16) | count;
- auto did_exchange = AK::atomic_compare_exchange_strong(lockp, current, desired, AK::MemoryOrder::memory_order_release);
- if (!did_exchange)
- continue; // tough luck, try again.
- }
- // Finally, unlocked at last!
- return 0;
- }
- int pthread_rwlock_wrlock(pthread_rwlock_t* lockp)
- {
- if (!lockp)
- return EINVAL;
- return rwlock_wrlock_maybe_timed(lockp, nullptr, false, 0, 0);
- }
- int pthread_rwlockattr_destroy(pthread_rwlockattr_t*)
- {
- return 0;
- }
- int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t* __restrict, int* __restrict)
- {
- VERIFY_NOT_REACHED();
- }
- int pthread_rwlockattr_init(pthread_rwlockattr_t*)
- {
- VERIFY_NOT_REACHED();
- }
- int pthread_rwlockattr_setpshared(pthread_rwlockattr_t*, int)
- {
- VERIFY_NOT_REACHED();
- }
- int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void))
- {
- if (prepare)
- __pthread_fork_atfork_register_prepare(prepare);
- if (parent)
- __pthread_fork_atfork_register_parent(parent);
- if (child)
- __pthread_fork_atfork_register_child(child);
- return 0;
- }
- } // extern "C"
|