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
|
/*
* This file is part of nzbget. See <http://nzbget.net>.
*
* Copyright (C) 2004 Sven Henkel <sidddy@users.sourceforge.net>
* Copyright (C) 2007-2019 Andrey Prygunkov <hugbug@users.sourceforge.net>
*
* 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.
*
* This program 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 "nzbget.h"
#include "Log.h"
#include "Thread.h"
int Thread::m_threadCount = 1; // take the main program thread into account
std::unique_ptr<Mutex> Thread::m_threadMutex;
void Thread::Init()
{
debug("Initializing global thread data");
m_threadMutex = std::make_unique<Mutex>();
}
Thread::Thread()
{
debug("Creating Thread");
}
Thread::~Thread()
{
debug("Destroying Thread");
}
void Thread::Start()
{
debug("Starting Thread");
m_running = true;
// NOTE: "m_threadMutex" ensures that "t" lives until the very end of the function
Guard guard(m_threadMutex);
// start the new thread
std::thread t([&]{
{
// trying to lock "m_threadMutex", this will wait until function "Start()" is completed
// and "t" is detached.
Guard guard(m_threadMutex);
}
thread_handler();
});
// save the native handle to be able to cancel (Kill) the thread and then detach
m_threadObj = t.native_handle();
t.detach();
}
void Thread::Stop()
{
debug("Stopping Thread");
m_stopped = true;
}
void Thread::Resume()
{
debug("Resuming Thread");
m_stopped = false;
}
bool Thread::Kill()
{
debug("Killing Thread");
Guard guard(m_threadMutex);
#ifdef WIN32
bool terminated = TerminateThread(m_threadObj, 0) != 0;
#else
#ifdef HAVE_PTHREAD_CANCEL
bool terminated = pthread_cancel(m_threadObj) == 0;
#else
bool terminated = false;
warn("Could not kill thread: thread cancelling isn't supported on this platform");
#endif
#endif
if (terminated)
{
m_threadCount--;
}
return terminated;
}
void Thread::thread_handler()
{
m_threadCount++;
debug("Entering Thread-func");
Run();
debug("Thread-func exited");
m_running = false;
m_threadCount--;
if (m_autoDestroy)
{
debug("Autodestroying Thread-object");
delete this;
}
}
int Thread::GetThreadCount()
{
Guard guard(m_threadMutex);
return m_threadCount;
}
|