/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-09-20 20:46:17 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20150920204617-yhd8fal54o6mgwio
I have no idea why this is not working...0_o...

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
 
2
2
#include "Thread.h"
3
 
#include "Map.h"
4
 
 
5
 
#ifndef __STDC_NO_THREADS__
6
 
#pragma message ("No Thread support...")
7
 
#include <threads.h>
8
 
#endif
9
3
 
10
4
#if __WIN32__ || __WIN64__
11
5
  #if (NTDDI_VERSION >= NTDDI_WIN8)
45
39
 ********************************** SMutex ************************************
46
40
 **************************************************************************** */
47
41
 
48
 
UNUSED
49
 
static SMap * _global_mutex_list = NULL;
50
 
 
51
 
UNUSED
52
 
static sulong _global_mutex_lock_id = 0;
53
 
 
54
 
UNUSED
55
 
static sulong _global_mutex_refcount = 0;
56
 
 
57
 
#define _mutex_ref() global_mutex_refcount++
58
 
 
59
 
void
60
 
_mutex_unref () {
61
 
  _global_mutex_refcount--;
62
 
  if (_global_mutex_refcount == 0) {
63
 
    s_map_free (_global_mutex_list, TRUE);
64
 
  }
65
 
}
66
 
 
67
 
#ifndef __STDC_NO_THREADS__
68
 
/* We have Std Thread support */
69
42
struct SMutex {
70
 
  sulong lock_id;
71
43
  mtx_t mutex;
 
44
  _Atomic(sboolean) locked;
72
45
};
73
46
 
74
 
void
75
 
_internal_s_mutex_free (SMutex * self) {
76
 
  free (self);
77
 
}
78
47
 
79
48
SMutex *
80
49
s_mutex_new () {
81
 
  if (!_global_mutex_list) {
82
 
    _global_mutex_list = s_map_new (FREEFUNC(_internal_s_mutex_free));
83
 
    _mutex_ref ();
84
 
  }
85
 
}
86
 
 
87
 
 
88
 
 
89
 
#else /* __STDC_NO_THREADS__ */
90
 
/* We do not have Std Thread support */
91
 
 
92
 
struct SMutex {
93
 
  suint lock_id;
94
 
  _Atomic sboolean is_locked;
95
 
};
96
 
#endif
97
 
 
98
 
 
 
50
  SMutex * self = malloc (sizeof (SMutex));
 
51
  
 
52
  atomic_init(self->locked, FALSE);
 
53
  
 
54
  mtx_init (&(self->mutex), mtx_plain);
 
55
}
 
56
 
 
57
void
 
58
s_mutex_free (SMutex * self) {
 
59
  _globa_mutex_unref ();
 
60
  mtx_destroy (&(self->mutex));
 
61
  free (self);
 
62
}
 
63
 
 
64
void
 
65
s_mutex_lock (SMutex * self) {
 
66
  mtx_lock (&(self->mutex));
 
67
  atomic_store(self->locked, TRUE);
 
68
}
 
69
 
 
70
void
 
71
s_mutex_unlock (SMutex * self) {
 
72
  mtx_unlock (&(self->mutex));
 
73
  atomic_store(self->locked, FALSE);
 
74
}
 
75
 
 
76
sboolean
 
77
s_mutex_check_lock (SMutex * self) {
 
78
  return atomic_load(self->locked);
 
79
}
99
80