Skip to content

Commit

Permalink
Migrate Cobalt Core to pthread (#3236)
Browse files Browse the repository at this point in the history
Test-On-Device: true
b/302335657

Change-Id: Ida8c88f82a720da6e7c0871336b32005072846c7
  • Loading branch information
y4vor authored May 14, 2024
1 parent 3ea1999 commit 286e132
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 31 deletions.
11 changes: 6 additions & 5 deletions cobalt/base/log_message_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@

#include "cobalt/base/log_message_handler.h"

#include <pthread.h>

#include "base/threading/thread_restrictions.h"
#include "starboard/thread.h"

namespace base {

Expand Down Expand Up @@ -67,14 +68,14 @@ bool LogMessageHandler::OnLogMessage(int severity, const char* file, int line,
}

// Ignore recursive calls.
static SbThreadId logging_thread = kSbThreadInvalidId;
if (logging_thread == SbThreadGetId()) {
static pthread_t logging_thread = 0;
if (pthread_equal(logging_thread, pthread_self())) {
return false;
}

LogMessageHandler* instance = GetInstance();
AutoLock auto_lock(instance->lock_);
logging_thread = SbThreadGetId();
logging_thread = pthread_self();

bool suppress = instance->suppress_log_output_;
for (CallbackMap::const_iterator it = instance->callbacks_.begin();
Expand All @@ -84,7 +85,7 @@ bool LogMessageHandler::OnLogMessage(int severity, const char* file, int line,
}
}

logging_thread = kSbThreadInvalidId;
logging_thread = 0;
return suppress;
}

Expand Down
10 changes: 5 additions & 5 deletions cobalt/dom/serialized_algorithm_runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class SerializedAlgorithmRunner {
: synchronization_required_(synchronization_required), mutex_(mutex) {
if (synchronization_required_) {
// Crash if we are trying to re-acquire again on the same thread.
CHECK_NE(acquired_thread_id_, SbThreadGetId());
CHECK(!pthread_equal(acquired_thread_id_, pthread_self()));

int64_t start_usec = starboard::CurrentMonotonicTime();
int64_t wait_interval_usec =
Expand All @@ -112,21 +112,21 @@ class SerializedAlgorithmRunner {
CHECK_LT(starboard::CurrentMonotonicTime() - start_usec,
1 * base::Time::kMicrosecondsPerSecond);
}
acquired_thread_id_ = SbThreadGetId();
acquired_thread_id_ = pthread_self();
}
}
~ScopedLockWhenRequired() {
if (synchronization_required_) {
CHECK_EQ(acquired_thread_id_, SbThreadGetId());
acquired_thread_id_ = kSbThreadInvalidId;
CHECK(pthread_equal(acquired_thread_id_, pthread_self()));
acquired_thread_id_ = 0;
mutex_.Release();
}
}

private:
const bool synchronization_required_;
const starboard::Mutex& mutex_;
SbThreadId acquired_thread_id_ = kSbThreadInvalidId;
pthread_t acquired_thread_id_ = 0;
};

Handle(bool synchronization_required,
Expand Down
9 changes: 5 additions & 4 deletions cobalt/loader/fetcher_cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ FetcherCache::FetcherCache(const char* name, size_t capacity)

FetcherCache::~FetcherCache() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
CHECK_EQ(thread_id_, SbThreadGetId());
CHECK(pthread_equal(thread_id_, pthread_self()));
CHECK(destroy_soon_called_);

while (!cache_entries_.empty()) {
Expand All @@ -273,7 +273,7 @@ Loader::FetcherCreator FetcherCache::GetFetcherCreator(

void FetcherCache::NotifyResourceRequested(const std::string& url) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
CHECK_EQ(thread_id_, SbThreadGetId());
CHECK(pthread_equal(thread_id_, pthread_self()));

auto iter = cache_entries_.find(url);
if (iter != cache_entries_.end()) {
Expand Down Expand Up @@ -301,7 +301,7 @@ std::unique_ptr<Fetcher> FetcherCache::CreateCachedFetcher(
#if !defined(COBALT_BUILD_TYPE_GOLD)
CHECK(!destroy_soon_called_);
#endif // !defined(COBALT_BUILD_TYPE_GOLD)
CHECK_EQ(thread_id_, SbThreadGetId());
CHECK(pthread_equal(thread_id_, pthread_self()));

auto iterator = cache_entries_.find(url.spec());
if (iterator != cache_entries_.end()) {
Expand Down Expand Up @@ -330,7 +330,8 @@ void FetcherCache::OnFetchSuccess(
#if !defined(COBALT_BUILD_TYPE_GOLD)
CHECK(!destroy_soon_called_);
#endif // !defined(COBALT_BUILD_TYPE_GOLD)
CHECK_EQ(thread_id_, SbThreadGetId());

CHECK(pthread_equal(thread_id_, pthread_self()));

if (data.capacity() > capacity_) {
return;
Expand Down
8 changes: 5 additions & 3 deletions cobalt/loader/fetcher_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#ifndef COBALT_LOADER_FETCHER_CACHE_H_
#define COBALT_LOADER_FETCHER_CACHE_H_

#include <pthread.h>

#include <atomic>
#include <memory>
#include <string>
Expand Down Expand Up @@ -44,12 +46,12 @@ class FetcherCache : public base::RefCountedThreadSafe<FetcherCache> {
void NotifyResourceRequested(const std::string& url);
size_t size() const {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
CHECK_EQ(thread_id_, SbThreadGetId());
CHECK(pthread_equal(thread_id_, pthread_self()));
return total_size_;
}
size_t capacity() const {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
CHECK_EQ(thread_id_, SbThreadGetId());
CHECK(pthread_equal(thread_id_, pthread_self()));
return capacity_;
}

Expand All @@ -75,7 +77,7 @@ class FetcherCache : public base::RefCountedThreadSafe<FetcherCache> {

// TODO(b/270993319): For debugging cache integrity issues in production only,
// remove after identifying the root cause.
const SbThreadId thread_id_ = SbThreadGetId();
const pthread_t thread_id_ = pthread_self();
std::atomic_bool destroy_soon_called_{false};

const size_t capacity_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@

#include "absl/base/internal/sysinfo.h"

#include <pthread.h>

#include "absl/base/attributes.h"

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

namespace absl {
ABSL_NAMESPACE_BEGIN
Expand All @@ -37,7 +38,7 @@ int NumCPUs() {

// Return a per-thread small integer ID from pthread's thread-specific data.
pid_t GetTID() {
return SbThreadGetId();
return reinterpret_cast<uintptr_t>(reinterpret_cast<void*>(pthread_self()));
}

// GetCachedTID() caches the thread ID in thread-local storage (which is a
Expand Down
5 changes: 1 addition & 4 deletions third_party/skia/src/core/SkThreadID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@

#include "include/private/SkThreadID.h"

#if defined(STARBOARD)
#include "starboard/thread.h"
SkThreadID SkGetThreadID() { return SbThreadGetId(); }
#elif defined(SK_BUILD_FOR_WIN)
#if defined(SK_BUILD_FOR_WIN)
#include "src/core/SkLeanWindows.h"
SkThreadID SkGetThreadID() { return GetCurrentThreadId(); }
#else
Expand Down
24 changes: 16 additions & 8 deletions v8/src/base/platform/platform-starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,9 @@ int OS::GetCurrentProcessId() {
return 0;
}

int OS::GetCurrentThreadId() { return SbThreadGetId(); }
int OS::GetCurrentThreadId() {
return reinterpret_cast<uintptr_t>(reinterpret_cast<void*>(pthread_self()));
}

int OS::GetLastError() { return SbSystemGetLastError(); }

Expand Down Expand Up @@ -363,8 +365,8 @@ void OS::StrNCpy(char* dest, int length, const char* src, size_t n) {

class Thread::PlatformData {
public:
PlatformData() : thread_(kSbThreadInvalid) {}
SbThread thread_; // Thread handle for pthread.
PlatformData() : thread_(0) {}
pthread_t thread_; // Thread handle for pthread.
// Synchronizes thread creation
Mutex thread_creation_mutex_;
};
Expand Down Expand Up @@ -399,13 +401,19 @@ void Thread::set_name(const char* name) {
}

bool Thread::Start() {
data_->thread_ =
SbThreadCreate(stack_size_, kSbThreadNoPriority, kSbThreadNoAffinity,
true, name_, ThreadEntry, this);
return SbThreadIsValid(data_->thread_);
pthread_attr_t attr;
if (pthread_attr_init(&attr) != 0) {
return false;
}
pthread_attr_setstacksize(&attr, stack_size_);
pthread_create(&data_->thread_, &attr, ThreadEntry, this);

pthread_attr_destroy(&attr);

return data_->thread_ != 0;
}

void Thread::Join() { SbThreadJoin(data_->thread_, nullptr); }
void Thread::Join() { pthread_join(data_->thread_, nullptr); }

Thread::LocalStorageKey Thread::CreateThreadLocalKey() {
pthread_key_t key = 0;
Expand Down

0 comments on commit 286e132

Please sign in to comment.