diff options
-rw-r--r-- | third_party/libjingle/files/talk/base/thread.cc | 12 | ||||
-rw-r--r-- | third_party/libjingle/files/talk/base/thread.h | 2 |
2 files changed, 14 insertions, 0 deletions
diff --git a/third_party/libjingle/files/talk/base/thread.cc b/third_party/libjingle/files/talk/base/thread.cc index 8fc45e1..78db60d 100644 --- a/third_party/libjingle/files/talk/base/thread.cc +++ b/third_party/libjingle/files/talk/base/thread.cc @@ -98,6 +98,11 @@ void ThreadManager::Remove(Thread *thread) { threads_.erase(std::remove(threads_.begin(), threads_.end(), thread), threads_.end()); } +bool ThreadManager::ThreadActive(Thread *thread) { + CritScope cs(&crit_); + return(std::find(threads_.begin(), threads_.end(), thread) != threads_.end()); +} + Thread::Thread(SocketServer* ss) : MessageQueue(ss), priority_(PRIORITY_NORMAL) { g_thmgr.Add(this); started_ = false; @@ -121,11 +126,13 @@ void Thread::Start() { param.sched_priority = 15; // +15 = pthread_attr_setschedparam(&attr, ¶m); } + CritScope cs(&crit_); pthread_create(&thread_, &attr, PreRun, this); started_ = true; } void Thread::Join() { + CritScope cs(&crit_); if (started_) { void *pv; pthread_join(thread_, &pv); @@ -166,6 +173,7 @@ void Thread::Start() { if (priority_ != PRIORITY_NORMAL) { flags = CREATE_SUSPENDED; } + CritScope cs(&crit_); thread_ = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)PreRun, this, flags, NULL); if (thread_) { if (priority_ != PRIORITY_NORMAL) { @@ -179,6 +187,7 @@ void Thread::Start() { } void Thread::Join() { + CritScope cs(&crit_); if (started_) { WaitForSingleObject(thread_, INFINITE); CloseHandle(thread_); @@ -189,6 +198,9 @@ void Thread::Join() { void *Thread::PreRun(void *pv) { Thread *thread = (Thread *)pv; + // Make sure the thread hasn't been deleted. + if (!g_thmgr.ThreadActive(thread)) + return NULL; ThreadManager::SetCurrent(thread); #if defined(WIN32) && defined(_DEBUG) char buf[256]; diff --git a/third_party/libjingle/files/talk/base/thread.h b/third_party/libjingle/files/talk/base/thread.h index 5da0aed..367203a 100644 --- a/third_party/libjingle/files/talk/base/thread.h +++ b/third_party/libjingle/files/talk/base/thread.h @@ -55,6 +55,7 @@ public: static void SetCurrent(Thread *thread); void Add(Thread *thread); void Remove(Thread *thread); + bool ThreadActive(Thread *thread); private: Thread *main_thread_; @@ -132,6 +133,7 @@ private: std::list<_SendMessage> sendlist_; ThreadPriority priority_; + CriticalSection crit_; bool started_; bool has_sends_; |