diff --git a/src/system_wrappers/interface/sleep.h b/src/system_wrappers/interface/sleep.h new file mode 100644 index 0000000000..735efefcc7 --- /dev/null +++ b/src/system_wrappers/interface/sleep.h @@ -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 diff --git a/src/system_wrappers/source/Android.mk b/src/system_wrappers/source/Android.mk index 08c6827905..dee84ca294 100644 --- a/src/system_wrappers/source/Android.mk +++ b/src/system_wrappers/source/Android.mk @@ -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 diff --git a/src/system_wrappers/source/sleep.cc b/src/system_wrappers/source/sleep.cc new file mode 100644 index 0000000000..be8523857c --- /dev/null +++ b/src/system_wrappers/source/sleep.cc @@ -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 +#else +// For nanosleep() +#include +#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 diff --git a/src/system_wrappers/source/system_wrappers.gyp b/src/system_wrappers/source/system_wrappers.gyp index dbbf2cfdd1..a9286e7b29 100644 --- a/src/system_wrappers/source/system_wrappers.gyp +++ b/src/system_wrappers/source/system_wrappers.gyp @@ -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', diff --git a/src/system_wrappers/source/trace_impl.cc b/src/system_wrappers/source/trace_impl.cc index 5600d3944a..b1b4f6fd4f 100644 --- a/src/system_wrappers/source/trace_impl.cc +++ b/src/system_wrappers/source/trace_impl.cc @@ -17,11 +17,12 @@ #include "trace_win.h" #else #include -#include #include #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