forked from andig/canprogs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKCriticalSection.cpp
executable file
·120 lines (107 loc) · 3.26 KB
/
KCriticalSection.cpp
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
*
* Copyright (C) 2007-2014 Jürg Müller, CH-5524
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see http://www.gnu.org/licenses/ .
*/
#if !defined(__NO_MULTITHREADS__)
#if defined(__WINDOWS__)
#include <windows.h>
#else
#include <pthread.h>
#include <sys/sem.h>
#endif
#endif
#include "NTypes.h"
#include "KCriticalSection.h"
KCriticalSection::KCriticalSection()
{
mSection = 0;
#if !defined(__NO_MULTITHREADS__)
#if defined(__WINDOWS__)
mSection = new CRITICAL_SECTION;
InitializeCriticalSection((CRITICAL_SECTION *) mSection);
#else
bool AttrOk = false;
pthread_mutexattr_t Attribute;
mSection = new pthread_mutex_t;
if (!pthread_mutexattr_init(&Attribute))
{
AttrOk = pthread_mutexattr_settype(&Attribute, PTHREAD_MUTEX_RECURSIVE /* 1 */) == 0;
pthread_mutex_init((pthread_mutex_t *) mSection, AttrOk ? &Attribute : NULL);
pthread_mutexattr_destroy(&Attribute);
} else
pthread_mutex_init((pthread_mutex_t *) mSection, NULL);
#endif
#endif
}
KCriticalSection::~KCriticalSection()
{
#if !defined(__NO_MULTITHREADS__)
#if defined(__WINDOWS__)
DeleteCriticalSection((CRITICAL_SECTION *) mSection);
delete (CRITICAL_SECTION *) mSection;
#else
pthread_mutex_destroy((pthread_mutex_t *) mSection);
delete (pthread_mutex_t *) mSection;
#endif
#endif
mSection = NULL;
}
bool KCriticalSection::Acquire()
{
if (!mSection)
return false;
#if !defined(__NO_MULTITHREADS__)
#if defined(__WINDOWS__)
EnterCriticalSection((CRITICAL_SECTION *) mSection);
#else
pthread_mutex_lock((pthread_mutex_t *) mSection);
#endif
#endif
return true;
}
bool KCriticalSection::TryEnter()
{
if (mSection)
{
#if !defined(__NO_MULTITHREADS__)
#if defined(__WINDOWS__)
#if(_WIN32_WINNT >= 0x0400)
// TryEnterCriticalSection ist unter Windows 95 nicht verf�gbar
// Bei einem erfolgreichen TryEnterCriticalSection muss danach die
// CriticalSection mittels LeaveCriticalSection wieder freigegeben werden.
return TryEnterCriticalSection((CRITICAL_SECTION *) mSection) != 0;
#else
Acquire();
return true;
#endif
#else
return pthread_mutex_trylock((pthread_mutex_t *) mSection) == 0;
#endif
#endif
}
return false;
}
void KCriticalSection::Release()
{
if (!mSection)
return;
#if !defined(__NO_MULTITHREADS__)
#if defined(__WINDOWS__)
LeaveCriticalSection((CRITICAL_SECTION *) mSection);
#else
pthread_mutex_unlock((pthread_mutex_t *) mSection);
#endif
#endif
}