ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/posix_sem.cpp
Revision: 1.1
Committed: 1999-10-03T14:16:25Z (24 years, 9 months ago) by cebix
Branch: MAIN
Branch point for: cebix
Log Message:
Initial revision

File Contents

# Content
1 /*
2 * posix_sem.cpp - POSIX.4 semaphores "emulation"
3 * Copyright (C) 1999 Orlando Bassotto
4 *
5 * Basilisk II (C) 1997-1999 Christian Bauer
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 /*
23 * OK, I had really big problems with SysV semaphores :/
24 * I rewrote those one giving a look to the source of linuxthreads
25 * with mutex. Seems to be working correctly now.
26 */
27
28 #include <stdio.h>
29 #include <sys/types.h>
30 #include <errno.h>
31 #include <pthread.h>
32 #include "semaphore.h"
33
34 extern "C" {
35
36 int sem_init(sem_t* sem, int pshared, unsigned int value)
37 {
38 if(sem==NULL||value>SEM_VALUE_MAX) {
39 errno = EINVAL;
40 return -1;
41 }
42 if(pshared) {
43 errno = ENOSYS;
44 return -1;
45 }
46 pthread_mutex_init(&sem->sem_lock, NULL);
47 sem->sem_value = value;
48 sem->sem_waiting = 0;
49 return 0;
50 }
51
52
53 int sem_destroy(sem_t* sem)
54 {
55 if(sem==NULL) {
56 errno = EINVAL;
57 return -1;
58 }
59 if(sem->sem_waiting) {
60 errno = EBUSY;
61 return -1;
62 }
63 pthread_mutex_destroy(&sem->sem_lock);
64 sem->sem_waiting = 0;
65 sem->sem_lock = NULL;
66 sem->sem_value = 0;
67 return 0;
68 }
69
70 sem_t sem_open(const char* name, int oflag, ...)
71 {
72 errno = ENOSYS;
73 return *(sem_t*)NULL;
74 }
75
76 int sem_close(sem_t* sem)
77 {
78 errno = ENOSYS;
79 return -1;
80 }
81
82 int sem_unlink(const char* name)
83 {
84 errno = ENOSYS;
85 return -1;
86 }
87
88 int sem_wait(sem_t* sem)
89 {
90 struct timespec req = { 1, 0 };
91
92 if(sem==NULL) {
93 errno = EINVAL;
94 return -1;
95 }
96 pthread_mutex_lock(&sem->sem_lock);
97 sem->sem_waiting++;
98 if(sem->sem_value > 0) {
99 --sem->sem_value;
100 return 0;
101 }
102 while(!sem->sem_value) nanosleep(NULL, &req);
103 pthread_mutex_unlock(&sem->sem_lock);
104 return 0;
105 }
106
107 int sem_trywait(sem_t* sem)
108 {
109 errno = ENOSYS;
110 return -1;
111 }
112
113 int sem_post(sem_t* sem)
114 {
115 if(sem==NULL) {
116 errno = EINVAL;
117 return -1;
118 }
119 if(!sem->sem_waiting) {
120 if(sem->sem_value >= SEM_VALUE_MAX) {
121 errno = ERANGE;
122 pthread_mutex_unlock(&sem->sem_lock);
123 return -1;
124 }
125 ++sem->sem_value;
126 pthread_mutex_unlock(&sem->sem_lock);
127 }
128 else {
129 sem->sem_waiting--;
130 ++sem->sem_value;
131 // pthread_mutex_unlock(&sem->sem_lock);
132 }
133 return 0;
134 }
135
136 int sem_getvalue(sem_t* sem, int* sval)
137 {
138 errno = ENOSYS;
139 return -1;
140 }
141
142 }