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 |
#if 0
|
|
|
88
by Gustav Hartvigsson
* Made the SMutex code compile. |
3 |
// We get this from src/external/threads.h now.
|
4 |
#if __WIN32__ || __WIN64__
|
|
|
77
by Gustav Hartvigsson
* More work on SStream... Requiers Mutex suppert :-) |
5 |
#if (NTDDI_VERSION >= NTDDI_WIN8)
|
6 |
#include <Synchapi.h>
|
|
7 |
#else
|
|
8 |
#include <WinBase.h>
|
|
9 |
#endif
|
|
10 |
#else
|
|
11 |
/* To get nanosleep () we must define __USE_POSIX199309 for som reason. |
|
12 |
*/
|
|
13 |
#define __USE_POSIX199309 |
|
14 |
#include <time.h> |
|
15 |
#undef __USE_POSIX199309 |
|
16 |
#endif
|
|
17 |
#endif
|
|
|
88
by Gustav Hartvigsson
* Made the SMutex code compile. |
18 |
|
|
77
by Gustav Hartvigsson
* More work on SStream... Requiers Mutex suppert :-) |
19 |
/*
|
20 |
Utility functions associated with threads.
|
|
21 |
*/
|
|
22 |
void
|
|
23 |
s_usleep (slong us) { |
|
24 |
#if __WIN32__ || __WIN64__
|
|
25 |
/* 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. |
26 |
Sleep (us / 1000) |
|
77
by Gustav Hartvigsson
* More work on SStream... Requiers Mutex suppert :-) |
27 |
#else
|
28 |
/* We are not a windows system, so lets assume that we have posix's nanosleep |
|
29 |
* Taken from:
|
|
|
79
by Gustav Hartvigsson
* Clean up of SMap's for each code. |
30 |
*/
|
|
77
by Gustav Hartvigsson
* More work on SStream... Requiers Mutex suppert :-) |
31 |
struct timespec req = {0}; |
32 |
req.tv_sec = (int)(us / 1000); |
|
33 |
req.tv_nsec = us * 1000000L; |
|
34 |
nanosleep(&req, (struct timespec *)NULL); |
|
35 |
#endif
|
|
36 |
}
|
|
37 |
||
38 |
||
39 |
/* ****************************************************************************
|
|
40 |
********************************** SMutex ************************************
|
|
41 |
**************************************************************************** */
|
|
42 |
||
43 |
struct SMutex { |
|
44 |
mtx_t mutex; |
|
45 |
_Atomic(sboolean) locked; |
|
|
87
by Gustav Hartvigsson
I have no idea why this is not working...0_o... |
46 |
};
|
|
77
by Gustav Hartvigsson
* More work on SStream... Requiers Mutex suppert :-) |
47 |
|
48 |
||
|
79
by Gustav Hartvigsson
* Clean up of SMap's for each code. |
49 |
SMutex * |
50 |
s_mutex_new () { |
|
51 |
SMutex * self = malloc (sizeof (SMutex)); |
|
|
87
by Gustav Hartvigsson
I have no idea why this is not working...0_o... |
52 |
|
53 |
atomic_init(&(self->locked), FALSE); |
|
|
88
by Gustav Hartvigsson
* Made the SMutex code compile. |
54 |
|
|
87
by Gustav Hartvigsson
I have no idea why this is not working...0_o... |
55 |
mtx_init (&(self->mutex), mtx_plain); |
56 |
|
|
|
88
by Gustav Hartvigsson
* Made the SMutex code compile. |
57 |
return self; |
58 |
}
|
|
|
87
by Gustav Hartvigsson
I have no idea why this is not working...0_o... |
59 |
|
60 |
void
|
|
61 |
s_mutex_free (SMutex * self) { |
|
62 |
mtx_destroy (&(self->mutex)); |
|
63 |
free (self); |
|
64 |
}
|
|
65 |
||
66 |
sint
|
|
|
89
by Gustav Hartvigsson
* Started working on Threads |
67 |
s_mutex_lock (SMutex * self) { |
|
87
by Gustav Hartvigsson
I have no idea why this is not working...0_o... |
68 |
sint ret_val = mtx_lock (&(self->mutex)); |
|
89
by Gustav Hartvigsson
* Started working on Threads |
69 |
atomic_store(&(self->locked), TRUE); |
|
88
by Gustav Hartvigsson
* Made the SMutex code compile. |
70 |
return ret_val; |
71 |
}
|
|
|
87
by Gustav Hartvigsson
I have no idea why this is not working...0_o... |
72 |
|
73 |
sint
|
|
|
89
by Gustav Hartvigsson
* Started working on Threads |
74 |
s_mutex_unlock (SMutex * self) { |
|
87
by Gustav Hartvigsson
I have no idea why this is not working...0_o... |
75 |
sint ret_val = mtx_unlock (&(self->mutex)); |
|
89
by Gustav Hartvigsson
* Started working on Threads |
76 |
atomic_store(&(self->locked), FALSE); |
|
88
by Gustav Hartvigsson
* Made the SMutex code compile. |
77 |
return ret_val; |
|
89
by Gustav Hartvigsson
* Started working on Threads |
78 |
}
|
|
87
by Gustav Hartvigsson
I have no idea why this is not working...0_o... |
79 |
|
80 |
sboolean
|
|
81 |
s_mutex_check_lock (SMutex * self) { |
|
82 |
return atomic_load(&(self->locked)); |
|
|
88
by Gustav Hartvigsson
* Made the SMutex code compile. |
83 |
}
|
|
87
by Gustav Hartvigsson
I have no idea why this is not working...0_o... |
84 |
|
|
79
by Gustav Hartvigsson
* Clean up of SMap's for each code. |
85 |
|
|
89
by Gustav Hartvigsson
* Started working on Threads |
86 |
/* ****************************************************************************
|
87 |
********************************** SThread ***********************************
|
|
88 |
**************************************************************************** */
|
|
89 |
||
90 |
/*
|
|
91 |
* Since SThread are, effectivaly, the same as thrd_t, we can cast it
|
|
92 |
* back and forth without any real problems...?
|
|
93 |
*/
|
|
94 |
struct SThread { |
|
95 |
thrd_t thread; |
|
96 |
RunFunc func; |
|
97 |
|
|
98 |
_Atomic(sboolean) is_running; |
|
99 |
};
|
|
100 |
||
101 |
SThread * |
|
102 |
s_thread_new (RunFunc func) { |
|
103 |
SThread * self = malloc (sizeof (SThread)); |
|
104 |
|
|
105 |
self->func = func; |
|
106 |
|
|
107 |
atomic_store (&(self->is_running), FALSE); |
|
108 |
|
|
109 |
return self; |
|
110 |
}
|
|
111 |
||
112 |
void
|
|
113 |
s_thread_free (SThread * self) { |
|
114 |
assert (!(atomic_load(&(self->is_running)))); |
|
115 |
free (self); |
|
116 |
}
|
|
117 |
||
118 |
sboolean
|
|
119 |
s_thread_run (SThread * self, spointer user_data) { |
|
120 |
return thrd_create ((thrd_t *)self, (thrd_start_t)self->func, user_data); |
|
121 |
atomic_store(&(self->is_running), TRUE); |
|
122 |
}
|
|
123 |