1 | /* Copyright (C) Yuri Dario & 2000 MySQL AB
|
---|
2 | All the above parties has a full, independent copyright to
|
---|
3 | the following code, including the right to use the code in
|
---|
4 | any manner without any demands from the other parties.
|
---|
5 |
|
---|
6 | This library is free software; you can redistribute it and/or
|
---|
7 | modify it under the terms of the GNU Library General Public
|
---|
8 | License as published by the Free Software Foundation; either
|
---|
9 | version 2 of the License, or (at your option) any later version.
|
---|
10 |
|
---|
11 | This library is distributed in the hope that it will be useful,
|
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
14 | Library General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU Library General Public
|
---|
17 | License along with this library; if not, write to the Free
|
---|
18 | Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
|
---|
19 | MA 02111-1307, USA */
|
---|
20 |
|
---|
21 | /*****************************************************************************
|
---|
22 | ** The following is a simple implementation of posix conditions
|
---|
23 | *****************************************************************************/
|
---|
24 |
|
---|
25 | #define INCL_DOS
|
---|
26 | #include <os2.h>
|
---|
27 |
|
---|
28 | #include <process.h>
|
---|
29 | #include <strings.h>
|
---|
30 | #include <sys/timeb.h>
|
---|
31 |
|
---|
32 | //#define DEBUG
|
---|
33 |
|
---|
34 | #include "pthread.h"
|
---|
35 |
|
---|
36 |
|
---|
37 | int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
|
---|
38 | {
|
---|
39 | APIRET rc = 0;
|
---|
40 |
|
---|
41 | cond->waiting = -1;
|
---|
42 | cond->semaphore = -1;
|
---|
43 |
|
---|
44 | /* Warp3 FP29 or Warp4 FP4 or better required */
|
---|
45 | rc = DosCreateEventSem( NULL, (PHEV)&cond->semaphore, 0x0800, 0);
|
---|
46 | if (rc)
|
---|
47 | return ENOMEM;
|
---|
48 |
|
---|
49 | cond->waiting=0;
|
---|
50 |
|
---|
51 | #ifdef DEBUG
|
---|
52 | printf( "pthread_cond_init cond->semaphore %x\n", cond->semaphore);
|
---|
53 | #endif
|
---|
54 |
|
---|
55 | return 0;
|
---|
56 | }
|
---|
57 |
|
---|
58 | int pthread_cond_destroy(pthread_cond_t *cond)
|
---|
59 | {
|
---|
60 | APIRET rc;
|
---|
61 |
|
---|
62 | #ifdef DEBUG
|
---|
63 | printf( "pthread_cond_destroy cond->semaphore %x\n", cond->semaphore);
|
---|
64 | #endif
|
---|
65 |
|
---|
66 | do {
|
---|
67 | rc = DosCloseEventSem(cond->semaphore);
|
---|
68 | if (rc == 301) DosPostEventSem(cond->semaphore);
|
---|
69 | } while (rc == 301);
|
---|
70 | if (rc)
|
---|
71 | return EINVAL;
|
---|
72 |
|
---|
73 | return 0;
|
---|
74 | }
|
---|
75 |
|
---|
76 |
|
---|
77 | int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
|
---|
78 | {
|
---|
79 | APIRET rc;
|
---|
80 | int rval;
|
---|
81 |
|
---|
82 | // initialize static semaphores created with PTHREAD_COND_INITIALIZER state.
|
---|
83 | if (cond->semaphore == -1)
|
---|
84 | pthread_cond_init( cond, NULL);
|
---|
85 |
|
---|
86 | rval = 0;
|
---|
87 | cond->waiting++;
|
---|
88 |
|
---|
89 | #ifdef DEBUG
|
---|
90 | printf( "pthread_cond_wait cond->semaphore %x, cond->waiting %d\n", cond->semaphore, cond->waiting);
|
---|
91 | #endif
|
---|
92 |
|
---|
93 | if (mutex) pthread_mutex_unlock(mutex);
|
---|
94 |
|
---|
95 | rc = DosWaitEventSem(cond->semaphore,SEM_INDEFINITE_WAIT);
|
---|
96 | if (rc != 0)
|
---|
97 | rval = EINVAL;
|
---|
98 |
|
---|
99 | if (mutex) pthread_mutex_lock(mutex);
|
---|
100 |
|
---|
101 | cond->waiting--;
|
---|
102 |
|
---|
103 | return rval;
|
---|
104 | }
|
---|
105 |
|
---|
106 | int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
|
---|
107 | struct timespec const *abstime)
|
---|
108 | {
|
---|
109 | struct timeb curtime;
|
---|
110 | long timeout;
|
---|
111 | APIRET rc;
|
---|
112 | int rval;
|
---|
113 |
|
---|
114 | // initialize static semaphores created with PTHREAD_COND_INITIALIZER state.
|
---|
115 | if (cond->semaphore == -1)
|
---|
116 | pthread_cond_init( cond, NULL);
|
---|
117 |
|
---|
118 | _ftime(&curtime);
|
---|
119 | timeout= ((long) (abstime->tv_sec - curtime.time)*1000L +
|
---|
120 | (long)((abstime->tv_nsec/1000) - curtime.millitm)/1000L);
|
---|
121 | if (timeout < 0) /* Some safety */
|
---|
122 | timeout = 0L;
|
---|
123 |
|
---|
124 | rval = 0;
|
---|
125 | cond->waiting++;
|
---|
126 |
|
---|
127 | #ifdef DEBUG
|
---|
128 | printf( "pthread_cond_timedwait cond->semaphore %x, cond->waiting %d\n", cond->semaphore, cond->waiting);
|
---|
129 | #endif
|
---|
130 |
|
---|
131 | if (mutex) pthread_mutex_unlock(mutex);
|
---|
132 |
|
---|
133 | rc = DosWaitEventSem(cond->semaphore, timeout);
|
---|
134 | if (rc != 0)
|
---|
135 | rval = ETIMEDOUT;
|
---|
136 |
|
---|
137 | if (mutex) pthread_mutex_lock(mutex);
|
---|
138 |
|
---|
139 | cond->waiting--;
|
---|
140 |
|
---|
141 | return rval;
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | int pthread_cond_signal(pthread_cond_t *cond)
|
---|
146 | {
|
---|
147 | APIRET rc;
|
---|
148 |
|
---|
149 | // initialize static semaphores created with PTHREAD_COND_INITIALIZER state.
|
---|
150 | if (cond->semaphore == -1)
|
---|
151 | pthread_cond_init( cond, NULL);
|
---|
152 |
|
---|
153 | /* Bring the next thread off the condition queue: */
|
---|
154 | rc = DosPostEventSem(cond->semaphore);
|
---|
155 | return 0;
|
---|
156 | }
|
---|
157 |
|
---|
158 |
|
---|
159 | int pthread_cond_broadcast(pthread_cond_t *cond)
|
---|
160 | {
|
---|
161 | int i;
|
---|
162 | APIRET rc;
|
---|
163 |
|
---|
164 | // initialize static semaphores created with PTHREAD_COND_INITIALIZER state.
|
---|
165 | if (cond->semaphore == -1)
|
---|
166 | pthread_cond_init( cond, NULL);
|
---|
167 |
|
---|
168 | /*
|
---|
169 | * Enter a loop to bring all threads off the
|
---|
170 | * condition queue:
|
---|
171 | */
|
---|
172 | i = cond->waiting;
|
---|
173 | #ifdef DEBUG
|
---|
174 | printf( "pthread_cond_broadcast cond->semaphore %x, cond->waiting %d\n", cond->semaphore, cond->waiting);
|
---|
175 | #endif
|
---|
176 |
|
---|
177 | while (i--) rc = DosPostEventSem(cond->semaphore);
|
---|
178 |
|
---|
179 | return 0 ;
|
---|
180 | }
|
---|
181 |
|
---|
182 |
|
---|
183 | int pthread_attr_init(pthread_attr_t *connect_att)
|
---|
184 | {
|
---|
185 | connect_att->dwStackSize = 0;
|
---|
186 | connect_att->dwCreatingFlag = 0;
|
---|
187 | connect_att->priority = 0;
|
---|
188 | return 0;
|
---|
189 | }
|
---|
190 |
|
---|
191 | int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize)
|
---|
192 | {
|
---|
193 | if (attr)
|
---|
194 | *stacksize = attr->dwStackSize;
|
---|
195 | else
|
---|
196 | return EINVAL;
|
---|
197 | return 0;
|
---|
198 | }
|
---|
199 |
|
---|
200 | int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
|
---|
201 | {
|
---|
202 | if (attr)
|
---|
203 | attr->dwStackSize = stacksize;
|
---|
204 | else
|
---|
205 | return EINVAL;
|
---|
206 | return 0;
|
---|
207 | }
|
---|
208 |
|
---|
209 | int pthread_attr_setprio(pthread_attr_t *connect_att,int priority)
|
---|
210 | {
|
---|
211 | connect_att->priority=priority;
|
---|
212 | return 0;
|
---|
213 | }
|
---|
214 |
|
---|
215 | int pthread_attr_destroy(pthread_attr_t *connect_att)
|
---|
216 | {
|
---|
217 | bzero( connect_att,sizeof(*connect_att));
|
---|
218 | return 0;
|
---|
219 | }
|
---|