summaryrefslogtreecommitdiff
path: root/os_wrappers/CriticalSection.h
blob: 7b64f90906f88c72f5b04fafcb77158d3d35dccf (plain)
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
/*
 * CriticalSection.h
 *
 * Copyright (C) ST-Ericsson SA 2011
 * Authors: Srimanta Panda <srimanta.panda@stericsson.com>,
 *          Ola Borgelin <ola.borgelin@stericsson.com>,
 *          Karin Hedlund <karin.hedlund@stericsson.com>,
 *          Markus Andersson <markus.m.andersson@stericsson.com> for ST-Ericsson.
 * License terms: 3-clause BSD license
 *
 */

/*
 * @addtogroup OS Wrappers
 * @{
 */

#pragma once

#ifdef _WIN32
#include <windows.h>
#include <process.h>
#else
#include <pthread.h>
#endif

/**
 * @brief Simple wrapper for OS synchronization primitive.
 *
 * This class wraps Win32 critical section and POSIX threads mutex.
 */
class CriticalSection
{
public:
    /**
     * @brief Constructor creating new critical section.
     */
    CriticalSection();

    /**
     * @brief Destructor that destroys the critical section.
     */
    virtual ~CriticalSection();

    /**
     * @brief This method enters/locks the critical section.
     */
    void enter();

    /**
     * @brief This method leaves/unlocks the critical section.
     */
    void leave();
private:
#ifdef _WIN32
    CRITICAL_SECTION mutex_;
#else
    pthread_mutex_t mutex_;
#endif
};

/**
 * @brief RAII class for critical section locking/unlocking.
 */
class CSLock
{
public:
    /**
     * @brief Constructor that enters/locks the passed critical section.
     *
     * @param[in] section - critical section to lock.
     */
    CSLock(CriticalSection* section);

    /**
     * @brief Destructor that leaves/unlocks the associated critical section.
     */
    virtual ~CSLock();
private:
    CriticalSection* section_;
    CSLock(const CSLock&);
    CSLock& operator=(const CSLock&);
};

/* @} */