Skip to content

Commit

Permalink
Cherry pick PR #3237: Migrate starboard/share to pthread (#3250)
Browse files Browse the repository at this point in the history
Refer to the original PR: #3237

Test-On-Device: true
b/302335657

Change-Id: Ieea01e931c83bc6b067ec9b1de5ddf44c3eb81cf

Co-authored-by: Yavor Goulishev <[email protected]>
  • Loading branch information
cobalt-github-releaser-bot and y4vor authored May 15, 2024
1 parent 7b8ac8b commit e8bf025
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 26 deletions.
10 changes: 5 additions & 5 deletions starboard/shared/libevent/socket_waiter_internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ void GetSocketPipe(SbSocket* client_socket, SbSocket* server_socket) {
} // namespace

SbSocketWaiterPrivate::SbSocketWaiterPrivate()
: thread_(SbThreadGetCurrent()),
: thread_(pthread_self()),
base_(event_base_new()),
waiting_(false),
woken_up_(false) {
Expand Down Expand Up @@ -177,7 +177,7 @@ bool SbSocketWaiterPrivate::Add(SbSocket socket,
SbSocketWaiterCallback callback,
int interests,
bool persistent) {
SB_DCHECK(SbThreadIsCurrent(thread_));
SB_DCHECK(pthread_equal(pthread_self(), thread_));

if (!SbSocketIsValid(socket)) {
SB_DLOG(ERROR) << __FUNCTION__ << ": Socket (" << socket << ") is invalid.";
Expand Down Expand Up @@ -234,7 +234,7 @@ bool SbSocketWaiterPrivate::Add(SbSocket socket,
}

bool SbSocketWaiterPrivate::Remove(SbSocket socket) {
SB_DCHECK(SbThreadIsCurrent(thread_));
SB_DCHECK(pthread_equal(pthread_self(), thread_));
if (!SbSocketIsValid(socket)) {
SB_DLOG(ERROR) << __FUNCTION__ << ": Socket (" << socket << ") is invalid.";
return false;
Expand All @@ -261,15 +261,15 @@ bool SbSocketWaiterPrivate::Remove(SbSocket socket) {
}

void SbSocketWaiterPrivate::Wait() {
SB_DCHECK(SbThreadIsCurrent(thread_));
SB_DCHECK(pthread_equal(pthread_self(), thread_));

// We basically wait for the largest amount of time to achieve an indefinite
// block.
WaitTimed(kSbInt64Max);
}

SbSocketWaiterResult SbSocketWaiterPrivate::WaitTimed(int64_t duration_usec) {
SB_DCHECK(SbThreadIsCurrent(thread_));
SB_DCHECK(pthread_equal(pthread_self(), thread_));

// The way to do this is apparently to create a timeout event, call WakeUp
// inside that callback, and then just do a normal wait.
Expand Down
5 changes: 3 additions & 2 deletions starboard/shared/libevent/socket_waiter_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
#ifndef STARBOARD_SHARED_LIBEVENT_SOCKET_WAITER_INTERNAL_H_
#define STARBOARD_SHARED_LIBEVENT_SOCKET_WAITER_INTERNAL_H_

#include <pthread.h>

#include <map>

#include "starboard/common/socket.h"
#include "starboard/shared/internal_only.h"
#include "starboard/socket_waiter.h"
#include "starboard/thread.h"
#include "starboard/types.h"
#include "third_party/libevent/event.h"

Expand Down Expand Up @@ -115,7 +116,7 @@ struct SbSocketWaiterPrivate {

// The thread this waiter was created on. Immutable, so accessible from any
// thread.
const SbThread thread_;
const pthread_t thread_;

// The libevent event_base backing this waiter. Immutable, so accessible from
// any thread.
Expand Down
4 changes: 2 additions & 2 deletions starboard/shared/starboard/application.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Application* Application::g_instance = NULL;
#if SB_API_VERSION >= 15
Application::Application(SbEventHandleCallback sb_event_handle_callback)
: error_level_(0),
thread_(SbThreadGetCurrent()),
thread_(pthread_self()),
start_link_(NULL),
state_(kStateUnstarted),
sb_event_handle_callback_(sb_event_handle_callback) {
Expand All @@ -79,7 +79,7 @@ Application::Application(SbEventHandleCallback sb_event_handle_callback)
#else
Application::Application()
: error_level_(0),
thread_(SbThreadGetCurrent()),
thread_(pthread_self()),
start_link_(NULL),
state_(kStateUnstarted) {
#endif // SB_API_VERSION >= 15
Expand Down
7 changes: 4 additions & 3 deletions starboard/shared/starboard/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#ifndef STARBOARD_SHARED_STARBOARD_APPLICATION_H_
#define STARBOARD_SHARED_STARBOARD_APPLICATION_H_

#include <pthread.h>

#include <memory>
#include <vector>

Expand All @@ -32,7 +34,6 @@
#include "starboard/shared/internal_only.h"
#include "starboard/shared/starboard/command_line.h"
#include "starboard/shared/starboard/player/filter/video_frame_internal.h"
#include "starboard/thread.h"
#include "starboard/types.h"
#include "starboard/window.h"

Expand Down Expand Up @@ -376,7 +377,7 @@ class Application {

// Returns whether the current thread is the Application thread.
bool IsCurrentThread() const {
return SbThreadIsEqual(thread_, SbThreadGetCurrent());
return pthread_equal(thread_, pthread_self());
}

// Returns the current application state.
Expand Down Expand Up @@ -439,7 +440,7 @@ class Application {

// The thread that this application was created on, which is assumed to be the
// main thread.
SbThread thread_;
pthread_t thread_;

// CommandLine instance initialized in |Run|.
std::unique_ptr<CommandLine> command_line_;
Expand Down
6 changes: 3 additions & 3 deletions starboard/shared/starboard/player/job_queue.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ void ResetCurrentThreadJobQueue() {

} // namespace

JobQueue::JobQueue() : thread_id_(SbThreadGetId()), condition_(mutex_) {
SB_DCHECK(SbThreadIsValidId(thread_id_));
JobQueue::JobQueue() : thread_id_(pthread_self()), condition_(mutex_) {
SB_DCHECK(thread_id_ != 0);
SetCurrentThreadJobQueue(this);
}

Expand Down Expand Up @@ -155,7 +155,7 @@ bool JobQueue::BelongsToCurrentThread() const {
// The ctor already ensures that the current JobQueue is the only JobQueue of
// the thread, checking for thread id is more light-weighted then calling
// JobQueue::current() and compare the result with |this|.
return thread_id_ == SbThreadGetId();
return pthread_equal(thread_id_, pthread_self());
}

// static
Expand Down
4 changes: 3 additions & 1 deletion starboard/shared/starboard/player/job_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#ifndef STARBOARD_SHARED_STARBOARD_PLAYER_JOB_QUEUE_H_
#define STARBOARD_SHARED_STARBOARD_PLAYER_JOB_QUEUE_H_

#include <pthread.h>

#include <functional>
#include <map>
#include <utility>
Expand Down Expand Up @@ -171,7 +173,7 @@ class JobQueue {
// be run.
bool TryToRunOneJob(bool wait_for_next_job);

const SbThreadId thread_id_;
const pthread_t thread_id_;
Mutex mutex_;
ConditionVariable condition_;
int64_t current_job_token_ = JobToken::kInvalidToken + 1;
Expand Down
28 changes: 18 additions & 10 deletions starboard/shared/starboard/thread_checker.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
#ifndef STARBOARD_SHARED_STARBOARD_THREAD_CHECKER_H_
#define STARBOARD_SHARED_STARBOARD_THREAD_CHECKER_H_

#include <pthread.h>

#include "starboard/atomic.h"
#include "starboard/thread.h"

namespace starboard {
namespace shared {
Expand All @@ -43,9 +44,10 @@ class ThreadChecker {

explicit ThreadChecker(Type type = kSetThreadIdOnCreation) {
if (type == kSetThreadIdOnCreation)
thread_id_ = SbThreadGetId();
thread_id_ =
reinterpret_cast<uintptr_t>(reinterpret_cast<void*>(pthread_self()));
else
thread_id_ = kSbThreadInvalidId;
thread_id_ = 0;
}

// Detached the thread checker from its current thread. The thread checker
Expand All @@ -55,19 +57,25 @@ class ThreadChecker {
void Detach() {
// This is safe as when this function is called, it is expected that it
// won't be called on its current thread.
thread_id_ = kSbThreadInvalidId;
thread_id_ = 0;
}

bool CalledOnValidThread() const {
SbThreadId current_thread_id = SbThreadGetId();
SbThreadId stored_thread_id = SbAtomicNoBarrier_CompareAndSwap(
&thread_id_, kSbThreadInvalidId, current_thread_id);
return stored_thread_id == kSbThreadInvalidId ||
stored_thread_id == current_thread_id;
uintptr_t current_thread_id =
reinterpret_cast<uintptr_t>(reinterpret_cast<void*>(pthread_self()));
#if SB_HAS(64_BIT_ATOMICS)
uintptr_t stored_thread_id = SbAtomicNoBarrier_CompareAndSwap64(
reinterpret_cast<SbAtomic64*>(&thread_id_), 0, current_thread_id);
#else
uintptr_t stored_thread_id = SbAtomicNoBarrier_CompareAndSwap(
reinterpret_cast<SbAtomic32*>(&thread_id_), 0, current_thread_id);
#endif

return stored_thread_id == 0 || stored_thread_id == current_thread_id;
}

private:
mutable SbThreadId thread_id_;
mutable uintptr_t thread_id_;
};

#endif // defined(COBALT_BUILD_TYPE_GOLD)
Expand Down

0 comments on commit e8bf025

Please sign in to comment.