vmm: Remove vthread_attr_kernel_init()
[akaros.git] / user / pthread / semaphore.c
1 #include <parlib/uthread.h>
2 #include <parlib/assert.h>
3 #include <semaphore.h>
4 #include <stdio.h>
5 #include <errno.h>
6
7 int sem_init(sem_t *__sem, int __pshared, unsigned int __value)
8 {
9         if (__pshared == TRUE) {
10                 printf("__pshared functionality of sem_init is not yet implemented!");
11                 return -1;
12         }
13         uth_semaphore_init(&__sem->real_sem, __value);
14         return 0;
15 }
16
17 int sem_destroy(sem_t *__sem)
18 {
19         uth_semaphore_destroy(&__sem->real_sem);
20         return 0;
21 }
22
23 sem_t *sem_open(__const char *__name, int __oflag, ...)
24 {
25         printf("sem_open is not yet implemented!");
26         return NULL;
27 }
28
29 int sem_close(sem_t *__sem)
30 {
31         printf("sem_close is not yet implemented!");
32         return -1;
33 }
34
35 int sem_unlink(__const char *__name)
36 {
37         printf("sem_unlink is not yet implemented!");
38         return -1;
39 }
40
41 int sem_wait(sem_t *__sem)
42 {
43         uth_semaphore_down(&__sem->real_sem);
44         return 0;
45 }
46
47 int sem_timedwait(sem_t *__sem, const struct timespec *abs_timeout)
48 {
49         if (!uth_semaphore_timed_down(&__sem->real_sem, abs_timeout)) {
50                 errno = ETIMEDOUT;
51                 return -1;
52         }
53         return 0;
54 }
55
56 int sem_trywait(sem_t *__sem)
57 {
58         if (!uth_semaphore_trydown(&__sem->real_sem))
59                 return -1;
60         return 0;
61 }
62
63 int sem_post(sem_t *__sem)
64 {
65         uth_semaphore_up(&__sem->real_sem);
66         return 0;
67 }
68
69 int sem_getvalue(sem_t *__restrict __sem, int *__restrict __sval)
70 {
71         spin_pdr_lock(&__sem->real_sem.lock);
72         *__sval = __sem->real_sem.count;
73         spin_pdr_unlock(&__sem->real_sem.lock);
74         return 0;
75 }