ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/posix_sem.cpp
Revision: 1.9
Committed: 2005-11-22T21:57:59Z (19 years ago) by gbeauche
Branch: MAIN
CVS Tags: nigel-build-19, nigel-build-17
Changes since 1.8: +2 -2 lines
Log Message:
undo unwanted change for now (double check this one later)

File Contents

# Content
1 /*
2 * posix_sem.cpp - POSIX.4 semaphores "emulation"
3 * Copyright (C) 1999 Orlando Bassotto
4 *
5 * Basilisk II (C) 1997-2005 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 <sys/types.h>
29 #include <stdio.h>
30 #include <errno.h>
31 #include <time.h>
32 #include <pthread.h>
33
34 #include "semaphore.h"
35
36 extern "C" {
37
38 int sem_init(sem_t* sem, int pshared, unsigned int value)
39 {
40 if(sem==NULL||value>SEM_VALUE_MAX) {
41 errno = EINVAL;
42 return -1;
43 }
44 if(pshared) {
45 errno = ENOSYS;
46 return -1;
47 }
48 pthread_mutex_init(&sem->sem_lock, NULL);
49 sem->sem_value = value;
50 sem->sem_waiting = 0;
51 return 0;
52 }
53
54
55 int sem_destroy(sem_t* sem)
56 {
57 if(sem==NULL) {
58 errno = EINVAL;
59 return -1;
60 }
61 if(sem->sem_waiting) {
62 errno = EBUSY;
63 return -1;
64 }
65 pthread_mutex_destroy(&sem->sem_lock);
66 sem->sem_waiting = 0;
67 sem->sem_value = 0;
68 return 0;
69 }
70
71 sem_t sem_open(const char* name, int oflag, ...)
72 {
73 errno = ENOSYS;
74 return *(sem_t*)NULL;
75 }
76
77 int sem_close(sem_t* sem)
78 {
79 errno = ENOSYS;
80 return -1;
81 }
82
83 int sem_unlink(const char* name)
84 {
85 errno = ENOSYS;
86 return -1;
87 }
88
89 int sem_wait(sem_t* sem)
90 {
91 struct timespec req = { 1, 0 };
92
93 if(sem==NULL) {
94 errno = EINVAL;
95 return -1;
96 }
97 pthread_mutex_lock(&sem->sem_lock);
98 sem->sem_waiting++;
99 if(sem->sem_value > 0) {
100 --sem->sem_value;
101 return 0;
102 }
103 while(!sem->sem_value) nanosleep(NULL, &req);
104 pthread_mutex_unlock(&sem->sem_lock);
105 return 0;
106 }
107
108 int sem_trywait(sem_t* sem)
109 {
110 errno = ENOSYS;
111 return -1;
112 }
113
114 int sem_post(sem_t* sem)
115 {
116 if(sem==NULL) {
117 errno = EINVAL;
118 return -1;
119 }
120 if(!sem->sem_waiting) {
121 if(sem->sem_value >= SEM_VALUE_MAX) {
122 errno = ERANGE;
123 pthread_mutex_unlock(&sem->sem_lock);
124 return -1;
125 }
126 ++sem->sem_value;
127 pthread_mutex_unlock(&sem->sem_lock);
128 }
129 else {
130 sem->sem_waiting--;
131 ++sem->sem_value;
132 // pthread_mutex_unlock(&sem->sem_lock);
133 }
134 return 0;
135 }
136
137 int sem_getvalue(sem_t* sem, int* sval)
138 {
139 errno = ENOSYS;
140 return -1;
141 }
142
143 }