forked from adiesner/GarminPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpsDevice.cpp
92 lines (75 loc) · 2.64 KB
/
gpsDevice.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
* GarminPlugin
* Copyright (C) Andreas Diesner 2010 <andreas.diesner [AT] gmx [DOT] de>
*
* GarminPlugin is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GarminPlugin 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "gpsDevice.h"
#include "log.h"
pthread_mutex_t shareVariables_mtx= PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t waitThreadCond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t waitThreadMutex = PTHREAD_MUTEX_INITIALIZER;
GpsDevice::GpsDevice() : threadId (0) {
}
GpsDevice::~GpsDevice() {
Log::dbg("Destructor of GpsDevice "+ this->displayName + " called");
cancelThread();
}
bool GpsDevice::startThread() {
int code = pthread_create(&(this->threadId), NULL, GpsDevice::workerThread, (void*)this);
if (code != 0) {
Log::err("Creation of thread failed!");
return false; // Error happened
}
return true;
}
void GpsDevice::lockVariables() {
pthread_mutex_lock( &shareVariables_mtx ); // LOCK Shared variables
}
void GpsDevice::unlockVariables() {
pthread_mutex_unlock( &shareVariables_mtx); // UNLOCK Shared variables
}
void GpsDevice::waitThread() {
Log::dbg("Thread is going to sleep!");
pthread_mutex_lock(&waitThreadMutex);
while (2 == this->threadState) {
pthread_cond_wait(&waitThreadCond, &waitThreadMutex);
}
pthread_mutex_unlock(&waitThreadMutex);
Log::dbg("Thread was woken up!");
}
void GpsDevice::signalThread() {
Log::dbg("Thread wake up signal sending...");
// wake up thread
pthread_mutex_lock(&waitThreadMutex);
pthread_cond_signal(&waitThreadCond);
pthread_mutex_unlock(&waitThreadMutex);
Log::dbg("Thread wake up signal was sent!");
}
void GpsDevice::cancelThread() {
Log::dbg("Cancel Thread in GPSDevice für "+this->displayName);
if (this->threadId > 0) {
pthread_cancel(this->threadId);
}
}
/*static*/
void * GpsDevice::workerThread(void * pthis) {
Log::dbg("Thread started");
GpsDevice * obj = (GpsDevice*)pthis;
obj->doWork();
Log::dbg("Thread finished");
obj->threadId = 0;
return NULL;
}