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