00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
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
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
00133
00134