UbixOS  2.0
ubthread.c
Go to the documentation of this file.
1 /*-
2  * Copyright (c) 2002-2018 The UbixOS Project.
3  * All rights reserved.
4  *
5  * This was developed by Christopher W. Olsen for the UbixOS Project.
6  *
7  * Redistribution and use in source and binary forms, with or without modification, are permitted
8  * provided that the following conditions are met:
9  *
10  * 1) Redistributions of source code must retain the above copyright notice, this list of
11  * conditions, the following disclaimer and the list of authors.
12  * 2) Redistributions in binary form must reproduce the above copyright notice, this list of
13  * conditions, the following disclaimer and the list of authors in the documentation and/or
14  * other materials provided with the distribution.
15  * 3) Neither the name of the UbixOS Project nor the names of its contributors may be used to
16  * endorse or promote products derived from this software without specific prior written
17  * permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
20  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /* All these must be converted to be done atomically */
30 
31 #include <ubixos/ubthread.h>
32 #include <ubixos/exec.h>
33 #include <ubixos/sched.h>
34 #include <ubixos/time.h>
35 #include <ubixos/spinlock.h>
36 #include <ubixos/vitals.h>
37 #include <lib/kmalloc.h>
38 #include <lib/kprintf.h>
39 #include <ubixos/kpanic.h>
40 #include <sys/stdatomic.h>
41 
42 struct ubthread_cond_list *conds = 0x0;
43 struct ubthread_mutex_list *mutex = 0x0;
44 
46  return (_current);
47 }
48 
50  ubthread_cond_t ubcond = kmalloc(sizeof(struct ubthread_cond));
51  memset(ubcond, 0x0, sizeof(struct ubthread_cond));
52 
53  ubcond->id = (int) cond;
54  ubcond->lock = ATOMIC_VAR_INIT(0);
55 
56  *cond = ubcond;
57  return (0x0);
58 }
59 
61  ubthread_mutex_t ubmutex = kmalloc(sizeof(struct ubthread_mutex));
62  memset(ubmutex, 0x0, sizeof(struct ubthread_mutex));
63 
64  ubmutex->id = (int) mutex;
65  ubmutex->lock = ATOMIC_VAR_INIT(0);
66 
67  *mutex = ubmutex;
68  return (0x0);
69 }
70 
72  kfree(*cond);
73  *cond = 0x0;
74  return (0x0);
75 }
76 
78  kfree(*mutex);
79  *mutex = 0x0;
80  return (0x0);
81 }
82 
83 int ubthread_create(kTask_t **thread, const uInt32 *attr, void (*tproc)(void), void *arg) {
84  *thread = (void *) execThread(tproc,0x2000, arg);
85  return (0x0);
86 }
87 
89  ubthread_mutex_t ubmutex = *mutex;
90 
91  if (ubmutex->lock == TRUE && ubmutex->pid == _current->id) {
92  kprintf("Mutex Already Locked By This Thread");
93  kpanic("WHY?");
94  return (0x0);
95  }
96 
97  while (1) {
98  if (xchg_32(&ubmutex->lock, TRUE) == FALSE)
99  break;
100 
101  while (ubmutex->lock == TRUE)
102  sched_yield();
103  }
104 
105  ubmutex->pid = _current->id;
106  return (0x0);
107 }
108 
110  ubthread_mutex_t ubmutex = *mutex;
111 
112  if (ubmutex->lock != TRUE)
113  kpanic("NOT LOCKED?");
114 
115  if (ubmutex->pid != _current->id)
116  kprintf("Trying To Unlock Mutex From No Locking Thread[%i - %i:0x%X]\n", ubmutex->pid, _current->id, *ubmutex);
117 
118  while (1) {
119  if (xchg_32(&ubmutex->lock, FALSE) == TRUE)
120  break;
121  while (ubmutex->lock == FALSE)
122  sched_yield();
123  }
124 
125  ubmutex->pid = 0x0;
126  return (0x0);
127 }
128 
130  ubthread_cond_t ubcond = *cond;
131  ubthread_mutex_t ubmutex = *mutex;
132 
133  uint32_t enterTime = systemVitals->sysUptime + 20;
134 
136 
137  while (enterTime > systemVitals->sysUptime) {
138  if (ubcond->lock == FALSE)
139  break;
140  sched_yield();
141  }
142 
144 
145  return (0x0);
146 }
147 
149  ubthread_cond_t ubcond = *cond;
151  while (ubcond->lock == TRUE)
152  sched_yield();
154  return (0x0);
155 }
156 
158  ubthread_cond_t ubcond = *cond;
159  while (xchg_32(&ubcond->lock, FALSE))
160  sched_yield();
161  return (0x0);
162 }
163 
165  ubthread_cond_t ubcond = *cond;
166  while (xchg_32(&ubcond->lock, FALSE))
167  sched_yield();
168  return (0x0);
169 }
170 
taskStruct
Definition: sched.h:62
ATOMIC_VAR_INIT
#define ATOMIC_VAR_INIT(value)
Definition: stdatomic.h:89
ubthread_mutex_unlock
int ubthread_mutex_unlock(ubthread_mutex_t *mutex)
Definition: ubthread.c:109
spinlock.h
ubthread_mutex_lock
int ubthread_mutex_lock(ubthread_mutex_t *mutex)
Definition: ubthread.c:88
ubthread_cond_signal
int ubthread_cond_signal(ubthread_cond_t *cond)
Definition: ubthread.c:157
ubthread_mutex
Definition: ubthread.h:52
ubthread_self
kTask_t * ubthread_self()
Definition: ubthread.c:45
uInt32
unsigned long int uInt32
Definition: objgfx30.h:49
ubthread_mutex::id
int id
Definition: ubthread.h:53
TRUE
Definition: types.h:82
ubthread_create
int ubthread_create(kTask_t **thread, const uInt32 *attr, void(*tproc)(void), void *arg)
Definition: ubthread.c:83
ubthread_mutex_list
Definition: ubthread.h:68
FALSE
Definition: types.h:82
kfree
void kfree(void *baseAddr)
Definition: kmalloc.c:342
ubthread_mutex_init
int ubthread_mutex_init(ubthread_mutex_t *mutex, const uint32_t attr)
Definition: ubthread.c:60
thread
Definition: thread.h:40
exec.h
sched.h
kpanic
void kpanic(const char *fmt,...)
print panic message and halt system
Definition: kpanic.c:41
ubthread_cond::id
int id
Definition: ubthread.h:48
kpanic.h
taskStruct::id
pidType id
Definition: sched.h:63
systemVitals
vitalsNode * systemVitals
Definition: vitals.c:35
kprintf.h
mutex
struct ubthread_mutex_list * mutex
Definition: ubthread.c:43
time.h
vitals.h
ubthread_mutex_destroy
int ubthread_mutex_destroy(ubthread_mutex_t *mutex)
Definition: ubthread.c:77
ubthread.h
ubthread_cond_wait
int ubthread_cond_wait(ubthread_cond_t *cond, ubthread_mutex_t *mutex)
Definition: ubthread.c:148
uint32_t
__uint32_t uint32_t
Definition: types.h:46
ubthread_cond_init
int ubthread_cond_init(ubthread_cond_t *cond, const uint32_t attr)
Definition: ubthread.c:49
execThread
uInt32 execThread(void(*tproc)(void), uInt32 stack, char *arg)
Definition: exec.c:66
ubthread_cond_list
Definition: ubthread.h:63
_current
kTask_t * _current
Definition: sched.c:50
ubthread_mutex::lock
bool lock
Definition: ubthread.h:54
stdatomic.h
ubthread_cond_timedwait
int ubthread_cond_timedwait(ubthread_cond_t *cond, ubthread_mutex_t *mutex, const struct timespec *abstime)
Definition: ubthread.c:129
kmalloc
void * kmalloc(uInt32 len)
Definition: kmalloc.c:241
memset
void * memset(void *dst, int c, size_t length)
ubthread_cond_destroy
int ubthread_cond_destroy(ubthread_cond_t *cond)
Definition: ubthread.c:71
ubthread_cond
Definition: ubthread.h:47
vitalsStruct::sysUptime
uint32_t sysUptime
Definition: vitals.h:38
conds
struct ubthread_cond_list * conds
Definition: ubthread.c:42
ubthread_mutex::pid
pidType pid
Definition: ubthread.h:55
ubthread_cond::lock
bool lock
Definition: ubthread.h:49
timespec
Definition: _timespec.h:11
kprintf
int kprintf(const char *,...)
Definition: kprintf.c:259
ubthread_cond_broadcast
int ubthread_cond_broadcast(ubthread_cond_t *cond)
Definition: ubthread.c:164
kmalloc.h
sched_yield
void sched_yield()
Definition: sched.c:244