Skip to content

Commit

Permalink
Deprecate SbThreadSleep (#2925)
Browse files Browse the repository at this point in the history
b/334139073

Change-Id: I8cb0562619e5e0a2392536d1637c3abd8ed326d8
(cherry picked from commit bb806e0)
  • Loading branch information
y4vor authored and anonymous1-me committed Apr 19, 2024
1 parent 1e5927a commit ca5bc57
Show file tree
Hide file tree
Showing 66 changed files with 322 additions and 116 deletions.
3 changes: 2 additions & 1 deletion base/threading/platform_thread_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "base/threading/platform_thread.h"

#include <sched.h>
#include <unistd.h>

#include "base/logging.h"
#include "base/threading/thread_id_name_manager.h"
Expand Down Expand Up @@ -120,7 +121,7 @@ void PlatformThread::YieldCurrentThread() {

// static
void PlatformThread::Sleep(TimeDelta duration) {
SbThreadSleep(duration.InMicroseconds());
usleep(duration.InMicroseconds());
}

// static
Expand Down
4 changes: 3 additions & 1 deletion cobalt/dom/serialized_algorithm_runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#ifndef COBALT_DOM_SERIALIZED_ALGORITHM_RUNNER_H_
#define COBALT_DOM_SERIALIZED_ALGORITHM_RUNNER_H_

#include <unistd.h>

#include <algorithm>
#include <memory>
#include <utility>
Expand Down Expand Up @@ -101,7 +103,7 @@ class SerializedAlgorithmRunner {
if (mutex_.AcquireTry()) {
break;
}
SbThreadSleep(wait_interval_usec);
usleep(static_cast<unsigned int>(wait_interval_usec));
// Double the wait interval upon every failure, but cap it at
// kMaxWaitIntervalUsec.
wait_interval_usec =
Expand Down
10 changes: 6 additions & 4 deletions cobalt/media/base/cval_stats_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#include "cobalt/media/base/cval_stats.h"

#include <unistd.h>

#include <set>
#include <string>

Expand Down Expand Up @@ -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);

Expand All @@ -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<std::string> result = cvm->GetValueAsString(kCValnameLatest);
Expand All @@ -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);
}

Expand All @@ -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);
}

Expand Down
4 changes: 3 additions & 1 deletion cobalt/media_session/media_session_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#include "cobalt/media_session/media_session.h"

#include <unistd.h>

#include <limits>
#include <memory>

Expand Down Expand Up @@ -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_; }
Expand Down
7 changes: 2 additions & 5 deletions cobalt/trace_event/sample_benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <unistd.h>

#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",
Expand Down
4 changes: 3 additions & 1 deletion cobalt/updater/noop_sandbox.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@

// This is a test app for Evergreen that does nothing.

#include <unistd.h>

#include "base/time/time.h"
#include "starboard/event.h"
#include "starboard/system.h"
#include "starboard/thread.h"

void SbEventHandle(const SbEvent* event) {
// No-op app. Exit after 1s.
SbThreadSleep(1 * base::Time::kMicrosecondsPerSecond);
usleep(1 * base::Time::kMicrosecondsPerSecond);
SbSystemRequestStop(0);
}
4 changes: 3 additions & 1 deletion cobalt/watchdog/watchdog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#include "cobalt/watchdog/watchdog.h"

#include <unistd.h>

#include <algorithm>
#include <sstream>
#include <utility>
Expand Down Expand Up @@ -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();
}
}
Expand Down
58 changes: 30 additions & 28 deletions cobalt/watchdog/watchdog_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#include "cobalt/watchdog/watchdog.h"

#include <unistd.h>

#include <set>
#include <utility>
#include <vector>
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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<base::Value> uncleared_violations_map_optional =
Expand All @@ -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<base::Value> violations_map_optional =
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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<base::Value> violations_map_optional =
Expand Down Expand Up @@ -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, "");
Expand Down Expand Up @@ -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, "");
Expand Down Expand Up @@ -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"));
}
Expand All @@ -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"));
}
Expand All @@ -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));
Expand All @@ -513,12 +515,12 @@ TEST_F(WatchdogTest, PingByClientShouldPreventViolations) {
std::shared_ptr<Client> 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));
}
Expand All @@ -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(), "");
}

Expand All @@ -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(), "");
}

Expand All @@ -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"));
}
Expand All @@ -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);
Expand All @@ -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(
Expand All @@ -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"));

Expand All @@ -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"));

Expand Down Expand Up @@ -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<base::Value> violations_map_optional =
Expand All @@ -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<base::Value> violations_map_optional =
Expand Down
4 changes: 4 additions & 0 deletions starboard/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ since the version previous to it.

## Version 16

### Deprecated `SbThreadSleep`
Replaced the `SbThreadSleep` with the POSIX usleep() defined in the
`<unistd.h>` 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`.
Expand Down
Loading

0 comments on commit ca5bc57

Please sign in to comment.