summaryrefslogtreecommitdiff
path: root/src/MTS_Condition.cpp
blob: d238d5017838223b7724c34341fd63c6615bee2f (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
115
/*
 * 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_Condition.h>
#include <mts/MTS_Lock.h>
#include <mts/MTS_System.h>
#include <mts/MTS_Logger.h>
#include <cassert>
#include <cerrno>
#include <stdexcept>

using namespace MTS;

Condition::Condition(Lock* pLock)
: m_pLock(pLock) {
    if (pLock == 0) {
        throw std::invalid_argument("Condition| constructing lock is null");
    }
#ifdef WIN32
    m_apCondition.reset(CreateEvent(NULL, FALSE, FALSE, NULL));
    if (m_pCondition.get() == NULL) {
        throw std::bad_alloc();
    }
#else
    m_apCondition.reset(new pthread_cond_t());
    const uint32_t result = pthread_cond_init(m_apCondition.get(), NULL);
    if (result != 0) {
        throw std::runtime_error("Condition| failed to initialize condition");
    }
#endif
}

Condition::~Condition() {
    if (!m_apCondition.isNull()) {
#ifdef WIN32
    const BOOL ok = CloseHandle(m_apCondition.get());
    assert(ok);
#else
    const uint32_t result = pthread_cond_destroy(m_apCondition.get());
    if (result != 0) {
        printWarning("Condition| failed to destroy condition");
    }
    assert(result == 0);
#endif
    }
}

void Condition::wait() {
#ifdef WIN32
    wait(INFINITE);
#else
    const uint32_t result = pthread_cond_wait(m_apCondition.get(),
    m_pLock->m_apMutex.get());
    if (result != 0) {
        printWarning("Condition| failed to wait on condition");
    }
    assert(result == 0);
#endif
}

void Condition::wait(uint32_t millis) {
    assert(m_pLock->isLocked());
    if(!m_pLock->isLocked()) {
        printWarning("Condition| lock was not locked. not waiting on signal.");
        return;
    }
#ifdef WIN32
    ResetEvent(m_apCondition.get());
    m_pLock->unlock();
    const DWORD waitResult = WaitForSingleObject(m_apCondition.get(), millis);
    assert(waitResult == WAIT_OBJECT_0 || waitResult == WAIT_TIMEOUT);
    m_pLock->lock();
#else
    timespec abstime;
    int64_t micros = System::timeMicros() + (millis * 1000);
    abstime.tv_sec = static_cast<long>(micros / 1000000);
    abstime.tv_nsec = static_cast<long>((micros % 1000000) * 1000);
    const uint32_t result = pthread_cond_timedwait(m_apCondition.get(),
    m_pLock->m_apMutex.get(), &abstime);
    if (result != 0 && result != ETIMEDOUT) {
        printWarning("Condition| failed to time wait on condition");
    }
    assert(result == 0 || result == ETIMEDOUT);
#endif
}

void Condition::signal() {
#ifdef WIN32
    const BOOL ok = SetEvent(m_apCondition.get());
    assert(ok);
#else
    const uint32_t result = pthread_cond_broadcast(m_apCondition.get());
    if (result != 0) {
        printWarning("Condition| failed to signal condition");
    }
    assert(result == 0);
#endif
}