summaryrefslogtreecommitdiffabout
path: root/pwmanager/pwmanager/spinforsignal.cpp
Unidiff
Diffstat (limited to 'pwmanager/pwmanager/spinforsignal.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--pwmanager/pwmanager/spinforsignal.cpp91
1 files changed, 91 insertions, 0 deletions
diff --git a/pwmanager/pwmanager/spinforsignal.cpp b/pwmanager/pwmanager/spinforsignal.cpp
new file mode 100644
index 0000000..0401d43
--- a/dev/null
+++ b/pwmanager/pwmanager/spinforsignal.cpp
@@ -0,0 +1,91 @@
1/***************************************************************************
2 * *
3 * copyright (C) 2003, 2004 by Michael Buesch *
4 * email: mbuesch@freenet.de *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License version 2 *
8 * as published by the Free Software Foundation. *
9 * *
10 ***************************************************************************/
11
12/***************************************************************************
13 * copyright (C) 2004 by Ulf Schenk
14 * This file is originaly based on version 1.0.1 of pwmanager
15 * and was modified to run on embedded devices that run microkde
16 *
17 * $Id$
18 **************************************************************************/
19
20#include "spinforsignal.h"
21#include "pwmexception.h"
22
23#ifndef PWM_EMBEDDED
24#include <kapp.h>
25#endif
26
27#include <time.h>
28#include <pthread.h>
29
30static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
31
32
33SpinForSignal::SpinForSignal()
34 : QObject()
35{
36 doSpin = false;
37}
38
39void SpinForSignal::spinSleep()
40{
41 const struct timespec t = { 0, 100 };
42 nanosleep(&t, 0);
43}
44
45void SpinForSignal::_spin()
46{
47 doSpin = true;
48 printDebug("spinning for signal.");
49 do {
50 kapp->processEvents();
51 spinSleep();
52 } while (doSpin);
53 printDebug("spinning stopped.");
54}
55
56void SpinForSignal::spin(uint32_t *u32, string *str)
57{
58 _spin();
59 if (pthread_mutex_lock(&mutex)) {
60 printError("spin(uint32_t *u32, string *str): pthread_mutex_lock failed");
61 return;
62 }
63 *u32 = u32_storage;
64 *str = str_storage;
65 pthread_mutex_unlock(&mutex);
66}
67
68void SpinForSignal::u32_str_slot(uint32_t u32, const string &str)
69{
70 if (doSpin) {
71 printDebug("ul_str_slot(unsigned long ul, const string &str)");
72 u32_storage = u32;
73 str_storage = str;
74 doSpin = false;
75 }
76}
77
78void SpinForSignal::cancelSpin()
79{
80 if (pthread_mutex_lock(&mutex)) {
81 printError("cancelSpin(): pthread_mutex_lock failed");
82 return;
83 }
84 printDebug("spinning cancelled.");
85 u32_storage = 0;
86 str_storage = "";
87 doSpin = false;
88 pthread_mutex_unlock(&mutex);
89}
90
91#include "spinforsignal.moc"