bzr branch
http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk
|
77
by Gustav Hartvigsson
* More work on SStream... Requiers Mutex suppert :-) |
1 |
#include "Thread.h" |
2 |
||
3 |
/*
|
|
4 |
Utility functions associated with threads.
|
|
5 |
*/
|
|
6 |
void
|
|
7 |
s_usleep (slong us) { |
|
8 |
#if __WIN32__ || __WIN64__
|
|
9 |
/* We are a windows system, so we have to implement our own little sleeper.*/ |
|
|
79
by Gustav Hartvigsson
* Clean up of SMap's for each code. |
10 |
Sleep (us / 1000) |
|
77
by Gustav Hartvigsson
* More work on SStream... Requiers Mutex suppert :-) |
11 |
#else
|
12 |
/* We are not a windows system, so lets assume that we have posix's nanosleep |
|
13 |
* Taken from:
|
|
|
109.1.1
by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList? |
14 |
*/
|
|
77
by Gustav Hartvigsson
* More work on SStream... Requiers Mutex suppert :-) |
15 |
struct timespec req = {0}; |
16 |
req.tv_sec = (int)(us / 1000); |
|
17 |
req.tv_nsec = us * 1000000L; |
|
18 |
nanosleep(&req, (struct timespec *)NULL); |
|
19 |
#endif
|
|
20 |
}
|
|
21 |
||
22 |
char * |
|
|
116.1.4
by Gustav Hartvigsson
* Removed stupid, non-sense error from SLinkedList. |
23 |
s_thread_status_get_name (SThreadStatus status) { |
24 |
switch (status) { |
|
|
150
by Gustav Hartvigsson
* Fixed the tests in the CMake file |
25 |
case (S_THREAD_STATUS_SUCCESS): |
26 |
return SThreadStatusName[1]; |
|
27 |
break; |
|
28 |
case (S_THREAD_STATUS_TIMEOUT): |
|
29 |
return SThreadStatusName[2]; |
|
30 |
break; |
|
31 |
case (S_THREAD_STATUS_ERROR): |
|
32 |
return SThreadStatusName[3]; |
|
33 |
break; |
|
34 |
case (S_THREAD_STATUS_BUSY): |
|
35 |
return SThreadStatusName[4]; |
|
36 |
break; |
|
37 |
case (S_THREAD_STATUS_NO_MEM): |
|
38 |
return SThreadStatusName[5]; |
|
39 |
break; |
|
40 |
case (S_THREAD_STATUS_LAST): |
|
41 |
return SThreadStatusName[6]; |
|
42 |
break; |
|
43 |
default: |
|
44 |
return NULL; |
|
45 |
} |
|
46 |
|
|
47 |
}
|
|
|
116.1.4
by Gustav Hartvigsson
* Removed stupid, non-sense error from SLinkedList. |
48 |
|
|
77
by Gustav Hartvigsson
* More work on SStream... Requiers Mutex suppert :-) |
49 |
/* ****************************************************************************
|
50 |
********************************** SMutex ************************************
|
|
51 |
**************************************************************************** */
|
|
52 |
||
53 |
struct SMutex { |
|
54 |
mtx_t * mutex; |
|
|
116.1.4
by Gustav Hartvigsson
* Removed stupid, non-sense error from SLinkedList. |
55 |
_Atomic(sboolean) locked; |
|
87
by Gustav Hartvigsson
I have no idea why this is not working...0_o... |
56 |
};
|
|
77
by Gustav Hartvigsson
* More work on SStream... Requiers Mutex suppert :-) |
57 |
|
58 |
||
|
79
by Gustav Hartvigsson
* Clean up of SMap's for each code. |
59 |
SMutex * |
60 |
s_mutex_new () { |
|
61 |
SMutex * self = s_malloc (sizeof (SMutex)); |
|
|
121.1.3
by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set. |
62 |
|
63 |
self->mutex = s_malloc (sizeof (mtx_t)); |
|
64 |
||
65 |
atomic_init(&(self->locked), FALSE); |
|
|
88
by Gustav Hartvigsson
* Made the SMutex code compile. |
66 |
|
|
121.1.3
by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set. |
67 |
sint status = mtx_init (self->mutex, mtx_plain); |
|
116.1.4
by Gustav Hartvigsson
* Removed stupid, non-sense error from SLinkedList. |
68 |
|
|
121.1.3
by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set. |
69 |
if (status == thrd_success) { |
|
116.1.4
by Gustav Hartvigsson
* Removed stupid, non-sense error from SLinkedList. |
70 |
return self; |
71 |
} |
|
72 |
s_free (self); |
|
|
121.1.3
by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set. |
73 |
s_err_print ("Could not create thrad. Error: %s.\n", |
|
116.1.4
by Gustav Hartvigsson
* Removed stupid, non-sense error from SLinkedList. |
74 |
s_thread_status_get_name (status)); |
75 |
print_backtrace (); |
|
76 |
return NULL; |
|
77 |
}
|
|
|
87
by Gustav Hartvigsson
I have no idea why this is not working...0_o... |
78 |
|
79 |
void
|
|
80 |
s_mutex_free (SMutex * self) { |
|
81 |
mtx_destroy (self->mutex); |
|
|
116.1.4
by Gustav Hartvigsson
* Removed stupid, non-sense error from SLinkedList. |
82 |
s_free (self->mutex); |
|
121.1.3
by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set. |
83 |
s_free (self); |
84 |
}
|
|
|
87
by Gustav Hartvigsson
I have no idea why this is not working...0_o... |
85 |
|
86 |
SThreadStatus
|
|
|
116.1.4
by Gustav Hartvigsson
* Removed stupid, non-sense error from SLinkedList. |
87 |
s_mutex_lock (SMutex * self) { |
|
87
by Gustav Hartvigsson
I have no idea why this is not working...0_o... |
88 |
sint ret_val = mtx_lock (self->mutex); |
|
116.1.4
by Gustav Hartvigsson
* Removed stupid, non-sense error from SLinkedList. |
89 |
atomic_store(&(self->locked), TRUE); |
|
88
by Gustav Hartvigsson
* Made the SMutex code compile. |
90 |
return ret_val; |
91 |
}
|
|
|
87
by Gustav Hartvigsson
I have no idea why this is not working...0_o... |
92 |
|
93 |
SThreadStatus
|
|
|
116.1.4
by Gustav Hartvigsson
* Removed stupid, non-sense error from SLinkedList. |
94 |
s_mutex_unlock (SMutex * self) { |
|
87
by Gustav Hartvigsson
I have no idea why this is not working...0_o... |
95 |
sint ret_val = mtx_unlock (self->mutex); |
|
116.1.4
by Gustav Hartvigsson
* Removed stupid, non-sense error from SLinkedList. |
96 |
atomic_store(&(self->locked), FALSE); |
|
88
by Gustav Hartvigsson
* Made the SMutex code compile. |
97 |
return ret_val; |
|
89
by Gustav Hartvigsson
* Started working on Threads |
98 |
}
|
|
87
by Gustav Hartvigsson
I have no idea why this is not working...0_o... |
99 |
|
100 |
SThreadStatus
|
|
|
116.1.4
by Gustav Hartvigsson
* Removed stupid, non-sense error from SLinkedList. |
101 |
s_mutex_check_lock (SMutex * self) { |
|
87
by Gustav Hartvigsson
I have no idea why this is not working...0_o... |
102 |
return atomic_load(&(self->locked)); |
|
88
by Gustav Hartvigsson
* Made the SMutex code compile. |
103 |
}
|
|
87
by Gustav Hartvigsson
I have no idea why this is not working...0_o... |
104 |
|
|
79
by Gustav Hartvigsson
* Clean up of SMap's for each code. |
105 |
|
|
89
by Gustav Hartvigsson
* Started working on Threads |
106 |
|
|
116.1.4
by Gustav Hartvigsson
* Removed stupid, non-sense error from SLinkedList. |
107 |
/* ****************************************************************************
|
|
89
by Gustav Hartvigsson
* Started working on Threads |
108 |
********************************** SThread ***********************************
|
109 |
**************************************************************************** */
|
|
110 |
||
111 |
/*
|
|
112 |
* Since SThread are, effectivaly, the same as thrd_t, we can cast it
|
|
113 |
* back and forth without any real problems...?
|
|
114 |
*/
|
|
115 |
struct SThread { |
|
116 |
thrd_t thread; |
|
117 |
RunFunc func; |
|
118 |
||
|
109.1.1
by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList? |
119 |
_Atomic(sboolean) is_running; |
|
89
by Gustav Hartvigsson
* Started working on Threads |
120 |
};
|
121 |
||
122 |
SThread * |
|
123 |
s_thread_new (RunFunc func) { |
|
124 |
SThread * self = s_malloc (sizeof (SThread)); |
|
|
121.1.3
by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set. |
125 |
|
126 |
self->func = func; |
|
|
89
by Gustav Hartvigsson
* Started working on Threads |
127 |
|
|
109.1.1
by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList? |
128 |
atomic_store (&(self->is_running), FALSE); |
|
89
by Gustav Hartvigsson
* Started working on Threads |
129 |
|
|
109.1.1
by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList? |
130 |
return self; |
|
89
by Gustav Hartvigsson
* Started working on Threads |
131 |
}
|
132 |
||
133 |
void
|
|
134 |
s_thread_free (SThread * self) { |
|
135 |
assert (!(atomic_load(&(self->is_running)))); |
|
136 |
s_free (self); |
|
|
121.1.3
by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set. |
137 |
}
|
|
89
by Gustav Hartvigsson
* Started working on Threads |
138 |
|
139 |
SThreadStatus
|
|
|
150
by Gustav Hartvigsson
* Fixed the tests in the CMake file |
140 |
s_thread_run (SThread * self, spointer user_data) { |
|
89
by Gustav Hartvigsson
* Started working on Threads |
141 |
if (atomic_load(&(self->is_running)) == TRUE ) { |
|
150
by Gustav Hartvigsson
* Fixed the tests in the CMake file |
142 |
s_warn_print ("Trying to run a thread that allready is running." |
143 |
"Returning.\n"); |
|
144 |
return S_THREAD_STATUS_ERROR; |
|
145 |
} |
|
146 |
atomic_store(&(self->is_running), TRUE); |
|
147 |
return thrd_create ((thrd_t *)self, (thrd_start_t)self->func, user_data); |
|
|
89
by Gustav Hartvigsson
* Started working on Threads |
148 |
}
|
|
150
by Gustav Hartvigsson
* Fixed the tests in the CMake file |
149 |
|
150 |
void
|
|
151 |
s_thread_stop (SThread * self, sint res) { |
|
152 |
if (!thrd_equal (self->thread, thrd_current ())) { |
|
153 |
s_warn_print ("Trying to exit thread that is not owned by current thread.\n" |
|
154 |
"Can only be called from running thread.\n"); |
|
155 |
return; |
|
156 |
} |
|
157 |
thrd_exit (res); |
|
158 |
|
|
159 |
atomic_store (&(self->is_running), FALSE); |
|
160 |
}
|
|
161 |
||
162 |
||
163 |