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 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;
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(pthread_cond_t * cond, pthread_mutex_t * mutex,
123 /*-----------------------------------------------------------------------------------*/
124 static struct sys_thread *
125 introduce_thread(Kernel_Thread *id /*pthread_t id*/)
127 struct sys_thread *thread;
129 thread = v3ee_alloc(sizeof(struct sys_thread)); //!!!!!! malloc
131 if (thread != NULL) {
132 //pthread_mutex_lock(&threads_mutex);
133 Mutex_Lock(&threads_mutex);
135 thread->next = threads;
136 thread->timeouts.next = NULL;
137 thread->pthread = id;
140 //pthread_mutex_unlock(&threads_mutex);
141 Mutex_Unlock(&threads_mutex);
147 static int thread_equal(struct Kernel_Thread *kth1, struct Kernel_Thread *kth2){ //added
149 return (kth1->pid == kth2->pid);
153 /*-----------------------------------------------------------------------------------*/
154 static struct sys_thread *
157 struct sys_thread *st;
160 struct Kernel_Thread *pt;
162 //pt = pthread_self();
163 //!!!!!!!!!!!Do these have the same means? Not sure
164 pt = Get_Current(void);
166 //pthread_mutex_lock(&threads_mutex);
167 Mutex_Lock(&threads_mutex);
169 for(st = threads; st != NULL; st = st->next) {
170 if (thread_equal(&(st->pthread), pt)) {
171 //pthread_mutex_unlock(&threads_mutex);
172 Mutex_Unlock(&threads_mutex);
178 //pthread_mutex_unlock(&threads_mutex);
179 Mutex_Unlock(&threads_mutex);
181 st = introduce_thread(pt);
184 PrintBoth("lwIP error: In current_thread: current_thread???\n");
192 //!!!!!!!!!!!!backto this function later
193 /*-----------------------------------------------------------------------------------*/
195 sys_thread_new(char *name, void (* function)(void *arg), void *arg, int stacksize, int prio)
199 struct Kernel_Thread tmp;
200 struct sys_thread *st = NULL;
204 code = pthread_create(&tmp,
211 st = introduce_thread(tmp);
215 LWIP_DEBUGF(SYS_DEBUG, ("sys_thread_new: pthread_create %d, st = 0x%x",
223 /*-----------------------------------------------------------------------------------*/
225 sys_mbox_new(int size)
227 struct sys_mbox *mbox;
229 mbox = malloc(sizeof(struct sys_mbox));
231 mbox->first = mbox->last = 0;
232 mbox->mail = sys_sem_new_(0);
233 mbox->mutex = sys_sem_new_(1);
237 lwip_stats.sys.mbox.used++;
238 if (lwip_stats.sys.mbox.used > lwip_stats.sys.mbox.max) {
239 lwip_stats.sys.mbox.max = lwip_stats.sys.mbox.used;
241 #endif /* SYS_STATS */
245 /*-----------------------------------------------------------------------------------*/
247 sys_mbox_free(struct sys_mbox *mbox)
249 if (mbox != SYS_MBOX_NULL) {
251 lwip_stats.sys.mbox.used--;
252 #endif /* SYS_STATS */
253 sys_sem_wait(mbox->mutex);
255 sys_sem_free_(mbox->mail);
256 sys_sem_free_(mbox->mutex);
257 mbox->mail = mbox->mutex = NULL;
258 /* LWIP_DEBUGF("sys_mbox_free: mbox 0x%lx\n", mbox); */
260 free(mbox); //!!!! need change
263 /*-----------------------------------------------------------------------------------*/
265 sys_mbox_trypost(struct sys_mbox *mbox, void *msg)
269 sys_sem_wait(mbox->mutex);
271 LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_trypost: mbox %p msg %p\n",
272 (void *)mbox, (void *)msg));
274 if ((mbox->last + 1) >= (mbox->first + SYS_MBOX_SIZE))
277 mbox->msgs[mbox->last % SYS_MBOX_SIZE] = msg;
279 if (mbox->last == mbox->first) {
288 sys_sem_signal(mbox->mail);
291 sys_sem_signal(mbox->mutex);
295 /*-----------------------------------------------------------------------------------*/
297 sys_mbox_post(struct sys_mbox *mbox, void *msg)
301 sys_sem_wait(mbox->mutex);
303 LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_post: mbox %p msg %p\n", (void *)mbox, (void *)msg));
305 while ((mbox->last + 1) >= (mbox->first + SYS_MBOX_SIZE)) {
307 sys_sem_signal(mbox->mutex);
308 sys_arch_sem_wait(mbox->mail, 0);
309 sys_arch_sem_wait(mbox->mutex, 0);
313 mbox->msgs[mbox->last % SYS_MBOX_SIZE] = msg;
315 if (mbox->last == mbox->first) {
324 sys_sem_signal(mbox->mail);
327 sys_sem_signal(mbox->mutex);
329 /*-----------------------------------------------------------------------------------*/
331 sys_arch_mbox_tryfetch(struct sys_mbox *mbox, void **msg)
333 sys_arch_sem_wait(mbox->mutex, 0);
335 if (mbox->first == mbox->last) {
336 sys_sem_signal(mbox->mutex);
337 return SYS_MBOX_EMPTY;
341 LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_tryfetch: mbox %p msg %p\n", (void *)mbox, *msg));
342 *msg = mbox->msgs[mbox->first % SYS_MBOX_SIZE];
345 LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_tryfetch: mbox %p, null msg\n", (void *)mbox));
350 if (mbox->wait_send) {
351 sys_sem_signal(mbox->mail);
354 sys_sem_signal(mbox->mutex);
358 /*-----------------------------------------------------------------------------------*/
360 sys_arch_mbox_fetch(struct sys_mbox *mbox, void **msg, u32_t timeout)
364 /* The mutex lock is quick so we don't bother with the timeout
366 sys_arch_sem_wait(mbox->mutex, 0);
368 while (mbox->first == mbox->last) {
369 sys_sem_signal(mbox->mutex);
371 /* We block while waiting for a mail to arrive in the mailbox. We
372 must be prepared to timeout. */
374 time = sys_arch_sem_wait(mbox->mail, timeout);
376 if (time == SYS_ARCH_TIMEOUT) {
377 return SYS_ARCH_TIMEOUT;
380 sys_arch_sem_wait(mbox->mail, 0);
383 sys_arch_sem_wait(mbox->mutex, 0);
387 LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_fetch: mbox %p msg %p\n", (void *)mbox, *msg));
388 *msg = mbox->msgs[mbox->first % SYS_MBOX_SIZE];
391 LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_fetch: mbox %p, null msg\n", (void *)mbox));
396 if (mbox->wait_send) {
397 sys_sem_signal(mbox->mail);
400 sys_sem_signal(mbox->mutex);
404 /*-----------------------------------------------------------------------------------*/
406 sys_sem_new(u8_t count)
409 lwip_stats.sys.sem.used++;
410 if (lwip_stats.sys.sem.used > lwip_stats.sys.sem.max) {
411 lwip_stats.sys.sem.max = lwip_stats.sys.sem.used;
413 #endif /* SYS_STATS */
414 return sys_sem_new_(count);
417 /*-----------------------------------------------------------------------------------*/
418 static struct sys_sem *
419 sys_sem_new_(u8_t count)
423 sem = V3_Malloc(sizeof(struct sys_sem));
427 //pthread_cond_init(&(sem->cond), NULL);
428 Cond_Init(&(sem->cond));
430 //pthread_mutex_init(&(sem->mutex), NULL);
431 Mutex_Init(&(sem->mutex));
437 /*-----------------------------------------------------------------------------------*/
439 cond_wait(struct Condition *cond, struct Mutex *mutex, u32_t timeout /* pthread_cond_t *cond, pthread_mutex_t *mutex, u32_t timeout */)
442 unsigned long sec, usec;
443 struct timeval rtime1, rtime2;
449 //!!!!!!!!!!!!! OK, we need a timerly-condition wait here
451 /* Get a timestamp and add the timeout value. */
452 gettimeofday(&rtime1, &tz);
454 usec = rtime1.tv_usec;
455 usec += timeout % 1000 * 1000;
456 sec += (int)(timeout / 1000) + (int)(usec / 1000000);
457 usec = usec % 1000000;
458 ts.tv_nsec = usec * 1000;
461 retval = pthread_cond_timedwait(cond, mutex, &ts);
463 if (retval == ETIMEDOUT) {
464 return SYS_ARCH_TIMEOUT;
466 /* Calculate for how long we waited for the cond. */
467 gettimeofday(&rtime2, &tz);
468 tdiff = (rtime2.tv_sec - rtime1.tv_sec) * 1000 +
469 (rtime2.tv_usec - rtime1.tv_usec) / 1000;
478 //pthread_cond_wait(cond, mutex);
479 Cond_Wait(cond, mutex);
481 return SYS_ARCH_TIMEOUT;
484 /*-----------------------------------------------------------------------------------*/
486 sys_arch_sem_wait(struct sys_sem *sem, u32_t timeout)
490 //pthread_mutex_lock(&(sem->mutex));
491 Mutex_Lock(&(sem->mutex));
493 while (sem->c <= 0) {
495 time = cond_wait(&(sem->cond), &(sem->mutex), timeout);
497 if (time == SYS_ARCH_TIMEOUT) {
498 //pthread_mutex_unlock(&(sem->mutex));
499 Mutex_Unlock(&(sem->mutex));
501 return SYS_ARCH_TIMEOUT;
503 /* pthread_mutex_unlock(&(sem->mutex));
505 } else { //!!!!!!! this else should be paired with if (time == SYS_ARCH_TIMEOUT) ?????
506 cond_wait(&(sem->cond), &(sem->mutex), 0);
510 //pthread_mutex_unlock(&(sem->mutex));
511 Mutex_Unlock(&(sem->mutex));
515 /*-----------------------------------------------------------------------------------*/
517 sys_sem_signal(struct sys_sem *sem)
519 //pthread_mutex_lock(&(sem->mutex));
520 Mutex_Lock(&(sem->mutex));
528 //pthread_cond_broadcast(&(sem->cond));
529 Cond_Broadcast(&(sem->cond));
531 //pthread_mutex_unlock(&(sem->mutex));
532 Mutex_Unlock(&(sem->mutex));
535 /*-----------------------------------------------------------------------------------*/
537 sys_sem_free(struct sys_sem *sem)
539 if (sem != SYS_SEM_NULL) {
541 lwip_stats.sys.sem.used--;
542 #endif /* SYS_STATS */
547 /*-----------------------------------------------------------------------------------*/
549 sys_sem_free_(struct sys_sem *sem)
551 //pthread_cond_destroy(&(sem->cond));
552 Cond_Destroy(&(sem->cond));
554 //pthread_mutex_destroy(&(sem->mutex));
555 Mutex_Destroy(&(sem->mutex));
560 //return time, back to this later
561 /*-----------------------------------------------------------------------------------*/
569 gettimeofday(&tv, &tz);
571 sec = tv.tv_sec - starttime.tv_sec;
572 usec = tv.tv_usec - starttime.tv_usec;
573 msec = sec * 1000 + usec / 1000;
577 /*-----------------------------------------------------------------------------------*/
582 gettimeofday(&starttime, &tz);
584 /*-----------------------------------------------------------------------------------*/
585 struct sys_timeouts *
586 sys_arch_timeouts(void)
588 struct sys_thread *thread;
590 thread = current_thread();
591 return &thread->timeouts;
593 /*-----------------------------------------------------------------------------------*/
594 /** sys_prot_t sys_arch_protect(void)
596 This optional function does a "fast" critical region protection and returns
597 the previous protection level. This function is only called during very short
598 critical regions. An embedded system which supports ISR-based drivers might
599 want to implement this function by disabling interrupts. Task-based systems
600 might want to implement this by using a mutex or disabling tasking. This
601 function should support recursive calls from the same task or interrupt. In
602 other words, sys_arch_protect() could be called while already protected. In
603 that case the return value indicates that it is already protected.
605 sys_arch_protect() is only required if your port is supporting an operating
609 sys_arch_protect(void)
611 /* Note that for the UNIX port, we are using a lightweight mutex, and our
612 * own counter (which is locked by the mutex). The return code is not actually
614 if (lwprot_thread != pthread_self())
616 /* We are locking the mutex where it has not been locked before *
617 * or is being locked by another thread */
618 pthread_mutex_lock(&lwprot_mutex);
619 lwprot_thread = pthread_self();
623 /* It is already locked by THIS thread */
627 /*-----------------------------------------------------------------------------------*/
628 /** void sys_arch_unprotect(sys_prot_t pval)
630 This optional function does a "fast" set of critical region protection to the
631 value specified by pval. See the documentation for sys_arch_protect() for
632 more information. This function is only required if your port is supporting
636 sys_arch_unprotect(sys_prot_t pval)
638 if (lwprot_thread == pthread_self())
640 if (--lwprot_count == 0)
642 lwprot_thread = (pthread_t) 0xDEAD;
643 pthread_mutex_unlock(&lwprot_mutex);
648 /*-----------------------------------------------------------------------------------*/
650 #ifndef MAX_JIFFY_OFFSET
651 #define MAX_JIFFY_OFFSET ((~0UL >> 1)-1)
662 unsigned long sec = tv.tv_sec;
663 long usec = tv.tv_usec;
665 gettimeofday(&tv,NULL);
667 if (sec >= (MAX_JIFFY_OFFSET / HZ))
668 return MAX_JIFFY_OFFSET;
669 usec += 1000000L / HZ - 1;
670 usec /= 1000000L / HZ;
671 return HZ * sec + usec;
676 #include <geekos/serial.h>
678 void ppp_trace(int level, const char *format, ...)
683 va_start(args, format);
685 //vprintf(format, args);
686 SerialPrintList(format, args);