Debian Codegraph @

lwip-contrib/ports/unix/sys_arch.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 static struct sys_thread *threads = NULL;
63 static pthread_mutex_t threads_mutex = PTHREAD_MUTEX_INITIALIZER;
64
65 struct sys_mbox_msg {
66 struct sys_mbox_msg *next;
67 void *msg;
68 };
69
70 #define SYS_MBOX_SIZE 128
71
72 struct sys_mbox {
73 int first, last;
74 void *msgs[SYS_MBOX_SIZE];
75 struct sys_sem *mail;
76 struct sys_sem *mutex;
77 int wait_send;
78 };
79
80 struct sys_sem {
81 unsigned int c;
82 pthread_cond_t cond;
83 pthread_mutex_t mutex;
84 };
85
86 struct sys_thread {
87 struct sys_thread *next;
88 struct sys_timeouts timeouts;
89 pthread_t pthread;
90 };
91
92
93 static struct timeval starttime;
94
95 static pthread_mutex_t lwprot_mutex = PTHREAD_MUTEX_INITIALIZER;
96 static pthread_t lwprot_thread = (pthread_t) 0xDEAD;
97 static int lwprot_count = 0;
98
99 static struct sys_sem *sys_sem_new_(u8_t count);
100 static void sys_sem_free_(struct sys_sem *sem);
101
102 static u32_t cond_wait(pthread_cond_t * cond, pthread_mutex_t * mutex,
103 u32_t timeout);
104
105 /*-----------------------------------------------------------------------------------*/
106 static struct sys_thread *
107 introduce_thread(pthread_t id)
108 {
109 struct sys_thread *thread;
110
111 thread = malloc(sizeof(struct sys_thread));
112
113 if (thread) {
114 pthread_mutex_lock(&threads_mutex);
115 thread->next = threads;
116 thread->timeouts.next = NULL;
117 thread->pthread = id;
118 threads = thread;
119 pthread_mutex_unlock(&threads_mutex);
120 }
121
122 return thread;
123 }
124 /*-----------------------------------------------------------------------------------*/
125 static struct sys_thread *
126 current_thread(void)
127 {
128 struct sys_thread *st;
129 pthread_t pt;
130 pt = pthread_self();
131 pthread_mutex_lock(&threads_mutex);
132
133 for(st = threads; st != NULL; st = st->next) {
134 if (pthread_equal(st->pthread, pt)) {
135 pthread_mutex_unlock(&threads_mutex);
136
137 return st;
138 }
139 }
140
141 pthread_mutex_unlock(&threads_mutex);
142
143 st = introduce_thread(pt);
144
145 if (!st) {
146 printf("current_thread???\n");
147 abort();
148 }
149
150 return st;
151 }
152 /*-----------------------------------------------------------------------------------*/
153 sys_thread_t
154 sys_thread_new(void (*function)(void *arg), void *arg, int prio)
155 {
156 int code;
157 pthread_t tmp;
158 struct sys_thread *st = NULL;
159
160 code = pthread_create(&tmp,
161 NULL,
162 (void *(*)(void *))
163 function,
164 arg);
165
166 if (0 == code) {
167 st = introduce_thread(tmp);
168 }
169
170 if (NULL == st) {
171 LWIP_DEBUGF(SYS_DEBUG, ("sys_thread_new: pthread_create %d, st = 0x%x",
172 code, (int)st));
173 abort();
174 }
175 return st;
176 }
177 /*-----------------------------------------------------------------------------------*/
178 struct sys_mbox *
179 sys_mbox_new()
180 {
181 struct sys_mbox *mbox;
182
183 mbox = malloc(sizeof(struct sys_mbox));
184 mbox->first = mbox->last = 0;
185 mbox->mail = sys_sem_new_(0);
186 mbox->mutex = sys_sem_new_(1);
187 mbox->wait_send = 0;
188
189 #if SYS_STATS
190 lwip_stats.sys.mbox.used++;
191 if (lwip_stats.sys.mbox.used > lwip_stats.sys.mbox.max) {
192 lwip_stats.sys.mbox.max = lwip_stats.sys.mbox.used;
193 }
194 #endif /* SYS_STATS */
195
196 return mbox;
197 }
198 /*-----------------------------------------------------------------------------------*/
199 void
200 sys_mbox_free(struct sys_mbox *mbox)
201 {
202 if (mbox != SYS_MBOX_NULL) {
203 #if SYS_STATS
204 lwip_stats.sys.mbox.used--;
205 #endif /* SYS_STATS */
206 sys_sem_wait(mbox->mutex);
207
208 sys_sem_free_(mbox->mail);
209 sys_sem_free_(mbox->mutex);
210 mbox->mail = mbox->mutex = NULL;
211 /* LWIP_DEBUGF("sys_mbox_free: mbox 0x%lx\n", mbox); */
212 free(mbox);
213 }
214 }
215
216 /*-----------------------------------------------------------------------------------*/
217 void
218 sys_mbox_post(struct sys_mbox *mbox, void *msg)
219 {
220 u8_t first;
221
222 sys_sem_wait(mbox->mutex);
223
224 LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_post: mbox %p msg %p\n", (void *)mbox, (void *)msg));
225
226 while ((mbox->last + 1) >= (mbox->first + SYS_MBOX_SIZE)) {
227 mbox->wait_send++;
228 sys_sem_signal(mbox->mutex);
229 sys_arch_sem_wait(mbox->mail, 0);
230 sys_arch_sem_wait(mbox->mutex, 0);
231 mbox->wait_send--;
232 }
233
234 mbox->msgs[mbox->last % SYS_MBOX_SIZE] = msg;
235
236 if (mbox->last == mbox->first) {
237 first = 1;
238 } else {
239 first = 0;
240 }
241
242 mbox->last++;
243
244 if (first) {
245 sys_sem_signal(mbox->mail);
246 }
247
248 sys_sem_signal(mbox->mutex);
249 }
250 /*-----------------------------------------------------------------------------------*/
251 u32_t
252 sys_arch_mbox_fetch(struct sys_mbox *mbox, void **msg, u32_t timeout)
253 {
254 u32_t time = 0;
255
256 /* The mutex lock is quick so we don't bother with the timeout
257 stuff here. */
258 sys_arch_sem_wait(mbox->mutex, 0);
259
260 while (mbox->first == mbox->last) {
261 sys_sem_signal(mbox->mutex);
262
263 /* We block while waiting for a mail to arrive in the mailbox. We
264 must be prepared to timeout. */
265 if (timeout != 0) {
266 time = sys_arch_sem_wait(mbox->mail, timeout);
267
268 if (time == SYS_ARCH_TIMEOUT) {
269 return SYS_ARCH_TIMEOUT;
270 }
271 } else {
272 sys_arch_sem_wait(mbox->mail, 0);
273 }
274
275 sys_arch_sem_wait(mbox->mutex, 0);
276 }
277
278 if (msg != NULL) {
279 LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_fetch: mbox %p msg %p\n", (void *)mbox, *msg));
280 *msg = mbox->msgs[mbox->first % SYS_MBOX_SIZE];
281 }
282 else{
283 LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_fetch: mbox %p, null msg\n", (void *)mbox));
284 }
285
286 mbox->first++;
287
288 if (mbox->wait_send) {
289 sys_sem_signal(mbox->mail);
290 }
291
292 sys_sem_signal(mbox->mutex);
293
294 return time;
295 }
296 /*-----------------------------------------------------------------------------------*/
297 struct sys_sem *
298 sys_sem_new(u8_t count)
299 {
300 #if SYS_STATS
301 lwip_stats.sys.sem.used++;
302 if (lwip_stats.sys.sem.used > lwip_stats.sys.sem.max) {
303 lwip_stats.sys.sem.max = lwip_stats.sys.sem.used;
304 }
305 #endif /* SYS_STATS */
306 return sys_sem_new_(count);
307 }
308
309 /*-----------------------------------------------------------------------------------*/
310 static struct sys_sem *
311 sys_sem_new_(u8_t count)
312 {
313 struct sys_sem *sem;
314
315 sem = malloc(sizeof(struct sys_sem));
316 sem->c = count;
317
318 pthread_cond_init(&(sem->cond), NULL);
319 pthread_mutex_init(&(sem->mutex), NULL);
320
321 return sem;
322 }
323
324 /*-----------------------------------------------------------------------------------*/
325 static u32_t
326 cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex, u32_t timeout)
327 {
328 int tdiff;
329 unsigned long sec, usec;
330 struct timeval rtime1, rtime2;
331 struct timespec ts;
332 struct timezone tz;
333 int retval;
334
335 if (timeout > 0) {
336 /* Get a timestamp and add the timeout value. */
337 gettimeofday(&rtime1, &tz);
338 sec = rtime1.tv_sec;
339 usec = rtime1.tv_usec;
340 usec += timeout % 1000 * 1000;
341 sec += (int)(timeout / 1000) + (int)(usec / 1000000);
342 usec = usec % 1000000;
343 ts.tv_nsec = usec * 1000;
344 ts.tv_sec = sec;
345
346 retval = pthread_cond_timedwait(cond, mutex, &ts);
347
348 if (retval == ETIMEDOUT) {
349 return SYS_ARCH_TIMEOUT;
350 } else {
351 /* Calculate for how long we waited for the cond. */
352 gettimeofday(&rtime2, &tz);
353 tdiff = (rtime2.tv_sec - rtime1.tv_sec) * 1000 +
354 (rtime2.tv_usec - rtime1.tv_usec) / 1000;
355
356 if (tdiff <= 0) {
357 return 0;
358 }
359
360 return tdiff;
361 }
362 } else {
363 pthread_cond_wait(cond, mutex);
364 return SYS_ARCH_TIMEOUT;
365 }
366 }
367 /*-----------------------------------------------------------------------------------*/
368 u32_t
369 sys_arch_sem_wait(struct sys_sem *sem, u32_t timeout)
370 {
371 u32_t time = 0;
372
373 pthread_mutex_lock(&(sem->mutex));
374 while (sem->c <= 0) {
375 if (timeout > 0) {
376 time = cond_wait(&(sem->cond), &(sem->mutex), timeout);
377
378 if (time == SYS_ARCH_TIMEOUT) {
379 pthread_mutex_unlock(&(sem->mutex));
380 return SYS_ARCH_TIMEOUT;
381 }
382 /* pthread_mutex_unlock(&(sem->mutex));
383 return time; */
384 } else {
385 cond_wait(&(sem->cond), &(sem->mutex), 0);
386 }
387 }
388 sem->c--;
389 pthread_mutex_unlock(&(sem->mutex));
390 return time;
391 }
392 /*-----------------------------------------------------------------------------------*/
393 void
394 sys_sem_signal(struct sys_sem *sem)
395 {
396 pthread_mutex_lock(&(sem->mutex));
397 sem->c++;
398
399 if (sem->c > 1) {
400 sem->c = 1;
401 }
402
403 pthread_cond_broadcast(&(sem->cond));
404 pthread_mutex_unlock(&(sem->mutex));
405 }
406 /*-----------------------------------------------------------------------------------*/
407 void
408 sys_sem_free(struct sys_sem *sem)
409 {
410 if (sem != SYS_SEM_NULL) {
411 #if SYS_STATS
412 lwip_stats.sys.sem.used--;
413 #endif /* SYS_STATS */
414 sys_sem_free_(sem);
415 }
416 }
417
418 /*-----------------------------------------------------------------------------------*/
419 static void
420 sys_sem_free_(struct sys_sem *sem)
421 {
422 pthread_cond_destroy(&(sem->cond));
423 pthread_mutex_destroy(&(sem->mutex));
424 free(sem);
425 }
426 /*-----------------------------------------------------------------------------------*/
427 void
428 sys_init()
429 {
430 struct timezone tz;
431 gettimeofday(&starttime, &tz);
432 }
433 /*-----------------------------------------------------------------------------------*/
434 struct sys_timeouts *
435 sys_arch_timeouts(void)
436 {
437 struct sys_thread *thread;
438
439 thread = current_thread();
440 return &thread->timeouts;
441 }
442 /*-----------------------------------------------------------------------------------*/
443 /** sys_prot_t sys_arch_protect(void)
444
445 This optional function does a "fast" critical region protection and returns
446 the previous protection level. This function is only called during very short
447 critical regions. An embedded system which supports ISR-based drivers might
448 want to implement this function by disabling interrupts. Task-based systems
449 might want to implement this by using a mutex or disabling tasking. This
450 function should support recursive calls from the same task or interrupt. In
451 other words, sys_arch_protect() could be called while already protected. In
452 that case the return value indicates that it is already protected.
453
454 sys_arch_protect() is only required if your port is supporting an operating
455 system.
456 */
457 sys_prot_t
458 sys_arch_protect(void)
459 {
460 /* Note that for the UNIX port, we are using a lightweight mutex, and our
461 * own counter (which is locked by the mutex). The return code is not actually
462 * used. */
463 if (lwprot_thread != pthread_self())
464 {
465 /* We are locking the mutex where it has not been locked before *
466 * or is being locked by another thread */
467 pthread_mutex_lock(&lwprot_mutex);
468 lwprot_thread = pthread_self();
469 lwprot_count = 1;
470 }
471 else
472 /* It is already locked by THIS thread */
473 lwprot_count++;
474 return 0;
475 }
476 /*-----------------------------------------------------------------------------------*/
477 /** void sys_arch_unprotect(sys_prot_t pval)
478
479 This optional function does a "fast" set of critical region protection to the
480 value specified by pval. See the documentation for sys_arch_protect() for
481 more information. This function is only required if your port is supporting
482 an operating system.
483 */
484 void
485 sys_arch_unprotect(sys_prot_t pval)
486 {
487 if (lwprot_thread == pthread_self())
488 {
489 if (--lwprot_count == 0)
490 {
491 lwprot_thread = (pthread_t) 0xDEAD;
492 pthread_mutex_unlock(&lwprot_mutex);
493 }
494 }
495 }
496
497 /*-----------------------------------------------------------------------------------*/
498
499 #ifndef MAX_JIFFY_OFFSET
500 #define MAX_JIFFY_OFFSET ((~0UL >> 1)-1)
501 #endif
502
503 #ifndef HZ
504 #define HZ 100
505 #endif
506
507 unsigned long
508 sys_jiffies(void)
509 {
510 struct timeval tv;
511 unsigned long sec = tv.tv_sec;
512 long usec = tv.tv_usec;
513
514 gettimeofday(&tv,NULL);
515
516 if (sec >= (MAX_JIFFY_OFFSET / HZ))
517 return MAX_JIFFY_OFFSET;
518 usec += 1000000L / HZ - 1;
519 usec /= 1000000L / HZ;
520 return HZ * sec + usec;
521 }
522
523 #if PPP_DEBUG
524
525 #include <stdarg.h>
526
527 void ppp_trace(int level, const char *format, ...)
528 {
529 va_list args;
530
531 (void)level;
532 va_start(args, format);
533 vprintf(format, args);
534 va_end(args);
535 }
536 #endif