[3b672c]: libkwave / PluginWorkerThread.cpp Maximize Restore History

Download this file

PluginWorkerThread.cpp    178 lines (145 with data), 5.3 kB

  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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/***************************************************************************
libkwave/PluginWorkerThread.cpp - worker thread for Kwave plugins
-------------------
begin : Sun Apr 06 2008
copyright : (C) 2008 by Thomas Eschenbacher
email : Thomas.Eschenbacher@gmx.de
***************************************************************************/
/***************************************************************************
* *
* This program 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 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "config.h"
#include <signal.h>
#include <sys/time.h>
#include <stdio.h>
#include <QtGlobal> // for qWarning()
#undef DEBUG_FIND_DEADLOCKS
#ifdef DEBUG_FIND_DEADLOCKS
#include <execinfo.h> // for backtrace()
#include <errno.h>
#include <error.h> // for strerror()
#endif
#include "libkwave/Plugin.h"
#include "libkwave/PluginWorkerThread.h"
//***************************************************************************
extern "C" void _dummy_SIGHUP_handler(int)
{
printf("\r\n--- SIGHUP ---\r\n");
}
//***************************************************************************
Kwave::PluginWorkerThread::PluginWorkerThread(Kwave::Plugin *plugin,
QStringList params)
:QThread(plugin),
m_plugin(plugin),
m_params(params),
m_lock(), m_lock_sighup(),
m_should_stop(false),
m_tid(pthread_self()),
m_owner_tid(pthread_self())
{
QMutexLocker lock(&m_lock);
}
//***************************************************************************
Kwave::PluginWorkerThread::~PluginWorkerThread()
{
if (isRunning()) {
qDebug("PluginWorkerThread::~PluginWorkerThread(): waiting for normal shutdown");
wait(2000);
qDebug("PluginWorkerThread::~PluginWorkerThread(): stopping");
stop(2000);
}
Q_ASSERT(!isRunning());
}
//***************************************************************************
void Kwave::PluginWorkerThread::start()
{
QMutexLocker lock(&m_lock);
// reset the "should stop" command flag
m_should_stop = false;
QThread::start();
}
//***************************************************************************
int Kwave::PluginWorkerThread::stop(unsigned int timeout)
{
QMutexLocker lock(&m_lock);
if (!isRunning()) return 0; // already down
if (timeout < 1000) timeout = 1000;
// set the "should stop" flag
m_should_stop = true;
// send one SIGHUP in advance
{
QMutexLocker _lock(&m_lock_sighup);
if (!pthread_equal(m_tid, m_owner_tid))
pthread_kill(m_tid, SIGHUP);
if (!isRunning()) return 0;
}
// try to stop cooperatively
if (!isRunning()) return 0;
wait(timeout/10);
if (!isRunning()) return 0;
// try to interrupt by INT signal
qWarning("PluginWorkerThread::stop(): sending SIGHUP");
for (unsigned int i=0; i < 8; i++) {
{
QMutexLocker _lock(&m_lock_sighup);
if (!isRunning()) return 0;
if (!pthread_equal(m_tid, m_owner_tid))
pthread_kill(m_tid, SIGHUP);
}
if (!isRunning()) return 0;
wait(timeout/10);
if (!isRunning()) return 0;
}
#ifdef DEBUG_FIND_DEADLOCKS
if (running()) {
qDebug("PluginWorkerThread::stop(): pthread_self()=%08X",
(unsigned int)pthread_self());
void *buf[256];
size_t n = backtrace(buf, 256);
backtrace_symbols_fd(buf, n, 2);
}
#endif
qDebug("PluginWorkerThread::stop(): canceling thread");
terminate();
return -1;
}
//***************************************************************************
void Kwave::PluginWorkerThread::run()
{
sighandler_t old_handler;
Q_ASSERT(m_plugin);
if (!m_plugin) return;
/* install a SIGHUP handler and allow sending SIGHUP */
{
QMutexLocker _lock(&m_lock_sighup);
/* get the POSIX style thread ID, needed for sending SIGHUP */
m_tid = pthread_self();
/* install handler for SIGHUP */
old_handler = signal(SIGHUP, _dummy_SIGHUP_handler);
}
/* call the plugin's run(...) function */
m_plugin->run_wrapper(m_params);
/* uninstall the SIGHUP handler and forbid sending SIGHUP */
{
QMutexLocker _lock(&m_lock_sighup);
/* avoid sending any SIGHUP by setting the m_tid to "invalid" */
m_tid = m_owner_tid;
/* restore previous signal handler */
old_handler = signal(SIGHUP, old_handler);
}
}
//***************************************************************************
bool Kwave::PluginWorkerThread::shouldStop()
{
return (m_should_stop);
}
//***************************************************************************
using namespace Kwave;
#include "PluginWorkerThread.moc"
//***************************************************************************
//***************************************************************************