Skip to content

Commit

Permalink
Migrate starboard/common/thread to pthread (youtube#3113)
Browse files Browse the repository at this point in the history
b/302335657

Change-Id: I2964e3091e169d84f5e69a96f7d2a8af2db6263d
  • Loading branch information
y4vor authored May 1, 2024
1 parent 61caf48 commit 3023362
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 30 deletions.
32 changes: 10 additions & 22 deletions starboard/common/thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
* limitations under the License.
*/

#include <unistd.h>

#include "starboard/common/thread.h"

#include <pthread.h>
#include <unistd.h>

#include "starboard/common/atomic.h"
#include "starboard/common/log.h"
#include "starboard/common/mutex.h"
Expand All @@ -28,18 +29,12 @@ namespace starboard {

struct Thread::Data {
std::string name_;
SbThread thread_ = kSbThreadInvalid;
pthread_t thread_ = 0;
atomic_bool started_;
atomic_bool join_called_;
Semaphore join_sema_;
optional<Thread::Options> options_;
};

Thread::Options::Options()
: stack_size(0), // Signal for default stack size.
priority_(kSbThreadNoPriority),
joinable(true) {}

Thread::Thread(const std::string& name) {
d_.reset(new Thread::Data);
d_->name_ = name;
Expand All @@ -49,21 +44,14 @@ Thread::~Thread() {
SB_DCHECK(d_->join_called_.load()) << "Join not called on thread.";
}

void Thread::Start(const Options& options) {
SbThreadEntryPoint entry_point = ThreadEntryPoint;

void Thread::Start() {
SB_DCHECK(!d_->started_.load());
SB_DCHECK(!d_->options_.has_engaged());
d_->started_.store(true);
d_->options_ = options;

d_->thread_ =
SbThreadCreate(options.stack_size, options.priority_,
kSbThreadNoAffinity, // default affinity.
options.joinable, d_->name_.c_str(), entry_point, this);
pthread_create(&d_->thread_, NULL, ThreadEntryPoint, this);

// SbThreadCreate() above produced an invalid thread handle.
SB_DCHECK(d_->thread_ != kSbThreadInvalid);
// pthread_create() above produced an invalid thread handle.
SB_DCHECK(d_->thread_ != 0);
}

void Thread::Sleep(int64_t microseconds) {
Expand Down Expand Up @@ -92,18 +80,18 @@ starboard::atomic_bool* Thread::joined_bool() {

void* Thread::ThreadEntryPoint(void* context) {
Thread* this_ptr = static_cast<Thread*>(context);
pthread_setname_np(pthread_self(), this_ptr->d_->name_.c_str());
this_ptr->Run();
return NULL;
}

void Thread::Join() {
SB_DCHECK(d_->join_called_.load() == false);
SB_DCHECK(d_->options_->joinable) << "Detached thread should not be joined.";

d_->join_called_.store(true);
d_->join_sema_.Put();

if (!SbThreadJoin(d_->thread_, NULL)) {
if (pthread_join(d_->thread_, NULL) != 0) {
SB_DCHECK(false) << "Could not join thread.";
}
}
Expand Down
9 changes: 1 addition & 8 deletions starboard/common/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,6 @@ class atomic_bool;

class Thread {
public:
struct Options {
Options();
int64_t stack_size;
SbThreadPriority priority_;
bool joinable = true;
};

explicit Thread(const std::string& name);
template <size_t N>
explicit Thread(char const (&name)[N]) : Thread(std::string(name)) {
Expand All @@ -59,7 +52,7 @@ class Thread {

// Called by the main thread, this will cause Run() to be invoked
// on another thread.
virtual void Start(const Options& options = Options());
virtual void Start();
virtual void Join();
bool join_called() const;

Expand Down

0 comments on commit 3023362

Please sign in to comment.