/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 src/Thread.c

  • Committer: Gustav Hartvigsson
  • Date: 2015-10-01 10:46:50 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20151001104650-s6d8bphieu709ncs
* Started working on Threads
* Started working on Base[16,32,64] [en,de]coding functionality.
* added waring that suchar is not an unsigned char type.

Show diffs side-by-side

added added

removed removed

Lines of Context:
64
64
  free (self);
65
65
}
66
66
 
67
 
suint
 
67
sint
68
68
s_mutex_lock (SMutex * self) {
69
 
  suint ret_val = mtx_lock (&(self->mutex));
 
69
  sint ret_val = mtx_lock (&(self->mutex));
70
70
  atomic_store(&(self->locked), TRUE);
71
71
  return ret_val;
72
72
}
73
73
 
74
 
void
 
74
sint
75
75
s_mutex_unlock (SMutex * self) {
76
 
  mtx_unlock (&(self->mutex));
 
76
  sint ret_val = mtx_unlock (&(self->mutex));
77
77
  atomic_store(&(self->locked), FALSE);
 
78
  return ret_val;
78
79
}
79
80
 
80
81
sboolean
82
83
  return atomic_load(&(self->locked));
83
84
}
84
85
 
 
86
 
 
87
/* ****************************************************************************
 
88
 ********************************** SThread ***********************************
 
89
 **************************************************************************** */
 
90
 
 
91
/*
 
92
 * Since SThread are, effectivaly, the same as thrd_t, we can cast it
 
93
 * back and forth without any real problems...?
 
94
 */
 
95
struct SThread {
 
96
  thrd_t thread;
 
97
  RunFunc func;
 
98
  
 
99
  _Atomic(sboolean) is_running;
 
100
};
 
101
 
 
102
SThread *
 
103
s_thread_new (RunFunc func) {
 
104
  SThread * self = malloc (sizeof (SThread));
 
105
  
 
106
  self->func = func;
 
107
  
 
108
  atomic_store (&(self->is_running), FALSE);
 
109
  
 
110
  return self;
 
111
}
 
112
 
 
113
void
 
114
s_thread_free (SThread * self) {
 
115
  assert (!(atomic_load(&(self->is_running))));
 
116
  free (self);
 
117
}
 
118
 
 
119
sboolean
 
120
s_thread_run (SThread * self, spointer user_data) {
 
121
  return thrd_create ((thrd_t *)self, (thrd_start_t)self->func, user_data);
 
122
  atomic_store(&(self->is_running), TRUE);
 
123
}