-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdstceventloop.cpp
70 lines (57 loc) · 1.96 KB
/
dstceventloop.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
// Copyright (C) 2019, Jaguar Land Rover
// This program is licensed under the terms and conditions of the
// Mozilla Public License, version 2.0. The full text of the
// Mozilla Public License is at https://www.mozilla.org/MPL/2.0/
//
// Author:Steven Martin (smarti24@jaguarlandrover.com)
#include "dstceventloop.hpp"
#include <utility>
#include <iostream>
extern "C" {
#include "dstc.h"
}
namespace dstc {
std::mutex EventLoopRunner::_num_objects_mtx;
unsigned int EventLoopRunner::_num_objects = 0;
std::thread EventLoopRunner::_thread;
bool EventLoopRunner::_thread_running = false;
EventLoopRunner::EventLoopRunner()
{
_start();
}
EventLoopRunner::~EventLoopRunner() {
std::lock_guard<std::mutex> lock(_num_objects_mtx);
--_num_objects;
if (_num_objects == 0) {
_thread_running = false;
// catch corner case that thread has not quite yet started...
while(!_thread.joinable()) {}
_thread.join();
}
}
void EventLoopRunner::_start() {
bool start_thread = false;
{ // lock scope
std::lock_guard<std::mutex> lock(_num_objects_mtx);
if (_num_objects == 0) {
start_thread = true;
}
++_num_objects;
} // end lock scope
if (start_thread) {
_thread_running = true;
_thread = std::thread(
[] {
while(_thread_running) {
// if `dstc_process_events` reports a timeout, we yield the
// thread for 10 ms which should give other threads that may be
// blocked a chance to call into the API
if (dstc_process_events(10) == ETIME) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
}
);
}
}
}