Skip to content

Commit

Permalink
Cherry pick PR #3293: Migrate the SbThreadSampler to pthread (#3308)
Browse files Browse the repository at this point in the history
Refer to the original PR: #3293

Test-On-Device: true
b/340535150

Change-Id: I35a0a3e28304c3bfb7368640bd8589e654d9f588

Co-authored-by: Yavor Goulishev <[email protected]>
  • Loading branch information
cobalt-github-releaser-bot and y4vor authored May 21, 2024
1 parent 6b29508 commit 3a523bb
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 1 deletion.
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
14 changes: 14 additions & 0 deletions starboard/nplb/thread_sampler_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,24 @@
#include "starboard/common/atomic.h"
#include "starboard/common/log.h"
#include "starboard/common/time.h"

#if SB_API_VERSION < 16
#include "starboard/nplb/thread_helpers.h"
#else
#include "starboard/nplb/posix_compliance/posix_thread_helpers.h"
#endif
#include "starboard/thread.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace starboard {
namespace nplb {
namespace {

#if SB_API_VERSION < 16
class CountingThread : public AbstractTestThread {
#else
class CountingThread : public posix::AbstractTestThread {
#endif
public:
~CountingThread() { Stop(); }

Expand Down Expand Up @@ -64,7 +73,12 @@ 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
9 changes: 9 additions & 0 deletions starboard/shared/pthread/thread_sampler_create.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,18 @@

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

#if SB_API_VERSION < 16
SbThreadSampler SbThreadSamplerCreate(SbThread thread) {
if (!SbThreadIsValid(thread)) {
return kSbThreadSamplerInvalid;
}
return new SbThreadSamplerPrivate(thread);
}
#else
SbThreadSampler SbThreadSamplerCreate(pthread_t thread) {
if (thread == 0) {
return kSbThreadSamplerInvalid;
}
return new SbThreadSamplerPrivate(thread);
}
#endif
5 changes: 5 additions & 0 deletions 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,11 @@ void SignalHandler::HandleProfilerSignal(int signal,

} // namespace

#if SB_API_VERSION < 16
SbThreadSamplerPrivate::SbThreadSamplerPrivate(SbThread thread)
#else
SbThreadSamplerPrivate::SbThreadSamplerPrivate(pthread_t thread)
#endif
: thread_(thread) {
SignalHandler::AddSampler();
}
Expand Down
15 changes: 14 additions & 1 deletion starboard/shared/pthread/thread_sampler_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,33 @@
#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:
#if SB_API_VERSION < 16
explicit SbThreadSamplerPrivate(SbThread thread);
#else
explicit SbThreadSamplerPrivate(pthread_t thread);
#endif
~SbThreadSamplerPrivate();

SbThreadContext Freeze();
bool Thaw();

#if SB_API_VERSION < 16
SbThread thread() { return thread_; }
#else
pthread_t thread() { return thread_; }
#endif

private:
#if SB_API_VERSION < 16
SbThread thread_;
#else
pthread_t thread_;
#endif
};

#endif // STARBOARD_SHARED_PTHREAD_THREAD_SAMPLER_INTERNAL_H_
4 changes: 4 additions & 0 deletions starboard/shared/stub/thread_sampler_create.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
#include "starboard/common/log.h"
#include "starboard/thread.h"

#if SB_API_VERSION < 16
SbThreadSampler SbThreadSamplerCreate(SbThread thread) {
#else
SbThreadSampler SbThreadSamplerCreate(pthread_t thread) {
#endif
SB_NOTIMPLEMENTED() << "Profiling is not supported on this platform.";
return kSbThreadSamplerInvalid;
}
6 changes: 6 additions & 0 deletions 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,11 @@ SB_EXPORT bool SbThreadSamplerIsSupported();
//
// If successful, this function returns the newly created handle.
// If unsuccessful, this function returns |kSbThreadSamplerInvalid|.
#if SB_API_VERSION < 16
SB_EXPORT SbThreadSampler SbThreadSamplerCreate(SbThread thread);
#else
SB_EXPORT SbThreadSampler SbThreadSamplerCreate(pthread_t thread);
#endif

// 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
8 changes: 8 additions & 0 deletions v8/src/libsampler/sampler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,11 @@ namespace sampler {
class Sampler::PlatformData {
public:
PlatformData()
#if SB_API_VERSION < 16
: thread_(SbThreadGetCurrent()),
#else
: thread_(pthread_self()),
#endif
thread_sampler_(kSbThreadSamplerInvalid) {}
~PlatformData() { ReleaseThreadSampler(); }

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

private:
#if SB_API_VERSION < 16
SbThread thread_;
#else
pthread_t thread_;
#endif
SbThreadSampler thread_sampler_;
};

Expand Down

0 comments on commit 3a523bb

Please sign in to comment.