Skip to content

Commit

Permalink
Migrate the SbThreadSampler to pthread (#3238)
Browse files Browse the repository at this point in the history
b/340535150
Test-On-Device: true

Change-Id: I5007443fcfe09121255f9949e003ed40c8e3e2a2
  • Loading branch information
y4vor authored May 15, 2024
1 parent 2519c29 commit 597c7d1
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 14 deletions.
3 changes: 3 additions & 0 deletions starboard/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ since the version previous to it.

## Version 16

### Migrate the `SbThreadSampler` to use `pthread`.
Switched the `SbThreadSampler` API to use `pthread` instead of `SbThread`.

### Added support for pthread create attributes.
The standard pthread APIs `pthread_attr_init`, `pthread_attr_destroy`,
`pthread_attr_getdetachstate`, `pthread_attr_getstacksize`,
Expand Down
6 changes: 3 additions & 3 deletions starboard/nplb/thread_sampler_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
#include "starboard/common/atomic.h"
#include "starboard/common/log.h"
#include "starboard/common/time.h"
#include "starboard/nplb/thread_helpers.h"
#include "starboard/nplb/posix_compliance/posix_thread_helpers.h"
#include "starboard/thread.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace starboard {
namespace nplb {
namespace {

class CountingThread : public AbstractTestThread {
class CountingThread : public posix::AbstractTestThread {
public:
~CountingThread() { Stop(); }

Expand Down Expand Up @@ -64,7 +64,7 @@ TEST(ThreadSamplerTest, RainyDayCreateSamplerInvalidThread) {
// Creating a sampler for an invalid thread should not succeed, and even
// without without calling |SbThreadSamplerDelete| ASAN should not detect a
// memory leak.
SbThreadSampler sampler = SbThreadSamplerCreate(kSbThreadInvalid);
SbThreadSampler sampler = SbThreadSamplerCreate(0);
EXPECT_FALSE(SbThreadSamplerIsValid(sampler));
}

Expand Down
4 changes: 2 additions & 2 deletions starboard/shared/pthread/thread_sampler_create.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

#include "starboard/shared/pthread/thread_sampler_internal.h"

SbThreadSampler SbThreadSamplerCreate(SbThread thread) {
if (!SbThreadIsValid(thread)) {
SbThreadSampler SbThreadSamplerCreate(pthread_t thread) {
if (thread == 0) {
return kSbThreadSamplerInvalid;
}
return new SbThreadSamplerPrivate(thread);
Expand Down
3 changes: 2 additions & 1 deletion starboard/shared/pthread/thread_sampler_internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "starboard/shared/pthread/thread_sampler_internal.h"

#include <pthread.h>
#include <semaphore.h>
#include <signal.h>

Expand Down Expand Up @@ -129,7 +130,7 @@ void SignalHandler::HandleProfilerSignal(int signal,

} // namespace

SbThreadSamplerPrivate::SbThreadSamplerPrivate(SbThread thread)
SbThreadSamplerPrivate::SbThreadSamplerPrivate(pthread_t thread)
: thread_(thread) {
SignalHandler::AddSampler();
}
Expand Down
9 changes: 5 additions & 4 deletions starboard/shared/pthread/thread_sampler_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,21 @@
#ifndef STARBOARD_SHARED_PTHREAD_THREAD_SAMPLER_INTERNAL_H_
#define STARBOARD_SHARED_PTHREAD_THREAD_SAMPLER_INTERNAL_H_

#include <pthread.h>

#include "starboard/thread.h"

class SbThreadSamplerPrivate {
public:
explicit SbThreadSamplerPrivate(SbThread thread);
explicit SbThreadSamplerPrivate(pthread_t thread);
~SbThreadSamplerPrivate();

SbThreadContext Freeze();
bool Thaw();

SbThread thread() { return thread_; }
pthread_t thread() { return thread_; }

private:
SbThread thread_;
pthread_t thread_;
};

#endif // STARBOARD_SHARED_PTHREAD_THREAD_SAMPLER_INTERNAL_H_
2 changes: 1 addition & 1 deletion starboard/shared/stub/thread_sampler_create.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "starboard/common/log.h"
#include "starboard/thread.h"

SbThreadSampler SbThreadSamplerCreate(SbThread thread) {
SbThreadSampler SbThreadSamplerCreate(pthread_t thread) {
SB_NOTIMPLEMENTED() << "Profiling is not supported on this platform.";
return kSbThreadSamplerInvalid;
}
4 changes: 3 additions & 1 deletion starboard/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#ifndef STARBOARD_THREAD_H_
#define STARBOARD_THREAD_H_

#include <pthread.h>

#include "starboard/configuration.h"
#include "starboard/export.h"
#include "starboard/types.h"
Expand Down Expand Up @@ -333,7 +335,7 @@ SB_EXPORT bool SbThreadSamplerIsSupported();
//
// If successful, this function returns the newly created handle.
// If unsuccessful, this function returns |kSbThreadSamplerInvalid|.
SB_EXPORT SbThreadSampler SbThreadSamplerCreate(SbThread thread);
SB_EXPORT SbThreadSampler SbThreadSamplerCreate(pthread_t thread);

// Destroys the |sampler| and frees whatever resources it was using.
SB_EXPORT void SbThreadSamplerDestroy(SbThreadSampler sampler);
Expand Down
2 changes: 2 additions & 0 deletions third_party/musl/src/include/pthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "../../include/pthread.h"

#if !defined(STARBOARD)
hidden int __pthread_once(pthread_once_t *, void (*)(void));
hidden void __pthread_testcancel(void);
hidden int __pthread_setcancelstate(int, int *);
Expand All @@ -25,5 +26,6 @@ hidden int __pthread_rwlock_wrlock(pthread_rwlock_t *);
hidden int __pthread_rwlock_trywrlock(pthread_rwlock_t *);
hidden int __pthread_rwlock_timedwrlock(pthread_rwlock_t *__restrict, const struct timespec *__restrict);
hidden int __pthread_rwlock_unlock(pthread_rwlock_t *);
#endif

#endif
4 changes: 2 additions & 2 deletions v8/src/libsampler/sampler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ namespace sampler {
class Sampler::PlatformData {
public:
PlatformData()
: thread_(SbThreadGetCurrent()),
: thread_(pthread_self()),
thread_sampler_(kSbThreadSamplerInvalid) {}
~PlatformData() { ReleaseThreadSampler(); }

Expand All @@ -195,7 +195,7 @@ class Sampler::PlatformData {
}

private:
SbThread thread_;
pthread_t thread_;
SbThreadSampler thread_sampler_;
};

Expand Down

0 comments on commit 597c7d1

Please sign in to comment.