Skip to content

Commit

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

Test-On-Device: true
b/302335657

Change-Id: I72836264f952f580a25442975207dac23a315eda

Co-authored-by: Yavor Goulishev <[email protected]>
  • Loading branch information
cobalt-github-releaser-bot and y4vor authored May 15, 2024
1 parent e8bf025 commit d39c00a
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 22 deletions.
2 changes: 1 addition & 1 deletion starboard/common/log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ void LogMessage::Init(const char* file, int line) {
char name[128] = {0};
pthread_getname_np(pthread_self(), name, SB_ARRAY_SIZE_INT(name));
stream_ << '[';
stream_ << name << '/' << SbThreadGetId() << ':';
stream_ << name << '/' << pthread_self() << ':';
EzTimeValue time_value;
EzTimeValueGetNow(&time_value, NULL);
EzTimeT t = time_value.tv_sec;
Expand Down
18 changes: 9 additions & 9 deletions starboard/common/mutex.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,28 @@ void Mutex::Release() const {

void Mutex::DCheckAcquired() const {
#ifdef _DEBUG
SB_DCHECK(current_thread_acquired_ == SbThreadGetCurrent());
SB_DCHECK(pthread_equal(current_thread_acquired_, pthread_self()));
#endif // _DEBUG
}

#ifdef _DEBUG
void Mutex::debugInit() {
current_thread_acquired_ = kSbThreadInvalid;
current_thread_acquired_ = 0;
}
void Mutex::debugSetReleased() const {
SbThread current_thread = SbThreadGetCurrent();
SB_DCHECK(current_thread_acquired_ == current_thread);
current_thread_acquired_ = kSbThreadInvalid;
pthread_t current_thread = pthread_self();
SB_DCHECK(pthread_equal(current_thread_acquired_, current_thread));
current_thread_acquired_ = 0;
}
void Mutex::debugPreAcquire() const {
// Check that the mutex is not held by the current thread.
SbThread current_thread = SbThreadGetCurrent();
SB_DCHECK(current_thread_acquired_ != current_thread);
pthread_t current_thread = pthread_self();
SB_DCHECK(!pthread_equal(current_thread_acquired_, current_thread));
}
void Mutex::debugSetAcquired() const {
// Check that the thread has already not been held.
SB_DCHECK(current_thread_acquired_ == kSbThreadInvalid);
current_thread_acquired_ = SbThreadGetCurrent();
SB_DCHECK(current_thread_acquired_ == 0);
current_thread_acquired_ = pthread_self();
}
#else
void Mutex::debugInit() {}
Expand Down
4 changes: 3 additions & 1 deletion starboard/common/mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#ifndef STARBOARD_COMMON_MUTEX_H_
#define STARBOARD_COMMON_MUTEX_H_

#include <pthread.h>

#include "starboard/mutex.h"

namespace starboard {
Expand All @@ -39,7 +41,7 @@ class Mutex {
void debugSetReleased() const;
void debugPreAcquire() const;
void debugSetAcquired() const;
mutable SbThread current_thread_acquired_;
mutable pthread_t current_thread_acquired_;
#else
void debugInit();
void debugSetReleased() const;
Expand Down
17 changes: 8 additions & 9 deletions starboard/common/recursive_mutex.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@

namespace starboard {

RecursiveMutex::RecursiveMutex()
: owner_id_(kSbThreadInvalidId), recurse_count_(0) {}
RecursiveMutex::RecursiveMutex() : owner_id_(0), recurse_count_(0) {}

RecursiveMutex::~RecursiveMutex() {}

void RecursiveMutex::Acquire() {
SbThreadId current_thread = SbThreadGetId();
if (owner_id_ == current_thread) {
pthread_t current_thread = pthread_self();
if (pthread_equal(owner_id_, current_thread)) {
recurse_count_++;
SB_DCHECK(recurse_count_ > 0);
return;
Expand All @@ -35,20 +34,20 @@ void RecursiveMutex::Acquire() {
}

void RecursiveMutex::Release() {
SB_DCHECK(owner_id_ == SbThreadGetId());
if (owner_id_ == SbThreadGetId()) {
SB_DCHECK(pthread_equal(owner_id_, pthread_self()));
if (pthread_equal(owner_id_, pthread_self())) {
SB_DCHECK(0 < recurse_count_);
recurse_count_--;
if (recurse_count_ == 0) {
owner_id_ = kSbThreadInvalidId;
owner_id_ = 0;
mutex_.Release();
}
}
}

bool RecursiveMutex::AcquireTry() {
SbThreadId current_thread = SbThreadGetId();
if (owner_id_ == current_thread) {
pthread_t current_thread = pthread_self();
if (pthread_equal(owner_id_, current_thread)) {
recurse_count_++;
SB_DCHECK(recurse_count_ > 0);
return true;
Expand Down
2 changes: 1 addition & 1 deletion starboard/common/recursive_mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class RecursiveMutex {

private:
Mutex mutex_;
SbThreadId owner_id_;
pthread_t owner_id_;
// Only the owner is able to modify recurse_count_.
size_t recurse_count_;

Expand Down
6 changes: 5 additions & 1 deletion starboard/common/thread_collision_warner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "starboard/common/thread_collision_warner.h"

#include <pthread.h>

#include "starboard/atomic.h"
#include "starboard/common/log.h"
#include "starboard/thread.h"
Expand Down Expand Up @@ -34,7 +36,9 @@ ThreadCollisionWarner::Check::Check(ThreadCollisionWarner* warner)
ThreadCollisionWarner::Check::~Check() {}

static SbAtomic32 CurrentThread() {
const SbThreadId current_thread_id = SbThreadGetId();
uintptr_t current_thread_id =
reinterpret_cast<uintptr_t>(reinterpret_cast<void*>(pthread_self()));

// We need to get the thread id into an atomic data type. This might be a
// truncating conversion, but any loss-of-information just increases the
// chance of a false negative, not a false positive.
Expand Down

0 comments on commit d39c00a

Please sign in to comment.