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/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
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
00134
00135