Switch to QueueUserAPC for shutting down the thread (no event needed).
Also actually specifying the reserve stack size. BUG= R=pbos@webrtc.org Review URL: https://webrtc-codereview.appspot.com/37179004 Cr-Commit-Position: refs/heads/master@{#8331} git-svn-id: http://webrtc.googlecode.com/svn/trunk@8331 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
parent
fddeaf5daa
commit
9d94a0c736
@ -19,23 +19,26 @@
|
||||
#include "webrtc/system_wrappers/source/set_thread_name_win.h"
|
||||
|
||||
namespace webrtc {
|
||||
namespace {
|
||||
void CALLBACK RaiseFlag(ULONG_PTR param) {
|
||||
*reinterpret_cast<bool*>(param) = true;
|
||||
}
|
||||
}
|
||||
|
||||
ThreadWindows::ThreadWindows(ThreadRunFunction func, ThreadObj obj,
|
||||
ThreadPriority prio, const char* thread_name)
|
||||
: run_function_(func),
|
||||
obj_(obj),
|
||||
prio_(prio),
|
||||
event_(CreateEvent(NULL, FALSE, FALSE, NULL)),
|
||||
stop_(false),
|
||||
thread_(NULL),
|
||||
name_(thread_name ? thread_name : "webrtc") {
|
||||
DCHECK(func);
|
||||
DCHECK(event_);
|
||||
}
|
||||
|
||||
ThreadWindows::~ThreadWindows() {
|
||||
DCHECK(main_thread_.CalledOnValidThread());
|
||||
DCHECK(!thread_);
|
||||
CloseHandle(event_);
|
||||
}
|
||||
|
||||
// static
|
||||
@ -53,9 +56,13 @@ bool ThreadWindows::Start(unsigned int& id) {
|
||||
DCHECK(main_thread_.CalledOnValidThread());
|
||||
DCHECK(!thread_);
|
||||
|
||||
// See bug 2902 for stack size.
|
||||
stop_ = false;
|
||||
|
||||
// See bug 2902 for background on STACK_SIZE_PARAM_IS_A_RESERVATION.
|
||||
// Set the reserved stack stack size to 1M, which is the default on Windows
|
||||
// and Linux.
|
||||
DWORD thread_id;
|
||||
thread_ = ::CreateThread(NULL, 0, &StartThread, this,
|
||||
thread_ = ::CreateThread(NULL, 1024 * 1024, &StartThread, this,
|
||||
STACK_SIZE_PARAM_IS_A_RESERVATION, &thread_id);
|
||||
if (!thread_ ) {
|
||||
DCHECK(false) << "CreateThread failed";
|
||||
@ -92,7 +99,8 @@ bool ThreadWindows::Start(unsigned int& id) {
|
||||
bool ThreadWindows::Stop() {
|
||||
DCHECK(main_thread_.CalledOnValidThread());
|
||||
if (thread_) {
|
||||
SetEvent(event_);
|
||||
// Set stop_ to |true| on the worker thread.
|
||||
QueueUserAPC(&RaiseFlag, thread_, reinterpret_cast<ULONG_PTR>(&stop_));
|
||||
WaitForSingleObject(thread_, INFINITE);
|
||||
CloseHandle(thread_);
|
||||
thread_ = nullptr;
|
||||
@ -106,9 +114,14 @@ void ThreadWindows::Run() {
|
||||
SetThreadName(static_cast<DWORD>(-1), name_.c_str());
|
||||
|
||||
do {
|
||||
// The interface contract of Start/Stop is that for a successfull call to
|
||||
// Start, there should be at least one call to the run function. So we
|
||||
// call the function before checking |stop_|.
|
||||
if (!run_function_(obj_))
|
||||
break;
|
||||
} while (WaitForSingleObject(event_, 0) == WAIT_TIMEOUT);
|
||||
// Alertable sleep to permit RaiseFlag to run and update |stop_|.
|
||||
SleepEx(0, true);
|
||||
} while (!stop_);
|
||||
}
|
||||
|
||||
} // namespace webrtc
|
||||
|
@ -36,7 +36,7 @@ class ThreadWindows : public ThreadWrapper {
|
||||
|
||||
ThreadRunFunction const run_function_;
|
||||
void* const obj_;
|
||||
HANDLE event_; // Used to signal stoppage.
|
||||
bool stop_;
|
||||
// TODO(tommi): Consider having a SetPriority method instead of this variable.
|
||||
ThreadPriority prio_;
|
||||
HANDLE thread_;
|
||||
|
Loading…
x
Reference in New Issue
Block a user