An OS-independent sleep function, and one usage thereof.

BUG=603
TEST=none

Review URL: https://webrtc-codereview.appspot.com/659004

git-svn-id: http://webrtc.googlecode.com/svn/trunk@2412 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
hta@webrtc.org 2012-06-18 11:24:57 +00:00
parent 37198007ea
commit 41adcdbf13
5 changed files with 61 additions and 9 deletions

View File

@ -0,0 +1,19 @@
/*
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
// An OS-independent sleep function.
namespace webrtc {
// This function sleeps for the specified number of milliseconds.
// It may return early if the thread is woken by some other event,
// such as the delivery of a signal on Unix.
void SleepMs(int msecs);
} // namespace webrtc

View File

@ -38,6 +38,7 @@ LOCAL_SRC_FILES := \
cpu_linux.cc \
critical_section_posix.cc \
event_posix.cc \
sleep.cc \
thread_posix.cc \
trace_posix.cc \
rw_lock_posix.cc

View File

@ -0,0 +1,36 @@
/*
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
// An OS-independent sleep function.
#include "system_wrappers/interface/sleep.h"
#ifdef _WIN32
// For Sleep()
#include <windows.h>
#else
// For nanosleep()
#include <time.h>
#endif
namespace webrtc {
void SleepMs(int msecs) {
#ifdef _WIN32
Sleep(msecs);
#else
struct timespec short_wait;
struct timespec remainder;
short_wait.tv_sec = msecs / 1000;
short_wait.tv_nsec = (msecs % 1000) * 1000 * 1000;
nanosleep(&short_wait, &remainder);
#endif
}
} // namespace webrtc

View File

@ -42,6 +42,7 @@
'../interface/rw_lock_wrapper.h',
'../interface/scoped_ptr.h',
'../interface/scoped_refptr.h',
'../interface/sleep.h',
'../interface/sort.h',
'../interface/static_instance.h',
'../interface/thread_wrapper.h',
@ -88,6 +89,7 @@
'rw_lock_posix.h',
'rw_lock_win.cc',
'rw_lock_win.h',
'sleep.cc',
'sort.cc',
'thread.cc',
'thread_posix.cc',

View File

@ -17,11 +17,12 @@
#include "trace_win.h"
#else
#include <stdio.h>
#include <time.h>
#include <stdarg.h>
#include "trace_posix.h"
#endif // _WIN32
#include "system_wrappers/interface/sleep.h"
#define KEY_LEN_CHARS 31
#ifdef _WIN32
@ -108,14 +109,7 @@ bool TraceImpl::StopThread()
// TODO (hellner): why not use condition variables to do this? Or let the
// worker thread die and let this thread flush remaining
// messages?
#ifdef _WIN32
Sleep(10);
#else
timespec t;
t.tv_sec = 0;
t.tv_nsec = 10*1000000;
nanosleep(&t,NULL);
#endif
SleepMs(10);
_thread.SetNotAlive();
// Make sure the thread finishes as quickly as possible (instead of having