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