15142c2acSIngo Weinhold /* 2d6d439f3SHamish Morrison * Copyright 2015, Hamish Morrison, hamishm53@gmail.com. 324df6592SIngo Weinhold * Copyright 2008-2011, Ingo Weinhold, ingo_weinhold@gmx.de. 45142c2acSIngo Weinhold * Distributed under the terms of the MIT License. 55142c2acSIngo Weinhold */ 65142c2acSIngo Weinhold 75142c2acSIngo Weinhold #include <semaphore.h> 85142c2acSIngo Weinhold 95142c2acSIngo Weinhold #include <errno.h> 105142c2acSIngo Weinhold #include <fcntl.h> 115142c2acSIngo Weinhold #include <stdarg.h> 125142c2acSIngo Weinhold #include <stdlib.h> 13*f1fafe31SAugustin Cavalier #include <pthread.h> 145142c2acSIngo Weinhold 155142c2acSIngo Weinhold #include <OS.h> 165142c2acSIngo Weinhold 175142c2acSIngo Weinhold #include <AutoDeleter.h> 18ae901935SOliver Tappe #include <errno_private.h> 196b202f4eSIngo Weinhold #include <posix/realtime_sem_defs.h> 205142c2acSIngo Weinhold #include <syscall_utils.h> 215142c2acSIngo Weinhold #include <syscalls.h> 22d6d439f3SHamish Morrison #include <user_mutex_defs.h> 23d6d439f3SHamish Morrison 24d6d439f3SHamish Morrison 25d6d439f3SHamish Morrison #define SEM_TYPE_NAMED 1 26d6d439f3SHamish Morrison #define SEM_TYPE_UNNAMED 2 27d6d439f3SHamish Morrison 28d6d439f3SHamish Morrison 29d6d439f3SHamish Morrison static int32 30d6d439f3SHamish Morrison atomic_add_if_greater(int32* value, int32 amount, int32 testValue) 31d6d439f3SHamish Morrison { 32d6d439f3SHamish Morrison int32 current = atomic_get(value); 33d6d439f3SHamish Morrison while (current > testValue) { 34d6d439f3SHamish Morrison int32 old = atomic_test_and_set(value, current + amount, current); 35d6d439f3SHamish Morrison if (old == current) 36d6d439f3SHamish Morrison return old; 37d6d439f3SHamish Morrison current = old; 38d6d439f3SHamish Morrison } 39d6d439f3SHamish Morrison return current; 40d6d439f3SHamish Morrison } 415142c2acSIngo Weinhold 425142c2acSIngo Weinhold 435142c2acSIngo Weinhold sem_t* 445142c2acSIngo Weinhold sem_open(const char* name, int openFlags,...) 455142c2acSIngo Weinhold { 465142c2acSIngo Weinhold if (name == NULL) { 47ae901935SOliver Tappe __set_errno(B_BAD_VALUE); 485142c2acSIngo Weinhold return SEM_FAILED; 495142c2acSIngo Weinhold } 505142c2acSIngo Weinhold 515142c2acSIngo Weinhold // get the mode and semaphore count parameters, if O_CREAT is specified 525142c2acSIngo Weinhold mode_t mode = 0; 535142c2acSIngo Weinhold unsigned semCount = 0; 545142c2acSIngo Weinhold 555142c2acSIngo Weinhold if ((openFlags & O_CREAT) != 0) { 565142c2acSIngo Weinhold va_list args; 575142c2acSIngo Weinhold va_start(args, openFlags); 585142c2acSIngo Weinhold mode = va_arg(args, mode_t); 595142c2acSIngo Weinhold semCount = va_arg(args, unsigned); 605142c2acSIngo Weinhold va_end(args); 61e8f1b18eSIngo Weinhold } else { 62e8f1b18eSIngo Weinhold // clear O_EXCL, if O_CREAT is not given 63e8f1b18eSIngo Weinhold openFlags &= ~O_EXCL; 645142c2acSIngo Weinhold } 655142c2acSIngo Weinhold 665142c2acSIngo Weinhold // Allocate a sem_t structure -- we don't know, whether this is the first 675142c2acSIngo Weinhold // call of this process to open the semaphore. If it is, we will keep the 685142c2acSIngo Weinhold // structure, otherwise we will delete it later. 695142c2acSIngo Weinhold sem_t* sem = (sem_t*)malloc(sizeof(sem_t)); 705142c2acSIngo Weinhold if (sem == NULL) { 71ae901935SOliver Tappe __set_errno(B_NO_MEMORY); 725142c2acSIngo Weinhold return SEM_FAILED; 735142c2acSIngo Weinhold } 74d6d439f3SHamish Morrison 75d6d439f3SHamish Morrison sem->type = SEM_TYPE_NAMED; 765142c2acSIngo Weinhold MemoryDeleter semDeleter(sem); 775142c2acSIngo Weinhold 785142c2acSIngo Weinhold // ask the kernel to open the semaphore 795142c2acSIngo Weinhold sem_t* usedSem; 805142c2acSIngo Weinhold status_t error = _kern_realtime_sem_open(name, openFlags, mode, semCount, 815142c2acSIngo Weinhold sem, &usedSem); 825142c2acSIngo Weinhold if (error != B_OK) { 83ae901935SOliver Tappe __set_errno(error); 845142c2acSIngo Weinhold return SEM_FAILED; 855142c2acSIngo Weinhold } 865142c2acSIngo Weinhold 875142c2acSIngo Weinhold if (usedSem == sem) 885142c2acSIngo Weinhold semDeleter.Detach(); 895142c2acSIngo Weinhold 905142c2acSIngo Weinhold return usedSem; 915142c2acSIngo Weinhold } 925142c2acSIngo Weinhold 935142c2acSIngo Weinhold 945142c2acSIngo Weinhold int 955142c2acSIngo Weinhold sem_close(sem_t* semaphore) 965142c2acSIngo Weinhold { 975142c2acSIngo Weinhold sem_t* deleteSem = NULL; 98d6d439f3SHamish Morrison status_t error = _kern_realtime_sem_close(semaphore->u.named_sem_id, 99d6d439f3SHamish Morrison &deleteSem); 1005142c2acSIngo Weinhold if (error == B_OK) 1015142c2acSIngo Weinhold free(deleteSem); 1025142c2acSIngo Weinhold 1035142c2acSIngo Weinhold RETURN_AND_SET_ERRNO(error); 1045142c2acSIngo Weinhold } 1055142c2acSIngo Weinhold 1065142c2acSIngo Weinhold 1075142c2acSIngo Weinhold int 1085142c2acSIngo Weinhold sem_unlink(const char* name) 1095142c2acSIngo Weinhold { 1105142c2acSIngo Weinhold RETURN_AND_SET_ERRNO(_kern_realtime_sem_unlink(name)); 1115142c2acSIngo Weinhold } 1125142c2acSIngo Weinhold 1135142c2acSIngo Weinhold 1145142c2acSIngo Weinhold int 1155142c2acSIngo Weinhold sem_init(sem_t* semaphore, int shared, unsigned value) 1165142c2acSIngo Weinhold { 117d6d439f3SHamish Morrison semaphore->type = SEM_TYPE_UNNAMED; 118d6d439f3SHamish Morrison semaphore->u.unnamed_sem = value; 119d6d439f3SHamish Morrison return 0; 1205142c2acSIngo Weinhold } 1215142c2acSIngo Weinhold 1225142c2acSIngo Weinhold 1235142c2acSIngo Weinhold int 1245142c2acSIngo Weinhold sem_destroy(sem_t* semaphore) 1255142c2acSIngo Weinhold { 126d6d439f3SHamish Morrison if (semaphore->type != SEM_TYPE_UNNAMED) 127d6d439f3SHamish Morrison RETURN_AND_SET_ERRNO(EINVAL); 128d6d439f3SHamish Morrison 129d6d439f3SHamish Morrison return 0; 130d6d439f3SHamish Morrison } 131d6d439f3SHamish Morrison 132d6d439f3SHamish Morrison 133d6d439f3SHamish Morrison static int 134d6d439f3SHamish Morrison unnamed_sem_post(sem_t* semaphore) { 135d6d439f3SHamish Morrison int32* sem = (int32*)&semaphore->u.unnamed_sem; 136d6d439f3SHamish Morrison int32 oldValue = atomic_add_if_greater(sem, 1, -1); 137d6d439f3SHamish Morrison if (oldValue > -1) 138d6d439f3SHamish Morrison return 0; 139d6d439f3SHamish Morrison 140d6d439f3SHamish Morrison return _kern_mutex_sem_release(sem); 141d6d439f3SHamish Morrison } 142d6d439f3SHamish Morrison 143d6d439f3SHamish Morrison 144d6d439f3SHamish Morrison static int 145d6d439f3SHamish Morrison unnamed_sem_trywait(sem_t* semaphore) { 146d6d439f3SHamish Morrison int32* sem = (int32*)&semaphore->u.unnamed_sem; 147d6d439f3SHamish Morrison int32 oldValue = atomic_add_if_greater(sem, -1, 0); 148d6d439f3SHamish Morrison if (oldValue > 0) 149d6d439f3SHamish Morrison return 0; 150d6d439f3SHamish Morrison 151d6d439f3SHamish Morrison return EAGAIN; 152d6d439f3SHamish Morrison } 153d6d439f3SHamish Morrison 154d6d439f3SHamish Morrison 155d6d439f3SHamish Morrison static int 156d6d439f3SHamish Morrison unnamed_sem_timedwait(sem_t* semaphore, const struct timespec* timeout) { 157d6d439f3SHamish Morrison int32* sem = (int32*)&semaphore->u.unnamed_sem; 158d6d439f3SHamish Morrison 159d6d439f3SHamish Morrison bigtime_t timeoutMicros = B_INFINITE_TIMEOUT; 160d6d439f3SHamish Morrison if (timeout != NULL) { 161d6d439f3SHamish Morrison timeoutMicros = ((bigtime_t)timeout->tv_sec) * 1000000 162d6d439f3SHamish Morrison + timeout->tv_nsec / 1000; 163d6d439f3SHamish Morrison } 164d6d439f3SHamish Morrison 165d6d439f3SHamish Morrison int result = unnamed_sem_trywait(semaphore); 166d6d439f3SHamish Morrison if (result == 0) 167d6d439f3SHamish Morrison return 0; 168d6d439f3SHamish Morrison 169d6d439f3SHamish Morrison return _kern_mutex_sem_acquire(sem, NULL, 170d6d439f3SHamish Morrison timeoutMicros == B_INFINITE_TIMEOUT ? 0 : B_ABSOLUTE_REAL_TIME_TIMEOUT, 171d6d439f3SHamish Morrison timeoutMicros); 1725142c2acSIngo Weinhold } 1735142c2acSIngo Weinhold 1745142c2acSIngo Weinhold 1755142c2acSIngo Weinhold int 1765142c2acSIngo Weinhold sem_post(sem_t* semaphore) 1775142c2acSIngo Weinhold { 178d6d439f3SHamish Morrison status_t error; 179d6d439f3SHamish Morrison if (semaphore->type == SEM_TYPE_NAMED) 180d6d439f3SHamish Morrison error = _kern_realtime_sem_post(semaphore->u.named_sem_id); 181d6d439f3SHamish Morrison else 182d6d439f3SHamish Morrison error = unnamed_sem_post(semaphore); 183d6d439f3SHamish Morrison 184d6d439f3SHamish Morrison RETURN_AND_SET_ERRNO(error); 1855142c2acSIngo Weinhold } 1865142c2acSIngo Weinhold 1875142c2acSIngo Weinhold 188d6d439f3SHamish Morrison static int 189d6d439f3SHamish Morrison named_sem_timedwait(sem_t* semaphore, const struct timespec* timeout) 1905142c2acSIngo Weinhold { 191f1a3e05dSJérôme Duval if (timeout != NULL 192f1a3e05dSJérôme Duval && (timeout->tv_nsec < 0 || timeout->tv_nsec >= 1000000000)) { 193d6d439f3SHamish Morrison status_t err = _kern_realtime_sem_wait(semaphore->u.named_sem_id, 0); 1943cdae651SSam Toyer if (err == B_WOULD_BLOCK) 1953cdae651SSam Toyer err = EINVAL; 1963cdae651SSam Toyer // do nothing, return err as it is. 197d6d439f3SHamish Morrison return err; 1983cdae651SSam Toyer } 1995142c2acSIngo Weinhold 2003cdae651SSam Toyer bigtime_t timeoutMicros = B_INFINITE_TIMEOUT; 2013cdae651SSam Toyer if (timeout != NULL) { 2023cdae651SSam Toyer timeoutMicros = ((bigtime_t)timeout->tv_sec) * 1000000 2033cdae651SSam Toyer + timeout->tv_nsec / 1000; 2043cdae651SSam Toyer } 205d6d439f3SHamish Morrison status_t err = _kern_realtime_sem_wait(semaphore->u.named_sem_id, 206d6d439f3SHamish Morrison timeoutMicros); 2073cdae651SSam Toyer if (err == B_WOULD_BLOCK) 2083cdae651SSam Toyer err = ETIMEDOUT; 2093cdae651SSam Toyer 210d6d439f3SHamish Morrison return err; 2115142c2acSIngo Weinhold } 2125142c2acSIngo Weinhold 2135142c2acSIngo Weinhold 2145142c2acSIngo Weinhold int 2155142c2acSIngo Weinhold sem_trywait(sem_t* semaphore) 2165142c2acSIngo Weinhold { 217d6d439f3SHamish Morrison status_t error; 218d6d439f3SHamish Morrison if (semaphore->type == SEM_TYPE_NAMED) 219d6d439f3SHamish Morrison error = _kern_realtime_sem_wait(semaphore->u.named_sem_id, 0); 220d6d439f3SHamish Morrison else 221d6d439f3SHamish Morrison error = unnamed_sem_trywait(semaphore); 222d6d439f3SHamish Morrison 223d6d439f3SHamish Morrison RETURN_AND_SET_ERRNO(error); 2245142c2acSIngo Weinhold } 2255142c2acSIngo Weinhold 2265142c2acSIngo Weinhold 2275142c2acSIngo Weinhold int 2285142c2acSIngo Weinhold sem_wait(sem_t* semaphore) 2295142c2acSIngo Weinhold { 230d6d439f3SHamish Morrison status_t error; 231d6d439f3SHamish Morrison if (semaphore->type == SEM_TYPE_NAMED) 232d6d439f3SHamish Morrison error = named_sem_timedwait(semaphore, NULL); 233d6d439f3SHamish Morrison else 234d6d439f3SHamish Morrison error = unnamed_sem_timedwait(semaphore, NULL); 235d6d439f3SHamish Morrison 236d6d439f3SHamish Morrison RETURN_AND_SET_ERRNO_TEST_CANCEL(error); 237d6d439f3SHamish Morrison } 238d6d439f3SHamish Morrison 239d6d439f3SHamish Morrison 240d6d439f3SHamish Morrison int 241d6d439f3SHamish Morrison sem_timedwait(sem_t* semaphore, const struct timespec* timeout) 242d6d439f3SHamish Morrison { 243d6d439f3SHamish Morrison status_t error; 244d6d439f3SHamish Morrison if (semaphore->type == SEM_TYPE_NAMED) 245d6d439f3SHamish Morrison error = named_sem_timedwait(semaphore, timeout); 246d6d439f3SHamish Morrison else 247d6d439f3SHamish Morrison error = unnamed_sem_timedwait(semaphore, timeout); 248d6d439f3SHamish Morrison 249d6d439f3SHamish Morrison RETURN_AND_SET_ERRNO_TEST_CANCEL(error); 2505142c2acSIngo Weinhold } 2515142c2acSIngo Weinhold 2525142c2acSIngo Weinhold 2535142c2acSIngo Weinhold int 2545142c2acSIngo Weinhold sem_getvalue(sem_t* semaphore, int* value) 2555142c2acSIngo Weinhold { 256d6d439f3SHamish Morrison if (semaphore->type == SEM_TYPE_NAMED) { 257d6d439f3SHamish Morrison RETURN_AND_SET_ERRNO(_kern_realtime_sem_get_value( 258d6d439f3SHamish Morrison semaphore->u.named_sem_id, value)); 259d6d439f3SHamish Morrison } else { 260d6d439f3SHamish Morrison *value = semaphore->u.unnamed_sem < 0 ? 0 : semaphore->u.unnamed_sem; 261d6d439f3SHamish Morrison return 0; 262d6d439f3SHamish Morrison } 2635142c2acSIngo Weinhold } 264