summaryrefslogtreecommitdiffstats
path: root/source/cCriticalSection.cpp
blob: 85f89c195aa5b47608863aadcf73e4f233d6e8c5 (plain) (blame)
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
#include "cCriticalSection.h"
#include "cMCLogger.h"

#ifdef _WIN32
#include <Windows.h>
#else
#include <pthread.h>
#endif

cCriticalSection::cCriticalSection()
{
#ifdef _WIN32
	m_CriticalSectionPtr = new CRITICAL_SECTION;
	InitializeCriticalSection( (CRITICAL_SECTION*)m_CriticalSectionPtr );
#else
	m_Attributes = new pthread_mutexattr_t;
	pthread_mutexattr_init((pthread_mutexattr_t*)m_Attributes);
	pthread_mutexattr_settype((pthread_mutexattr_t*)m_Attributes, PTHREAD_MUTEX_RECURSIVE);

    m_CriticalSectionPtr = new pthread_mutex_t;
    if( pthread_mutex_init( (pthread_mutex_t*)m_CriticalSectionPtr, (pthread_mutexattr_t*)m_Attributes ) != 0 )
    {
        LOG("ERROR: Could not initialize Critical Section!");
    }
#endif
}

cCriticalSection::~cCriticalSection()
{
#ifdef _WIN32
	DeleteCriticalSection( (CRITICAL_SECTION*)m_CriticalSectionPtr );
	delete (CRITICAL_SECTION*)m_CriticalSectionPtr;
#else
    if( pthread_mutex_destroy( (pthread_mutex_t*)m_CriticalSectionPtr ) != 0 )
    {
        LOG("ERROR: Could not destroy Critical Section!");
    }
    delete (pthread_mutex_t*)m_CriticalSectionPtr;
	pthread_mutexattr_destroy( (pthread_mutexattr_t*)m_Attributes );
	delete (pthread_mutexattr_t*)m_Attributes;
#endif
}

void cCriticalSection::Lock()
{
#ifdef _WIN32
	EnterCriticalSection( (CRITICAL_SECTION*)m_CriticalSectionPtr );
#else
    pthread_mutex_lock( (pthread_mutex_t*)m_CriticalSectionPtr );
#endif
}

void cCriticalSection::Unlock()
{
#ifdef _WIN32
	LeaveCriticalSection( (CRITICAL_SECTION*)m_CriticalSectionPtr );
#else
    pthread_mutex_unlock( (pthread_mutex_t*)m_CriticalSectionPtr );
#endif
}