ubthread.c

Go to the documentation of this file.
00001 /*****************************************************************************************
00002  Copyright (c) 2002-2004 The UbixOS Project
00003  All rights reserved.
00004 
00005  Redistribution and use in source and binary forms, with or without modification, are
00006  permitted provided that the following conditions are met:
00007 
00008  Redistributions of source code must retain the above copyright notice, this list of
00009  conditions, the following disclaimer and the list of authors.  Redistributions in binary
00010  form must reproduce the above copyright notice, this list of conditions, the following
00011  disclaimer and the list of authors in the documentation and/or other materials provided
00012  with the distribution. Neither the name of the UbixOS Project nor the names of its
00013  contributors may be used to endorse or promote products derived from this software
00014  without specific prior written permission.
00015 
00016  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
00017  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00018  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
00019  THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00020  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00021  OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00022  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
00023  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00024  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00025 
00026  $Id$
00027 
00028 *****************************************************************************************/
00029 
00030 /* All these must be converted to be done atomically */
00031 
00032 #include <ubixos/ubthread.h>
00033 #include <ubixos/vitals.h>
00034 #include <ubixos/exec.h>
00035 #include <ubixos/sched.h>
00036 #include <ubixos/vitals.h>
00037 #include <ubixos/time.h>
00038 #include <ubixos/spinlock.h>
00039 #include <lib/kmalloc.h>
00040 #include <lib/kprintf.h>
00041 
00042 struct ubthread_cond_list *conds = 0x0;
00043 struct ubthread_mutex_list *mutex = 0x0;
00044 
00045 kTask_t *ubthread_self() {
00046   return(_current);
00047   }
00048 
00049 int ubthread_cond_init(ubthread_cond_t *cond,const uInt32 attr) { 
00050   ubthread_cond_t  ubcond = kmalloc(sizeof(struct ubthread_cond));
00051   ubcond->id     = (int)cond;
00052   ubcond->locked = UNLOCKED;
00053   *cond = ubcond;
00054   return(0x0);
00055   }
00056 
00057 int ubthread_mutex_init(ubthread_mutex_t *mutex,const uInt32 attr) { 
00058   ubthread_mutex_t ubmutex = kmalloc(sizeof(struct ubthread_mutex));
00059   ubmutex->id     = (int)mutex;
00060   ubmutex->locked = UNLOCKED;
00061   *mutex = ubmutex;
00062   return(0x0);
00063   }
00064 
00065 int ubthread_cond_destroy(ubthread_cond_t *cond) {
00066   kfree(*cond);
00067   *cond = 0x0;
00068   return(0x0);
00069   }
00070 
00071 int ubthread_mutex_destroy(ubthread_mutex_t *mutex) {
00072   kfree(*mutex);
00073   *mutex = 0x0;
00074   return(0x0);
00075   }
00076 
00077 int ubthread_create(kTask_t **thread,const uInt32 *attr,void (* tproc)(void), void *arg) {
00078   *thread = (void *)execThread(tproc,(int)(kmalloc(0x2000)+0x2000),arg);
00079   return(0x0);
00080   }
00081 
00082 int ubthread_mutex_lock(ubthread_mutex_t *mutex) {
00083   ubthread_mutex_t ubmutex = *mutex;
00084   if (ubmutex->locked == LOCKED) {
00085     kprintf("Mutex Already Lock By %x Trying To Be Relocked By %x\n",ubmutex->pid,_current->id);
00086     while (ubmutex->locked == LOCKED);
00087     }
00088   ubmutex->locked = LOCKED;
00089   ubmutex->pid    = _current->id;
00090   return(0x0);
00091   }
00092 
00093 int ubthread_mutex_unlock(ubthread_mutex_t *mutex) {
00094   ubthread_mutex_t ubmutex = *mutex;
00095  if (ubmutex->pid == _current->id) {
00096     ubmutex->locked = UNLOCKED;
00097     return(0x0);
00098     }
00099   else {
00100     //kprintf("Trying To Unlock Mutex From No Locking Thread\n");
00101     ubmutex->locked = UNLOCKED;
00102     return(-1);
00103     }
00104   }
00105 
00106 int ubthread_cond_timedwait(ubthread_cond_t *cond, ubthread_mutex_t *mutex, const struct timespec *abstime) {
00107   ubthread_cond_t  ubcond  = *cond;
00108   ubthread_mutex_t ubmutex = *mutex;
00109   uInt32 enterTime = systemVitals->sysUptime+20;
00110   while (enterTime > systemVitals->sysUptime) {
00111     if (ubcond->locked == UNLOCKED) break;
00112     sched_yield();
00113     }
00114   ubmutex->locked = UNLOCKED;
00115   return(0x0);
00116   }
00117 
00118 int ubthread_cond_wait(ubthread_cond_t *cond, ubthread_mutex_t *mutex) {
00119   ubthread_cond_t  ubcond  = *cond;
00120   ubthread_mutex_t ubmutex = *mutex;
00121   while (ubcond->locked == LOCKED) sched_yield();
00122   ubmutex->locked = UNLOCKED;
00123   return(0x0);
00124   }
00125 
00126 int ubthread_cond_signal(ubthread_cond_t *cond) {
00127   ubthread_cond_t ubcond = *cond;
00128   ubcond->locked = UNLOCKED;
00129   return(0x0);
00130   }
00131 
00132 /***
00133  END
00134  ***/
00135 

Generated on Sun Dec 3 02:38:04 2006 for UbixOS V2 by  doxygen 1.4.7