kernel: Use a mutex to allocate new posix timers for a thread

This commit is contained in:
apio 2024-11-23 20:02:34 +01:00
parent 9e65131452
commit 7b2977a036
Signed by: apio
GPG Key ID: B8A7D06E42258954
3 changed files with 9 additions and 3 deletions

View File

@ -118,7 +118,6 @@ Result<u64> sys_timer_create(Registers*, SyscallArgs args)
if (ksevp.sigev_signo <= 0 || ksevp.sigev_signo > NSIG) return err(EINVAL);
int id = TRY(current->allocate_timerid());
current->posix_timers[id] = Timer {};
Timer* timer = current->posix_timers[id].value_ptr();
timer->signo = ksevp.sigev_signo;

View File

@ -61,10 +61,15 @@ Result<FileDescriptor*> Thread::resolve_fd(int fd)
Result<int> Thread::allocate_timerid()
{
ScopedMutexLock lock(posix_timer_mutex);
for (int i = 0; i < MAX_POSIX_TIMERS; i++)
{
// FIXME: Possible race condition, this should be used alongside a mutex.
if (!posix_timers[i].has_value()) { return i; }
if (!posix_timers[i].has_value())
{
posix_timers[i] = Timer {};
return i;
}
}
return err(EMFILE);

View File

@ -3,6 +3,7 @@
#include "arch/MMU.h"
#include "fs/OpenFileDescription.h"
#include "fs/VFS.h"
#include "lib/Mutex.h"
#include "memory/AddressSpace.h"
#include <bits/signal.h>
#include <luna/Bitset.h>
@ -109,6 +110,7 @@ struct Thread : public LinkedListNode<Thread>
Clock profiling_clock;
Option<Timer> posix_timers[MAX_POSIX_TIMERS];
Mutex posix_timer_mutex;
Result<int> allocate_timerid();
Result<Timer*> resolve_timerid(int id);