Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(262)

Side by Side Diff: third_party/libjingle/files/talk/base/thread.cc

Issue 274079: Prevent thread creation if Join has already been called.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « third_party/libjingle/files/talk/base/thread.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * libjingle 2 * libjingle
3 * Copyright 2004--2005, Google Inc. 3 * Copyright 2004--2005, Google Inc.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met: 6 * modification, are permitted provided that the following conditions are met:
7 * 7 *
8 * 1. Redistributions of source code must retain the above copyright notice, 8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer. 9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice, 10 * 2. Redistributions in binary form must reproduce the above copyright notice,
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 } 99 }
100 100
101 bool ThreadManager::ThreadActive(Thread *thread) { 101 bool ThreadManager::ThreadActive(Thread *thread) {
102 CritScope cs(&crit_); 102 CritScope cs(&crit_);
103 return(std::find(threads_.begin(), threads_.end(), thread) != threads_.end()); 103 return(std::find(threads_.begin(), threads_.end(), thread) != threads_.end());
104 } 104 }
105 105
106 Thread::Thread(SocketServer* ss) : MessageQueue(ss), priority_(PRIORITY_NORMAL) { 106 Thread::Thread(SocketServer* ss) : MessageQueue(ss), priority_(PRIORITY_NORMAL) {
107 g_thmgr.Add(this); 107 g_thmgr.Add(this);
108 started_ = false; 108 started_ = false;
109 stopped_ = false;
109 has_sends_ = false; 110 has_sends_ = false;
110 } 111 }
111 112
112 Thread::~Thread() { 113 Thread::~Thread() {
113 Stop(); 114 Stop();
114 if (active_) 115 if (active_)
115 Clear(NULL); 116 Clear(NULL);
116 g_thmgr.Remove(this); 117 g_thmgr.Remove(this);
117 } 118 }
118 119
119 #ifdef POSIX 120 #ifdef POSIX
120 void Thread::Start() { 121 void Thread::Start() {
121 pthread_attr_t attr; 122 pthread_attr_t attr;
122 pthread_attr_init(&attr); 123 pthread_attr_init(&attr);
123 if (priority_ == PRIORITY_IDLE) { 124 if (priority_ == PRIORITY_IDLE) {
124 struct sched_param param; 125 struct sched_param param;
125 pthread_attr_getschedparam(&attr, &param); 126 pthread_attr_getschedparam(&attr, &param);
126 param.sched_priority = 15; // +15 = 127 param.sched_priority = 15; // +15 =
127 pthread_attr_setschedparam(&attr, &param); 128 pthread_attr_setschedparam(&attr, &param);
128 } 129 }
129 CritScope cs(&started_crit_); 130 CritScope cs(&started_crit_);
131 // Make sure Join() hasn't been called yet.
132 if (stopped_)
133 return;
130 pthread_create(&thread_, &attr, PreRun, this); 134 pthread_create(&thread_, &attr, PreRun, this);
131 started_ = true; 135 started_ = true;
132 } 136 }
133 137
134 void Thread::Join() { 138 void Thread::Join() {
135 CritScope cs(&started_crit_); 139 CritScope cs(&started_crit_);
140 stopped_ = true;
136 if (started_) { 141 if (started_) {
137 void *pv; 142 void *pv;
138 pthread_join(thread_, &pv); 143 pthread_join(thread_, &pv);
139 } 144 }
140 } 145 }
141 #endif 146 #endif
142 147
143 #ifdef WIN32 148 #ifdef WIN32
144 149
145 typedef struct tagTHREADNAME_INFO 150 typedef struct tagTHREADNAME_INFO
(...skipping 21 matching lines...) Expand all
167 { 172 {
168 } 173 }
169 } 174 }
170 175
171 void Thread::Start() { 176 void Thread::Start() {
172 DWORD flags = 0; 177 DWORD flags = 0;
173 if (priority_ != PRIORITY_NORMAL) { 178 if (priority_ != PRIORITY_NORMAL) {
174 flags = CREATE_SUSPENDED; 179 flags = CREATE_SUSPENDED;
175 } 180 }
176 CritScope cs(&started_crit_); 181 CritScope cs(&started_crit_);
182 // Make sure Join() hasn't been called yet.
183 if (stopped_)
184 return;
177 thread_ = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)PreRun, this, flags, N ULL); 185 thread_ = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)PreRun, this, flags, N ULL);
178 if (thread_) { 186 if (thread_) {
179 if (priority_ != PRIORITY_NORMAL) { 187 if (priority_ != PRIORITY_NORMAL) {
180 if (priority_ == PRIORITY_IDLE) { 188 if (priority_ == PRIORITY_IDLE) {
181 ::SetThreadPriority(thread_, THREAD_PRIORITY_IDLE); 189 ::SetThreadPriority(thread_, THREAD_PRIORITY_IDLE);
182 } 190 }
183 ::ResumeThread(thread_); 191 ::ResumeThread(thread_);
184 } 192 }
185 } 193 }
186 started_ = true; 194 started_ = true;
187 } 195 }
188 196
189 void Thread::Join() { 197 void Thread::Join() {
190 CritScope cs(&started_crit_); 198 CritScope cs(&started_crit_);
199 stopped_ = true;
191 if (started_) { 200 if (started_) {
192 WaitForSingleObject(thread_, INFINITE); 201 WaitForSingleObject(thread_, INFINITE);
193 CloseHandle(thread_); 202 CloseHandle(thread_);
194 started_ = false; 203 started_ = false;
195 } 204 }
196 } 205 }
197 #endif 206 #endif
198 207
199 void *Thread::PreRun(void *pv) { 208 void *Thread::PreRun(void *pv) {
200 Thread *thread = (Thread *)pv; 209 Thread *thread = (Thread *)pv;
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
356 } 365 }
357 } 366 }
358 367
359 AutoThread::~AutoThread() { 368 AutoThread::~AutoThread() {
360 if (ThreadManager::CurrentThread() == this) { 369 if (ThreadManager::CurrentThread() == this) {
361 ThreadManager::SetCurrent(NULL); 370 ThreadManager::SetCurrent(NULL);
362 } 371 }
363 } 372 }
364 373
365 } // namespace talk_base 374 } // namespace talk_base
OLDNEW
« no previous file with comments | « third_party/libjingle/files/talk/base/thread.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698