/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
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[0];
151.1.1 by Gustav Hartvigsson
* fixed... sort of... the Meson Build system... Still buggy.
27
      break;
28
    case (S_THREAD_STATUS_TIMEOUT):
29
      return SThreadStatusName[1];
150 by Gustav Hartvigsson
* Fixed the tests in the CMake file
30
      break;
31
    case (S_THREAD_STATUS_ERROR):
151.1.1 by Gustav Hartvigsson
* fixed... sort of... the Meson Build system... Still buggy.
32
      return SThreadStatusName[2];
150 by Gustav Hartvigsson
* Fixed the tests in the CMake file
33
      break;
34
    case (S_THREAD_STATUS_BUSY):
151.1.1 by Gustav Hartvigsson
* fixed... sort of... the Meson Build system... Still buggy.
35
      return SThreadStatusName[3];
150 by Gustav Hartvigsson
* Fixed the tests in the CMake file
36
      break;
37
    case (S_THREAD_STATUS_NO_MEM):
151.1.1 by Gustav Hartvigsson
* fixed... sort of... the Meson Build system... Still buggy.
38
      return SThreadStatusName[4];
150 by Gustav Hartvigsson
* Fixed the tests in the CMake file
39
      break;
40
    case (S_THREAD_STATUS_LAST):
151.1.1 by Gustav Hartvigsson
* fixed... sort of... the Meson Build system... Still buggy.
41
      return SThreadStatusName[5];
150 by Gustav Hartvigsson
* Fixed the tests in the CMake file
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
schar *
151.1.1 by Gustav Hartvigsson
* fixed... sort of... the Meson Build system... Still buggy.
59
s_mutex_flag_get_name (SMutexFlag flag) {
60
  switch (flag) {
61
    case (S_MUTEX_FLAG_PLAIN):
62
      return SMutexFlagName[0];
63
      break;
64
    case (S_MUTEX_FLAG_TIMED):
65
      return SMutexFlagName[1];
66
      break;
67
    case (S_MUTEX_FLAG_RECURSIVE):
68
      return SMutexFlagName[2];
69
      break;
70
    case (S_MUTEX_FLAG_LAST):
71
      return SMutexFlagName[3];
72
      break;
73
    default:
74
      return NULL;
75
  }
76
}
77
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
78
SMutex *
79
s_mutex_new () {
80
  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.
81
82
  self->mutex = s_malloc (sizeof (mtx_t));
83
84
  atomic_init(&(self->locked), FALSE);
88 by Gustav Hartvigsson
* Made the SMutex code compile.
85
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
86
  sint status = mtx_init (self->mutex, mtx_plain);
116.1.4 by Gustav Hartvigsson
* Removed stupid, non-sense error from SLinkedList.
87
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
88
  if (status == thrd_success) {
116.1.4 by Gustav Hartvigsson
* Removed stupid, non-sense error from SLinkedList.
89
    return self;
90
  }
91
  s_free (self);
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
92
  s_err_print ("Could not create thrad. Error: %s.\n",
116.1.4 by Gustav Hartvigsson
* Removed stupid, non-sense error from SLinkedList.
93
               s_thread_status_get_name (status));
94
  print_backtrace ();
95
  return NULL;
96
}
87 by Gustav Hartvigsson
I have no idea why this is not working...0_o...
97
98
void
99
s_mutex_free (SMutex * self) {
100
  mtx_destroy (self->mutex);
116.1.4 by Gustav Hartvigsson
* Removed stupid, non-sense error from SLinkedList.
101
  s_free (self->mutex);
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
102
  s_free (self);
103
}
87 by Gustav Hartvigsson
I have no idea why this is not working...0_o...
104
105
SThreadStatus
116.1.4 by Gustav Hartvigsson
* Removed stupid, non-sense error from SLinkedList.
106
s_mutex_lock (SMutex * self) {
87 by Gustav Hartvigsson
I have no idea why this is not working...0_o...
107
  sint ret_val = mtx_lock (self->mutex);
116.1.4 by Gustav Hartvigsson
* Removed stupid, non-sense error from SLinkedList.
108
  atomic_store(&(self->locked), TRUE);
88 by Gustav Hartvigsson
* Made the SMutex code compile.
109
  return ret_val;
110
}
87 by Gustav Hartvigsson
I have no idea why this is not working...0_o...
111
112
SThreadStatus
116.1.4 by Gustav Hartvigsson
* Removed stupid, non-sense error from SLinkedList.
113
s_mutex_unlock (SMutex * self) {
87 by Gustav Hartvigsson
I have no idea why this is not working...0_o...
114
  sint ret_val = mtx_unlock (self->mutex);
116.1.4 by Gustav Hartvigsson
* Removed stupid, non-sense error from SLinkedList.
115
  atomic_store(&(self->locked), FALSE);
88 by Gustav Hartvigsson
* Made the SMutex code compile.
116
  return ret_val;
89 by Gustav Hartvigsson
* Started working on Threads
117
}
87 by Gustav Hartvigsson
I have no idea why this is not working...0_o...
118
119
SThreadStatus
116.1.4 by Gustav Hartvigsson
* Removed stupid, non-sense error from SLinkedList.
120
s_mutex_check_lock (SMutex * self) {
87 by Gustav Hartvigsson
I have no idea why this is not working...0_o...
121
  return atomic_load(&(self->locked));
88 by Gustav Hartvigsson
* Made the SMutex code compile.
122
}
87 by Gustav Hartvigsson
I have no idea why this is not working...0_o...
123
79 by Gustav Hartvigsson
* Clean up of SMap's for each code.
124
89 by Gustav Hartvigsson
* Started working on Threads
125
116.1.4 by Gustav Hartvigsson
* Removed stupid, non-sense error from SLinkedList.
126
/* ****************************************************************************
89 by Gustav Hartvigsson
* Started working on Threads
127
 ********************************** SThread ***********************************
128
 **************************************************************************** */
129
130
/*
131
 * Since SThread are, effectivaly, the same as thrd_t, we can cast it
132
 * back and forth without any real problems...?
133
 */
134
struct SThread {
135
  thrd_t thread;
136
  RunFunc func;
137
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
138
  _Atomic(sboolean) is_running;
89 by Gustav Hartvigsson
* Started working on Threads
139
};
140
141
SThread *
142
s_thread_new (RunFunc func) {
143
  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.
144
145
  self->func = func;
89 by Gustav Hartvigsson
* Started working on Threads
146
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
147
  atomic_store (&(self->is_running), FALSE);
89 by Gustav Hartvigsson
* Started working on Threads
148
109.1.1 by Gustav Hartvigsson
* SMap seems to be broken... Or could it be SObject's Callback stuff? Or SLinkedList?
149
  return self;
89 by Gustav Hartvigsson
* Started working on Threads
150
}
151
152
void
153
s_thread_free (SThread * self) {
154
  assert (!(atomic_load(&(self->is_running))));
155
  s_free (self);
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
156
}
89 by Gustav Hartvigsson
* Started working on Threads
157
158
SThreadStatus
150 by Gustav Hartvigsson
* Fixed the tests in the CMake file
159
s_thread_run (SThread * self, spointer user_data) {
89 by Gustav Hartvigsson
* Started working on Threads
160
  if (atomic_load(&(self->is_running)) == TRUE ) {
150 by Gustav Hartvigsson
* Fixed the tests in the CMake file
161
    s_err_print ("Trying to run a thread that allready is running."
151.1.1 by Gustav Hartvigsson
* fixed... sort of... the Meson Build system... Still buggy.
162
                  "Returning.\n");
150 by Gustav Hartvigsson
* Fixed the tests in the CMake file
163
    return S_THREAD_STATUS_ERROR;
164
  }
165
  atomic_store(&(self->is_running), TRUE);
166
  return thrd_create ((thrd_t *)self, (thrd_start_t)self->func, user_data);
89 by Gustav Hartvigsson
* Started working on Threads
167
}
150 by Gustav Hartvigsson
* Fixed the tests in the CMake file
168
169
void
170
s_thread_stop (SThread * self, sint res) {
171
  if (!thrd_equal (self->thread, thrd_current ())) {
172
    s_err_print ("Trying to exit thread that is not owned by current thread.\n"
151.1.1 by Gustav Hartvigsson
* fixed... sort of... the Meson Build system... Still buggy.
173
                  "Can only be called from running thread.\n");
150 by Gustav Hartvigsson
* Fixed the tests in the CMake file
174
    return;
175
  }
176
  atomic_store (&(self->is_running), FALSE);
151.1.1 by Gustav Hartvigsson
* fixed... sort of... the Meson Build system... Still buggy.
177
  thrd_exit (res);
150 by Gustav Hartvigsson
* Fixed the tests in the CMake file
178
}
179
180
181
151.1.1 by Gustav Hartvigsson
* fixed... sort of... the Meson Build system... Still buggy.
182