2 * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
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.
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
27 * This file is part of the lwIP TCP/IP stack.
29 * Author: Adam Dunkels <adam@sics.se>
34 * Wed Apr 17 16:05:29 EDT 2002 (James Roth)
36 * - Fixed an unlikely sys_thread_new() race condition.
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().
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
47 #include "lwip/debug.h"
51 #include <sys/types.h>
56 #include <geekos/synch.h>
57 #include <geekos/kthread.h>
58 #include <geekos/debug.h>
59 #include <geekos/malloc.h>
63 #include "lwip/stats.h"
65 #define UMAX(a, b) ((a) > (b) ? (a) : (b))
67 static struct sys_thread *threads = NULL;
69 //static pthread_mutex_t threads_mutex = PTHREAD_MUTEX_INITIALIZER;
70 static struct Mutex threads_mutex; // !!!! need to be initiated, void Mutex_Init(struct Mutex* mutex);
73 struct sys_mbox_msg *next;
77 #define SYS_MBOX_SIZE 128
81 void *msgs[SYS_MBOX_SIZE];
83 struct sys_sem *mutex;
90 //pthread_cond_t cond;
91 struct Condition cond;
93 //pthread_mutex_t mutex;
98 struct sys_thread *next;
99 struct sys_timeouts timeouts;
101 // pthread_t pthread;
102 struct Kernel_Thread *pthread;
106 static struct timeval starttime;
108 //static pthread_mutex_t lwprot_mutex = PTHREAD_MUTEX_INITIALIZER;
109 static struct Mutex lwprot_mutex; // !!!! need to be initiated, void Mutex_Init(struct Mutex* mutex);
111 //static pthread_t lwprot_thread = (pthread_t) 0xDEAD;
112 static struct Kernel_Thread lwprot_thread = (struct Kernel_Thread) 0xDEAD; //!!!!! how to set it to a NULL thread?
114 static int lwprot_count = 0;
116 static struct sys_sem *sys_sem_new_(u8_t count);
117 static void sys_sem_free_(struct sys_sem *sem);
119 static u32_t cond_wait(struct Condition *cond, struct Mutex *mutex, u32_t timeout);
122 /*-----------------------------------------------------------------------------------*/
123 static struct sys_thread *
124 introduce_thread(struct Kernel_Thread *id /*pthread_t id*/)
126 struct sys_thread *thread;
128 thread = (struct sys_thread *)Malloc(sizeof(struct sys_thread));
130 if (thread != NULL) {
131 //pthread_mutex_lock(&threads_mutex);
132 Mutex_Lock(&threads_mutex);
134 thread->next = threads;
135 thread->timeouts.next = NULL;
136 thread->pthread = id;
139 //pthread_mutex_unlock(&threads_mutex);
140 Mutex_Unlock(&threads_mutex);
146 static int thread_equal(struct Kernel_Thread *kth1, struct Kernel_Thread *kth2){ //added
148 return (kth1->pid == kth2->pid);
152 /*-----------------------------------------------------------------------------------*/
153 static struct sys_thread *
156 struct sys_thread *st;
159 struct Kernel_Thread *pt;
161 //pt = pthread_self();
162 //!!!!!!!!!!!Do these have the same means? Not sure
165 //pthread_mutex_lock(&threads_mutex);
166 Mutex_Lock(&threads_mutex);
168 for(st = threads; st != NULL; st = st->next) {
169 if (thread_equal(st->pthread, pt)) {
170 //pthread_mutex_unlock(&threads_mutex);
171 Mutex_Unlock(&threads_mutex);
177 //pthread_mutex_unlock(&threads_mutex);
178 Mutex_Unlock(&threads_mutex);
180 st = introduce_thread(pt);
183 PrintBoth("lwIP error: In current_thread: Can not get current_thread\n");
191 //!!!!!!!!!!!!backto this function later
192 /*-----------------------------------------------------------------------------------*/
194 sys_thread_new(char *name, void (* function)(void *arg), void *arg, int stacksize, int prio)
198 struct Kernel_Thread *tmp;
199 struct sys_thread *st = NULL;
201 //tmp = (struct Kernel_Thread *)Malloc(sizeof(struct Kernel_Thread));
203 /* code = pthread_create(&tmp,
209 tmp = Start_Kernel_Thread(function, arg, PRIORITY_NORMAL , false); //did not consider the priority here
212 st = introduce_thread(tmp);
216 LWIP_DEBUGF(SYS_DEBUG, ("sys_thread_new: pthread_create: 0x%x, st = 0x%x",
224 /*-----------------------------------------------------------------------------------*/
226 sys_mbox_new(int size)
228 struct sys_mbox *mbox;
230 mbox = (struct sys_mbox *)Malloc(sizeof(struct sys_mbox));
232 mbox->first = mbox->last = 0;
233 mbox->mail = sys_sem_new_(0);
234 mbox->mutex = sys_sem_new_(1);
238 lwip_stats.sys.mbox.used++;
239 if (lwip_stats.sys.mbox.used > lwip_stats.sys.mbox.max) {
240 lwip_stats.sys.mbox.max = lwip_stats.sys.mbox.used;
242 #endif /* SYS_STATS */
246 /*-----------------------------------------------------------------------------------*/
248 sys_mbox_free(struct sys_mbox *mbox)
250 if (mbox != SYS_MBOX_NULL) {
252 lwip_stats.sys.mbox.used--;
253 #endif /* SYS_STATS */
254 sys_sem_wait(mbox->mutex);
256 sys_sem_free_(mbox->mail);
257 sys_sem_free_(mbox->mutex);
258 mbox->mail = mbox->mutex = NULL;
259 /* LWIP_DEBUGF("sys_mbox_free: mbox 0x%lx\n", mbox); */
264 /*-----------------------------------------------------------------------------------*/
266 sys_mbox_trypost(struct sys_mbox *mbox, void *msg)
270 sys_sem_wait(mbox->mutex);
272 LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_trypost: mbox %p msg %p\n",
273 (void *)mbox, (void *)msg));
275 if ((mbox->last + 1) >= (mbox->first + SYS_MBOX_SIZE))
278 mbox->msgs[mbox->last % SYS_MBOX_SIZE] = msg;
280 if (mbox->last == mbox->first) {
289 sys_sem_signal(mbox->mail);
292 sys_sem_signal(mbox->mutex);
296 /*-----------------------------------------------------------------------------------*/
298 sys_mbox_post(struct sys_mbox *mbox, void *msg)
302 sys_sem_wait(mbox->mutex);
304 LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_post: mbox %p msg %p\n", (void *)mbox, (void *)msg));
306 while ((mbox->last + 1) >= (mbox->first + SYS_MBOX_SIZE)) {
308 sys_sem_signal(mbox->mutex);
309 sys_arch_sem_wait(mbox->mail, 0);
310 sys_arch_sem_wait(mbox->mutex, 0);
314 mbox->msgs[mbox->last % SYS_MBOX_SIZE] = msg;
316 if (mbox->last == mbox->first) {
325 sys_sem_signal(mbox->mail);
328 sys_sem_signal(mbox->mutex);
330 /*-----------------------------------------------------------------------------------*/
332 sys_arch_mbox_tryfetch(struct sys_mbox *mbox, void **msg)
334 sys_arch_sem_wait(mbox->mutex, 0);
336 if (mbox->first == mbox->last) {
337 sys_sem_signal(mbox->mutex);
338 return SYS_MBOX_EMPTY;
342 LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_tryfetch: mbox %p msg %p\n", (void *)mbox, *msg));
343 *msg = mbox->msgs[mbox->first % SYS_MBOX_SIZE];
346 LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_tryfetch: mbox %p, null msg\n", (void *)mbox));
351 if (mbox->wait_send) {
352 sys_sem_signal(mbox->mail);
355 sys_sem_signal(mbox->mutex);
359 /*-----------------------------------------------------------------------------------*/
361 sys_arch_mbox_fetch(struct sys_mbox *mbox, void **msg, u32_t timeout)
365 /* The mutex lock is quick so we don't bother with the timeout
367 sys_arch_sem_wait(mbox->mutex, 0);
369 while (mbox->first == mbox->last) {
370 sys_sem_signal(mbox->mutex);
372 /* We block while waiting for a mail to arrive in the mailbox. We
373 must be prepared to timeout. */
375 time = sys_arch_sem_wait(mbox->mail, timeout);
377 if (time == SYS_ARCH_TIMEOUT) {
378 return SYS_ARCH_TIMEOUT;
381 sys_arch_sem_wait(mbox->mail, 0);
384 sys_arch_sem_wait(mbox->mutex, 0);
388 LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_fetch: mbox %p msg %p\n", (void *)mbox, *msg));
389 *msg = mbox->msgs[mbox->first % SYS_MBOX_SIZE];
392 LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_fetch: mbox %p, null msg\n", (void *)mbox));
397 if (mbox->wait_send) {
398 sys_sem_signal(mbox->mail);
401 sys_sem_signal(mbox->mutex);
405 /*-----------------------------------------------------------------------------------*/
407 sys_sem_new(u8_t count)
410 lwip_stats.sys.sem.used++;
411 if (lwip_stats.sys.sem.used > lwip_stats.sys.sem.max) {
412 lwip_stats.sys.sem.max = lwip_stats.sys.sem.used;
414 #endif /* SYS_STATS */
415 return sys_sem_new_(count);
418 /*-----------------------------------------------------------------------------------*/
419 static struct sys_sem *
420 sys_sem_new_(u8_t count)
424 sem = (struct sys_sem *)Malloc(sizeof(struct sys_sem));
428 //pthread_cond_init(&(sem->cond), NULL);
429 Cond_Init(&(sem->cond));
431 //pthread_mutex_init(&(sem->mutex), NULL);
432 Mutex_Init(&(sem->mutex));
438 /*-----------------------------------------------------------------------------------*/
440 cond_wait(struct Condition *cond, struct Mutex *mutex, u32_t timeout /* timeout is in miliseconds *//* pthread_cond_t *cond, pthread_mutex_t *mutex, u32_t timeout */)
445 struct timeval rtime1, rtime2;
453 /* Get a timestamp and add the timeout value. */
454 /*gettimeofday(&rtime1, &tz);
456 usec = rtime1.tv_usec;
457 usec += timeout % 1000 * 1000;
458 sec += (int)(timeout / 1000) + (int)(usec / 1000000);
459 usec = usec % 1000000;
460 ts.tv_nsec = usec * 1000;
465 if (Cond_Wait_Timeout(cond, mutex, timeout) == 1) //return due to timeout
468 //retval = pthread_cond_timedwait(cond, mutex, &ts);
470 if (retval == ETIMEDOUT) {
471 return SYS_ARCH_TIMEOUT;
473 /* Calculate for how long we waited for the cond. */
476 gettimeofday(&rtime2, &tz);
477 tdiff = (rtime2.tv_sec - rtime1.tv_sec) * 1000 +
478 (rtime2.tv_usec - rtime1.tv_usec) / 1000;
480 tdiff = clock_time();
491 //pthread_cond_wait(cond, mutex);
492 Cond_Wait(cond, mutex);
494 return SYS_ARCH_TIMEOUT;
497 /*-----------------------------------------------------------------------------------*/
499 sys_arch_sem_wait(struct sys_sem *sem, u32_t timeout)
503 //pthread_mutex_lock(&(sem->mutex));
504 Mutex_Lock(&(sem->mutex));
506 while (sem->c <= 0) {
508 time = cond_wait(&(sem->cond), &(sem->mutex), timeout);
510 if (time == SYS_ARCH_TIMEOUT) {
511 //pthread_mutex_unlock(&(sem->mutex));
512 Mutex_Unlock(&(sem->mutex));
514 return SYS_ARCH_TIMEOUT;
516 /* pthread_mutex_unlock(&(sem->mutex));
518 } else { // if timeout = 0
519 cond_wait(&(sem->cond), &(sem->mutex), 0);
523 //pthread_mutex_unlock(&(sem->mutex));
524 Mutex_Unlock(&(sem->mutex));
528 /*-----------------------------------------------------------------------------------*/
530 sys_sem_signal(struct sys_sem *sem)
532 //pthread_mutex_lock(&(sem->mutex));
533 Mutex_Lock(&(sem->mutex));
541 //pthread_cond_broadcast(&(sem->cond));
542 Cond_Broadcast(&(sem->cond));
544 //pthread_mutex_unlock(&(sem->mutex));
545 Mutex_Unlock(&(sem->mutex));
548 /*-----------------------------------------------------------------------------------*/
550 sys_sem_free(struct sys_sem *sem)
552 if (sem != SYS_SEM_NULL) {
554 lwip_stats.sys.sem.used--;
555 #endif /* SYS_STATS */
560 /*-----------------------------------------------------------------------------------*/
562 sys_sem_free_(struct sys_sem *sem)
564 //pthread_cond_destroy(&(sem->cond));
565 Cond_Destroy(&(sem->cond));
567 //pthread_mutex_destroy(&(sem->mutex));
568 Mutex_Destroy(&(sem->mutex));
574 //return time, we do not need this
575 /*-----------------------------------------------------------------------------------*/
583 gettimeofday(&tv, &tz);
585 sec = tv.tv_sec - starttime.tv_sec;
586 usec = tv.tv_usec - starttime.tv_usec;
587 msec = sec * 1000 + usec / 1000;
593 /*-----------------------------------------------------------------------------------*/
597 //struct timezone tz;
598 //gettimeofday(&starttime, &tz);
600 Mutex_Init(&threads_mutex);
601 Mutex_Init(&lwprot_mutex);
604 /*-----------------------------------------------------------------------------------*/
605 struct sys_timeouts *
606 sys_arch_timeouts(void)
608 struct sys_thread *thread;
610 thread = current_thread();
611 return &thread->timeouts;
613 /*-----------------------------------------------------------------------------------*/
614 /** sys_prot_t sys_arch_protect(void)
616 This optional function does a "fast" critical region protection and returns
617 the previous protection level. This function is only called during very short
618 critical regions. An embedded system which supports ISR-based drivers might
619 want to implement this function by disabling interrupts. Task-based systems
620 might want to implement this by using a mutex or disabling tasking. This
621 function should support recursive calls from the same task or interrupt. In
622 other words, sys_arch_protect() could be called while already protected. In
623 that case the return value indicates that it is already protected.
625 sys_arch_protect() is only required if your port is supporting an operating
629 sys_arch_protect(void)
631 /* Note that for the UNIX port, we are using a lightweight mutex, and our
632 * own counter (which is locked by the mutex). The return code is not actually
634 if (lwprot_thread != current_thread() /*lwprot_thread != pthread_self()*/)
636 /* We are locking the mutex where it has not been locked before *
637 * or is being locked by another thread */
638 Mutex_Lock(&lwprot_mutex);
639 lwprot_thread = current_thread();
643 /* It is already locked by THIS thread */
647 /*-----------------------------------------------------------------------------------*/
648 /** void sys_arch_unprotect(sys_prot_t pval)
650 This optional function does a "fast" set of critical region protection to the
651 value specified by pval. See the documentation for sys_arch_protect() for
652 more information. This function is only required if your port is supporting
656 sys_arch_unprotect(sys_prot_t pval)
658 if (lwprot_thread == current_thread())
660 if (--lwprot_count == 0)
662 lwprot_thread = (Kernel_Thread) 0xDEAD;
663 Mutex_Unlock(&lwprot_mutex);
668 /*-----------------------------------------------------------------------------------*/
670 #ifndef MAX_JIFFY_OFFSET
671 #define MAX_JIFFY_OFFSET ((~0UL >> 1)-1)
682 unsigned long sec = tv.tv_sec;
683 long usec = tv.tv_usec;
685 gettimeofday(&tv,NULL);
687 if (sec >= (MAX_JIFFY_OFFSET / HZ))
688 return MAX_JIFFY_OFFSET;
689 usec += 1000000L / HZ - 1;
690 usec /= 1000000L / HZ;
691 return HZ * sec + usec;
696 #include <geekos/serial.h>
698 void ppp_trace(int level, const char *format, ...)
703 va_start(args, format);
705 //vprintf(format, args);
706 SerialPrintList(format, args);