Skip to content

Commit

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

Change-Id: I5007443fcfe09121255f9949e003ed40c8e3e2a2
  • Loading branch information
y4vor committed May 14, 2024
1 parent 71e2175 commit 84723fa
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 12 deletions.
8 changes: 6 additions & 2 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,11 @@ 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.
#if SB_API_VERSION < 16
SbThreadSampler sampler = SbThreadSamplerCreate(kSbThreadInvalid);
#else
SbThreadSampler sampler = SbThreadSamplerCreate(0);
#endif
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_
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
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 84723fa

Please sign in to comment.