- Main Page
- Data Structures
- Files
- Directories

- Search for |

- File List
- Globals

# 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: ubthread_8c-source.html 88 2016-01-12 00:11:29Z reddawg $
    00027
    00028 *****************************************************************************************/
    00029
    00030 /* All these must be converted to be done atomically */
    00031
    00032 #include <ubixos/ubthread.h>
    00033 #include <ubixos/exec.h>
    00034 #include <ubixos/sched.h>
    00035 #include <ubixos/time.h>
    00036 #include <ubixos/spinlock.h>
    00037 #include <ubixos/vitals.h>
    00038 #include <lib/kmalloc.h>
    00039 #include <lib/kprintf.h>
    00040
    00041 struct ubthread_cond_list *conds = 0x0;
    00042 struct ubthread_mutex_list *mutex = 0x0;
    00043
    00044 kTask_t *ubthread_self() {
    00045   return(_current);
    00046   }
    00047
    00048 int ubthread_cond_init(ubthread_cond_t *cond,const uInt32 attr) {
    00049   ubthread_cond_t  ubcond = kmalloc(sizeof(struct ubthread_cond));
    00050   ubcond->id     = (int)cond;
    00051   ubcond->locked = UNLOCKED;
    00052   *cond = ubcond;
    00053   return(0x0);
    00054   }
    00055
    00056 int ubthread_mutex_init(ubthread_mutex_t *mutex,const uInt32 attr) {
    00057   ubthread_mutex_t ubmutex = kmalloc(sizeof(struct ubthread_mutex));
    00058   ubmutex->id     = (int)mutex;
    00059   ubmutex->locked = UNLOCKED;
    00060   *mutex = ubmutex;
    00061   return(0x0);
    00062   }
    00063
    00064 int ubthread_cond_destroy(ubthread_cond_t *cond) {
    00065   kfree(*cond);
    00066   *cond = 0x0;
    00067   return(0x0);
    00068   }
    00069
    00070 int ubthread_mutex_destroy(ubthread_mutex_t *mutex) {
    00071   kfree(*mutex);
    00072   *mutex = 0x0;
    00073   return(0x0);
    00074   }
    00075
    00076 int ubthread_create(kTask_t **thread,const uInt32 *attr,void (* tproc)(void), void *arg) {
    00077   *thread = (void *)execThread(tproc,(int)(kmalloc(0x2000)+0x2000),arg);
    00078   return(0x0);
    00079   }
    00080
    00081 int ubthread_mutex_lock(ubthread_mutex_t *mutex) {
    00082   ubthread_mutex_t ubmutex = *mutex;
    00083   if (ubmutex->locked == LOCKED) {
    00084     kprintf("Mutex Already Lock By %x Trying To Be Relocked By %x\n",ubmutex->pid,_current->id);
    00085     while (ubmutex->locked == LOCKED);
    00086     }
    00087   ubmutex->locked = LOCKED;
    00088   ubmutex->pid    = _current->id;
    00089   return(0x0);
    00090   }
    00091
    00092 int ubthread_mutex_unlock(ubthread_mutex_t *mutex) {
    00093   ubthread_mutex_t ubmutex = *mutex;
    00094  if (ubmutex->pid == _current->id) {
    00095     ubmutex->locked = UNLOCKED;
    00096     return(0x0);
    00097     }
    00098   else {
    00099     //kprintf("Trying To Unlock Mutex From No Locking Thread\n");
    00100     ubmutex->locked = UNLOCKED;
    00101     return(-1);
    00102     }
    00103   }
    00104
    00105 int ubthread_cond_timedwait(ubthread_cond_t *cond, ubthread_mutex_t *mutex, const struct timespec *abstime) {
    00106   ubthread_cond_t  ubcond  = *cond;
    00107   ubthread_mutex_t ubmutex = *mutex;
    00108   uInt32 enterTime = systemVitals->sysUptime+20;
    00109   while (enterTime > systemVitals->sysUptime) {
    00110     if (ubcond->locked == UNLOCKED) break;
    00111     sched_yield();
    00112     }
    00113   ubmutex->locked = UNLOCKED;
    00114   return(0x0);
    00115   }
    00116
    00117 int ubthread_cond_wait(ubthread_cond_t *cond, ubthread_mutex_t *mutex) {
    00118   ubthread_cond_t  ubcond  = *cond;
    00119   ubthread_mutex_t ubmutex = *mutex;
    00120   while (ubcond->locked == LOCKED) sched_yield();
    00121   ubmutex->locked = UNLOCKED;
    00122   return(0x0);
    00123   }
    00124
    00125 int ubthread_cond_signal(ubthread_cond_t *cond) {
    00126   ubthread_cond_t ubcond = *cond;
    00127   ubcond->locked = UNLOCKED;
    00128   return(0x0);
    00129   }
    00130
    00131 /***
    00132  END
    00133  ***/
    00134