X-Git-Url: http://v3vee.org/palacios/gitweb/gitweb.cgi?a=blobdiff_plain;f=palacios%2Fsrc%2Flwip%2Farch%2Fsys_arch.c;h=289ed70bda27fa28a2876dbb59df26dec13a43ad;hb=18994f099b7316da6f6c9752aa59b08259fb0057;hp=61464006e2e46c3a9e0baf8943bf534c5e6ef830;hpb=6cb574c527f3cbdab6659eea3d2277cb2776da67;p=palacios.git diff --git a/palacios/src/lwip/arch/sys_arch.c b/palacios/src/lwip/arch/sys_arch.c index 6146400..289ed70 100644 --- a/palacios/src/lwip/arch/sys_arch.c +++ b/palacios/src/lwip/arch/sys_arch.c @@ -55,8 +55,8 @@ #include #include -#include #include +#include #include "lwip/sys.h" #include "lwip/opt.h" @@ -106,27 +106,26 @@ struct sys_thread { static struct timeval starttime; //static pthread_mutex_t lwprot_mutex = PTHREAD_MUTEX_INITIALIZER; -static Mutex lwprot_mutex; // !!!! need to be initiated, void Mutex_Init(struct Mutex* mutex); +static struct Mutex lwprot_mutex; // !!!! need to be initiated, void Mutex_Init(struct Mutex* mutex); //static pthread_t lwprot_thread = (pthread_t) 0xDEAD; -static struct Kernel_Thread lwprot_thread = (struct Kernel_Thread) 0xDEAD; +static struct Kernel_Thread lwprot_thread = (struct Kernel_Thread) 0xDEAD; //!!!!! how to set it to a NULL thread? static int lwprot_count = 0; static struct sys_sem *sys_sem_new_(u8_t count); static void sys_sem_free_(struct sys_sem *sem); -static u32_t cond_wait(pthread_cond_t * cond, pthread_mutex_t * mutex, - u32_t timeout); +static u32_t cond_wait(struct Condition *cond, struct Mutex *mutex, u32_t timeout); /*-----------------------------------------------------------------------------------*/ static struct sys_thread * -introduce_thread(Kernel_Thread *id /*pthread_t id*/) +introduce_thread(struct Kernel_Thread *id /*pthread_t id*/) { struct sys_thread *thread; - thread = v3ee_alloc(sizeof(struct sys_thread)); //!!!!!! malloc + thread = (struct sys_thread *)Malloc(sizeof(struct sys_thread)); if (thread != NULL) { //pthread_mutex_lock(&threads_mutex); @@ -161,13 +160,13 @@ current_thread(void) //pt = pthread_self(); //!!!!!!!!!!!Do these have the same means? Not sure - pt = Get_Current(void); + pt = Get_Current(); //pthread_mutex_lock(&threads_mutex); Mutex_Lock(&threads_mutex); for(st = threads; st != NULL; st = st->next) { - if (thread_equal(&(st->pthread), pt)) { + if (thread_equal(st->pthread, pt)) { //pthread_mutex_unlock(&threads_mutex); Mutex_Unlock(&threads_mutex); @@ -181,7 +180,7 @@ current_thread(void) st = introduce_thread(pt); if (!st) { - PrintBoth("lwIP error: In current_thread: current_thread???\n"); + PrintBoth("lwIP error: In current_thread: Can not get current_thread\n"); abort(); } @@ -196,24 +195,26 @@ sys_thread_new(char *name, void (* function)(void *arg), void *arg, int stacksiz { int code; //pthread_t tmp; - struct Kernel_Thread tmp; + struct Kernel_Thread *tmp; struct sys_thread *st = NULL; + //tmp = (struct Kernel_Thread *)Malloc(sizeof(struct Kernel_Thread)); - - code = pthread_create(&tmp, + /* code = pthread_create(&tmp, NULL, (void *(*)(void *)) function, - arg); + arg); */ + + tmp = Start_Kernel_Thread(function, arg, PRIORITY_NORMAL , false); //did not consider the priority here - if (0 == code) { + if (tmp != NULL) { st = introduce_thread(tmp); } if (NULL == st) { - LWIP_DEBUGF(SYS_DEBUG, ("sys_thread_new: pthread_create %d, st = 0x%x", - code, (int)st)); + LWIP_DEBUGF(SYS_DEBUG, ("sys_thread_new: pthread_create: 0x%x, st = 0x%x", + (int)tmp, (int)st)); abort(); } return st; @@ -226,7 +227,7 @@ sys_mbox_new(int size) { struct sys_mbox *mbox; - mbox = malloc(sizeof(struct sys_mbox)); + mbox = (struct sys_mbox *)Malloc(sizeof(struct sys_mbox)); if (mbox != NULL) { mbox->first = mbox->last = 0; mbox->mail = sys_sem_new_(0); @@ -257,7 +258,7 @@ sys_mbox_free(struct sys_mbox *mbox) mbox->mail = mbox->mutex = NULL; /* LWIP_DEBUGF("sys_mbox_free: mbox 0x%lx\n", mbox); */ - free(mbox); //!!!! need change + Free(mbox); } } /*-----------------------------------------------------------------------------------*/ @@ -420,7 +421,7 @@ sys_sem_new_(u8_t count) { struct sys_sem *sem; - sem = V3_Malloc(sizeof(struct sys_sem)); + sem = (struct sys_sem *)Malloc(sizeof(struct sys_sem)); if (sem != NULL) { sem->c = count; @@ -436,20 +437,21 @@ sys_sem_new_(u8_t count) /*-----------------------------------------------------------------------------------*/ static u32_t -cond_wait(struct Condition *cond, struct Mutex *mutex, u32_t timeout /* pthread_cond_t *cond, pthread_mutex_t *mutex, u32_t timeout */) +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 */) { - int tdiff; - unsigned long sec, usec; + ulong_t tdiff, msec; + + /* struct timeval rtime1, rtime2; struct timespec ts; struct timezone tz; + */ int retval; - - //!!!!!!!!!!!!! OK, we need a timerly-condition wait here if (timeout > 0) { + /* Get a timestamp and add the timeout value. */ - gettimeofday(&rtime1, &tz); + /*gettimeofday(&rtime1, &tz); sec = rtime1.tv_sec; usec = rtime1.tv_usec; usec += timeout % 1000 * 1000; @@ -457,16 +459,27 @@ cond_wait(struct Condition *cond, struct Mutex *mutex, u32_t timeout /* pthread_ usec = usec % 1000000; ts.tv_nsec = usec * 1000; ts.tv_sec = sec; - - retval = pthread_cond_timedwait(cond, mutex, &ts); + */ + msec = clock_time(); + + if (Cond_Wait_Timeout(cond, mutex, timeout) == 1) //return due to timeout + retval = ETIMEDOUT; + + //retval = pthread_cond_timedwait(cond, mutex, &ts); if (retval == ETIMEDOUT) { return SYS_ARCH_TIMEOUT; } else { /* Calculate for how long we waited for the cond. */ + + /* gettimeofday(&rtime2, &tz); tdiff = (rtime2.tv_sec - rtime1.tv_sec) * 1000 + (rtime2.tv_usec - rtime1.tv_usec) / 1000; + */ + tdiff = clock_time(); + + tdiff -= msec; if (tdiff <= 0) { return 0; @@ -502,7 +515,7 @@ sys_arch_sem_wait(struct sys_sem *sem, u32_t timeout) } /* pthread_mutex_unlock(&(sem->mutex)); return time; */ - } else { //!!!!!!! this else should be paired with if (time == SYS_ARCH_TIMEOUT) ????? + } else { // if timeout = 0 cond_wait(&(sem->cond), &(sem->mutex), 0); } } @@ -554,10 +567,11 @@ sys_sem_free_(struct sys_sem *sem) //pthread_mutex_destroy(&(sem->mutex)); Mutex_Destroy(&(sem->mutex)); - V3_Free(sem); + Free(sem); } -//return time, back to this later +#if 0 +//return time, we do not need this /*-----------------------------------------------------------------------------------*/ unsigned long sys_unix_now() @@ -574,12 +588,18 @@ sys_unix_now() return msec; } +#endif + /*-----------------------------------------------------------------------------------*/ void sys_init() { - struct timezone tz; - gettimeofday(&starttime, &tz); + //struct timezone tz; + //gettimeofday(&starttime, &tz); + + Mutex_Init(&threads_mutex); + Mutex_Init(&lwprot_mutex); + } /*-----------------------------------------------------------------------------------*/ struct sys_timeouts * @@ -611,12 +631,12 @@ sys_arch_protect(void) /* Note that for the UNIX port, we are using a lightweight mutex, and our * own counter (which is locked by the mutex). The return code is not actually * used. */ - if (lwprot_thread != pthread_self()) + if (lwprot_thread != current_thread() /*lwprot_thread != pthread_self()*/) { /* We are locking the mutex where it has not been locked before * * or is being locked by another thread */ - pthread_mutex_lock(&lwprot_mutex); - lwprot_thread = pthread_self(); + Mutex_Lock(&lwprot_mutex); + lwprot_thread = current_thread(); lwprot_count = 1; } else @@ -635,12 +655,12 @@ an operating system. void sys_arch_unprotect(sys_prot_t pval) { - if (lwprot_thread == pthread_self()) + if (lwprot_thread == current_thread()) { if (--lwprot_count == 0) { - lwprot_thread = (pthread_t) 0xDEAD; - pthread_mutex_unlock(&lwprot_mutex); + lwprot_thread = (Kernel_Thread) 0xDEAD; + Mutex_Unlock(&lwprot_mutex); } } }