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

Side by Side Diff: webrtc/system_wrappers/source/data_log.cc

Issue 1476453002: Clean up PlatformThread. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: IsRunning DCHECK Created 5 years 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
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after
311 } 311 }
312 312
313 int DataLog::NextRow(const std::string& table_name) { 313 int DataLog::NextRow(const std::string& table_name) {
314 DataLogImpl* data_log = DataLogImpl::StaticInstance(); 314 DataLogImpl* data_log = DataLogImpl::StaticInstance();
315 if (data_log == NULL) 315 if (data_log == NULL)
316 return -1; 316 return -1;
317 return data_log->DataLogImpl::StaticInstance()->NextRow(table_name); 317 return data_log->DataLogImpl::StaticInstance()->NextRow(table_name);
318 } 318 }
319 319
320 DataLogImpl::DataLogImpl() 320 DataLogImpl::DataLogImpl()
321 : counter_(1), 321 : counter_(1),
322 tables_(), 322 tables_(),
323 flush_event_(EventWrapper::Create()), 323 flush_event_(EventWrapper::Create()),
324 tables_lock_(RWLockWrapper::CreateRWLock()) { 324 file_writer_thread_(
325 } 325 new rtc::PlatformThread(DataLogImpl::Run, instance_, "DataLog")),
326 tables_lock_(RWLockWrapper::CreateRWLock()) {}
326 327
327 DataLogImpl::~DataLogImpl() { 328 DataLogImpl::~DataLogImpl() {
328 StopThread(); 329 StopThread();
329 Flush(); // Write any remaining rows 330 Flush(); // Write any remaining rows
330 delete flush_event_; 331 delete flush_event_;
331 for (TableMap::iterator it = tables_.begin(); it != tables_.end();) { 332 for (TableMap::iterator it = tables_.begin(); it != tables_.end();) {
332 delete static_cast<LogTable*>(it->second); 333 delete static_cast<LogTable*>(it->second);
333 // For maps all iterators (except the erased) are valid after an erase 334 // For maps all iterators (except the erased) are valid after an erase
334 tables_.erase(it++); 335 tables_.erase(it++);
335 } 336 }
336 delete tables_lock_; 337 delete tables_lock_;
337 } 338 }
338 339
339 int DataLogImpl::CreateLog() { 340 int DataLogImpl::CreateLog() {
340 CriticalSectionScoped synchronize(crit_sect_.get()); 341 CriticalSectionScoped synchronize(crit_sect_.get());
341 if (instance_ == NULL) { 342 if (instance_ == NULL) {
342 instance_ = new DataLogImpl(); 343 instance_ = new DataLogImpl();
343 return instance_->Init(); 344 return instance_->Init();
344 } else { 345 } else {
345 ++instance_->counter_; 346 ++instance_->counter_;
346 } 347 }
347 return 0; 348 return 0;
348 } 349 }
349 350
350 int DataLogImpl::Init() { 351 int DataLogImpl::Init() {
351 file_writer_thread_ = 352 file_writer_thread_->Start();
352 PlatformThread::CreateThread(DataLogImpl::Run, instance_, "DataLog"); 353 file_writer_thread_->SetPriority(rtc::kHighestPriority);
353 bool success = file_writer_thread_->Start();
354 if (!success)
355 return -1;
356 file_writer_thread_->SetPriority(kHighestPriority);
357 return 0; 354 return 0;
358 } 355 }
359 356
360 DataLogImpl* DataLogImpl::StaticInstance() { 357 DataLogImpl* DataLogImpl::StaticInstance() {
361 return instance_; 358 return instance_;
362 } 359 }
363 360
364 void DataLogImpl::ReturnLog() { 361 void DataLogImpl::ReturnLog() {
365 CriticalSectionScoped synchronize(crit_sect_.get()); 362 CriticalSectionScoped synchronize(crit_sect_.get());
366 if (instance_ && instance_->counter_ > 1) { 363 if (instance_ && instance_->counter_ > 1) {
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
399 if (tables_.count(table_name) == 0) 396 if (tables_.count(table_name) == 0)
400 return -1; 397 return -1;
401 return tables_[table_name]->InsertCell(column_name, value_container); 398 return tables_[table_name]->InsertCell(column_name, value_container);
402 } 399 }
403 400
404 int DataLogImpl::NextRow(const std::string& table_name) { 401 int DataLogImpl::NextRow(const std::string& table_name) {
405 ReadLockScoped synchronize(*tables_lock_); 402 ReadLockScoped synchronize(*tables_lock_);
406 if (tables_.count(table_name) == 0) 403 if (tables_.count(table_name) == 0)
407 return -1; 404 return -1;
408 tables_[table_name]->NextRow(); 405 tables_[table_name]->NextRow();
409 if (!file_writer_thread_) { 406 // Signal a complete row
410 // Write every row to file as they get complete. 407 flush_event_->Set();
411 tables_[table_name]->Flush();
412 } else {
413 // Signal a complete row
414 flush_event_->Set();
415 }
416 return 0; 408 return 0;
417 } 409 }
418 410
419 void DataLogImpl::Flush() { 411 void DataLogImpl::Flush() {
420 ReadLockScoped synchronize(*tables_lock_); 412 ReadLockScoped synchronize(*tables_lock_);
421 for (TableMap::iterator it = tables_.begin(); it != tables_.end(); ++it) { 413 for (TableMap::iterator it = tables_.begin(); it != tables_.end(); ++it) {
422 it->second->Flush(); 414 it->second->Flush();
423 } 415 }
424 } 416 }
425 417
426 bool DataLogImpl::Run(void* obj) { 418 bool DataLogImpl::Run(void* obj) {
427 static_cast<DataLogImpl*>(obj)->Process(); 419 static_cast<DataLogImpl*>(obj)->Process();
428 return true; 420 return true;
429 } 421 }
430 422
431 void DataLogImpl::Process() { 423 void DataLogImpl::Process() {
432 // Wait for a row to be complete 424 // Wait for a row to be complete
433 flush_event_->Wait(WEBRTC_EVENT_INFINITE); 425 flush_event_->Wait(WEBRTC_EVENT_INFINITE);
434 Flush(); 426 Flush();
435 } 427 }
436 428
437 void DataLogImpl::StopThread() { 429 void DataLogImpl::StopThread() {
438 if (file_writer_thread_) { 430 flush_event_->Set();
439 flush_event_->Set(); 431 file_writer_thread_->Stop();
440 file_writer_thread_->Stop();
441 }
442 } 432 }
443 433
444 } // namespace webrtc 434 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/system_wrappers/source/critical_section_unittest.cc ('k') | webrtc/system_wrappers/source/event_timer_posix.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698