mirror of
https://github.com/RGBCube/serenity
synced 2025-05-28 08:25:07 +00:00
LibPthread: Implement a basic first pthread mutex
This patch adds these API's: - pthread_mutex_init() - pthread_mutex_lock() - pthread_mutex_unlock() No mutex attributes are supported yet, so we only do the simplest mutex wihout recursive locking.
This commit is contained in:
parent
73d6a69b3f
commit
66a2b582c3
3 changed files with 84 additions and 7 deletions
|
@ -1,7 +1,9 @@
|
|||
#include <AK/Atomic.h>
|
||||
#include <AK/StdLibExtras.h>
|
||||
#include <Kernel/Syscall.h>
|
||||
#include <pthread.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
|
||||
extern "C" {
|
||||
|
||||
|
@ -27,4 +29,31 @@ int pthread_join(pthread_t thread, void** exit_value_ptr)
|
|||
int rc = syscall(SC_join_thread, thread, exit_value_ptr);
|
||||
__RETURN_WITH_ERRNO(rc, rc, -1);
|
||||
}
|
||||
|
||||
int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attributes)
|
||||
{
|
||||
// FIXME: Implement mutex attributes
|
||||
UNUSED_PARAM(attributes);
|
||||
*mutex = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pthread_mutex_lock(pthread_mutex_t* mutex)
|
||||
{
|
||||
auto* atomic = reinterpret_cast<Atomic<u32>*>(mutex);
|
||||
for (;;) {
|
||||
u32 expected = false;
|
||||
if (atomic->compare_exchange_strong(expected, true, AK::memory_order_acq_rel))
|
||||
return 0;
|
||||
sched_yield();
|
||||
}
|
||||
}
|
||||
|
||||
int pthread_mutex_unlock(pthread_mutex_t* mutex)
|
||||
{
|
||||
auto* atomic = reinterpret_cast<Atomic<u32>*>(mutex);
|
||||
atomic->store(false, AK::memory_order_release);
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue