/simpletypesystem/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk

« back to all changes in this revision

Viewing changes to libssts/Thread.c

* Merged in new Main Loop branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
#endif
21
21
}
22
22
 
 
23
char *
 
24
s_thread_status_get_name (SThreadStatus status) {
 
25
  return SThreadStatusName[status];
 
26
}
23
27
 
24
28
/* ****************************************************************************
25
29
 ********************************** SMutex ************************************
26
30
 **************************************************************************** */
27
31
 
28
32
struct SMutex {
29
 
  mtx_t mutex;
 
33
  mtx_t *  mutex;
30
34
  _Atomic(sboolean) locked;
31
35
};
32
36
 
34
38
SMutex *
35
39
s_mutex_new () {
36
40
  SMutex * self = malloc (sizeof (SMutex));
37
 
 
 
41
  
 
42
  self->mutex = malloc (sizeof (mtx_t));
 
43
  
38
44
  atomic_init(&(self->locked), FALSE);
39
 
 
40
 
  mtx_init (&(self->mutex), mtx_plain);
41
 
 
42
 
  return self;
 
45
  
 
46
  sint status = mtx_init (self->mutex, mtx_plain);
 
47
  
 
48
  if (status == thrd_success) {
 
49
    return self;
 
50
  }
 
51
  free (self);
 
52
  s_err_print ("Could not create thrad. Error: %s.\n",
 
53
               s_thread_status_get_name (status));
 
54
  print_backtrace ();
 
55
  return NULL;
43
56
}
44
57
 
45
58
void
46
59
s_mutex_free (SMutex * self) {
47
 
  mtx_destroy (&(self->mutex));
 
60
  mtx_destroy (self->mutex);
 
61
  free (self->mutex);
48
62
  free (self);
49
63
}
50
64
 
51
 
sint
 
65
SThreadStatus
52
66
s_mutex_lock (SMutex * self) {
53
 
  sint ret_val = mtx_lock (&(self->mutex));
 
67
  sint ret_val = mtx_lock (self->mutex);
54
68
  atomic_store(&(self->locked), TRUE);
55
69
  return ret_val;
56
70
}
57
71
 
58
 
sint
 
72
SThreadStatus
59
73
s_mutex_unlock (SMutex * self) {
60
 
  sint ret_val = mtx_unlock (&(self->mutex));
 
74
  sint ret_val = mtx_unlock (self->mutex);
61
75
  atomic_store(&(self->locked), FALSE);
62
76
  return ret_val;
63
77
}
64
78
 
65
 
sboolean
 
79
SThreadStatus
66
80
s_mutex_check_lock (SMutex * self) {
67
81
  return atomic_load(&(self->locked));
68
82
}
69
83
 
70
84
 
 
85
 
71
86
/* ****************************************************************************
72
87
 ********************************** SThread ***********************************
73
88
 **************************************************************************** */
86
101
SThread *
87
102
s_thread_new (RunFunc func) {
88
103
  SThread * self = malloc (sizeof (SThread));
89
 
 
 
104
  
90
105
  self->func = func;
91
106
 
92
107
  atomic_store (&(self->is_running), FALSE);