Repository navigation
Expand file tree
/
Copy pathsynchapi.h
More file actions
57 lines (49 loc) · 1.76 KB
/
Copy pathsynchapi.h
File metadata and controls
57 lines (49 loc) · 1.76 KB
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
/************* Synchronization primitives ******************/
#pragma once
#include <pthread.h>
#include <stdlib.h>
#include "windef.h"
struct CRITICAL_SECTION
{
pthread_mutex_t mutex;
// Critical sections are often used in static objects.
// But on Linux destruction order is even less predictable than on Windows.
// Let's try to hide this problem.
bool initialized = false;
// Just to make sure that code comply to MS specs.
CRITICAL_SECTION() = default;
CRITICAL_SECTION(const CRITICAL_SECTION&) = delete;
CRITICAL_SECTION(CRITICAL_SECTION&&) = delete;
CRITICAL_SECTION& operator=(const CRITICAL_SECTION&) = delete;
};
inline void InitializeCriticalSection(CRITICAL_SECTION* lpCriticalSection)
{
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&lpCriticalSection->mutex, &attr);
lpCriticalSection->initialized = true;
pthread_mutexattr_destroy(&attr);
}
inline void DeleteCriticalSection(CRITICAL_SECTION* lpCriticalSection)
{
lpCriticalSection->initialized = false;
pthread_mutex_destroy(&lpCriticalSection->mutex);
}
inline void EnterCriticalSection(CRITICAL_SECTION* lpCriticalSection)
{
if (lpCriticalSection->initialized)
pthread_mutex_lock(&lpCriticalSection->mutex);
// else to abort() or not to abort, that is the question
}
inline void LeaveCriticalSection(CRITICAL_SECTION* lpCriticalSection)
{
// This function used to be used in destructors so it must not throw.
// On the other hand an attempt to leave critical section that is not
// owned is a serious bug so it is better to throw and cause std::terminate()
// to be called for further debugging.
if (lpCriticalSection->initialized && pthread_mutex_unlock(&lpCriticalSection->mutex) != 0)
{
abort();
}
}