1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
#include "Thread.h"
#include "Map.h"
#ifndef __STDC_NO_THREADS__
#pragma message ("No Thread support...")
#include <threads.h>
#endif
#if __WIN32__ || __WIN64__
#if (NTDDI_VERSION >= NTDDI_WIN8)
#include <Synchapi.h>
#else
#include <WinBase.h>
#endif
#else
/* To get nanosleep () we must define __USE_POSIX199309 for som reason.
*/
#define __USE_POSIX199309
#include <time.h>
#undef __USE_POSIX199309
#endif
/*
Utility functions associated with threads.
*/
void
s_usleep (slong us) {
#if __WIN32__ || __WIN64__
/* We are a windows system, so we have to implement our own little sleeper.*/
Sleep (us / 1000)
#else
/* We are not a windows system, so lets assume that we have posix's nanosleep
* Taken from:
*/
struct timespec req = {0};
req.tv_sec = (int)(us / 1000);
req.tv_nsec = us * 1000000L;
nanosleep(&req, (struct timespec *)NULL);
#endif
}
/* ****************************************************************************
********************************** SMutex ************************************
**************************************************************************** */
UNUSED
static SMap * _global_mutex_list = NULL;
UNUSED
static sulong _global_mutex_lock_id = 0;
UNUSED
static sulong _global_mutex_refcount = 0;
#define _mutex_ref() global_mutex_refcount++
void
_mutex_unref () {
_global_mutex_refcount--;
if (_global_mutex_refcount == 0) {
s_map_free (_global_mutex_list, TRUE);
}
}
#ifndef __STDC_NO_THREADS__
/* We have Std Thread support */
struct SMutex {
sulong lock_id;
mtx_t mutex;
};
void
_internal_s_mutex_free (SMutex * self) {
free (self);
}
SMutex *
s_mutex_new () {
if (!_global_mutex_list) {
_global_mutex_list = s_map_new (FREEFUNC(_internal_s_mutex_free));
_mutex_ref ();
}
}
#else /* __STDC_NO_THREADS__ */
/* We do not have Std Thread support */
struct SMutex {
suint lock_id;
};
#endif
|