sys_arch.c
ubixos/types.h
ubixos/sched.h
ubixos/ubthread.h
ubixos/kpanic.h
lib/kprintf.h
lib/kmalloc.h
net/debug.h
net/sys.h
net/opt.h
net/stats.h
ubixos/spinlock.h
sys/thread.h
ubixos/kpanic.h
net/stats.h
ubixos/sched.h
ubixos/spinlock.h
src/sys/net/net/sys_arch.c
arch/sys_arch.h
ubixos/types.h
sys/_types.h
ubixos/time.h
vfs/file.h
sys/tss.h
lib/kmalloc.h
net/lwipopts.h
net/memp.h
vfs/vfs.h
vfs/mount.h
lib/kprintf.h
ubixos/ubthread.h
ubixfs/dirCache.h
net/opt.h
sys/sysproto.h
ubixos/elf.h
sys/signal.h
net/sys.h
ubixos/tty.h
sys/io.h
net/debug.h
arch/cc.h
sys_mbox
sys_mbox_msg
sys_sem
sys_thread
thread_start_param
SYS_MBOX_SIZE
100
sys_arch_mbox_fetch
sys_mbox_post
UMAX
a
b
((a) > (b) ? (a) : (b))
uInt16 static uInt16 cond_wait (ubthread_cond_t *cond, ubthread_mutex_t *mutex, uInt16 timeout) cond_wait ubthread_cond_t * cond ubthread_mutex_t * mutex uInt16 timeout ETIMEDOUT gettimeofday mutex timespec::tv_nsec timespec::tv_sec timeval::tv_sec timeval::tv_usec ubthread_cond_timedwait ubthread_cond_wait sys_arch_sem_wait
struct sys_thread * static struct sys_thread* current_thread (void) current_thread void netThreadSpinlock sys_thread::next NULL spinLock spinUnlock threads sys_thread::ubthread ubthread_self sys_arch_timeouts
uInt16 uInt16 sys_arch_mbox_fetch (struct sys_mbox *mbox, void **msg, uInt16 timeout) sys_arch_mbox_fetch struct sys_mbox * mbox void ** msg uInt16 timeout sys_mbox::first sys_mbox::last sys_mbox::mail mbox sys_mbox::msgs sys_mbox::mutex NULL sys_arch_sem_wait SYS_MBOX_SIZE sys_sem_signal
uInt16 uInt16 sys_arch_sem_wait (struct sys_sem *sem, uInt16 timeout) sys_arch_sem_wait struct sys_sem * sem uInt16 timeout sys_sem::c sys_sem::cond cond_wait sys_sem::mutex ubthread_mutex_lock ubthread_mutex_unlock
sys_timeouts * struct sys_timeouts* sys_arch_timeouts (void) sys_arch_timeouts void current_thread sys_thread::timeouts
void void sys_init () sys_init gettimeofday starttime net_init
void void sys_mbox_free (struct sys_mbox *mbox) sys_mbox_free struct sys_mbox * mbox kfree sys_mbox::mail mbox sys_mbox::mutex NULL SYS_MBOX_NULL sys_sem_free_ sys_sem_wait
sys_mbox * struct sys_mbox* sys_mbox_new () sys_mbox_new sys_mbox::first kmalloc sys_mbox::last sys_mbox::mail mbox sys_mbox::mutex sys_sem_new_ accept_function do_listen netconn_bind netconn_connect netconn_listen netconn_new tcpip_init
void void sys_mbox_post (struct sys_mbox *mbox, void *msg) sys_mbox_post struct sys_mbox * mbox void * msg sys_mbox::first sys_mbox::last sys_mbox::mail mbox sys_mbox::msgs sys_mbox::mutex SYS_MBOX_SIZE sys_sem_signal sys_sem_wait
void void sys_sem_free (struct sys_sem *sem) sys_sem_free struct sys_sem * sem sys_sem_free_ SYS_SEM_NULL
void static void sys_sem_free_ (struct sys_sem *sem) sys_sem_free_ struct sys_sem * sem sys_sem::cond kfree sys_sem::mutex ubthread_cond_destroy ubthread_mutex_destroy sys_mbox_free sys_sem_free
sys_sem * struct sys_sem* sys_sem_new (uInt8 count) sys_sem_new uInt8 count sys_sem_new_ accept_function netconn_write netMainThread
struct sys_sem * static struct sys_sem * sys_sem_new_ (uInt8 count) sys_sem_new_ uInt8 count sys_sem::c sys_sem::cond kmalloc sys_sem::mutex NULL ubthread_cond_init ubthread_mutex_init sys_mbox_new sys_sem_new
void void sys_sem_signal (struct sys_sem *sem) sys_sem_signal struct sys_sem * sem sys_sem::c sys_sem::cond sys_sem::mutex ubthread_cond_signal ubthread_mutex_lock ubthread_mutex_unlock
void void sys_thread_new (void(*function)(void), void *arg) sys_thread_new void(*)(void) function void * arg kmalloc kpanic kprintf netThreadSpinlock sys_timeouts::next sys_thread::next NULL spinLock spinUnlock threads sys_thread::timeouts sys_thread::ubthread ubthread_create bot_init low_level_init net_init shell_init tcpip_init udpecho_init
unsigned long unsigned long sys_unix_now () sys_unix_now gettimeofday starttime timeval::tv_sec timeval::tv_usec
spinLock_t spinLock_t netThreadSpinlock netThreadSpinlock SPIN_LOCK_INITIALIZER current_thread sys_thread_new
struct timeval struct timeval starttime starttime sys_init sys_unix_now
struct sys_thread * struct sys_thread* threads threads 0x0 current_thread sys_thread_new

    /*
    *Copyright(c)2001,SwedishInstituteofComputerScience.
    *Allrightsreserved.
    *
    *Redistributionanduseinsourceandbinaryforms,withorwithout
    *modification,arepermittedprovidedthatthefollowingconditions
    *aremet:
    *1.Redistributionsofsourcecodemustretaintheabovecopyright
    *notice,thislistofconditionsandthefollowingdisclaimer.
    *2.Redistributionsinbinaryformmustreproducetheabovecopyright
    *notice,thislistofconditionsandthefollowingdisclaimerinthe
    *documentationand/orothermaterialsprovidedwiththedistribution.
    *3.NeitherthenameoftheInstitutenorthenamesofitscontributors
    *maybeusedtoendorseorpromoteproductsderivedfromthissoftware
    *withoutspecificpriorwrittenpermission.
    *
    *THISSOFTWAREISPROVIDEDBYTHEINSTITUTEANDCONTRIBUTORS``ASIS''AND
    *ANYEXPRESSORIMPLIEDWARRANTIES,INCLUDING,BUTNOTLIMITEDTO,THE
    *IMPLIEDWARRANTIESOFMERCHANTABILITYANDFITNESSFORAPARTICULARPURPOSE
    *AREDISCLAIMED.INNOEVENTSHALLTHEINSTITUTEORCONTRIBUTORSBELIABLE
    *FORANYDIRECT,INDIRECT,INCIDENTAL,SPECIAL,EXEMPLARY,ORCONSEQUENTIAL
    *DAMAGES(INCLUDING,BUTNOTLIMITEDTO,PROCUREMENTOFSUBSTITUTEGOODS
    *ORSERVICES;LOSSOFUSE,DATA,ORPROFITS;ORBUSINESSINTERRUPTION)
    *HOWEVERCAUSEDANDONANYTHEORYOFLIABILITY,WHETHERINCONTRACT,STRICT
    *LIABILITY,ORTORT(INCLUDINGNEGLIGENCEOROTHERWISE)ARISINGINANYWAY
    *OUTOFTHEUSEOFTHISSOFTWARE,EVENIFADVISEDOFTHEPOSSIBILITYOF
    *SUCHDAMAGE.
    *
    *ThisfileispartofthelwIPTCP/IPstack.
    *
    *Author:AdamDunkels<adam@sics.se>
    *SubAuthor:ChristopherOlsen<colsen@domaintlantic.com>
    *
    *Notes:
    *Modifiedtoworkwiththeubixoperatingsystem
    *
    *$Log: sys__arch_8c.xml,v $
    *Revision 1.4  2006/12/12 14:09:16  reddawg
    *Changes
    *
    *Revision1.1.1.12006/06/0112:46:16reddawg
    *ubix2
    *
    *Revision1.22005/10/1200:13:37reddawg
    *Removed
    *
    *Revision1.1.1.12005/09/2617:24:31reddawg
    *nomessage
    *
    *Revision1.62004/09/1121:30:37apwillia
    *Fixraceconditionsinnetthreadandscheduler
    *
    *Revision1.52004/09/0720:58:35reddawg
    *timetorollbackican'tthinkstraightbyfriday
    *
    *Revision1.42004/05/2522:49:29reddawg
    *StupidOldCODE!!!
    *
    *Revision1.32004/05/1904:07:43reddawg
    *kmalloc(size,pid)nomoreitisnokmalloc(size);thewayitshouldofbeen
    *
    *Revision1.22004/05/1903:35:02reddawg
    *FixedAFewOrderingIssuesInTheServiceStartupRoutine
    *
    *Revision1.1.1.12004/04/1512:07:14reddawg
    *UbixOSv1.0
    *
    *Revision1.132004/04/1321:29:53reddawg
    *Wenowhavesocketsworking.Lotsoffunctionalitytobeaddedtocontinually
    *improveontheexistinglayersnowitscleanuptimetogetthingsinabetter
    *workingorder.
    *
    *Revision1.122004/04/1316:08:07reddawg
    *Removedalloftheolddebugcodetheproblemseemstobeinubthreadswith
    *ubthread_mutex_init
    *
    *Revision1.112004/04/1316:05:40reddawg
    *FunctionRenaming
    *
    *
    *
    *$Id: sys__arch_8c.xml 88 2016-01-12 00:11:29Z reddawg $
    */

    #include<ubixos/types.h>
    #include<ubixos/sched.h>
    #include<ubixos/ubthread.h>
    #include<ubixos/kpanic.h>
    #include<lib/kprintf.h>
    #include<lib/kmalloc.h>

    #include"net/debug.h"
    #include"net/sys.h"
    #include"net/opt.h"
    #include"net/stats.h"

    #include<ubixos/spinlock.h>

    #defineUMAX(a,b)((a)>(b)?(a):(b))

    staticstructsys_thread*threads=0x0;
    staticspinLock_tnetThreadSpinlock=SPIN_LOCK_INITIALIZER;

    structsys_mbox_msg{
    structsys_mbox_msg*next;
    void*msg;
    };

    #defineSYS_MBOX_SIZE100

    structsys_mbox{
    uInt16first,last;
    void*msgs[SYS_MBOX_SIZE];
    structsys_sem*mail;
    structsys_sem*mutex;
    };

    structsys_sem{
    unsignedintc;
    ubthread_cond_tcond;
    ubthread_mutex_tmutex;
    };

    structsys_thread{
    structsys_thread*next;
    structsys_timeoutstimeouts;
    kTask_t*ubthread;
    };

    staticstructtimevalstarttime;

    staticstructsys_sem*sys_sem_new_(uInt8count);
    staticvoidsys_sem_free_(structsys_sem*sem);

    staticuInt16cond_wait(ubthread_cond_t*cond,ubthread_mutex_t*mutex,uInt16timeout);

    staticstructsys_thread*current_thread(void){
    structsys_thread*st;
    kTask_t*pt;
    pt=ubthread_self();
    spinLock(&netThreadSpinlock);
    for(st=threads;st!=NULL;st=st->next){
    if(st->ubthread==pt){
    spinUnlock(&netThreadSpinlock);
    returnst;
    }
    }
    spinUnlock(&netThreadSpinlock);
    kprintf("sys:current_thread:couldnotfindcurrentthread!\n");
    kprintf("ThisisduetoaraceconditionintheLinuxThreads\n");
    kprintf("ubthreadsimplementation.Starttheprogramagain.\n");

    kpanic("ABORT");
    return(0x0);
    }

    structthread_start_param{
    structsys_thread*thread;
    void(*function)(void*);
    void*arg;
    };

    /*
    staticvoid*thread_start(void*arg){
    structthread_start_param*tp=arg;
    tp->thread->ubthread=ubthread_self();
    tp->function(tp->arg);
    kfree(tp);
    return(NULL);
    }
    */

    voidsys_thread_new(void(*function)(void),void*arg){
    structsys_thread*thread=0x0;
    //structthread_start_param*thread_param;
    kprintf("sys_thread:[0x%X]\n",sizeof(structsys_thread));

    thread=kmalloc(sizeof(structsys_thread));
    kprintf("THREAD:[0x%X]\n",thread);
    spinLock(&netThreadSpinlock);
    thread->next=threads;
    thread->timeouts.next=NULL;
    thread->ubthread=0x0;
    threads=thread;
    spinUnlock(&netThreadSpinlock);

    /*
    thread_param=kmalloc(sizeof(structthread_start_param));

    thread_param->function=function;
    thread_param->arg=arg;
    thread_param->thread=thread;
    */
    //execThread((void*)function,0x0,0x0);

    kprintf("thread->ubthread:[0x%X]\n",thread->ubthread);
    if(ubthread_create(&thread->ubthread,0x0,(void*)(function),arg)!=0x0){
    kpanic("sys_thread_new:ubthread_create");
    }
    kprintf("thread->ubthread:[0x%X]\n",thread->ubthread);

    }

    structsys_mbox*sys_mbox_new(){
    structsys_mbox*mbox;

    mbox=kmalloc(sizeof(structsys_mbox));
    mbox->first=mbox->last=0;
    mbox->mail=sys_sem_new_(0);
    mbox->mutex=sys_sem_new_(1);

    return(mbox);
    }

    voidsys_mbox_free(structsys_mbox*mbox){
    if(mbox!=SYS_MBOX_NULL){
    sys_sem_wait(mbox->mutex);

    sys_sem_free_(mbox->mail);
    sys_sem_free_(mbox->mutex);
    mbox->mail=mbox->mutex=NULL;
    //kprintf("sys_mbox_free:mbox0x%lx\n",mbox);
    kfree(mbox);
    }
    }

    voidsys_mbox_post(structsys_mbox*mbox,void*msg){
    uInt8first;

    sys_sem_wait(mbox->mutex);

    //kprintf("sys_mbox_post:mbox%pmsg%p\n",mbox,msg);

    mbox->msgs[mbox->last]=msg;

    if(mbox->last==mbox->first){
    first=1;
    }
    else{
    first=0;
    }

    mbox->last++;
    if(mbox->last==SYS_MBOX_SIZE){
    mbox->last=0;
    }

    if(first){
    sys_sem_signal(mbox->mail);
    }
    sys_sem_signal(mbox->mutex);
    }

    uInt16sys_arch_mbox_fetch(structsys_mbox*mbox,void**msg,uInt16timeout){
    uInt16time=1;

    /*Themutexlockisquicksowedon'tbotherwiththetimeout
    stuffhere.*/
    sys_arch_sem_wait(mbox->mutex,0);

    while(mbox->first==mbox->last){
    sys_sem_signal(mbox->mutex);

    /*Weblockwhilewaitingforamailtoarriveinthemailbox.We
    mustbepreparedtotimeout.*/
    if(timeout!=0){
    time=sys_arch_sem_wait(mbox->mail,timeout);

    /*Iftime==0,thesem_waittimedout,andwereturn0.*/
    if(time==0){
    return0;
    }
    }else{
    sys_arch_sem_wait(mbox->mail,0);
    }

    sys_arch_sem_wait(mbox->mutex,0);
    }

    if(msg!=NULL){
    //kprintf("sys_mbox_fetch:mbox%pmsg%p\n",mbox,*msg);
    *msg=mbox->msgs[mbox->first];
    }

    mbox->first++;
    if(mbox->first==SYS_MBOX_SIZE){
    mbox->first=0;
    }

    sys_sem_signal(mbox->mutex);

    return(time);
    }

    structsys_sem*sys_sem_new(uInt8count){
    returnsys_sem_new_(count);
    }

    staticstructsys_sem*sys_sem_new_(uInt8count){
    structsys_sem*sem;

    sem=kmalloc(sizeof(structsys_sem));
    sem->c=count;

    ubthread_cond_init(&(sem->cond),NULL);
    ubthread_mutex_init(&(sem->mutex),NULL);

    returnsem;
    }

    staticuInt16cond_wait(ubthread_cond_t*cond,ubthread_mutex_t*mutex,uInt16timeout){
    unsignedinttdiff;
    unsignedlongsec,usec;
    structtimevalrtime1,rtime2;
    structtimespects;
    structtimezonetz;
    intretval;

    if(timeout>0){
    /*Getatimestampandaddthetimeoutvalue.*/
    gettimeofday(&rtime1,&tz);
    sec=rtime1.tv_sec;
    usec=rtime1.tv_usec;
    usec+=timeout%1000*1000;
    sec+=(int)(timeout/1000)+(int)(usec/1000000);
    usec=usec%1000000;
    ts.tv_nsec=usec*1000;
    ts.tv_sec=sec;

    retval=ubthread_cond_timedwait(cond,mutex,&ts);
    if(retval==ETIMEDOUT){
    return0;
    }
    else{
    /*Calculateforhowlongwewaitedforthecond.*/
    gettimeofday(&rtime2,&tz);
    tdiff=(rtime2.tv_sec-rtime1.tv_sec)*1000+
    (rtime2.tv_usec-rtime1.tv_usec)/1000;
    if(tdiff==0){
    return1;
    }
    returntdiff;
    }
    }
    else{
    ubthread_cond_wait(cond,mutex);
    return0;
    }
    }

    uInt16sys_arch_sem_wait(structsys_sem*sem,uInt16timeout){
    uInt16time=1;
    ubthread_mutex_lock(&(sem->mutex));
    while(sem->c<=0){
    if(timeout>0){
    time=cond_wait(&(sem->cond),&(sem->mutex),timeout);
    if(time==0){
    ubthread_mutex_unlock(&(sem->mutex));
    return0;
    }
    }else{
    cond_wait(&(sem->cond),&(sem->mutex),0);
    }
    }
    sem->c--;
    ubthread_mutex_unlock(&(sem->mutex));
    return(time);
    }

    voidsys_sem_signal(structsys_sem*sem){
    ubthread_mutex_lock(&(sem->mutex));
    sem->c++;
    if(sem->c>1)
    sem->c=1;
    ubthread_cond_signal(&(sem->cond));
    ubthread_mutex_unlock(&(sem->mutex));
    }

    voidsys_sem_free(structsys_sem*sem){
    if(sem!=SYS_SEM_NULL){
    sys_sem_free_(sem);
    }
    }

    staticvoidsys_sem_free_(structsys_sem*sem){
    ubthread_cond_destroy(&(sem->cond));
    ubthread_mutex_destroy(&(sem->mutex));
    kfree(sem);
    }

    unsignedlongsys_unix_now(){
    structtimevaltv;
    structtimezonetz;
    longsec,usec;
    unsignedlongmsec;
    gettimeofday(&tv,&tz);

    sec=tv.tv_sec-starttime.tv_sec;
    usec=tv.tv_usec-starttime.tv_usec;
    msec=sec*1000+usec/1000;
    returnmsec;
    }

    voidsys_init(){
    structtimezonetz;
    gettimeofday(&starttime,&tz);
    }

    structsys_timeouts*sys_arch_timeouts(void){
    structsys_thread*thread;
    thread=current_thread();
    return(&thread->timeouts);
    }

    /***
    END
    ***/