1
|
|
/*
|
2
|
|
*******************************************************************************
|
3
|
|
\file mt.c
|
4
|
|
\brief Multithreading
|
5
|
|
\project bee2 [cryptographic library]
|
6
|
|
\author (C) Sergey Agievich [agievich@{bsu.by|gmail.com}]
|
7
|
|
\created 2014.10.10
|
8
|
|
\version 2015.08.27
|
9
|
|
\license This program is released under the GNU General Public License
|
10
|
|
version 3. See Copyright Notices in bee2/info.h.
|
11
|
|
*******************************************************************************
|
12
|
|
*/
|
13
|
|
|
14
|
|
#include "bee2/core/mem.h"
|
15
|
|
#include "bee2/core/mt.h"
|
16
|
|
#include "bee2/core/util.h"
|
17
|
|
|
18
|
|
/*
|
19
|
|
*******************************************************************************
|
20
|
|
Мьютексы
|
21
|
|
*******************************************************************************
|
22
|
|
*/
|
23
|
|
|
24
|
|
#ifdef OS_WIN
|
25
|
|
|
26
|
|
bool_t mtMtxCreate(mt_mtx_t* mtx)
|
27
|
|
{
|
28
|
|
ASSERT(memIsValid(mtx, sizeof(mt_mtx_t)));
|
29
|
|
*mtx = CreateMutex(0, FALSE, 0);
|
30
|
|
return *mtx != NULL;
|
31
|
|
}
|
32
|
|
|
33
|
|
bool_t mtMtxIsValid(const mt_mtx_t* mtx)
|
34
|
|
{
|
35
|
|
return memIsValid(mtx, sizeof(mt_mtx_t)) && *mtx != NULL;
|
36
|
|
}
|
37
|
|
|
38
|
|
void mtMtxLock(mt_mtx_t* mtx)
|
39
|
|
{
|
40
|
|
ASSERT(mtMtxIsValid(mtx));
|
41
|
|
WaitForSingleObject(*mtx, INFINITE);
|
42
|
|
}
|
43
|
|
|
44
|
|
void mtMtxUnlock(mt_mtx_t* mtx)
|
45
|
|
{
|
46
|
|
ASSERT(mtMtxIsValid(mtx));
|
47
|
|
ReleaseMutex(*mtx);
|
48
|
|
}
|
49
|
|
|
50
|
|
void mtMtxClose(mt_mtx_t* mtx)
|
51
|
|
{
|
52
|
|
ASSERT(mtMtxIsValid(mtx));
|
53
|
|
CloseHandle(*mtx);
|
54
|
|
}
|
55
|
|
|
56
|
|
#elif defined OS_UNIX
|
57
|
|
|
58
|
1
|
bool_t mtMtxCreate(mt_mtx_t* mtx)
|
59
|
|
{
|
60
|
1
|
ASSERT(memIsValid(mtx, sizeof(mt_mtx_t)));
|
61
|
1
|
return pthread_mutex_init(mtx, 0) == 0;
|
62
|
|
}
|
63
|
|
|
64
|
1
|
bool_t mtMtxIsValid(const mt_mtx_t* mtx)
|
65
|
|
{
|
66
|
1
|
return memIsValid(mtx, sizeof(mt_mtx_t));
|
67
|
|
}
|
68
|
|
|
69
|
1
|
void mtMtxLock(mt_mtx_t* mtx)
|
70
|
|
{
|
71
|
1
|
ASSERT(mtMtxIsValid(mtx));
|
72
|
1
|
pthread_mutex_lock(mtx);
|
73
|
|
}
|
74
|
|
|
75
|
1
|
void mtMtxUnlock(mt_mtx_t* mtx)
|
76
|
|
{
|
77
|
1
|
ASSERT(mtMtxIsValid(mtx));
|
78
|
1
|
pthread_mutex_unlock(mtx);
|
79
|
|
}
|
80
|
|
|
81
|
1
|
void mtMtxClose(mt_mtx_t* mtx)
|
82
|
|
{
|
83
|
1
|
ASSERT(mtMtxIsValid(mtx));
|
84
|
1
|
pthread_mutex_destroy(mtx);
|
85
|
|
}
|
86
|
|
|
87
|
|
#else
|
88
|
|
|
89
|
|
bool_t mtMtxCreate(mt_mtx_t* mtx)
|
90
|
|
{
|
91
|
|
return TRUE;
|
92
|
|
}
|
93
|
|
|
94
|
|
bool_t mtMtxIsValid(const mt_mtx_t* mtx)
|
95
|
|
{
|
96
|
|
return TRUE;
|
97
|
|
}
|
98
|
|
|
99
|
|
void mtMtxLock(mt_mtx_t* mtx)
|
100
|
|
{
|
101
|
|
ASSERT(mtMtxIsValid(mtx));
|
102
|
|
}
|
103
|
|
|
104
|
|
void mtMtxUnlock(mt_mtx_t* mtx)
|
105
|
|
{
|
106
|
|
ASSERT(mtMtxIsValid(mtx));
|
107
|
|
}
|
108
|
|
|
109
|
|
void mtMtxClose(mt_mtx_t* mtx)
|
110
|
|
{
|
111
|
|
ASSERT(mtMtxIsValid(mtx));
|
112
|
|
}
|
113
|
|
|
114
|
|
#endif // OS
|
115
|
|
|
116
|
|
/*
|
117
|
|
*******************************************************************************
|
118
|
|
Потоки
|
119
|
|
*******************************************************************************
|
120
|
|
*/
|
121
|
|
|
122
|
|
#ifdef OS_WIN
|
123
|
|
|
124
|
|
void mtSleep(u32 ms)
|
125
|
|
{
|
126
|
|
Sleep(ms);
|
127
|
|
}
|
128
|
|
|
129
|
|
#elif defined OS_UNIX
|
130
|
|
|
131
|
|
#include <time.h>
|
132
|
|
|
133
|
1
|
void mtSleep(u32 ms)
|
134
|
|
{
|
135
|
|
struct timespec ts;
|
136
|
1
|
ts.tv_sec = (time_t)ms, ts.tv_sec /= 1000;
|
137
|
1
|
ts.tv_nsec = (long)ms, ts.tv_nsec *= 1000, ts.tv_nsec *= 1000;
|
138
|
1
|
ts.tv_nsec %= 1000000000l;
|
139
|
1
|
nanosleep(&ts, 0);
|
140
|
|
}
|
141
|
|
|
142
|
|
#else
|
143
|
|
|
144
|
|
void mtSleep(u32 ms)
|
145
|
|
{
|
146
|
|
}
|
147
|
|
|
148
|
|
#endif // OS
|
149
|
|
|