/simpletypesystem/trunk

To get this branch, use:
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
23
/* ****************************************************************************
24
 ********************************** SMutex ************************************
25
 **************************************************************************** */
26
27
struct SMutex {
28
  mtx_t mutex;
29
  _Atomic(sboolean) locked;
87 by Gustav Hartvigsson
I have no idea why this is not working...0_o...
30
};
77 by Gustav Hartvigsson
* More work on SStream... Requiers Mutex suppert :-)
31
32
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
33
SMutex *
34
s_mutex_new () {
35
  SMutex * self = malloc (sizeof (SMutex));
87 by Gustav Hartvigsson
I have no idea why this is not working...0_o...
36
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
37
  atomic_init(&(self->locked), FALSE);
88 by Gustav Hartvigsson
* Made the SMutex code compile.
38
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
39
  mtx_init (&(self->mutex), mtx_plain);
87 by Gustav Hartvigsson
I have no idea why this is not working...0_o...
40
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
41
  return self;
88 by Gustav Hartvigsson
* Made the SMutex code compile.
42
}
87 by Gustav Hartvigsson
I have no idea why this is not working...0_o...
43
44
void
45
s_mutex_free (SMutex * self) {
46
  mtx_destroy (&(self->mutex));
47
  free (self);
48
}
49
50
sint
89 by Gustav Hartvigsson
* Started working on Threads
51
s_mutex_lock (SMutex * self) {
87 by Gustav Hartvigsson
I have no idea why this is not working...0_o...
52
  sint ret_val = mtx_lock (&(self->mutex));
89 by Gustav Hartvigsson
* Started working on Threads
53
  atomic_store(&(self->locked), TRUE);
88 by Gustav Hartvigsson
* Made the SMutex code compile.
54
  return ret_val;
55
}
87 by Gustav Hartvigsson
I have no idea why this is not working...0_o...
56
57
sint
89 by Gustav Hartvigsson
* Started working on Threads
58
s_mutex_unlock (SMutex * self) {
87 by Gustav Hartvigsson
I have no idea why this is not working...0_o...
59
  sint ret_val = mtx_unlock (&(self->mutex));
89 by Gustav Hartvigsson
* Started working on Threads
60
  atomic_store(&(self->locked), FALSE);
88 by Gustav Hartvigsson
* Made the SMutex code compile.
61
  return ret_val;
89 by Gustav Hartvigsson
* Started working on Threads
62
}
87 by Gustav Hartvigsson
I have no idea why this is not working...0_o...
63
64
sboolean
65
s_mutex_check_lock (SMutex * self) {
66
  return atomic_load(&(self->locked));
88 by Gustav Hartvigsson
* Made the SMutex code compile.
67
}
87 by Gustav Hartvigsson
I have no idea why this is not working...0_o...
68
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
69
89 by Gustav Hartvigsson
* Started working on Threads
70
/* ****************************************************************************
71
 ********************************** SThread ***********************************
72
 **************************************************************************** */
73
74
/*
75
 * Since SThread are, effectivaly, the same as thrd_t, we can cast it
76
 * back and forth without any real problems...?
77
 */
78
struct SThread {
79
  thrd_t thread;
80
  RunFunc func;
81
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
82
  _Atomic(sboolean) is_running;
89 by Gustav Hartvigsson
* Started working on Threads
83
};
84
85
SThread *
86
s_thread_new (RunFunc func) {
87
  SThread * self = malloc (sizeof (SThread));
88
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
89
  self->func = func;
89 by Gustav Hartvigsson
* Started working on Threads
90
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
91
  atomic_store (&(self->is_running), FALSE);
89 by Gustav Hartvigsson
* Started working on Threads
92
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
93
  return self;
89 by Gustav Hartvigsson
* Started working on Threads
94
}
95
96
void
97
s_thread_free (SThread * self) {
98
  assert (!(atomic_load(&(self->is_running))));
99
  free (self);
100
}
101
102
sboolean
103
s_thread_run (SThread * self, spointer user_data) {
104
  return thrd_create ((thrd_t *)self, (thrd_start_t)self->func, user_data);
105
  atomic_store(&(self->is_running), TRUE);
106
}
107