Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cherry pick PR #3113: Migrate starboard/common/thread to pthread #3129

Merged
merged 1 commit into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading