diff --git a/base/threading/platform_thread_starboard.cc b/base/threading/platform_thread_starboard.cc index 1da5ad712796..54a4a96e7a59 100644 --- a/base/threading/platform_thread_starboard.cc +++ b/base/threading/platform_thread_starboard.cc @@ -15,6 +15,7 @@ #include "base/threading/platform_thread.h" #include +#include #include "base/logging.h" #include "base/threading/thread_id_name_manager.h" @@ -120,7 +121,7 @@ void PlatformThread::YieldCurrentThread() { // static void PlatformThread::Sleep(TimeDelta duration) { - SbThreadSleep(duration.InMicroseconds()); + usleep(duration.InMicroseconds()); } // static diff --git a/cobalt/dom/serialized_algorithm_runner.h b/cobalt/dom/serialized_algorithm_runner.h index 2ee39c983510..c688b00aae2d 100644 --- a/cobalt/dom/serialized_algorithm_runner.h +++ b/cobalt/dom/serialized_algorithm_runner.h @@ -15,6 +15,8 @@ #ifndef COBALT_DOM_SERIALIZED_ALGORITHM_RUNNER_H_ #define COBALT_DOM_SERIALIZED_ALGORITHM_RUNNER_H_ +#include + #include #include #include @@ -101,7 +103,7 @@ class SerializedAlgorithmRunner { if (mutex_.AcquireTry()) { break; } - SbThreadSleep(wait_interval_usec); + usleep(static_cast(wait_interval_usec)); // Double the wait interval upon every failure, but cap it at // kMaxWaitIntervalUsec. wait_interval_usec = diff --git a/cobalt/media/base/cval_stats_test.cc b/cobalt/media/base/cval_stats_test.cc index e4de4c00b11e..6af6468296ef 100644 --- a/cobalt/media/base/cval_stats_test.cc +++ b/cobalt/media/base/cval_stats_test.cc @@ -14,6 +14,8 @@ #include "cobalt/media/base/cval_stats.h" +#include + #include #include @@ -58,7 +60,7 @@ TEST(MediaCValStatsTest, NothingRecorded) { CValStats cval_stats_; cval_stats_.StartTimer(MediaTiming::SbPlayerCreate, kPipelineIdentifier); - SbThreadSleep(kSleepTime.InMicroseconds()); + usleep(kSleepTime.InMicroseconds()); cval_stats_.StopTimer(MediaTiming::SbPlayerCreate, kPipelineIdentifier); @@ -75,7 +77,7 @@ TEST(MediaCValStatsTest, EnableRecording) { cval_stats_.Enable(true); cval_stats_.StartTimer(MediaTiming::SbPlayerCreate, kPipelineIdentifier); - SbThreadSleep(kSleepTime.InMicroseconds()); + usleep(kSleepTime.InMicroseconds()); cval_stats_.StopTimer(MediaTiming::SbPlayerCreate, kPipelineIdentifier); base::Optional result = cvm->GetValueAsString(kCValnameLatest); @@ -100,7 +102,7 @@ TEST(MediaCValStatsTest, DontGenerateHistoricalData) { for (int i = 0; i < kMediaDefaultMaxSamplesBeforeCalculation - 1; i++) { cval_stats_.StartTimer(MediaTiming::SbPlayerCreate, kPipelineIdentifier); - SbThreadSleep(kSleepTime.InMicroseconds()); + usleep(kSleepTime.InMicroseconds()); cval_stats_.StopTimer(MediaTiming::SbPlayerCreate, kPipelineIdentifier); } @@ -126,7 +128,7 @@ TEST(MediaCValStatsTest, GenerateHistoricalData) { for (int i = 0; i < kMediaDefaultMaxSamplesBeforeCalculation; i++) { cval_stats_.StartTimer(MediaTiming::SbPlayerCreate, kPipelineIdentifier); - SbThreadSleep(kSleepTime.InMicroseconds()); + usleep(kSleepTime.InMicroseconds()); cval_stats_.StopTimer(MediaTiming::SbPlayerCreate, kPipelineIdentifier); } diff --git a/cobalt/media_session/media_session_test.cc b/cobalt/media_session/media_session_test.cc index 262f4b3d3eff..ba3f56d9730f 100644 --- a/cobalt/media_session/media_session_test.cc +++ b/cobalt/media_session/media_session_test.cc @@ -14,6 +14,8 @@ #include "cobalt/media_session/media_session.h" +#include + #include #include @@ -74,7 +76,7 @@ class MockMediaSessionClient : public MediaSessionClient { if (current_change_count != session_change_count_) { break; } - SbThreadSleep(1 * base::Time::kMicrosecondsPerMillisecond); + usleep(1 * base::Time::kMicrosecondsPerMillisecond); } } MediaSessionState GetMediaSessionState() const { return session_state_; } diff --git a/cobalt/trace_event/sample_benchmark.cc b/cobalt/trace_event/sample_benchmark.cc index be7064c2f4ba..8f50f691b074 100644 --- a/cobalt/trace_event/sample_benchmark.cc +++ b/cobalt/trace_event/sample_benchmark.cc @@ -12,15 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include + #include "base/threading/thread.h" #include "build/build_config.h" #include "cobalt/trace_event/benchmark.h" -#if defined(STARBOARD) -#include "starboard/thread.h" -#define usleep(usec) SbThreadSleep(usec) -#endif - // A sample simple benchmark that tracks only a single event, in this case, // "LoopIteration". TRACE_EVENT_BENCHMARK1(SampleTestBenchmarkWithOneTrackedEvent, "LoopIteration", diff --git a/cobalt/updater/noop_sandbox.cc b/cobalt/updater/noop_sandbox.cc index 9b08a5ed4271..3b4be9202339 100644 --- a/cobalt/updater/noop_sandbox.cc +++ b/cobalt/updater/noop_sandbox.cc @@ -14,6 +14,8 @@ // This is a test app for Evergreen that does nothing. +#include + #include "base/time/time.h" #include "starboard/event.h" #include "starboard/system.h" @@ -21,6 +23,6 @@ void SbEventHandle(const SbEvent* event) { // No-op app. Exit after 1s. - SbThreadSleep(1 * base::Time::kMicrosecondsPerSecond); + usleep(1 * base::Time::kMicrosecondsPerSecond); SbSystemRequestStop(0); } diff --git a/cobalt/watchdog/watchdog.cc b/cobalt/watchdog/watchdog.cc index 7c58c6670eb0..f2c72b84a43a 100644 --- a/cobalt/watchdog/watchdog.cc +++ b/cobalt/watchdog/watchdog.cc @@ -14,6 +14,8 @@ #include "cobalt/watchdog/watchdog.h" +#include + #include #include #include @@ -823,7 +825,7 @@ void Watchdog::MaybeInjectDebugDelay(const std::string& name) { if (starboard::CurrentMonotonicTime() > time_last_delayed_microseconds_ + delay_wait_time_microseconds_) { - SbThreadSleep(delay_sleep_time_microseconds_); + usleep(delay_sleep_time_microseconds_); time_last_delayed_microseconds_ = starboard::CurrentMonotonicTime(); } } diff --git a/cobalt/watchdog/watchdog_test.cc b/cobalt/watchdog/watchdog_test.cc index 024be9ee4dd6..ee14ee6f7ff8 100644 --- a/cobalt/watchdog/watchdog_test.cc +++ b/cobalt/watchdog/watchdog_test.cc @@ -14,6 +14,8 @@ #include "cobalt/watchdog/watchdog.h" +#include + #include #include #include @@ -220,7 +222,7 @@ TEST_F(WatchdogTest, ViolationsJsonShouldPersistAndBeValid) { base::kApplicationStateStarted, kWatchdogMonitorFrequency)); ASSERT_TRUE(watchdog_->Ping("test-name", "test-ping")); - SbThreadSleep(kWatchdogSleepDuration); + usleep(kWatchdogSleepDuration); ASSERT_TRUE(watchdog_->Unregister("test-name")); TearDown(); watchdog_ = new watchdog::Watchdog(); @@ -297,7 +299,7 @@ TEST_F(WatchdogTest, RedundantViolationsShouldStack) { ASSERT_TRUE(watchdog_->Register("test-name", "test-desc", base::kApplicationStateStarted, kWatchdogMonitorFrequency)); - SbThreadSleep(kWatchdogSleepDuration); + usleep(kWatchdogSleepDuration); std::string json = watchdog_->GetWatchdogViolations({}, false); ASSERT_NE(json, ""); absl::optional uncleared_violations_map_optional = @@ -313,7 +315,7 @@ TEST_F(WatchdogTest, RedundantViolationsShouldStack) { *(*violations)[0].GetDict().FindString("timestampLastPingedMilliseconds"); int64_t uncleared_duration = std::stoll( *(*violations)[0].GetDict().FindString("violationDurationMilliseconds")); - SbThreadSleep(kWatchdogSleepDuration); + usleep(kWatchdogSleepDuration); json = watchdog_->GetWatchdogViolations({}, false); ASSERT_NE(json, ""); absl::optional violations_map_optional = @@ -337,7 +339,7 @@ TEST_F(WatchdogTest, ViolationsShouldResetAfterFetch) { ASSERT_TRUE(watchdog_->Register("test-name-1", "test-desc-1", base::kApplicationStateStarted, kWatchdogMonitorFrequency)); - SbThreadSleep(kWatchdogSleepDuration); + usleep(kWatchdogSleepDuration); ASSERT_TRUE(watchdog_->Unregister("test-name-1")); std::string json = watchdog_->GetWatchdogViolations(); ASSERT_NE(json.find("test-name-1"), std::string::npos); @@ -346,7 +348,7 @@ TEST_F(WatchdogTest, ViolationsShouldResetAfterFetch) { "test-name-2", "test-desc-2", base::kApplicationStateStarted, kWatchdogMonitorFrequency); ASSERT_NE(client, nullptr); - SbThreadSleep(kWatchdogSleepDuration); + usleep(kWatchdogSleepDuration); ASSERT_TRUE(watchdog_->UnregisterByClient(client)); json = watchdog_->GetWatchdogViolations(); ASSERT_EQ(json.find("test-name-1"), std::string::npos); @@ -361,7 +363,7 @@ TEST_F(WatchdogTest, PingInfosAreEvictedAfterMax) { for (int i = 0; i < 61; i++) { ASSERT_TRUE(watchdog_->Ping("test-name", std::to_string(i))); } - SbThreadSleep(kWatchdogSleepDuration); + usleep(kWatchdogSleepDuration); std::string json = watchdog_->GetWatchdogViolations(); ASSERT_NE(json, ""); absl::optional violations_map_optional = @@ -401,7 +403,7 @@ TEST_F(WatchdogTest, ViolationsAreEvictedAfterMax) { ASSERT_TRUE(watchdog_->Register("test-name-4", "test-desc-4", base::kApplicationStateStarted, kWatchdogMonitorFrequency)); - SbThreadSleep(kWatchdogSleepDuration); + usleep(kWatchdogSleepDuration); json = watchdog_->GetWatchdogViolations({}, false); ASSERT_NE(json, ""); @@ -429,7 +431,7 @@ TEST_F(WatchdogTest, ViolationsAreEvictedAfterMax) { ASSERT_EQ(violations->size(), 1); ASSERT_TRUE(watchdog_->Ping("test-name-3")); - SbThreadSleep(kWatchdogSleepDuration); + usleep(kWatchdogSleepDuration); json = watchdog_->GetWatchdogViolations(); ASSERT_NE(json, ""); @@ -464,7 +466,7 @@ TEST_F(WatchdogTest, UpdateStateShouldPreventViolations) { base::kApplicationStateStarted, kWatchdogMonitorFrequency)); watchdog_->UpdateState(base::kApplicationStateBlurred); - SbThreadSleep(kWatchdogSleepDuration); + usleep(kWatchdogSleepDuration); ASSERT_EQ(watchdog_->GetWatchdogViolations(), ""); ASSERT_TRUE(watchdog_->Unregister("test-name")); } @@ -474,7 +476,7 @@ TEST_F(WatchdogTest, TimeWaitShouldPreventViolations) { "test-name", "test-desc", base::kApplicationStateStarted, kWatchdogMonitorFrequency, kWatchdogSleepDuration + kWatchdogMonitorFrequency)); - SbThreadSleep(kWatchdogSleepDuration); + usleep(kWatchdogSleepDuration); ASSERT_EQ(watchdog_->GetWatchdogViolations(), ""); ASSERT_TRUE(watchdog_->Unregister("test-name")); } @@ -483,25 +485,25 @@ TEST_F(WatchdogTest, PingShouldPreventViolations) { ASSERT_TRUE(watchdog_->Register("test-name", "test-desc", base::kApplicationStateStarted, kWatchdogMonitorFrequency)); - SbThreadSleep(kWatchdogMonitorFrequency / 2); + usleep(kWatchdogMonitorFrequency / 2); ASSERT_TRUE(watchdog_->Ping("test-name")); - SbThreadSleep(kWatchdogMonitorFrequency / 2); + usleep(kWatchdogMonitorFrequency / 2); ASSERT_TRUE(watchdog_->Ping("test-name")); ASSERT_EQ(watchdog_->GetWatchdogViolations(), ""); - SbThreadSleep(kWatchdogMonitorFrequency / 2); + usleep(kWatchdogMonitorFrequency / 2); ASSERT_TRUE(watchdog_->Register("test-name", "test-desc", base::kApplicationStateStarted, kWatchdogMonitorFrequency, 0, PING)); - SbThreadSleep(kWatchdogMonitorFrequency / 2); + usleep(kWatchdogMonitorFrequency / 2); ASSERT_TRUE(watchdog_->Register("test-name", "test-desc", base::kApplicationStateStarted, kWatchdogMonitorFrequency, 0, PING)); ASSERT_EQ(watchdog_->GetWatchdogViolations(), ""); - SbThreadSleep(kWatchdogMonitorFrequency / 2); + usleep(kWatchdogMonitorFrequency / 2); ASSERT_TRUE(watchdog_->Register("test-name", "test-desc", base::kApplicationStateStarted, kWatchdogMonitorFrequency, 0, ALL)); - SbThreadSleep(kWatchdogMonitorFrequency / 2); + usleep(kWatchdogMonitorFrequency / 2); ASSERT_TRUE(watchdog_->Register("test-name", "test-desc", base::kApplicationStateStarted, kWatchdogMonitorFrequency, 0, ALL)); @@ -513,12 +515,12 @@ TEST_F(WatchdogTest, PingByClientShouldPreventViolations) { std::shared_ptr client = watchdog_->RegisterByClient( "test-name", "test-desc", base::kApplicationStateStarted, kWatchdogMonitorFrequency); - SbThreadSleep(kWatchdogMonitorFrequency / 2); + usleep(kWatchdogMonitorFrequency / 2); ASSERT_TRUE(watchdog_->PingByClient(client)); - SbThreadSleep(kWatchdogMonitorFrequency / 2); + usleep(kWatchdogMonitorFrequency / 2); ASSERT_TRUE(watchdog_->PingByClient(client)); ASSERT_EQ(watchdog_->GetWatchdogViolations(), ""); - SbThreadSleep(kWatchdogSleepDuration); + usleep(kWatchdogSleepDuration); ASSERT_NE(watchdog_->GetWatchdogViolations(), ""); ASSERT_TRUE(watchdog_->UnregisterByClient(client)); } @@ -528,7 +530,7 @@ TEST_F(WatchdogTest, UnregisterShouldPreventViolations) { base::kApplicationStateStarted, kWatchdogMonitorFrequency)); ASSERT_TRUE(watchdog_->Unregister("test-name")); - SbThreadSleep(kWatchdogSleepDuration); + usleep(kWatchdogSleepDuration); ASSERT_EQ(watchdog_->GetWatchdogViolations(), ""); } @@ -537,7 +539,7 @@ TEST_F(WatchdogTest, UnregisterByClientShouldPreventViolations) { "test-name", "test-desc", base::kApplicationStateStarted, kWatchdogMonitorFrequency); ASSERT_TRUE(watchdog_->UnregisterByClient(client)); - SbThreadSleep(kWatchdogSleepDuration); + usleep(kWatchdogSleepDuration); ASSERT_EQ(watchdog_->GetWatchdogViolations(), ""); } @@ -548,7 +550,7 @@ TEST_F(WatchdogTest, KillSwitchShouldPreventViolations) { ASSERT_TRUE(watchdog_->Register("test-name", "test-desc", base::kApplicationStateStarted, kWatchdogMonitorFrequency)); - SbThreadSleep(kWatchdogSleepDuration); + usleep(kWatchdogSleepDuration); ASSERT_EQ(watchdog_->GetWatchdogViolations(), ""); ASSERT_TRUE(watchdog_->Unregister("test-name")); } @@ -557,7 +559,7 @@ TEST_F(WatchdogTest, FrequentConsecutiveViolationsShouldNotWrite) { ASSERT_TRUE(watchdog_->Register("test-name", "test-desc", base::kApplicationStateStarted, kWatchdogMonitorFrequency)); - SbThreadSleep(kWatchdogSleepDuration); + usleep(kWatchdogSleepDuration); std::string write_json = ""; starboard::ScopedFile read_file(watchdog_->GetWatchdogFilePath().c_str(), kSbFileOpenOnly | kSbFileRead); @@ -569,7 +571,7 @@ TEST_F(WatchdogTest, FrequentConsecutiveViolationsShouldNotWrite) { } ASSERT_NE(write_json, ""); ASSERT_TRUE(watchdog_->Ping("test-name")); - SbThreadSleep(kWatchdogSleepDuration); + usleep(kWatchdogSleepDuration); ASSERT_TRUE(watchdog_->Unregister("test-name")); std::string no_write_json = ""; starboard::ScopedFile read_file_again( @@ -593,7 +595,7 @@ TEST_F(WatchdogTest, GetViolationClientNames) { ASSERT_TRUE(watchdog_->Register("test-name-2", "test-desc-2", base::kApplicationStateStarted, kWatchdogMonitorFrequency)); - SbThreadSleep(kWatchdogSleepDuration); + usleep(kWatchdogSleepDuration); ASSERT_TRUE(watchdog_->Unregister("test-name-1")); ASSERT_TRUE(watchdog_->Unregister("test-name-2")); @@ -615,7 +617,7 @@ TEST_F(WatchdogTest, GetPartialViolationsByClients) { ASSERT_TRUE(watchdog_->Register("test-name-2", "test-desc-2", base::kApplicationStateStarted, kWatchdogMonitorFrequency)); - SbThreadSleep(kWatchdogSleepDuration); + usleep(kWatchdogSleepDuration); ASSERT_TRUE(watchdog_->Unregister("test-name-1")); ASSERT_TRUE(watchdog_->Unregister("test-name-2")); @@ -682,7 +684,7 @@ TEST_F(WatchdogTest, ViolationContainsLogTrace) { watchdog_->LogEvent("2"); watchdog_->LogEvent("3"); - SbThreadSleep(kWatchdogSleepDuration); + usleep(kWatchdogSleepDuration); std::string json = watchdog_->GetWatchdogViolations(); absl::optional violations_map_optional = @@ -702,7 +704,7 @@ TEST_F(WatchdogTest, ViolationContainsEmptyLogTrace) { kWatchdogMonitorFrequency); watchdog_->Ping("test-name", "test-ping"); - SbThreadSleep(kWatchdogSleepDuration); + usleep(kWatchdogSleepDuration); std::string json = watchdog_->GetWatchdogViolations(); absl::optional violations_map_optional = diff --git a/starboard/CHANGELOG.md b/starboard/CHANGELOG.md index b51200fd901b..97a7da5aa35d 100644 --- a/starboard/CHANGELOG.md +++ b/starboard/CHANGELOG.md @@ -9,6 +9,10 @@ since the version previous to it. ## Version 16 +### Deprecated `SbThreadSleep` +Replaced the `SbThreadSleep` with the POSIX usleep() defined in the +`` header. + ### MAP_EXECUTABLE_MEMORY changed from build-time to runtime config SB_CAN_MAP_EXECUTABLE_MEMORY has been refactored into a run-time configuration constant `kSbCanMapExecutableMemory`. diff --git a/starboard/android/shared/audio_track_audio_sink_type.cc b/starboard/android/shared/audio_track_audio_sink_type.cc index e3781184ff0e..459c7b6a95d8 100644 --- a/starboard/android/shared/audio_track_audio_sink_type.cc +++ b/starboard/android/shared/audio_track_audio_sink_type.cc @@ -14,6 +14,7 @@ #include "starboard/android/shared/audio_track_audio_sink_type.h" +#include #include #include #include @@ -281,7 +282,7 @@ void AudioTrackAudioSink::AudioThreadFunc() { } if (!is_playing || frames_in_buffer == 0) { - SbThreadSleep(10'000); + usleep(10'000); continue; } @@ -322,7 +323,7 @@ void AudioTrackAudioSink::AudioThreadFunc() { sync_time); } - SbThreadSleep(10'000); + usleep(10'000); continue; } SB_DCHECK(expected_written_frames > 0); @@ -370,10 +371,10 @@ void AudioTrackAudioSink::AudioThreadFunc() { // be big enough to account for the unstable playback head reported at the // beginning of the playback and during underrun. if (playback_head_position > 0 && unplayed_frames_in_time > 500'000) { - SbThreadSleep(40'000); + usleep(40'000); } else if (!written_fully) { // Only sleep if the buffer is nearly full and the last write is partial. - SbThreadSleep(10'000); + usleep(10'000); } } diff --git a/starboard/android/shared/log.cc b/starboard/android/shared/log.cc index 9f2312cb1eaa..d15681f847ec 100644 --- a/starboard/android/shared/log.cc +++ b/starboard/android/shared/log.cc @@ -60,5 +60,5 @@ void SbLog(SbLogPriority priority, const char* message) { // In unit tests the logging is too fast for the android log to be read out // and we end up losing crucial logs. The test runner specifies a sleep time. - SbThreadSleep(::starboard::android::shared::GetLogSleepTime()); + usleep(::starboard::android::shared::GetLogSleepTime()); } diff --git a/starboard/android/shared/log_raw.cc b/starboard/android/shared/log_raw.cc index 989233b965fb..9e2a1c937c9a 100644 --- a/starboard/android/shared/log_raw.cc +++ b/starboard/android/shared/log_raw.cc @@ -13,6 +13,7 @@ // limitations under the License. #include +#include #include "starboard/android/shared/log_internal.h" #include "starboard/common/log.h" @@ -20,5 +21,5 @@ void SbLogRaw(const char* message) { __android_log_write(ANDROID_LOG_INFO, "starboard", message); - SbThreadSleep(::starboard::android::shared::GetLogSleepTime()); + usleep(::starboard::android::shared::GetLogSleepTime()); } diff --git a/starboard/common/thread.cc b/starboard/common/thread.cc index 9d1e24de1444..da6f212a4652 100644 --- a/starboard/common/thread.cc +++ b/starboard/common/thread.cc @@ -14,6 +14,8 @@ * limitations under the License. */ +#include + #include "starboard/common/thread.h" #include "starboard/common/atomic.h" @@ -65,7 +67,7 @@ void Thread::Start(const Options& options) { } void Thread::Sleep(int64_t microseconds) { - SbThreadSleep(microseconds); + usleep(microseconds); } void Thread::SleepMilliseconds(int value) { diff --git a/starboard/elf_loader/exported_symbols.cc b/starboard/elf_loader/exported_symbols.cc index 068683878b95..40e4ffb95988 100644 --- a/starboard/elf_loader/exported_symbols.cc +++ b/starboard/elf_loader/exported_symbols.cc @@ -392,8 +392,8 @@ ExportedSymbols::ExportedSymbols() { REGISTER_SYMBOL(SbThreadSamplerThaw); REGISTER_SYMBOL(SbThreadSetLocalValue); REGISTER_SYMBOL(SbThreadSetName); - REGISTER_SYMBOL(SbThreadSleep); #if SB_API_VERSION < 16 + REGISTER_SYMBOL(SbThreadSleep); REGISTER_SYMBOL(SbThreadYield); REGISTER_SYMBOL(SbTimeGetMonotonicNow); REGISTER_SYMBOL(SbTimeGetMonotonicThreadNow); @@ -470,6 +470,7 @@ ExportedSymbols::ExportedSymbols() { REGISTER_SYMBOL(vfwprintf); REGISTER_SYMBOL(vsnprintf); REGISTER_SYMBOL(vsscanf); + REGISTER_SYMBOL(usleep); // Custom mapped POSIX APIs to compatibility wrappers. // These will rely on Starboard-side implementations that properly translate diff --git a/starboard/nplb/BUILD.gn b/starboard/nplb/BUILD.gn index 3e4cb37b5410..04ceafbe07c1 100644 --- a/starboard/nplb/BUILD.gn +++ b/starboard/nplb/BUILD.gn @@ -179,6 +179,7 @@ target(gtest_target_type, "nplb") { "posix_compliance/posix_thread_is_equal_test.cc", "posix_compliance/posix_thread_join_test.cc", "posix_compliance/posix_thread_local_value_test.cc", + "posix_compliance/posix_thread_sleep_test.cc", "posix_compliance/posix_thread_yield_test.cc", "posix_compliance/posix_time_test.cc", "random_helpers.cc", diff --git a/starboard/nplb/atomic_test.cc b/starboard/nplb/atomic_test.cc index 563781dfff2b..64a3e095e0a7 100644 --- a/starboard/nplb/atomic_test.cc +++ b/starboard/nplb/atomic_test.cc @@ -15,6 +15,7 @@ // Adapted from base's atomicops_unittest. #include +#include #include "starboard/common/atomic.h" #include "starboard/memory.h" @@ -341,7 +342,7 @@ template void* TestOnceEntryPoint(void* raw_context) { // Force every thread to sleep immediately so the first thread doesn't always // just win. - SbThreadSleep(1000); + usleep(1000); TestOnceContext* context = reinterpret_cast*>(raw_context); SetData(context->state, context->out_data, context->data, context->size); diff --git a/starboard/nplb/audio_sink_test.cc b/starboard/nplb/audio_sink_test.cc index 0d3b164d62f1..4dc0c766f2f8 100644 --- a/starboard/nplb/audio_sink_test.cc +++ b/starboard/nplb/audio_sink_test.cc @@ -14,6 +14,8 @@ #include "starboard/audio_sink.h" +#include + #include #include "starboard/nplb/audio_sink_helpers.h" @@ -99,7 +101,7 @@ TEST(SbAudioSinkTest, Underflow) { environment.AppendFrame(frames_to_append); EXPECT_TRUE(environment.WaitUntilSomeFramesAreConsumed()); - SbThreadSleep(250'000); + usleep(250'000); ASSERT_GT(environment.GetFrameBufferFreeSpaceInFrames(), 0); environment.AppendFrame(environment.GetFrameBufferFreeSpaceInFrames()); EXPECT_TRUE(environment.WaitUntilAllFramesAreConsumed()); diff --git a/starboard/nplb/media_set_audio_write_duration_test.cc b/starboard/nplb/media_set_audio_write_duration_test.cc index c03077a357a0..004f3df61ad6 100644 --- a/starboard/nplb/media_set_audio_write_duration_test.cc +++ b/starboard/nplb/media_set_audio_write_duration_test.cc @@ -14,6 +14,8 @@ #include "starboard/media.h" +#include + #include "starboard/common/optional.h" #include "starboard/common/spin_lock.h" #include "starboard/common/time.h" @@ -133,7 +135,7 @@ class SbMediaSetAudioWriteDurationTest const int64_t kMaxWaitTime = 3'000'000LL; // 3 seconds while (player_state_ != desired_state && (CurrentMonotonicTime() - start_of_wait) < kMaxWaitTime) { - SbThreadSleep(kSmallWaitInterval); + usleep(kSmallWaitInterval); TryToWritePendingSample(); } ASSERT_EQ(desired_state, player_state_); @@ -210,7 +212,7 @@ TEST_P(SbMediaSetAudioWriteDurationTest, WriteLimitedInput) { #endif // SB_API_VERSION >= 15 while (CurrentMonotonicTime() - start_of_wait < kMaxWaitTime && info.current_media_timestamp == 0) { - SbThreadSleep(500'000); + usleep(500'000); #if SB_API_VERSION >= 15 SbPlayerGetInfo(player, &info); #else // SB_API_VERSION >= 15 @@ -264,7 +266,7 @@ TEST_P(SbMediaSetAudioWriteDurationTest, WriteContinuedLimitedInput) { #else // SB_API_VERSION >= 15 SbPlayerGetInfo2(player, &info); #endif // SB_API_VERSION >= 15 - SbThreadSleep(kSmallWaitInterval); + usleep(kSmallWaitInterval); TryToWritePendingSample(); } EXPECT_GE(info.current_media_timestamp, min_ending_playback_time); diff --git a/starboard/nplb/microphone_read_test.cc b/starboard/nplb/microphone_read_test.cc index a33cc3e05e24..6fcd8860d8ce 100644 --- a/starboard/nplb/microphone_read_test.cc +++ b/starboard/nplb/microphone_read_test.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include + #include "starboard/microphone.h" #include "starboard/nplb/microphone_helpers.h" #include "starboard/thread.h" @@ -88,7 +90,7 @@ TEST(SbMicrophoneReadTest, SunnyDayOpenSleepCloseAndOpenRead) { EXPECT_TRUE(SbMicrophoneOpen(microphone)); - SbThreadSleep(50'000); + usleep(50'000); EXPECT_TRUE(SbMicrophoneClose(microphone)); EXPECT_TRUE(SbMicrophoneOpen(microphone)); diff --git a/starboard/nplb/posix_compliance/posix_condition_variable_wait_test.cc b/starboard/nplb/posix_compliance/posix_condition_variable_wait_test.cc index 33d177795566..fb27b0c9dc13 100644 --- a/starboard/nplb/posix_compliance/posix_condition_variable_wait_test.cc +++ b/starboard/nplb/posix_compliance/posix_condition_variable_wait_test.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include + #include "starboard/configuration_constants.h" #include "starboard/nplb/posix_compliance/posix_thread_helpers.h" #include "starboard/thread.h" @@ -29,7 +31,7 @@ void* PosixTakeThenSignalEntryPoint(void* context) { test_context->do_signal.Take(); if (test_context->delay_after_signal > 0) { - SbThreadSleep(test_context->delay_after_signal); + usleep(test_context->delay_after_signal); } // Signal the condition variable. diff --git a/starboard/nplb/posix_compliance/posix_socket_send_test.cc b/starboard/nplb/posix_compliance/posix_socket_send_test.cc index a62c55c671ff..360c368584cc 100644 --- a/starboard/nplb/posix_compliance/posix_socket_send_test.cc +++ b/starboard/nplb/posix_compliance/posix_socket_send_test.cc @@ -16,6 +16,7 @@ // this is hooked up to something. #include +#include #include "starboard/nplb/posix_compliance/posix_socket_helpers.h" #include "starboard/thread.h" @@ -184,7 +185,7 @@ TEST(PosixSocketSendTest, RainyDaySendToSocketConnectionReset) { int kNumRetries = 1000; for (int i = 0; i < kNumRetries; ++i) { char buff[kChunkSize] = {}; - SbThreadSleep(1000); + usleep(1000); result = send(client_socket_fd, buff, sizeof(buff), kSendFlags); if (result < 0) { diff --git a/starboard/nplb/posix_compliance/posix_socket_sendto_test.cc b/starboard/nplb/posix_compliance/posix_socket_sendto_test.cc index 090f2bb6a0f9..81774a79294b 100644 --- a/starboard/nplb/posix_compliance/posix_socket_sendto_test.cc +++ b/starboard/nplb/posix_compliance/posix_socket_sendto_test.cc @@ -16,6 +16,8 @@ // this is hooked up to something. #include +#include + #include "starboard/nplb/posix_compliance/posix_socket_helpers.h" #include "starboard/thread.h" @@ -186,7 +188,7 @@ TEST(PosixSocketSendtoTest, RainyDaySendToSocketConnectionReset) { int kNumRetries = 1000; for (int i = 0; i < kNumRetries; ++i) { char buff[kChunkSize] = {}; - SbThreadSleep(1000); + usleep(1000); result = sendto(client_socket_fd, buff, sizeof(buff), kSendFlags, NULL, 0); if (result < 0) { diff --git a/starboard/nplb/posix_compliance/posix_thread_helpers.cc b/starboard/nplb/posix_compliance/posix_thread_helpers.cc index 28bcbb319d2d..451be69d1eb7 100644 --- a/starboard/nplb/posix_compliance/posix_thread_helpers.cc +++ b/starboard/nplb/posix_compliance/posix_thread_helpers.cc @@ -14,6 +14,8 @@ #include "starboard/nplb/posix_compliance/posix_thread_helpers.h" +#include + #include "starboard/common/condition_variable.h" #include "starboard/common/mutex.h" #include "starboard/thread.h" @@ -47,7 +49,7 @@ void* TakeThenSignalEntryPoint(void* context) { test_context->do_signal.Take(); if (test_context->delay_after_signal > 0) { - SbThreadSleep(test_context->delay_after_signal); + usleep(test_context->delay_after_signal); } // Signal the condition variable. diff --git a/starboard/nplb/posix_compliance/posix_thread_join_test.cc b/starboard/nplb/posix_compliance/posix_thread_join_test.cc index 2e021e7ca46b..4b76beff54b0 100644 --- a/starboard/nplb/posix_compliance/posix_thread_join_test.cc +++ b/starboard/nplb/posix_compliance/posix_thread_join_test.cc @@ -15,6 +15,7 @@ // Thread joining is mostly tested in the other tests. #include +#include #include "testing/gtest/include/gtest/gtest.h" @@ -27,7 +28,7 @@ void* ThreadEntryPoint(void* input) { static const int64_t kSleepTime = 10'000; // 10 ms. // Wait to write the value to increase likelihood of catching // a race condition. - SbThreadSleep(kSleepTime); + usleep(kSleepTime); (*value)++; return NULL; } diff --git a/starboard/nplb/posix_compliance/posix_thread_sleep_test.cc b/starboard/nplb/posix_compliance/posix_thread_sleep_test.cc new file mode 100644 index 000000000000..069474ba7790 --- /dev/null +++ b/starboard/nplb/posix_compliance/posix_thread_sleep_test.cc @@ -0,0 +1,48 @@ +// Copyright 2024 The Cobalt Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include "starboard/common/time.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace starboard { +namespace nplb { +namespace { + +// Allow millisecond-level precision. +const int64_t kPrecision = 1000; // 1ms + +TEST(PosixThreadSleepTest, SunnyDay) { + usleep(0); + // Well, my work here is done. +} + +TEST(PosixThreadSleepTest, SunnyDayAtLeastDelay) { + const int kTrials = 12; + const int64_t one = 1; + for (int trial = 0; trial < kTrials; ++trial) { + // This tests several delays, between about 15 to about 4 milliseconds. + const int64_t kDelay = 1'000'000LL / (one << ((trial % 3) + 6)); + int64_t start = CurrentMonotonicTime(); + usleep(kDelay); + int64_t end = CurrentMonotonicTime(); + EXPECT_LE(start + kDelay, end + kPrecision) + << "Trial " << trial << ", kDelay=" << kDelay; + } +} + +} // namespace +} // namespace nplb +} // namespace starboard diff --git a/starboard/nplb/rwlock_test.cc b/starboard/nplb/rwlock_test.cc index 9ec49902fde4..a7714f1ada24 100644 --- a/starboard/nplb/rwlock_test.cc +++ b/starboard/nplb/rwlock_test.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include + #include "starboard/common/rwlock.h" #include "starboard/common/time.h" #include "starboard/configuration.h" @@ -112,7 +114,7 @@ class ThreadHoldsWriteLockForTime : public AbstractTestThread { void Run() override { ScopedWriteLock write_lock(&shared_data_->rw_lock); shared_data_->signal_write_lock.Put(); - SbThreadSleep(shared_data_->time_to_hold); + usleep(shared_data_->time_to_hold); } private: diff --git a/starboard/nplb/semaphore_test.cc b/starboard/nplb/semaphore_test.cc index 16a618e9bf37..97379b4a82c6 100644 --- a/starboard/nplb/semaphore_test.cc +++ b/starboard/nplb/semaphore_test.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include + #include "starboard/common/semaphore.h" #include "starboard/common/time.h" #include "starboard/nplb/thread_helpers.h" @@ -96,10 +98,10 @@ TEST(Semaphore, FLAKY_ThreadTakesWait_PutBeforeTimeExpires) { // Create thread and wait for it to start executing. thread.Start(); while (!thread.thread_started_) { - SbThreadSleep(1000); + usleep(1000); } - SbThreadSleep(wait_time); + usleep(wait_time); thread.semaphore_.Put(); thread.Join(); @@ -129,14 +131,14 @@ TEST(Semaphore, ThreadTakesWait_TimeExpires) { // Create thread and wait for it to start executing. thread.Start(); while (!thread.thread_started_) { - SbThreadSleep(1000); + usleep(1000); } // It is possible for the thread to be preempted just before processing // Semaphore::TakeWait, so sleep for an extra amount of time to avoid the // semaphore being legitimately signalled during the wait time (because // the thread started TakeWait late). - SbThreadSleep(wait_time * 5); + usleep(wait_time * 5); thread.semaphore_.Put(); thread.Join(); diff --git a/starboard/nplb/socket_join_multicast_group_test.cc b/starboard/nplb/socket_join_multicast_group_test.cc index 7a2a64653f87..ca311dea00ef 100644 --- a/starboard/nplb/socket_join_multicast_group_test.cc +++ b/starboard/nplb/socket_join_multicast_group_test.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include + #include "starboard/common/socket.h" #include "starboard/common/time.h" #include "starboard/nplb/socket_helpers.h" @@ -92,7 +94,7 @@ TEST(SbSocketJoinMulticastGroupTest, SunnyDay) { receive_socket, buf, SB_ARRAY_SIZE_INT(buf), &receive_address); if (received < 0 && SbSocketGetLastError(receive_socket) == kSbSocketPending) { - SbThreadSleep(1000); + usleep(1000); continue; } EXPECT_EQ(SB_ARRAY_SIZE_INT(kBuf), received); diff --git a/starboard/nplb/socket_send_to_test.cc b/starboard/nplb/socket_send_to_test.cc index 7d9ac32d081c..ba34fbc21cbb 100644 --- a/starboard/nplb/socket_send_to_test.cc +++ b/starboard/nplb/socket_send_to_test.cc @@ -15,6 +15,8 @@ // SendTo is largely tested with ReceiveFrom, so look there for more involved // tests. +#include + #include #include "starboard/common/socket.h" @@ -169,7 +171,7 @@ TEST_P(PairSbSocketSendToTest, RainyDaySendToSocketConnectionReset) { int kNumRetries = 1000; for (int i = 0; i < kNumRetries; ++i) { char buff[kChunkSize] = {}; - SbThreadSleep(1000); + usleep(1000); int result = trio->client_socket->SendTo(buff, sizeof(buff), NULL); if (result < 0) { diff --git a/starboard/nplb/socket_waiter_wake_up_test.cc b/starboard/nplb/socket_waiter_wake_up_test.cc index 4295ff645d25..520429c10da4 100644 --- a/starboard/nplb/socket_waiter_wake_up_test.cc +++ b/starboard/nplb/socket_waiter_wake_up_test.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include + #include "starboard/common/time.h" #include "starboard/nplb/socket_helpers.h" #include "starboard/nplb/thread_helpers.h" @@ -41,7 +43,7 @@ void* WakeUpSleepEntryPoint(void* context) { WakeUpContext* wake_up_context = reinterpret_cast(context); SbSocketWaiter waiter = wake_up_context->waiter; wake_up_context->semaphore.Take(); - SbThreadSleep(kSocketTimeout); + usleep(kSocketTimeout); SbSocketWaiterWakeUp(waiter); return NULL; } diff --git a/starboard/nplb/thread_helpers.cc b/starboard/nplb/thread_helpers.cc index 1f160042f2ac..283eaf77d167 100644 --- a/starboard/nplb/thread_helpers.cc +++ b/starboard/nplb/thread_helpers.cc @@ -12,11 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include + #include "starboard/nplb/thread_helpers.h" #include "starboard/common/condition_variable.h" #include "starboard/common/mutex.h" -#include "starboard/thread.h" #include "starboard/types.h" #include "testing/gtest/include/gtest/gtest.h" @@ -46,7 +47,7 @@ void* TakeThenSignalEntryPoint(void* context) { test_context->do_signal.Take(); if (test_context->delay_after_signal > 0) { - SbThreadSleep(test_context->delay_after_signal); + usleep(test_context->delay_after_signal); } // Signal the condition variable. diff --git a/starboard/nplb/thread_join_test.cc b/starboard/nplb/thread_join_test.cc index eeb63a0c24c9..341aba335471 100644 --- a/starboard/nplb/thread_join_test.cc +++ b/starboard/nplb/thread_join_test.cc @@ -14,6 +14,8 @@ // Thread joining is mostly tested in the other tests. +#include + #include "starboard/thread.h" #include "testing/gtest/include/gtest/gtest.h" @@ -41,7 +43,7 @@ TEST(SbThreadLocalValueTest, ThreadJoinWaitsForFunctionRun) { static const int64_t kSleepTime = 10'000; // 10 ms. // Wait to write the value to increase likelihood of catching // a race condition. - SbThreadSleep(kSleepTime); + usleep(kSleepTime); (*value)++; return NULL; } diff --git a/starboard/nplb/thread_sampler_test.cc b/starboard/nplb/thread_sampler_test.cc index 65a7d29d887c..4f105fd6871e 100644 --- a/starboard/nplb/thread_sampler_test.cc +++ b/starboard/nplb/thread_sampler_test.cc @@ -13,6 +13,7 @@ // limitations under the License. #include +#include #include "starboard/common/atomic.h" #include "starboard/common/log.h" @@ -32,7 +33,7 @@ class CountingThread : public AbstractTestThread { void Run() override { while (!stop_.load()) { counter_.increment(); - SbThreadSleep(1000); + usleep(1000); } } diff --git a/starboard/nplb/thread_sleep_test.cc b/starboard/nplb/thread_sleep_test.cc index cdbd906a0bd9..1a71668be7ee 100644 --- a/starboard/nplb/thread_sleep_test.cc +++ b/starboard/nplb/thread_sleep_test.cc @@ -16,6 +16,8 @@ #include "starboard/thread.h" #include "testing/gtest/include/gtest/gtest.h" +#if SB_API_VERSION < 16 + namespace starboard { namespace nplb { namespace { @@ -57,3 +59,5 @@ TEST(SbThreadSleepTest, RainyDayNegativeDuration) { } // namespace } // namespace nplb } // namespace starboard + +#endif // SB_API_VERSION < 15 diff --git a/starboard/nplb/thread_test.cc b/starboard/nplb/thread_test.cc index 7a45baa1c0f4..b2cb4e79b9e9 100644 --- a/starboard/nplb/thread_test.cc +++ b/starboard/nplb/thread_test.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include + #include #include "starboard/common/atomic.h" @@ -40,7 +42,7 @@ class TestRunThread : public Thread { // Start(), and will exit on Join(). TEST(Thread, TestRunThread) { TestRunThread test_thread; - SbThreadSleep(100); + usleep(100); // Expect that test thread not in a run state when initialized. EXPECT_FALSE(test_thread.finished_.load()); EXPECT_FALSE(test_thread.join_called()); diff --git a/starboard/raspi/shared/open_max/open_max_image_decode_component.cc b/starboard/raspi/shared/open_max/open_max_image_decode_component.cc index a2c1fe0cca96..e143730a512a 100644 --- a/starboard/raspi/shared/open_max/open_max_image_decode_component.cc +++ b/starboard/raspi/shared/open_max/open_max_image_decode_component.cc @@ -14,6 +14,8 @@ #include "starboard/raspi/shared/open_max/open_max_image_decode_component.h" +#include + #include #include "starboard/common/time.h" @@ -95,7 +97,7 @@ SbDecodeTarget OpenMaxImageDecodeComponent::Decode( break; } else if (write_size == 0 && output_size == 0) { // Wait for buffers to become available. - SbThreadSleep(1000); + usleep(1000); } } diff --git a/starboard/raspi/shared/open_max/video_decoder.cc b/starboard/raspi/shared/open_max/video_decoder.cc index 32e411cc02ac..98fcd50c7db5 100644 --- a/starboard/raspi/shared/open_max/video_decoder.cc +++ b/starboard/raspi/shared/open_max/video_decoder.cc @@ -14,6 +14,8 @@ #include "starboard/raspi/shared/open_max/video_decoder.h" +#include + namespace starboard { namespace raspi { namespace shared { @@ -77,7 +79,7 @@ void VideoDecoder::WriteInputBuffers(const InputBuffers& input_buffers) { const auto& input_buffer = input_buffers[0]; queue_.Put(new Event(input_buffer)); if (!TryToDeliverOneFrame()) { - SbThreadSleep(1000); + usleep(1000); // Call the callback with NULL frame to ensure that the host knows that // more data is expected. decoder_status_cb_(kNeedMoreInput, NULL); @@ -185,7 +187,7 @@ void VideoDecoder::RunLoop() { current_buffer = NULL; offset = 0; } else { - SbThreadSleep(1000); + usleep(1000); continue; } } diff --git a/starboard/shared/alsa/alsa_audio_sink_type.cc b/starboard/shared/alsa/alsa_audio_sink_type.cc index d0fe33af204c..794fa4fa99c1 100644 --- a/starboard/shared/alsa/alsa_audio_sink_type.cc +++ b/starboard/shared/alsa/alsa_audio_sink_type.cc @@ -16,6 +16,8 @@ #include +#include + #include #include @@ -281,7 +283,7 @@ bool AlsaAudioSink::IdleLoop() { AlsaWriteFrames(playback_handle_, silence_frames_, kFramesPerRequest); AlsaDrain(playback_handle_); } - SbThreadSleep(time_to_wait_); + usleep(time_to_wait_); } return false; @@ -324,7 +326,7 @@ bool AlsaAudioSink::PlaybackLoop() { WriteFrames(playback_rate, std::min(kFramesPerRequest, frames_in_buffer), frames_in_buffer, offset_in_frames); } else { - SbThreadSleep(time_to_wait_); + usleep(time_to_wait_); } } diff --git a/starboard/shared/pulse/pulse_audio_sink_type.cc b/starboard/shared/pulse/pulse_audio_sink_type.cc index 7e200cefa210..ecaf01a3b288 100644 --- a/starboard/shared/pulse/pulse_audio_sink_type.cc +++ b/starboard/shared/pulse/pulse_audio_sink_type.cc @@ -16,6 +16,8 @@ #include +#include + #include #include #include @@ -571,9 +573,9 @@ void PulseAudioSinkType::AudioThreadFunc() { pa_mainloop_iterate(mainloop_, 0, NULL); } if (has_running_sink) { - SbThreadSleep(kAudioRunningSleepIntervalUsec); + usleep(kAudioRunningSleepIntervalUsec); } else { - SbThreadSleep(kAudioIdleSleepIntervalUsec); + usleep(kAudioIdleSleepIntervalUsec); } } } diff --git a/starboard/shared/starboard/audio_sink/stub_audio_sink_type.cc b/starboard/shared/starboard/audio_sink/stub_audio_sink_type.cc index 1bf89cfcee24..96f14ee7eb1f 100644 --- a/starboard/shared/starboard/audio_sink/stub_audio_sink_type.cc +++ b/starboard/shared/starboard/audio_sink/stub_audio_sink_type.cc @@ -14,6 +14,8 @@ #include "starboard/shared/starboard/audio_sink/stub_audio_sink_type.h" +#include + #include #include "starboard/common/mutex.h" @@ -113,11 +115,11 @@ void StubAudioSink::AudioThreadFunc() { int frames_to_consume = std::min(kMaxFramesToConsumePerRequest, frames_in_buffer); - SbThreadSleep(frames_to_consume * 1'000'000LL / sampling_frequency_hz_); + usleep(frames_to_consume * 1'000'000LL / sampling_frequency_hz_); consume_frames_func_(frames_to_consume, CurrentMonotonicTime(), context_); } else { // Wait for five millisecond if we are paused. - SbThreadSleep(5'000); + usleep(5'000); } } } diff --git a/starboard/shared/starboard/net_args.cc b/starboard/shared/starboard/net_args.cc index 15f59837e918..afd1f2d4e542 100644 --- a/starboard/shared/starboard/net_args.cc +++ b/starboard/shared/starboard/net_args.cc @@ -14,6 +14,8 @@ #include "starboard/shared/starboard/net_args.h" +#include + #include #include #include @@ -90,7 +92,7 @@ std::unique_ptr WaitForClientConnection(Socket* listen_sock, if (CurrentMonotonicTime() > expire_time) { return std::unique_ptr(); } - SbThreadSleep(1000); + usleep(1000); } } diff --git a/starboard/shared/starboard/net_log.cc b/starboard/shared/starboard/net_log.cc index 8902fc4fdf3c..f4e65823d257 100644 --- a/starboard/shared/starboard/net_log.cc +++ b/starboard/shared/starboard/net_log.cc @@ -14,6 +14,7 @@ #include "starboard/shared/starboard/net_log.h" +#include #include #include @@ -447,7 +448,7 @@ void NetLogWaitForClientConnected(int64_t timeout) { if (CurrentMonotonicTime() > expire_time) { break; } - SbThreadSleep(1000); + usleep(1000); } } #endif diff --git a/starboard/shared/starboard/player/filter/punchout_video_renderer_sink.cc b/starboard/shared/starboard/player/filter/punchout_video_renderer_sink.cc index 85415fb65d60..b47de1a8b09a 100644 --- a/starboard/shared/starboard/player/filter/punchout_video_renderer_sink.cc +++ b/starboard/shared/starboard/player/filter/punchout_video_renderer_sink.cc @@ -14,6 +14,8 @@ #include "starboard/shared/starboard/player/filter/punchout_video_renderer_sink.h" +#include + #include "starboard/common/log.h" #include "starboard/configuration.h" #include "starboard/shared/starboard/application.h" @@ -75,7 +77,7 @@ void PunchoutVideoRendererSink::SetBounds(int z_index, void PunchoutVideoRendererSink::RunLoop() { while (!stop_requested_.load()) { render_cb_(std::bind(&PunchoutVideoRendererSink::DrawFrame, this, _1, _2)); - SbThreadSleep(render_interval_); + usleep(render_interval_); } ScopedLock lock(mutex_); shared::starboard::Application::Get()->HandleFrame( diff --git a/starboard/shared/starboard/player/filter/testing/adaptive_audio_decoder_test.cc b/starboard/shared/starboard/player/filter/testing/adaptive_audio_decoder_test.cc index c5d5cd842d43..48cd6c6b3aa9 100644 --- a/starboard/shared/starboard/player/filter/testing/adaptive_audio_decoder_test.cc +++ b/starboard/shared/starboard/player/filter/testing/adaptive_audio_decoder_test.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include + #include #include #include @@ -155,7 +157,7 @@ class AdaptiveAudioDecoderTest return; } } - SbThreadSleep(1000); + usleep(1000); } *event = kError; FAIL(); diff --git a/starboard/shared/starboard/player/filter/testing/audio_decoder_test.cc b/starboard/shared/starboard/player/filter/testing/audio_decoder_test.cc index 48ce9672c074..cf7fcb3e764b 100644 --- a/starboard/shared/starboard/player/filter/testing/audio_decoder_test.cc +++ b/starboard/shared/starboard/player/filter/testing/audio_decoder_test.cc @@ -14,6 +14,8 @@ #include "starboard/shared/starboard/player/filter/audio_decoder_internal.h" +#include + #include #include #include @@ -166,7 +168,7 @@ class AudioDecoderTest return; } } - SbThreadSleep(1000); + usleep(1000); } *event = kError; } diff --git a/starboard/shared/starboard/player/filter/testing/player_components_test.cc b/starboard/shared/starboard/player/filter/testing/player_components_test.cc index e4726bef7f2c..ef8c4e322449 100644 --- a/starboard/shared/starboard/player/filter/testing/player_components_test.cc +++ b/starboard/shared/starboard/player/filter/testing/player_components_test.cc @@ -14,6 +14,8 @@ #include "starboard/shared/starboard/player/filter/player_components.h" +#include + #include #include #include @@ -278,7 +280,7 @@ class PlayerComponentsTest bool written = TryToWriteOneInputBuffer(max_timestamp); if (!written) { ASSERT_NO_FATAL_FAILURE(RenderAndProcessPendingJobs()); - SbThreadSleep(5000); + usleep(5000); } } } @@ -307,7 +309,7 @@ class PlayerComponentsTest last_input_filled_time = CurrentMonotonicTime(); } else { ASSERT_NO_FATAL_FAILURE(RenderAndProcessPendingJobs()); - SbThreadSleep(5000); + usleep(5000); } } } @@ -344,7 +346,7 @@ class PlayerComponentsTest last_input_filled_time = CurrentMonotonicTime(); } else { ASSERT_NO_FATAL_FAILURE(RenderAndProcessPendingJobs()); - SbThreadSleep(5000); + usleep(5000); } } } @@ -377,7 +379,7 @@ class PlayerComponentsTest << GetCurrentVideoBufferTimestamp() << "), current media time is " << GetMediaTime() << "."; ASSERT_NO_FATAL_FAILURE(RenderAndProcessPendingJobs()); - SbThreadSleep(5000); + usleep(5000); } current_time = GetMediaTime(); // TODO: investigate and reduce the tolerance. @@ -565,13 +567,13 @@ TEST_P(PlayerComponentsTest, Pause) { int64_t start_time = CurrentMonotonicTime(); while (CurrentMonotonicTime() < start_time + 200'000) { ASSERT_NO_FATAL_FAILURE(RenderAndProcessPendingJobs()); - SbThreadSleep(5000); + usleep(5000); } int64_t media_time = GetMediaTime(); start_time = CurrentMonotonicTime(); while (CurrentMonotonicTime() < start_time + 200'000) { ASSERT_NO_FATAL_FAILURE(RenderAndProcessPendingJobs()); - SbThreadSleep(5000); + usleep(5000); } ASSERT_EQ(media_time, GetMediaTime()); diff --git a/starboard/shared/starboard/player/filter/testing/test_util.cc b/starboard/shared/starboard/player/filter/testing/test_util.cc index 831d7c163af0..c2c75c23641b 100644 --- a/starboard/shared/starboard/player/filter/testing/test_util.cc +++ b/starboard/shared/starboard/player/filter/testing/test_util.cc @@ -14,6 +14,8 @@ #include "starboard/shared/starboard/player/filter/testing/test_util.h" +#include + #include "starboard/audio_sink.h" #include "starboard/common/log.h" #include "starboard/directory.h" @@ -200,7 +202,7 @@ std::vector GetSupportedVideoTests() { test_params.push_back(std::make_tuple(filename, output_mode)); break; } else if (need_to_check_with_wait && !decoder_has_been_checked_once) { - SbThreadSleep(1'000'000); + usleep(1'000'000); } else { break; } diff --git a/starboard/shared/starboard/player/filter/testing/video_decoder_test_fixture.cc b/starboard/shared/starboard/player/filter/testing/video_decoder_test_fixture.cc index a343df605bb4..9b38c8b6b432 100644 --- a/starboard/shared/starboard/player/filter/testing/video_decoder_test_fixture.cc +++ b/starboard/shared/starboard/player/filter/testing/video_decoder_test_fixture.cc @@ -14,6 +14,8 @@ #include "starboard/shared/starboard/player/filter/testing/video_decoder_test_fixture.h" +#include + #include #include #include @@ -180,14 +182,14 @@ void VideoDecoderTestFixture::WaitForNextEvent(Event* event, int64_t timeout) { return; } } - SbThreadSleep(1000); + usleep(1000); } while (CurrentMonotonicTime() - start < timeout); event->status = kTimeout; SB_LOG(WARNING) << "WaitForNextEvent() timeout."; } bool VideoDecoderTestFixture::HasPendingEvents() { - SbThreadSleep(5000); + usleep(5000); ScopedLock scoped_lock(mutex_); return !event_queue_.empty(); } diff --git a/starboard/shared/starboard/player/job_queue_test.cc b/starboard/shared/starboard/player/job_queue_test.cc index 520868367dbe..4bb417e437cc 100644 --- a/starboard/shared/starboard/player/job_queue_test.cc +++ b/starboard/shared/starboard/player/job_queue_test.cc @@ -14,6 +14,8 @@ #include "starboard/shared/starboard/player/job_queue.h" +#include + #include #include @@ -57,7 +59,7 @@ TEST_F(JobQueueTest, OwnedScheduledJobsAreExecutedInOrder) { Schedule([&]() { values.push_back(8); }, 3 * kPrecisionUsec); // Sleep past the last scheduled job. - SbThreadSleep(4 * kPrecisionUsec); + usleep(4 * kPrecisionUsec); job_queue_.RunUntilIdle(); EXPECT_THAT(values, ElementsAre(1, 2, 3, 4, 5, 6, 7, 8)); diff --git a/starboard/shared/starboard/player/job_thread_test.cc b/starboard/shared/starboard/player/job_thread_test.cc index a6c1a9eeba7e..ded68c4cb820 100644 --- a/starboard/shared/starboard/player/job_thread_test.cc +++ b/starboard/shared/starboard/player/job_thread_test.cc @@ -14,6 +14,8 @@ #include "starboard/shared/starboard/player/job_thread.h" +#include + #include #include @@ -52,7 +54,7 @@ TEST(JobThreadTest, ScheduledJobsAreExecutedInOrder) { job_thread.Schedule([&]() { values.push_back(8); }, 3 * kPrecisionUsec); // Sleep past the last scheduled job. - SbThreadSleep(4 * kPrecisionUsec); + usleep(4 * kPrecisionUsec); ExecutePendingJobs(&job_thread); @@ -64,7 +66,7 @@ TEST(JobThreadTest, ScheduleAndWaitWaits) { std::atomic_bool job_1 = {false}; JobThread job_thread{"JobThreadTests"}; job_thread.ScheduleAndWait([&]() { - SbThreadSleep(1 * kPrecisionUsec); + usleep(1 * kPrecisionUsec); job_1 = true; }); // Verify that the job ran and that it took at least as long as it slept. @@ -83,14 +85,14 @@ TEST(JobThreadTest, ScheduledJobsShouldNotExecuteAfterGoingOutOfScope) { job_thread.Schedule(job); // Wait for the job to run at least once and reschedule itself. - SbThreadSleep(1 * kPrecisionUsec); + usleep(1 * kPrecisionUsec); ExecutePendingJobs(&job_thread); } int end_value = counter; EXPECT_GE(counter, 1); // Sleep past two more (potential) executions and verify there were none. - SbThreadSleep(4 * kPrecisionUsec); + usleep(4 * kPrecisionUsec); EXPECT_EQ(counter, end_value); } @@ -121,7 +123,7 @@ TEST(JobThreadTest, CanceledJobsAreCanceled) { int checkpoint_2 = counter_2; // Sleep and wait for pending jobs to run. - SbThreadSleep(1 * kPrecisionUsec); + usleep(1 * kPrecisionUsec); ExecutePendingJobs(&job_thread); // Job 1 should not have run again. diff --git a/starboard/shared/uwp/analog_thumbstick_input_thread.cc b/starboard/shared/uwp/analog_thumbstick_input_thread.cc index fcaa59c29494..857c82851896 100644 --- a/starboard/shared/uwp/analog_thumbstick_input_thread.cc +++ b/starboard/shared/uwp/analog_thumbstick_input_thread.cc @@ -16,6 +16,8 @@ #include "starboard/shared/uwp/analog_thumbstick_input_thread.h" +#include + #include #include #include @@ -44,7 +46,7 @@ class AnalogThumbstickThread::Impl : public Thread { while (!join_called()) { Update(); // 120hz to provide smooth 60fps playback. - SbThreadSleep(1'000'000LL / kPollingFrequency); + usleep(1'000'000LL / kPollingFrequency); } } diff --git a/starboard/shared/uwp/application_uwp.cc b/starboard/shared/uwp/application_uwp.cc index 1e76ebfee38f..fff54a8eba23 100644 --- a/starboard/shared/uwp/application_uwp.cc +++ b/starboard/shared/uwp/application_uwp.cc @@ -24,6 +24,8 @@ #include #include +#include + #include #include #include @@ -333,7 +335,7 @@ std::string GetBinaryName() { void OnDeviceAdded(DeviceWatcher ^, DeviceInformation ^) { SB_LOG(INFO) << "DisplayStatusWatcher::OnDeviceAdded"; // We need delay to give time for the display initializing after connect. - SbThreadSleep(15'000); + usleep(15'000); MimeSupportabilityCache::GetInstance()->ClearCachedMimeSupportabilities(); @@ -716,8 +718,7 @@ ref class App sealed : public IFrameworkView { std::stringstream ss; ss << platformStringToString( Windows::Storage::ApplicationData::Current->LocalCacheFolder->Path); - ss << "\\" - << "" << command_line->GetSwitchValue(kLogPathSwitch); + ss << "\\" << "" << command_line->GetSwitchValue(kLogPathSwitch); std::string full_path_log_file = ss.str(); shared::uwp::OpenLogFileWin32(full_path_log_file.c_str()); } else { diff --git a/starboard/shared/uwp/watchdog_log.cc b/starboard/shared/uwp/watchdog_log.cc index 270d4643d55e..b8092897ded4 100644 --- a/starboard/shared/uwp/watchdog_log.cc +++ b/starboard/shared/uwp/watchdog_log.cc @@ -14,6 +14,8 @@ #include "starboard/shared/uwp/watchdog_log.h" +#include + #include #include "starboard/common/file.h" @@ -68,7 +70,7 @@ class WatchDogThread : public Thread { SbFileWrite(file_handle, kDone, static_cast(strlen(kDone))); RecordFileWriteStat(result); SbFileFlush(file_handle); - SbThreadSleep(50'000); + usleep(50'000); bool closed = SbFileClose(file_handle); SB_LOG_IF(ERROR, closed) << "Could not close file " << file_path_; } diff --git a/starboard/shared/win32/posix_emu/include/unistd.h b/starboard/shared/win32/posix_emu/include/unistd.h index b545d2d1a5b9..63b072fc2a37 100644 --- a/starboard/shared/win32/posix_emu/include/unistd.h +++ b/starboard/shared/win32/posix_emu/include/unistd.h @@ -23,6 +23,8 @@ extern "C" { // located in socket.cc. int close(int fd); +int usleep(unsigned int useconds); + #ifdef __cplusplus } #endif // __cplusplus diff --git a/starboard/shared/win32/posix_emu/usleep.cc b/starboard/shared/win32/posix_emu/usleep.cc new file mode 100644 index 000000000000..f0ca4c100750 --- /dev/null +++ b/starboard/shared/win32/posix_emu/usleep.cc @@ -0,0 +1,28 @@ +// Copyright 2024 The Cobalt Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include "starboard/shared/win32/time_utils.h" + +using starboard::shared::win32::ConvertUsecToMillisRoundUp; + +extern "C" { + +int usleep(unsigned int useconds) { + Sleep(ConvertUsecToMillisRoundUp(useconds)); + return 0; +} +} diff --git a/starboard/shared/win32/video_decoder.cc b/starboard/shared/win32/video_decoder.cc index 5734b3724d90..8bc9ef3e29cc 100644 --- a/starboard/shared/win32/video_decoder.cc +++ b/starboard/shared/win32/video_decoder.cc @@ -14,6 +14,8 @@ #include "starboard/shared/win32/video_decoder.h" +#include + #include #include "starboard/common/log.h" @@ -534,7 +536,7 @@ void VideoDecoder::ShutdownCodec() { // Microsoft recommends stalling to let other systems release their // references to the IMFSamples. if (video_codec_ == kSbMediaVideoCodecVp9) { - SbThreadSleep(150'000); + usleep(150'000); } decoder_.reset(); video_processor_.Reset(); @@ -666,7 +668,7 @@ void VideoDecoder::DecoderThreadRun() { } if (event == nullptr) { - SbThreadSleep(1000); + usleep(1000); } else { switch (event->type) { case Event::kWriteInputBuffer: @@ -771,7 +773,7 @@ void VideoDecoder::DecoderThreadRun() { if (!wrote_input && !read_output) { // Throttle decode loop since no I/O was possible. - SbThreadSleep(1000); + usleep(1000); } } } diff --git a/starboard/thread.h b/starboard/thread.h index a0ac9d9e7b7e..d8974f93fa2c 100644 --- a/starboard/thread.h +++ b/starboard/thread.h @@ -195,12 +195,14 @@ SB_EXPORT void SbThreadDetach(SbThread thread); SB_EXPORT void SbThreadYield(); #endif +#if SB_API_VERSION < 16 // Sleeps the currently executing thread. // // |duration|: The minimum amount of time, in microseconds, that the currently // executing thread should sleep. The function is a no-op if this value is // negative or |0|. SB_EXPORT void SbThreadSleep(int64_t duration); +#endif // Returns the handle of the currently executing thread. SB_EXPORT SbThread SbThreadGetCurrent(); diff --git a/starboard/tools/api_leak_detector/api_leak_detector.py b/starboard/tools/api_leak_detector/api_leak_detector.py index 759549ed7895..430e797dcf21 100755 --- a/starboard/tools/api_leak_detector/api_leak_detector.py +++ b/starboard/tools/api_leak_detector/api_leak_detector.py @@ -146,6 +146,7 @@ 'pthread_key_create', 'pthread_key_delete', 'pthread_setspecific', + 'usleep', ] diff --git a/starboard/win/shared/BUILD.gn b/starboard/win/shared/BUILD.gn index ab18826a6691..792217f80d54 100644 --- a/starboard/win/shared/BUILD.gn +++ b/starboard/win/shared/BUILD.gn @@ -243,6 +243,7 @@ static_library("starboard_platform") { "//starboard/shared/win32/posix_emu/string.cc", "//starboard/shared/win32/posix_emu/strings.cc", "//starboard/shared/win32/posix_emu/time.cc", + "//starboard/shared/win32/posix_emu/usleep.cc", "//starboard/shared/win32/set_non_blocking_internal.cc", "//starboard/shared/win32/set_non_blocking_internal.h", "//starboard/shared/win32/socket_accept.cc", diff --git a/third_party/abseil-cpp/absl/base/internal/spinlock_starboard.inc b/third_party/abseil-cpp/absl/base/internal/spinlock_starboard.inc index e90e0789e763..e8dd375e265b 100644 --- a/third_party/abseil-cpp/absl/base/internal/spinlock_starboard.inc +++ b/third_party/abseil-cpp/absl/base/internal/spinlock_starboard.inc @@ -17,6 +17,8 @@ #if defined(STARBOARD) #include "starboard/thread.h" +#include + #include #include #include @@ -35,7 +37,7 @@ ABSL_ATTRIBUTE_WEAK void ABSL_INTERNAL_C_SYMBOL(AbslInternalSpinLockDelay)( } else if (loop == 1) { sched_yield(); } else { - SbThreadSleep(loop); + usleep(loop); } } diff --git a/third_party/boringssl/src/crypto/thread_starboard.cc b/third_party/boringssl/src/crypto/thread_starboard.cc index 37b3390ddbe0..928d39dd3c8d 100644 --- a/third_party/boringssl/src/crypto/thread_starboard.cc +++ b/third_party/boringssl/src/crypto/thread_starboard.cc @@ -17,6 +17,7 @@ #include "internal.h" #if defined(STARBOARD) +#include #include @@ -70,7 +71,7 @@ void EnsureInitialized(struct CRYPTO_STATIC_MUTEX* lock) { return; } while (SbAtomicNoBarrier_Load(&lock->initialized) != kInitialized) { - SbThreadSleep(1000); // 1ms + usleep(1000); // 1ms } } diff --git a/third_party/llvm-project/libcxx/include/__external_threading b/third_party/llvm-project/libcxx/include/__external_threading index 6de7e6ae9b8e..93f1ea7c1593 100644 --- a/third_party/llvm-project/libcxx/include/__external_threading +++ b/third_party/llvm-project/libcxx/include/__external_threading @@ -5,6 +5,8 @@ #include <__config> #include +#include + #include "starboard/common/recursive_mutex.h" #include "starboard/condition_variable.h" #include "starboard/mutex.h" @@ -266,7 +268,7 @@ void __libcpp_thread_yield() { sched_yield(); } void __libcpp_thread_sleep_for(const chrono::nanoseconds& __ns) { // Convert nanoseconds (10^-9) to microseconds (10^-6). const int64_t duration = __ns.count() / 1000; - SbThreadSleep(duration); + usleep(duration); } int __libcpp_tls_create(__libcpp_tls_key* __key, void (*__at_exit)(void*)) { diff --git a/third_party/llvm-project/libcxx/src/thread.cpp b/third_party/llvm-project/libcxx/src/thread.cpp index d7fe18e3ef76..8cbbc5138f7b 100644 --- a/third_party/llvm-project/libcxx/src/thread.cpp +++ b/third_party/llvm-project/libcxx/src/thread.cpp @@ -75,7 +75,8 @@ thread::detach() unsigned thread::hardware_concurrency() noexcept { -#if defined(_SC_NPROCESSORS_ONLN) +// Excluded from Modular builds as Starboard doesn't provide sysconf +#if defined(_SC_NPROCESSORS_ONLN) && !SB_IS(MODULAR) long result = sysconf(_SC_NPROCESSORS_ONLN); // sysconf returns -1 if the name is invalid, the option does not exist or // does not have a definite limit. diff --git a/third_party/musl/BUILD.gn b/third_party/musl/BUILD.gn index 5d4d745e804f..e33588ac3cb2 100644 --- a/third_party/musl/BUILD.gn +++ b/third_party/musl/BUILD.gn @@ -406,6 +406,7 @@ static_library("c_internal") { "src/starboard/time/gmtime_r.c", "src/starboard/time/time.c", "src/starboard/sys/stat.c", + "src/starboard/unistd/unistd.c", "src/stdio/__toread.c", "src/stdio/__uflow.c", "src/stdio/fprintf.c", diff --git a/third_party/musl/src/starboard/unistd/unistd.c b/third_party/musl/src/starboard/unistd/unistd.c new file mode 100644 index 000000000000..f493b65d450d --- /dev/null +++ b/third_party/musl/src/starboard/unistd/unistd.c @@ -0,0 +1,25 @@ +// Copyright 2024 The Cobalt Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#if SB_API_VERSION < 16 +#include + +#include "starboard/thread.h" + +int usleep(useconds_t useconds) { + SbThreadSleep(useconds); + return 0; +} + +#endif // SB_API_VERSION < 16 diff --git a/v8/src/base/platform/platform-starboard.cc b/v8/src/base/platform/platform-starboard.cc index adfc111504fc..ecab056bc21b 100644 --- a/v8/src/base/platform/platform-starboard.cc +++ b/v8/src/base/platform/platform-starboard.cc @@ -7,6 +7,7 @@ // apps in the livingroom. #include +#include #include "src/base/lazy-instance.h" #include "src/base/macros.h" @@ -223,7 +224,7 @@ bool OS::HasLazyCommits() { return false; } -void OS::Sleep(TimeDelta interval) { SbThreadSleep(interval.InMicroseconds()); } +void OS::Sleep(TimeDelta interval) { usleep(interval.InMicroseconds()); } void OS::Abort() { SbSystemBreakIntoDebugger(); }