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 <palacios/vmm.h>
59 #include <geekos/debug.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 *)V3_Malloc(sizeof(struct sys_thread)); //!!!!!! malloc
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: 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 *)V3_Malloc(sizeof(struct Kernel_Thread));
203 code = pthread_create(&tmp,
210 st = introduce_thread(tmp);
214 LWIP_DEBUGF(SYS_DEBUG, ("sys_thread_new: pthread_create %d, st = 0x%x",
222 /*-----------------------------------------------------------------------------------*/
224 sys_mbox_new(int size)
226 struct sys_mbox *mbox;
228 mbox = (struct sys_mbox *)V3_Malloc(sizeof(struct sys_mbox));
230 mbox->first = mbox->last = 0;
231 mbox->mail = sys_sem_new_(0);
232 mbox->mutex = sys_sem_new_(1);
236 lwip_stats.sys.mbox.used++;
237 if (lwip_stats.sys.mbox.used > lwip_stats.sys.mbox.max) {
238 lwip_stats.sys.mbox.max = lwip_stats.sys.mbox.used;
240 #endif /* SYS_STATS */
244 /*-----------------------------------------------------------------------------------*/
246 sys_mbox_free(struct sys_mbox *mbox)
248 if (mbox != SYS_MBOX_NULL) {
250 lwip_stats.sys.mbox.used--;
251 #endif /* SYS_STATS */
252 sys_sem_wait(mbox->mutex);
254 sys_sem_free_(mbox->mail);
255 sys_sem_free_(mbox->mutex);
256 mbox->mail = mbox->mutex = NULL;
257 /* LWIP_DEBUGF("sys_mbox_free: mbox 0x%lx\n", mbox); */
262 /*-----------------------------------------------------------------------------------*/
264 sys_mbox_trypost(struct sys_mbox *mbox, void *msg)
268 sys_sem_wait(mbox->mutex);
270 LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_trypost: mbox %p msg %p\n",
271 (void *)mbox, (void *)msg));
273 if ((mbox->last + 1) >= (mbox->first + SYS_MBOX_SIZE))
276 mbox->msgs[mbox->last % SYS_MBOX_SIZE] = msg;
278 if (mbox->last == mbox->first) {
287 sys_sem_signal(mbox->mail);
290 sys_sem_signal(mbox->mutex);
294 /*-----------------------------------------------------------------------------------*/
296 sys_mbox_post(struct sys_mbox *mbox, void *msg)
300 sys_sem_wait(mbox->mutex);
302 LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_post: mbox %p msg %p\n", (void *)mbox, (void *)msg));
304 while ((mbox->last + 1) >= (mbox->first + SYS_MBOX_SIZE)) {
306 sys_sem_signal(mbox->mutex);
307 sys_arch_sem_wait(mbox->mail, 0);
308 sys_arch_sem_wait(mbox->mutex, 0);
312 mbox->msgs[mbox->last % SYS_MBOX_SIZE] = msg;
314 if (mbox->last == mbox->first) {
323 sys_sem_signal(mbox->mail);
326 sys_sem_signal(mbox->mutex);
328 /*-----------------------------------------------------------------------------------*/
330 sys_arch_mbox_tryfetch(struct sys_mbox *mbox, void **msg)
332 sys_arch_sem_wait(mbox->mutex, 0);
334 if (mbox->first == mbox->last) {
335 sys_sem_signal(mbox->mutex);
336 return SYS_MBOX_EMPTY;
340 LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_tryfetch: mbox %p msg %p\n", (void *)mbox, *msg));
341 *msg = mbox->msgs[mbox->first % SYS_MBOX_SIZE];
344 LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_tryfetch: mbox %p, null msg\n", (void *)mbox));
349 if (mbox->wait_send) {
350 sys_sem_signal(mbox->mail);
353 sys_sem_signal(mbox->mutex);
357 /*-----------------------------------------------------------------------------------*/
359 sys_arch_mbox_fetch(struct sys_mbox *mbox, void **msg, u32_t timeout)
363 /* The mutex lock is quick so we don't bother with the timeout
365 sys_arch_sem_wait(mbox->mutex, 0);
367 while (mbox->first == mbox->last) {
368 sys_sem_signal(mbox->mutex);
370 /* We block while waiting for a mail to arrive in the mailbox. We
371 must be prepared to timeout. */
373 time = sys_arch_sem_wait(mbox->mail, timeout);
375 if (time == SYS_ARCH_TIMEOUT) {
376 return SYS_ARCH_TIMEOUT;
379 sys_arch_sem_wait(mbox->mail, 0);
382 sys_arch_sem_wait(mbox->mutex, 0);
386 LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_fetch: mbox %p msg %p\n", (void *)mbox, *msg));
387 *msg = mbox->msgs[mbox->first % SYS_MBOX_SIZE];
390 LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_fetch: mbox %p, null msg\n", (void *)mbox));
395 if (mbox->wait_send) {
396 sys_sem_signal(mbox->mail);
399 sys_sem_signal(mbox->mutex);
403 /*-----------------------------------------------------------------------------------*/
405 sys_sem_new(u8_t count)
408 lwip_stats.sys.sem.used++;
409 if (lwip_stats.sys.sem.used > lwip_stats.sys.sem.max) {
410 lwip_stats.sys.sem.max = lwip_stats.sys.sem.used;
412 #endif /* SYS_STATS */
413 return sys_sem_new_(count);
416 /*-----------------------------------------------------------------------------------*/
417 static struct sys_sem *
418 sys_sem_new_(u8_t count)
422 sem = (struct sys_sem *)V3_Malloc(sizeof(struct sys_sem));
426 //pthread_cond_init(&(sem->cond), NULL);
427 Cond_Init(&(sem->cond));
429 //pthread_mutex_init(&(sem->mutex), NULL);
430 Mutex_Init(&(sem->mutex));
436 /*-----------------------------------------------------------------------------------*/
438 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 */)
441 unsigned long sec, usec;
442 struct timeval rtime1, rtime2;
448 //!!!!!!!!!!!!! OK, we need a timerly-condition wait here
450 /* Get a timestamp and add the timeout value. */
451 gettimeofday(&rtime1, &tz);
453 usec = rtime1.tv_usec;
454 usec += timeout % 1000 * 1000;
455 sec += (int)(timeout / 1000) + (int)(usec / 1000000);
456 usec = usec % 1000000;
457 ts.tv_nsec = usec * 1000;
460 retval = pthread_cond_timedwait(cond, mutex, &ts);
462 if (retval == ETIMEDOUT) {
463 return SYS_ARCH_TIMEOUT;
465 /* Calculate for how long we waited for the cond. */
466 gettimeofday(&rtime2, &tz);
467 tdiff = (rtime2.tv_sec - rtime1.tv_sec) * 1000 +
468 (rtime2.tv_usec - rtime1.tv_usec) / 1000;
477 //pthread_cond_wait(cond, mutex);
478 Cond_Wait(cond, mutex);
480 return SYS_ARCH_TIMEOUT;
483 /*-----------------------------------------------------------------------------------*/
485 sys_arch_sem_wait(struct sys_sem *sem, u32_t timeout)
489 //pthread_mutex_lock(&(sem->mutex));
490 Mutex_Lock(&(sem->mutex));
492 while (sem->c <= 0) {
494 time = cond_wait(&(sem->cond), &(sem->mutex), timeout);
496 if (time == SYS_ARCH_TIMEOUT) {
497 //pthread_mutex_unlock(&(sem->mutex));
498 Mutex_Unlock(&(sem->mutex));
500 return SYS_ARCH_TIMEOUT;
502 /* pthread_mutex_unlock(&(sem->mutex));
504 } else { //!!!!!!! this else should be paired with if (time == SYS_ARCH_TIMEOUT) ?????
505 cond_wait(&(sem->cond), &(sem->mutex), 0);
509 //pthread_mutex_unlock(&(sem->mutex));
510 Mutex_Unlock(&(sem->mutex));
514 /*-----------------------------------------------------------------------------------*/
516 sys_sem_signal(struct sys_sem *sem)
518 //pthread_mutex_lock(&(sem->mutex));
519 Mutex_Lock(&(sem->mutex));
527 //pthread_cond_broadcast(&(sem->cond));
528 Cond_Broadcast(&(sem->cond));
530 //pthread_mutex_unlock(&(sem->mutex));
531 Mutex_Unlock(&(sem->mutex));
534 /*-----------------------------------------------------------------------------------*/
536 sys_sem_free(struct sys_sem *sem)
538 if (sem != SYS_SEM_NULL) {
540 lwip_stats.sys.sem.used--;
541 #endif /* SYS_STATS */
546 /*-----------------------------------------------------------------------------------*/
548 sys_sem_free_(struct sys_sem *sem)
550 //pthread_cond_destroy(&(sem->cond));
551 Cond_Destroy(&(sem->cond));
553 //pthread_mutex_destroy(&(sem->mutex));
554 Mutex_Destroy(&(sem->mutex));
559 //return time, back to this later
560 /*-----------------------------------------------------------------------------------*/
568 gettimeofday(&tv, &tz);
570 sec = tv.tv_sec - starttime.tv_sec;
571 usec = tv.tv_usec - starttime.tv_usec;
572 msec = sec * 1000 + usec / 1000;
576 /*-----------------------------------------------------------------------------------*/
581 gettimeofday(&starttime, &tz);
583 /*-----------------------------------------------------------------------------------*/
584 struct sys_timeouts *
585 sys_arch_timeouts(void)
587 struct sys_thread *thread;
589 thread = current_thread();
590 return &thread->timeouts;
592 /*-----------------------------------------------------------------------------------*/
593 /** sys_prot_t sys_arch_protect(void)
595 This optional function does a "fast" critical region protection and returns
596 the previous protection level. This function is only called during very short
597 critical regions. An embedded system which supports ISR-based drivers might
598 want to implement this function by disabling interrupts. Task-based systems
599 might want to implement this by using a mutex or disabling tasking. This
600 function should support recursive calls from the same task or interrupt. In
601 other words, sys_arch_protect() could be called while already protected. In
602 that case the return value indicates that it is already protected.
604 sys_arch_protect() is only required if your port is supporting an operating
608 sys_arch_protect(void)
610 /* Note that for the UNIX port, we are using a lightweight mutex, and our
611 * own counter (which is locked by the mutex). The return code is not actually
613 if (lwprot_thread != pthread_self())
615 /* We are locking the mutex where it has not been locked before *
616 * or is being locked by another thread */
617 pthread_mutex_lock(&lwprot_mutex);
618 lwprot_thread = pthread_self();
622 /* It is already locked by THIS thread */
626 /*-----------------------------------------------------------------------------------*/
627 /** void sys_arch_unprotect(sys_prot_t pval)
629 This optional function does a "fast" set of critical region protection to the
630 value specified by pval. See the documentation for sys_arch_protect() for
631 more information. This function is only required if your port is supporting
635 sys_arch_unprotect(sys_prot_t pval)
637 if (lwprot_thread == pthread_self())
639 if (--lwprot_count == 0)
641 lwprot_thread = (pthread_t) 0xDEAD;
642 pthread_mutex_unlock(&lwprot_mutex);
647 /*-----------------------------------------------------------------------------------*/
649 #ifndef MAX_JIFFY_OFFSET
650 #define MAX_JIFFY_OFFSET ((~0UL >> 1)-1)
661 unsigned long sec = tv.tv_sec;
662 long usec = tv.tv_usec;
664 gettimeofday(&tv,NULL);
666 if (sec >= (MAX_JIFFY_OFFSET / HZ))
667 return MAX_JIFFY_OFFSET;
668 usec += 1000000L / HZ - 1;
669 usec /= 1000000L / HZ;
670 return HZ * sec + usec;
675 #include <geekos/serial.h>
677 void ppp_trace(int level, const char *format, ...)
682 va_start(args, format);
684 //vprintf(format, args);
685 SerialPrintList(format, args);