summaryrefslogtreecommitdiffabout
path: root/pwmanager/pwmanager/spinforsignal.cpp
blob: 0401d433f58bcc952955fa9be0664ba41b1b3db0 (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
/***************************************************************************
 *                                                                         *
 *   copyright (C) 2003, 2004 by Michael Buesch                            *
 *   email: mbuesch@freenet.de                                             *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License version 2        *
 *   as published by the Free Software Foundation.                         *
 *                                                                         *
 ***************************************************************************/

/***************************************************************************
 * copyright (C) 2004 by Ulf Schenk
 * This file is originaly based on version 1.0.1 of pwmanager
 * and was modified to run on embedded devices that run microkde
 *
 * $Id$
 **************************************************************************/  

#include "spinforsignal.h"
#include "pwmexception.h"

#ifndef PWM_EMBEDDED
#include <kapp.h>
#endif

#include <time.h>
#include <pthread.h> 

static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;


SpinForSignal::SpinForSignal()
 : QObject()
{
	doSpin = false;
}

void SpinForSignal::spinSleep()
{
	const struct timespec t = { 0, 100 };
	nanosleep(&t, 0);
}

void SpinForSignal::_spin()
{
	doSpin = true;
	printDebug("spinning for signal.");
	do {
		kapp->processEvents();
		spinSleep();
	} while (doSpin);
	printDebug("spinning stopped.");
}

void SpinForSignal::spin(uint32_t *u32, string *str)
{
	_spin();
	if (pthread_mutex_lock(&mutex)) {
		printError("spin(uint32_t *u32, string *str): pthread_mutex_lock failed");
		return;
	}
	*u32 = u32_storage;
	*str = str_storage;
	pthread_mutex_unlock(&mutex);
}

void SpinForSignal::u32_str_slot(uint32_t u32, const string &str)
{
	if (doSpin) {
		printDebug("ul_str_slot(unsigned long ul, const string &str)");
		u32_storage = u32;
		str_storage = str;
		doSpin = false;
	}
}

void SpinForSignal::cancelSpin()
{
	if (pthread_mutex_lock(&mutex)) {
		printError("cancelSpin(): pthread_mutex_lock failed");
		return;
	}
	printDebug("spinning cancelled.");
	u32_storage = 0;
	str_storage = "";
	doSpin = false;
	pthread_mutex_unlock(&mutex);
}

#include "spinforsignal.moc"