Skip to content

Commit

Permalink
Cherry pick PR #3160: Migrate default stack/priority pthread_create c…
Browse files Browse the repository at this point in the history
…alls (#3177)

Refer to the original PR: #3160

b/302335657

Change-Id: I4aeed905a653b63128036c39cce86549cc20be39

Co-authored-by: Yavor Goulishev <[email protected]>
  • Loading branch information
cobalt-github-releaser-bot and y4vor authored May 8, 2024
1 parent afc309e commit be6c323
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 95 deletions.
11 changes: 6 additions & 5 deletions cobalt/watchdog/watchdog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ bool Watchdog::InitializeCustom(

// Starts monitor thread.
is_monitoring_.store(true);
SB_DCHECK(!SbThreadIsValid(watchdog_thread_));
watchdog_thread_ = SbThreadCreate(0, kSbThreadNoPriority, kSbThreadNoAffinity,
true, "Watchdog", &Watchdog::Monitor, this);
SB_DCHECK(SbThreadIsValid(watchdog_thread_));

int res =
pthread_create(&watchdog_thread_, nullptr, &Watchdog::Monitor, this);
SB_DCHECK(res == 0);
return true;
}

Expand All @@ -123,7 +123,7 @@ void Watchdog::Uninitialize() {
is_monitoring_.store(false);
monitor_wait_.Signal();
mutex_.Release();
SbThreadJoin(watchdog_thread_, nullptr);
pthread_join(watchdog_thread_, nullptr);
}

std::shared_ptr<base::Value> Watchdog::GetViolationsMap() {
Expand Down Expand Up @@ -199,6 +199,7 @@ void Watchdog::WriteWatchdogViolations() {
}

void* Watchdog::Monitor(void* context) {
pthread_setname_np(pthread_self(), "Watchdog");
starboard::ScopedLock scoped_lock(static_cast<Watchdog*>(context)->mutex_);
while (1) {
int64_t current_monotonic_time = starboard::CurrentMonotonicTime();
Expand Down
5 changes: 3 additions & 2 deletions cobalt/watchdog/watchdog.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#ifndef COBALT_WATCHDOG_WATCHDOG_H_
#define COBALT_WATCHDOG_WATCHDOG_H_

#include <pthread.h>

#include <memory>
#include <string>
#include <unordered_map>
Expand All @@ -28,7 +30,6 @@
#include "starboard/common/atomic.h"
#include "starboard/common/condition_variable.h"
#include "starboard/common/mutex.h"
#include "starboard/thread.h"

namespace cobalt {
namespace watchdog {
Expand Down Expand Up @@ -195,7 +196,7 @@ class Watchdog : public Singleton<Watchdog> {
// Dictionary of lists of Watchdog violations represented as dictionaries.
std::shared_ptr<base::Value> violations_map_;
// Monitor thread.
SbThread watchdog_thread_;
pthread_t watchdog_thread_;
// Flag to stop monitor thread.
starboard::atomic_bool is_monitoring_;
// Conditional Variable to wait and shutdown monitor thread.
Expand Down
16 changes: 11 additions & 5 deletions starboard/android/shared/android_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ using ::starboard::shared::starboard::CommandLine;
typedef ::starboard::android::shared::ApplicationAndroid::AndroidCommand
AndroidCommand;

SbThread g_starboard_thread = kSbThreadInvalid;
pthread_t g_starboard_thread = 0;
Semaphore* g_app_created_semaphore = nullptr;

// Safeguard to avoid sending AndroidCommands either when there is no instance
Expand Down Expand Up @@ -214,6 +214,7 @@ void InstallCrashpadHandler(const CommandLine& command_line) {
#endif // SB_IS(EVERGREEN_COMPATIBLE)

void* ThreadEntryPoint(void* context) {
pthread_setname_np(pthread_self(), "StarboardMain");
g_app_created_semaphore = static_cast<Semaphore*>(context);

#if SB_API_VERSION >= 15
Expand Down Expand Up @@ -332,12 +333,17 @@ extern "C" SB_EXPORT_PLATFORM void GameActivity_onCreate(
void* savedState,
size_t savedStateSize) {
// Start the Starboard thread the first time an Activity is created.
if (!SbThreadIsValid(g_starboard_thread)) {
if (g_starboard_thread == 0) {
Semaphore semaphore;

g_starboard_thread =
SbThreadCreate(0, kSbThreadPriorityNormal, kSbThreadNoAffinity, false,
"StarboardMain", &ThreadEntryPoint, &semaphore);
pthread_attr_t attributes;
pthread_attr_init(&attributes);
pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_DETACHED);

pthread_create(&g_starboard_thread, &attributes, &ThreadEntryPoint,
&semaphore);

pthread_attr_destroy(&attributes);

// Wait for the ApplicationAndroid to be created.
semaphore.Take();
Expand Down
35 changes: 14 additions & 21 deletions starboard/shared/starboard/link_receiver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,12 @@ namespace {
scoped_ptr<Socket> CreateServerSocket(SbSocketAddressType address_type) {
scoped_ptr<Socket> socket(new Socket(address_type));
if (!socket->IsValid()) {
SB_LOG(ERROR) << __FUNCTION__ << ": "
<< "SbSocketCreate failed";
SB_LOG(ERROR) << __FUNCTION__ << ": " << "SbSocketCreate failed";
return scoped_ptr<Socket>().Pass();
}

if (!socket->SetReuseAddress(true)) {
SB_LOG(ERROR) << __FUNCTION__ << ": "
<< "SbSocketSetReuseAddress failed";
SB_LOG(ERROR) << __FUNCTION__ << ": " << "SbSocketSetReuseAddress failed";
return scoped_ptr<Socket>().Pass();
}

Expand All @@ -69,8 +67,8 @@ scoped_ptr<Socket> CreateLocallyBoundSocket(SbSocketAddressType address_type,
}
SbSocketError result = socket->Bind(&address);
if (result != kSbSocketOk) {
SB_LOG(ERROR) << __FUNCTION__ << ": "
<< "SbSocketBind to " << port << " failed: " << result;
SB_LOG(ERROR) << __FUNCTION__ << ": " << "SbSocketBind to " << port
<< " failed: " << result;
return scoped_ptr<Socket>().Pass();
}

Expand Down Expand Up @@ -117,8 +115,7 @@ std::string GetTemporaryDirectory() {
bool has_temp = SbSystemGetPath(kSbSystemPathTempDirectory, temp_path.get(),
kMaxPathLength);
if (!has_temp) {
SB_LOG(ERROR) << __FUNCTION__ << ": "
<< "No temporary directory.";
SB_LOG(ERROR) << __FUNCTION__ << ": " << "No temporary directory.";
return "";
}

Expand All @@ -136,8 +133,7 @@ void CreateTemporaryFile(const char* name, const char* contents, int size) {
path += name;
ScopedFile file(path.c_str(), kSbFileCreateAlways | kSbFileWrite);
if (!file.IsValid()) {
SB_LOG(ERROR) << __FUNCTION__ << ": "
<< "Unable to create: " << path;
SB_LOG(ERROR) << __FUNCTION__ << ": " << "Unable to create: " << path;
return;
}

Expand Down Expand Up @@ -212,7 +208,7 @@ class LinkReceiver::Impl {
int actual_port_;

// The thread owned by this server.
SbThread thread_;
pthread_t thread_;

// An atomic flag that indicates whether to quit to the server thread.
atomic_bool quit_;
Expand All @@ -239,24 +235,22 @@ class LinkReceiver::Impl {
LinkReceiver::Impl::Impl(Application* application, int port)
: application_(application),
specified_port_(port),
thread_(kSbThreadInvalid),
thread_(0),
waiter_(kSbSocketWaiterInvalid) {
thread_ =
SbThreadCreate(0, kSbThreadNoPriority, kSbThreadNoAffinity, true,
"LinkReceiver", &LinkReceiver::Impl::RunThread, this);
pthread_create(&thread_, nullptr, &LinkReceiver::Impl::RunThread, this);

// Block until waiter is set.
waiter_initialized_.Take();
}

LinkReceiver::Impl::~Impl() {
SB_DCHECK(!SbThreadIsEqual(thread_, SbThreadGetCurrent()));
SB_DCHECK(!pthread_equal(thread_, pthread_self()));
quit_.store(true);
if (SbSocketWaiterIsValid(waiter_)) {
SbSocketWaiterWakeUp(waiter_);
}
destroy_waiter_.Put();
SbThreadJoin(thread_, NULL);
pthread_join(thread_, NULL);
}

void LinkReceiver::Impl::Run() {
Expand Down Expand Up @@ -322,8 +316,7 @@ bool LinkReceiver::Impl::AddForAccept(Socket* socket) {
if (!SbSocketWaiterAdd(waiter_, socket->socket(), this,
&LinkReceiver::Impl::HandleAccept,
kSbSocketWaiterInterestRead, true)) {
SB_LOG(ERROR) << __FUNCTION__ << ": "
<< "SbSocketWaiterAdd failed.";
SB_LOG(ERROR) << __FUNCTION__ << ": " << "SbSocketWaiterAdd failed.";
return false;
}
return true;
Expand All @@ -333,8 +326,7 @@ bool LinkReceiver::Impl::AddForRead(Connection* connection) {
if (!SbSocketWaiterAdd(waiter_, connection->socket->socket(), this,
&LinkReceiver::Impl::HandleRead,
kSbSocketWaiterInterestRead, false)) {
SB_LOG(ERROR) << __FUNCTION__ << ": "
<< "SbSocketWaiterAdd failed.";
SB_LOG(ERROR) << __FUNCTION__ << ": " << "SbSocketWaiterAdd failed.";
return false;
}
return true;
Expand Down Expand Up @@ -393,6 +385,7 @@ void LinkReceiver::Impl::OnReadReady(Connection* connection) {
// static
void* LinkReceiver::Impl::RunThread(void* context) {
SB_DCHECK(context);
pthread_setname_np(pthread_self(), "LinkReceiver");
reinterpret_cast<LinkReceiver::Impl*>(context)->Run();
return NULL;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ PunchoutVideoRendererSink::PunchoutVideoRendererSink(SbPlayer player,
int64_t render_interval)
: player_(player),
render_interval_(render_interval),
thread_(kSbThreadInvalid),
thread_(0),
z_index_(0),
x_(0),
y_(0),
Expand All @@ -43,9 +43,9 @@ PunchoutVideoRendererSink::PunchoutVideoRendererSink(SbPlayer player,
}

PunchoutVideoRendererSink::~PunchoutVideoRendererSink() {
if (SbThreadIsValid(thread_)) {
if (thread_ != 0) {
stop_requested_.store(true);
SbThreadJoin(thread_, NULL);
pthread_join(thread_, NULL);
}
}

Expand All @@ -55,9 +55,8 @@ void PunchoutVideoRendererSink::SetRenderCB(RenderCB render_cb) {

render_cb_ = render_cb;

thread_ = SbThreadCreate(0, kSbThreadNoPriority, kSbThreadNoAffinity, true,
"punchoutvidsink",
&PunchoutVideoRendererSink::ThreadEntryPoint, this);
pthread_create(&thread_, nullptr,
&PunchoutVideoRendererSink::ThreadEntryPoint, this);
}

void PunchoutVideoRendererSink::SetBounds(int z_index,
Expand Down Expand Up @@ -97,6 +96,7 @@ PunchoutVideoRendererSink::DrawFrameStatus PunchoutVideoRendererSink::DrawFrame(

// static
void* PunchoutVideoRendererSink::ThreadEntryPoint(void* context) {
pthread_setname_np(pthread_self(), "punchoutvidsink");
PunchoutVideoRendererSink* this_ptr =
static_cast<PunchoutVideoRendererSink*>(context);
this_ptr->RunLoop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#ifndef STARBOARD_SHARED_STARBOARD_PLAYER_FILTER_PUNCHOUT_VIDEO_RENDERER_SINK_H_
#define STARBOARD_SHARED_STARBOARD_PLAYER_FILTER_PUNCHOUT_VIDEO_RENDERER_SINK_H_

#include <pthread.h>

#include "starboard/common/atomic.h"
#include "starboard/common/mutex.h"
#include "starboard/media.h"
Expand Down Expand Up @@ -47,7 +49,7 @@ class PunchoutVideoRendererSink : public VideoRendererSink {
SbPlayer player_;
int64_t render_interval_; // microseconds
RenderCB render_cb_;
SbThread thread_;
pthread_t thread_;
atomic_bool stop_requested_;

Mutex mutex_;
Expand Down
14 changes: 7 additions & 7 deletions starboard/shared/widevine/widevine_timer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,17 @@ void WidevineTimer::setTimeout(int64_t delay_in_milliseconds,
void* context) {
ScopedLock scoped_lock(mutex_);
if (active_clients_.empty()) {
SB_DCHECK(!SbThreadIsValid(thread_));
SB_DCHECK(thread_ == 0);
SB_DCHECK(!job_queue_);

ConditionVariable condition_variable(mutex_);
ThreadParam thread_param = {this, &condition_variable};
thread_ =
SbThreadCreate(0, kSbThreadNoPriority, kSbThreadNoAffinity, true,
"wv_timer", &WidevineTimer::ThreadFunc, &thread_param);
pthread_create(&thread_, nullptr, &WidevineTimer::ThreadFunc,
&thread_param);
condition_variable.Wait();
}

SB_DCHECK(SbThreadIsValid(thread_));
SB_DCHECK(thread_ != 0);
SB_DCHECK(job_queue_);

auto iter = active_clients_.find(client);
Expand Down Expand Up @@ -82,15 +81,16 @@ void WidevineTimer::cancel(IClient* client) {
if (active_clients_.empty()) {
// Kill the thread on the last |client|.
job_queue_->StopSoon();
SbThreadJoin(thread_, NULL);
thread_ = kSbThreadInvalid;
pthread_join(thread_, NULL);
thread_ = 0;
job_queue_ = NULL;
}
}

// static
void* WidevineTimer::ThreadFunc(void* param) {
SB_DCHECK(param);
pthread_setname_np(pthread_self(), "wv_timer");
ThreadParam* thread_param = static_cast<ThreadParam*>(param);
thread_param->timer->RunLoop(thread_param->condition_variable);
return NULL;
Expand Down
5 changes: 3 additions & 2 deletions starboard/shared/widevine/widevine_timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
#ifndef STARBOARD_SHARED_WIDEVINE_WIDEVINE_TIMER_H_
#define STARBOARD_SHARED_WIDEVINE_WIDEVINE_TIMER_H_

#include <pthread.h>

#include <map>

#include "starboard/common/condition_variable.h"
#include "starboard/common/mutex.h"
#include "starboard/shared/starboard/player/job_queue.h"
#include "starboard/thread.h"
#include "third_party/internal/ce_cdm/cdm/include/cdm.h"

namespace starboard {
Expand Down Expand Up @@ -51,7 +52,7 @@ class WidevineTimer : public ::widevine::Cdm::ITimer {
ConditionVariable* condition_variable);

Mutex mutex_;
SbThread thread_ = kSbThreadInvalid;
pthread_t thread_ = 0;
JobQueue* job_queue_ = NULL;
std::map<IClient*, JobQueue::JobOwner*> active_clients_;
};
Expand Down
14 changes: 7 additions & 7 deletions starboard/testing/fake_graphics_context_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,15 @@ FakeGraphicsContextProvider::~FakeGraphicsContextProvider() {
functor_queue_.Put(
std::bind(&FakeGraphicsContextProvider::DestroyContext, this));
functor_queue_.Wake();
SbThreadJoin(decode_target_context_thread_, NULL);
pthread_join(decode_target_context_thread_, NULL);
EGL_CALL(eglDestroySurface(display_, surface_));
EGL_CALL(eglTerminate(display_));
SbWindowDestroy(window_);
}

void FakeGraphicsContextProvider::RunOnGlesContextThread(
const std::function<void()>& functor) {
if (SbThreadIsCurrent(decode_target_context_thread_)) {
if (pthread_equal(pthread_self(), decode_target_context_thread_)) {
functor();
return;
}
Expand All @@ -131,7 +131,7 @@ void FakeGraphicsContextProvider::RunOnGlesContextThread(

void FakeGraphicsContextProvider::ReleaseDecodeTarget(
SbDecodeTarget decode_target) {
if (SbThreadIsCurrent(decode_target_context_thread_)) {
if (pthread_equal(pthread_self(), decode_target_context_thread_)) {
SbDecodeTargetRelease(decode_target);
return;
}
Expand All @@ -150,6 +150,7 @@ void FakeGraphicsContextProvider::ReleaseDecodeTarget(

// static
void* FakeGraphicsContextProvider::ThreadEntryPoint(void* context) {
pthread_setname_np(pthread_self(), "dt_context");
auto provider = static_cast<FakeGraphicsContextProvider*>(context);
provider->RunLoop();

Expand Down Expand Up @@ -241,9 +242,8 @@ void FakeGraphicsContextProvider::InitializeEGL() {
decoder_target_provider_.gles_context_runner = DecodeTargetGlesContextRunner;
decoder_target_provider_.gles_context_runner_context = this;

decode_target_context_thread_ = SbThreadCreate(
0, kSbThreadPriorityNormal, kSbThreadNoAffinity, true, "dt_context",
&FakeGraphicsContextProvider::ThreadEntryPoint, this);
pthread_create(&decode_target_context_thread_, nullptr,
&FakeGraphicsContextProvider::ThreadEntryPoint, this);
MakeNoContextCurrent();

functor_queue_.Put(
Expand All @@ -253,7 +253,7 @@ void FakeGraphicsContextProvider::InitializeEGL() {
void FakeGraphicsContextProvider::OnDecodeTargetGlesContextRunner(
SbDecodeTargetGlesContextRunnerTarget target_function,
void* target_function_context) {
if (SbThreadIsCurrent(decode_target_context_thread_)) {
if (pthread_equal(pthread_self(), decode_target_context_thread_)) {
target_function(target_function_context);
return;
}
Expand Down
Loading

0 comments on commit be6c323

Please sign in to comment.