summaryrefslogtreecommitdiff
path: root/src/MTS_Lock.cpp
blob: dc2e7c0e4b2d04238c70edc48e1a6641a0a9a2e9 (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
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
/*
 * Copyright (C) 2015 by Multi-Tech Systems
 *
 * This file is part of libmts.
 *
 * libmts 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, either version 2 of the License, or
 * (at your option) any later version.
 *
 * libmts 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 libmts.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <mts/MTS_Lock.h>
#include <mts/MTS_Logger.h>
#include <cassert>
#include <stdexcept>

using namespace MTS;

Lock::Lock()
: m_bLocked(false) {
#ifdef WIN32
    m_apMutex.reset(CreateMutex(NULL, FALSE, NULL));
    if (m_apMutex.get() == NULL) {
        throw std::bad_alloc();
    }
#else
    m_apMutexAttr.reset(new pthread_mutexattr_t());
    int result = pthread_mutexattr_init(m_apMutexAttr.get());
    if (result != 0) {
        throw std::runtime_error("failed to initialize mutex attributes");
    }
    result = pthread_mutexattr_settype(m_apMutexAttr.get(),
    PTHREAD_MUTEX_RECURSIVE);
    if (result != 0) {
        throw std::runtime_error("failed to set mutex recursive");
    }
    m_apMutex.reset(new pthread_mutex_t());
    result = pthread_mutex_init(m_apMutex.get(), m_apMutexAttr.get());
    if (result != 0) {
        throw std::runtime_error("failed to initialize mutex");
    }
#endif

}

Lock::~Lock() {
    if (isLocked()) {
        unlock();
    }
#ifdef WIN32
    const BOOL ok = CloseHandle(m_apMutex.release());
    assert(ok);
#else
    if (!m_apMutex.isNull()) {
        const int result = pthread_mutex_destroy(m_apMutex.get());
        if (result != 0) {
            printWarning("Lock| Failed to destroy mutex");
        }
        assert(result == 0);
    }
    if (!m_apMutexAttr.isNull()) {
        const int result = pthread_mutexattr_destroy(m_apMutexAttr.get());
        if (result != 0) {
            printWarning("Lock| Failed to destroy mutex attributes");
        }
        assert(result == 0);
    }
#endif
}

void Lock::lock() {
#ifdef WIN32
    const DWORD waitResult = WaitForSingleObject(m_apMutex.get(), INFINITE);
    assert(waitResult == WAIT_OBJECT_0);
#else
    const int result = pthread_mutex_lock(m_apMutex.get());
    if (result != 0) {
        printWarning("Lock| Failed to lock mutex");
    }
    assert(result == 0);
#endif
    m_bLocked = true;
}

void Lock::unlock() {
    m_bLocked = false;
#ifdef WIN32
    const BOOL ok = ReleaseMutex(m_apMutex.get());
    assert(ok);
#else
    const int result = pthread_mutex_unlock(m_apMutex.get());
    if (result != 0) {
        printWarning("Lock| Failed to unlock mutex");
    }
    assert(result == 0);
#endif
}

bool Lock::isLocked() const {
    return m_bLocked;
}

Condition* Lock::createCondition() {
    return new Condition(this);
}