Debian Codegraph @

lwip-contrib/ports/unix/sys_arch.2sem.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
25 * OF SUCH DAMAGE.
26 *
27 * This file is part of the lwIP TCP/IP stack.
28 *
29 * Author: Adam Dunkels <[email protected]>
30 *
31 */
32
33 /*
34 * Wed Apr 17 16:05:29 EDT 2002 (James Roth)
35 *
36 * - Fixed an unlikely sys_thread_new() race condition.
37 *
38 * - Made current_thread() work with threads which where
39 * not created with sys_thread_new(). This includes
40 * the main thread and threads made with pthread_create().
41 *
42 * - Catch overflows where more than SYS_MBOX_SIZE messages
43 * are waiting to be read. The sys_mbox_post() routine
44 * will block until there is more room instead of just
45 * leaking messages.
46 */
47 #include "lwip/debug.h"
48
49 #include <string.h>
50 #include <sys/time.h>
51 #include <sys/types.h>
52 #include <stdlib.h>
53 #include <unistd.h>
54 #include <pthread.h>
55
56 #include "lwip/sys.h"
57 #include "lwip/opt.h"
58 #include "lwip/stats.h"
59
60 #define UMAX(a, b) ((a) > (b) ? (a) : (b))
61
62 #define SYS_MBOX_SIZE 128
63
64 struct sys_mbox {
65 int first, last;
66 void *msgs[SYS_MBOX_SIZE];
67 struct sys_sem *notempty;
68 struct sys_sem *notfull;
69 struct sys_sem *mutex;
70 };
71
72 struct sys_sem {
73 unsigned int c;
74 pthread_cond_t cond;
75 pthread_mutex_t mutex;
76 };
77
78 static pthread_mutex_t lwprot_mutex = PTHREAD_MUTEX_INITIALIZER;
79 static pthread_t lwprot_thread = (pthread_t) 0xDEAD;
80 static int lwprot_count = 0;
81
82 static struct sys_sem *sys_sem_new_(u8_t count);
83 static void sys_sem_free_(struct sys_sem *sem);
84
85 static u32_t cond_wait(pthread_cond_t * cond, pthread_mutex_t * mutex,
86 u32_t timeout);
87
88 /*-----------------------------------------------------------------------------------*/
89 sys_thread_t
90 sys_thread_new(void (*function)(void *arg), void *arg, int prio)
91 {
92 int code;
93 pthread_t tmp;
94
95 code = pthread_create(&tmp,
96 NULL,
97 (void *(*)(void *))
98 function,
99 arg);
100
101 if (0 == code) {
102 return tmp;
103 } else
104 return -1;
105 }
106
107 /*-----------------------------------------------------------------------------------*/
108 struct sys_mbox *
109 sys_mbox_new()
110 {
111 struct sys_mbox *mbox;
112
113 mbox = malloc(sizeof(struct sys_mbox));
114 mbox->first = mbox->last = 0;
115 mbox->notempty = sys_sem_new_(0);
116 mbox->notfull = sys_sem_new_(SYS_MBOX_SIZE);
117 mbox->mutex = sys_sem_new_(1);
118
119 #if SYS_STATS
120 lwip_stats.sys.mbox.used++;
121 if (lwip_stats.sys.mbox.used > lwip_stats.sys.mbox.max) {
122 lwip_stats.sys.mbox.max = lwip_stats.sys.mbox.used;
123 }
124 #endif /* SYS_STATS */
125
126 return mbox;
127 }
128 /*-----------------------------------------------------------------------------------*/
129 void
130 sys_mbox_free(struct sys_mbox *mbox)
131 {
132 if (mbox != SYS_MBOX_NULL) {
133 #if SYS_STATS
134 lwip_stats.sys.mbox.used--;
135 #endif /* SYS_STATS */
136
137 sys_sem_free_(mbox->notempty);
138 sys_sem_free_(mbox->notfull);
139 sys_sem_free_(mbox->mutex);
140 /* LWIP_DEBUGF("sys_mbox_free: mbox 0x%lx\n", mbox); */
141 free(mbox);
142 }
143 }
144
145 /*-----------------------------------------------------------------------------------*/
146 void
147 sys_mbox_post(struct sys_mbox *mbox, void *msg)
148 {
149 u8_t first;
150
151 sys_sem_wait(mbox->notfull);
152
153 LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_post: mbox %p msg %p\n", (void *)mbox, (void *)msg));
154
155 sys_sem_wait(mbox->mutex);
156 //printf("sys_mbox_post %p %d %d %d %d\n",mbox,mbox->notfull->c, mbox->notempty->c,
157 //mbox->first, mbox->last);
158 //fflush(stdout);
159 mbox->msgs[mbox->last % SYS_MBOX_SIZE] = msg;
160 mbox->last++;
161 sys_sem_signal(mbox->mutex);
162
163 sys_sem_signal(mbox->notempty);
164 }
165 /*-----------------------------------------------------------------------------------*/
166 u32_t
167 sys_arch_mbox_fetch(struct sys_mbox *mbox, void **msg, u32_t timeout)
168 {
169 u32_t time = 0;
170
171 /* The mutex lock is quick so we don't bother with the timeout
172 stuff here. */
173 //printf("sys_mbox_fetch? %p %d %d\n",mbox,mbox->notfull->c, mbox->notempty->c);
174 //fflush(stdout);
175 time=sys_arch_sem_wait(mbox->notempty, timeout);
176 if ( timeout != 0 && time == SYS_ARCH_TIMEOUT) {
177 return SYS_ARCH_TIMEOUT;
178 }
179
180 sys_sem_wait(mbox->mutex);
181 //printf("sys_mbox_fetch %p %d %d %d %d\n",mbox,mbox->notfull->c, mbox->notempty->c,
182 //mbox->first, mbox->last);
183 //fflush(stdout);
184 if (msg != NULL) {
185 LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_fetch: mbox %p msg %p\n", (void *)mbox, *msg));
186 *msg = mbox->msgs[mbox->first % SYS_MBOX_SIZE];
187 }
188 else{
189 LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_fetch: mbox %p, null msg\n", (void *)mbox));
190 }
191
192 mbox->first++;
193 sys_sem_signal(mbox->mutex);
194 sys_sem_signal(mbox->notfull);
195
196 return time;
197 }
198 /*-----------------------------------------------------------------------------------*/
199 struct sys_sem *
200 sys_sem_new(u8_t count)
201 {
202 #if SYS_STATS
203 lwip_stats.sys.sem.used++;
204 if (lwip_stats.sys.sem.used > lwip_stats.sys.sem.max) {
205 lwip_stats.sys.sem.max = lwip_stats.sys.sem.used;
206 }
207 #endif /* SYS_STATS */
208 return sys_sem_new_(count);
209 }
210
211 /*-----------------------------------------------------------------------------------*/
212 static struct sys_sem *
213 sys_sem_new_(u8_t count)
214 {
215 struct sys_sem *sem;
216
217 sem = malloc(sizeof(struct sys_sem));
218 sem->c = count;
219
220 pthread_cond_init(&(sem->cond), NULL);
221 pthread_mutex_init(&(sem->mutex), NULL);
222
223 return sem;
224 }
225
226 /*-----------------------------------------------------------------------------------*/
227 static u32_t
228 cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex, u32_t timeout)
229 {
230 int tdiff;
231 unsigned long sec, usec;
232 struct timeval rtime1, rtime2;
233 struct timespec ts;
234 struct timezone tz;
235 int retval;
236
237 if (timeout > 0) {
238 /* Get a timestamp and add the timeout value. */
239 gettimeofday(&rtime1, &tz);
240 sec = rtime1.tv_sec;
241 usec = rtime1.tv_usec;
242 usec += timeout % 1000 * 1000;
243 sec += (int)(timeout / 1000) + (int)(usec / 1000000);
244 usec = usec % 1000000;
245 ts.tv_nsec = usec * 1000;
246 ts.tv_sec = sec;
247
248 retval = pthread_cond_timedwait(cond, mutex, &ts);
249
250 if (retval == ETIMEDOUT) {
251 return SYS_ARCH_TIMEOUT;
252 } else {
253 /* Calculate for how long we waited for the cond. */
254 gettimeofday(&rtime2, &tz);
255 tdiff = (rtime2.tv_sec - rtime1.tv_sec) * 1000 +
256 (rtime2.tv_usec - rtime1.tv_usec) / 1000;
257
258 if (tdiff <= 0) {
259 return 0;
260 }
261
262 return tdiff;
263 }
264 } else {
265 pthread_cond_wait(cond, mutex);
266 return SYS_ARCH_TIMEOUT;
267 }
268 }
269 /*-----------------------------------------------------------------------------------*/
270 u32_t
271 sys_arch_sem_wait(struct sys_sem *sem, u32_t timeout)
272 {
273 u32_t time = 0;
274
275 pthread_mutex_lock(&(sem->mutex));
276 while (sem->c <= 0) {
277 if (timeout > 0) {
278 time = cond_wait(&(sem->cond), &(sem->mutex), timeout);
279
280 if (time == SYS_ARCH_TIMEOUT) {
281 pthread_mutex_unlock(&(sem->mutex));
282 return SYS_ARCH_TIMEOUT;
283 }
284 /* pthread_mutex_unlock(&(sem->mutex));
285 return time; */
286 } else {
287 cond_wait(&(sem->cond), &(sem->mutex), 0);
288 }
289 }
290 sem->c--;
291 pthread_mutex_unlock(&(sem->mutex));
292 return time;
293 }
294 /*-----------------------------------------------------------------------------------*/
295 void
296 sys_sem_signal(struct sys_sem *sem)
297 {
298 pthread_mutex_lock(&(sem->mutex));
299 sem->c++;
300
301 /*
302 if (sem->c > 1) {
303 sem->c = 1;
304 }
305 */
306
307 pthread_cond_broadcast(&(sem->cond));
308 pthread_mutex_unlock(&(sem->mutex));
309 }
310 /*-----------------------------------------------------------------------------------*/
311 void
312 sys_sem_free(struct sys_sem *sem)
313 {
314 if (sem != SYS_SEM_NULL) {
315 #if SYS_STATS
316 lwip_stats.sys.sem.used--;
317 #endif /* SYS_STATS */
318 sys_sem_free_(sem);
319 }
320 }
321
322 /*-----------------------------------------------------------------------------------*/
323 static void
324 sys_sem_free_(struct sys_sem *sem)
325 {
326 pthread_cond_destroy(&(sem->cond));
327 pthread_mutex_destroy(&(sem->mutex));
328 free(sem);
329 }
330 /*-----------------------------------------------------------------------------------*/
331 unsigned long
332 time_now()
333 {
334 struct timeval tv;
335 struct timezone tz;
336 gettimeofday(&tv, &tz);
337
338 return tv.tv_sec;
339 }
340
341 /*-----------------------------------------------------------------------------------*/
342 void
343 sys_init()
344 {
345 }
346 /*-----------------------------------------------------------------------------------*/
347 static pthread_key_t key;
348 static pthread_once_t key_once = PTHREAD_ONCE_INIT;
349
350 static void del_key(void *ptr)
351 {
352 free(ptr);
353 }
354
355 static void
356 make_key()
357 {
358 //printf("new_key %p\n",pthread_self());
359 (void) pthread_key_create(&key, del_key);
360 }
361
362 struct sys_timeouts *
363 sys_arch_timeouts(void)
364 {
365 struct sys_timeouts *ptr;
366
367 (void) pthread_once(&key_once, make_key);
368 if ((ptr = pthread_getspecific(key)) == NULL) {
369 ptr = malloc(sizeof(struct sys_timeouts));
370 ptr->next=NULL;
371 (void) pthread_setspecific(key, ptr);
372 }
373
374 return ptr;
375 }
376
377 /*-----------------------------------------------------------------------------------*/
378 /** sys_prot_t sys_arch_protect(void)
379
380 This optional function does a "fast" critical region protection and returns
381 the previous protection level. This function is only called during very short
382 critical regions. An embedded system which supports ISR-based drivers might
383 want to implement this function by disabling interrupts. Task-based systems
384 might want to implement this by using a mutex or disabling tasking. This
385 function should support recursive calls from the same task or interrupt. In
386 other words, sys_arch_protect() could be called while already protected. In
387 that case the return value indicates that it is already protected.
388
389 sys_arch_protect() is only required if your port is supporting an operating
390 system.
391 */
392 sys_prot_t
393 sys_arch_protect(void)
394 {
395 /* Note that for the UNIX port, we are using a lightweight mutex, and our
396 * own counter (which is locked by the mutex). The return code is not actually
397 * used. */
398 if (lwprot_thread != pthread_self())
399 {
400 /* We are locking the mutex where it has not been locked before *
401 * or is being locked by another thread */
402 pthread_mutex_lock(&lwprot_mutex);
403 lwprot_thread = pthread_self();
404 lwprot_count = 1;
405 }
406 else
407 /* It is already locked by THIS thread */
408 lwprot_count++;
409 return 0;
410 }
411 /*-----------------------------------------------------------------------------------*/
412 /** void sys_arch_unprotect(sys_prot_t pval)
413
414 This optional function does a "fast" set of critical region protection to the
415 value specified by pval. See the documentation for sys_arch_protect() for
416 more information. This function is only required if your port is supporting
417 an operating system.
418 */
419 void
420 sys_arch_unprotect(sys_prot_t pval)
421 {
422 if (lwprot_thread == pthread_self())
423 {
424 if (--lwprot_count == 0)
425 {
426 lwprot_thread = (pthread_t) 0xDEAD;
427 pthread_mutex_unlock(&lwprot_mutex);
428 }
429 }
430 }
431
432 /*-----------------------------------------------------------------------------------*/
433
434 #ifndef MAX_JIFFY_OFFSET
435 #define MAX_JIFFY_OFFSET ((~0UL >> 1)-1)
436 #endif
437
438 #ifndef HZ
439 #define HZ 100
440 #endif
441
442 unsigned long
443 sys_jiffies(void)
444 {
445 struct timeval tv;
446 unsigned long sec = tv.tv_sec;
447 long usec = tv.tv_usec;
448
449 gettimeofday(&tv,NULL);
450
451 if (sec >= (MAX_JIFFY_OFFSET / HZ))
452 return MAX_JIFFY_OFFSET;
453 usec += 1000000L / HZ - 1;
454 usec /= 1000000L / HZ;
455 return HZ * sec + usec;
456 }
457
458 #if PPP_DEBUG
459
460 #include <stdarg.h>
461
462 void ppp_trace(int level, const char *format, ...)
463 {
464 va_list args;
465
466 (void)level;
467 va_start(args, format);
468 vprintf(format, args);
469 va_end(args);
470 }
471 #endif