Skip to content

Commit

Permalink
LibPthread: Support process-shared semaphores
Browse files Browse the repository at this point in the history
  • Loading branch information
IdanHo authored and awesomekling committed Jul 21, 2022
1 parent 3f83876 commit 35789f5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
15 changes: 6 additions & 9 deletions Userland/Libraries/LibC/semaphore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,16 @@ int sem_unlink(char const*)
}

// https://pubs.opengroup.org/onlinepubs/9699919799/functions/sem_init.html
int sem_init(sem_t* sem, int shared, unsigned int value)
int sem_init(sem_t* sem, int process_shared, unsigned int value)
{
if (shared) {
errno = ENOSYS;
return -1;
}

if (value > SEM_VALUE_MAX) {
errno = EINVAL;
return -1;
}

sem->magic = SEM_MAGIC;
sem->value = value;
sem->flags = process_shared ? SEM_FLAG_PROCESS_SHARED : 0;
return 0;
}

Expand Down Expand Up @@ -102,7 +98,7 @@ int sem_post(sem_t* sem)
// Check if another sem_post() call has handled it already.
if (!(value & POST_WAKES)) [[likely]]
return 0;
int rc = futex_wake(&sem->value, 1, false);
int rc = futex_wake(&sem->value, 1, sem->flags & SEM_FLAG_PROCESS_SHARED);
VERIFY(rc >= 0);
return 0;
}
Expand Down Expand Up @@ -153,6 +149,7 @@ int sem_timedwait(sem_t* sem, const struct timespec* abstime)

u32 value = AK::atomic_load(&sem->value, AK::memory_order_relaxed);
bool responsible_for_waking = false;
bool process_shared = sem->flags & SEM_FLAG_PROCESS_SHARED;

while (true) {
u32 count = value & ~POST_WAKES;
Expand All @@ -179,7 +176,7 @@ int sem_timedwait(sem_t* sem, const struct timespec* abstime)
// Re-evaluate.
continue;
if (going_to_wake) [[unlikely]] {
int rc = futex_wake(&sem->value, count - 1, false);
int rc = futex_wake(&sem->value, count - 1, process_shared);
VERIFY(rc >= 0);
}
return 0;
Expand All @@ -196,7 +193,7 @@ int sem_timedwait(sem_t* sem, const struct timespec* abstime)
}
// At this point, we're committed to sleeping.
responsible_for_waking = true;
futex_wait(&sem->value, value, abstime, CLOCK_REALTIME, false);
futex_wait(&sem->value, value, abstime, CLOCK_REALTIME, process_shared);
// This is the state we will probably see upon being waked:
value = 1;
}
Expand Down
2 changes: 2 additions & 0 deletions Userland/Libraries/LibC/semaphore.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@

__BEGIN_DECLS

#define SEM_FLAG_PROCESS_SHARED (1 << 0)
typedef struct {
uint32_t magic;
uint32_t value;
uint8_t flags;
} sem_t;

int sem_close(sem_t*);
Expand Down

0 comments on commit 35789f5

Please sign in to comment.