Skip to content

Commit

Permalink
Migrate trivial pthread_mutex calls (#3157)
Browse files Browse the repository at this point in the history
b/302335657

Change-Id: I6c29de7e1ae164da75c27c127a76b69f2bf11f4b
(cherry picked from commit b81741d)
  • Loading branch information
y4vor authored and anonymous1-me committed May 3, 2024
1 parent ad56050 commit bd5c2de
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
#ifndef COBALT_RENDERER_RASTERIZER_SKIA_SKIA_SRC_PORTS_SKMUTEX_STARBOARD_H_
#define COBALT_RENDERER_RASTERIZER_SKIA_SKIA_SRC_PORTS_SKMUTEX_STARBOARD_H_

#include "starboard/common/mutex.h"
#include <pthread.h>

#include "starboard/thread.h"

// A Starboard-based implementation of mutex, with support for static
Expand All @@ -27,18 +28,18 @@
// generation of a static initializer/finalizer.
struct SkBaseMutex {
void acquire() {
SbMutexAcquire(&mutex_);
pthread_mutex_lock(&mutex_);
SetAcquired();
}

void release() {
SetReleased();
SbMutexRelease(&mutex_);
pthread_mutex_unlock(&mutex_);
}

void assertHeld() { AssertAcquired(); }

SbMutex mutex_;
pthread_mutex_t mutex_;

#ifdef SK_DEBUG
void Init() {
Expand Down Expand Up @@ -81,9 +82,9 @@ class SkMutex : public SkBaseMutex {
public:
SkMutex() {
Init();
SbMutexCreate(&mutex_);
pthread_mutex_init(&mutex_, nullptr);
}
~SkMutex() { SbMutexDestroy(&mutex_); }
~SkMutex() { pthread_mutex_destroy(&mutex_); }
};

#define SK_BASE_MUTEX_INIT \
Expand Down
2 changes: 1 addition & 1 deletion glimp/egl/scoped_egl_lock.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
namespace glimp {
namespace egl {

SbMutex ScopedEGLLock::mutex_ = SB_MUTEX_INITIALIZER;
pthread_mutex_t ScopedEGLLock::mutex_ = PTHREAD_MUTEX_INITIALIZER;

} // namespace egl
} // namespace glimp
8 changes: 4 additions & 4 deletions glimp/egl/scoped_egl_lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@
#ifndef GLIMP_EGL_SCOPED_EGL_LOCK_H_
#define GLIMP_EGL_SCOPED_EGL_LOCK_H_

#include "starboard/common/mutex.h"
#include <pthread.h>

namespace glimp {
namespace egl {

// A helper class to enable easy locking of the glimp EGL global mutex.
class ScopedEGLLock {
public:
ScopedEGLLock() { SbMutexAcquire(&mutex_); }
~ScopedEGLLock() { SbMutexRelease(&mutex_); }
ScopedEGLLock() { pthread_mutex_lock(&mutex_); }
~ScopedEGLLock() { pthread_mutex_unlock(&mutex_); }

private:
static SbMutex mutex_;
static pthread_mutex_t mutex_;
};

} // namespace egl
Expand Down
21 changes: 10 additions & 11 deletions starboard/android/shared/android_media_session_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "starboard/android/shared/jni_env_ext.h"
#include "starboard/android/shared/jni_utils.h"
#include "starboard/common/log.h"
#include "starboard/common/mutex.h"

namespace starboard {
namespace android {
Expand Down Expand Up @@ -120,7 +119,7 @@ CobaltExtensionMediaSessionAction PlaybackStateActionToMediaSessionAction(
}

pthread_once_t once_flag = PTHREAD_ONCE_INIT;
SbMutex mutex;
pthread_mutex_t mutex;

// Callbacks to the last MediaSessionClient to become active, or null.
// Used to route Java callbacks.
Expand All @@ -132,12 +131,12 @@ CobaltExtensionMediaSessionInvokeActionCallback g_invoke_action_callback = NULL;
void* g_callback_context = NULL;

void OnceInit() {
SbMutexCreate(&mutex);
pthread_mutex_init(&mutex, nullptr);
}

void NativeInvokeAction(jlong action, jlong seek_ms) {
pthread_once(&once_flag, OnceInit);
SbMutexAcquire(&mutex);
pthread_mutex_lock(&mutex);

if (g_invoke_action_callback != NULL && g_callback_context != NULL) {
CobaltExtensionMediaSessionActionDetails details = {};
Expand All @@ -150,20 +149,20 @@ void NativeInvokeAction(jlong action, jlong seek_ms) {
g_invoke_action_callback(details, g_callback_context);
}

SbMutexRelease(&mutex);
pthread_mutex_unlock(&mutex);
}

void UpdateActiveSessionPlatformPlaybackState(
CobaltExtensionMediaSessionPlaybackState state) {
pthread_once(&once_flag, OnceInit);
SbMutexAcquire(&mutex);
pthread_mutex_lock(&mutex);

if (g_update_platform_playback_state_callback != NULL &&
g_callback_context != NULL) {
g_update_platform_playback_state_callback(state, g_callback_context);
}

SbMutexRelease(&mutex);
pthread_mutex_unlock(&mutex);
}

void OnMediaSessionStateChanged(
Expand Down Expand Up @@ -248,25 +247,25 @@ void RegisterMediaSessionCallbacks(
CobaltExtensionMediaSessionUpdatePlatformPlaybackStateCallback
update_platform_playback_state_callback) {
pthread_once(&once_flag, OnceInit);
SbMutexAcquire(&mutex);
pthread_mutex_lock(&mutex);

g_callback_context = callback_context;
g_invoke_action_callback = invoke_action_callback;
g_update_platform_playback_state_callback =
update_platform_playback_state_callback;

SbMutexRelease(&mutex);
pthread_mutex_unlock(&mutex);
}

void DestroyMediaSessionClientCallback() {
pthread_once(&once_flag, OnceInit);
SbMutexAcquire(&mutex);
pthread_mutex_lock(&mutex);

g_callback_context = NULL;
g_invoke_action_callback = NULL;
g_update_platform_playback_state_callback = NULL;

SbMutexRelease(&mutex);
pthread_mutex_unlock(&mutex);

JniEnvExt* env = JniEnvExt::Get();
env->CallStarboardVoidMethodOrAbort("deactivateMediaSession", "()V");
Expand Down
9 changes: 5 additions & 4 deletions starboard/elf_loader/elf_loader_sandbox.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <pthread.h>

#include <string>

#include "starboard/common/log.h"
Expand All @@ -21,7 +23,6 @@
#include "starboard/elf_loader/evergreen_info.h"
#include "starboard/elf_loader/sabi_string.h"
#include "starboard/event.h"
#include "starboard/mutex.h"
#include "starboard/shared/starboard/command_line.h"
#include "starboard/string.h"
#include "third_party/crashpad/crashpad/wrapper/annotations.h"
Expand Down Expand Up @@ -101,9 +102,9 @@ void LoadLibraryAndInitialize(const std::string& library_path,
}

void SbEventHandle(const SbEvent* event) {
static SbMutex mutex = SB_MUTEX_INITIALIZER;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

SB_CHECK(SbMutexAcquire(&mutex) == kSbMutexAcquired);
SB_CHECK(pthread_mutex_lock(&mutex) == 0);

if (!g_sb_event_func) {
const SbEventStartData* data = static_cast<SbEventStartData*>(event->data);
Expand All @@ -117,5 +118,5 @@ void SbEventHandle(const SbEvent* event) {

g_sb_event_func(event);

SB_CHECK(SbMutexRelease(&mutex) == true);
SB_CHECK(pthread_mutex_unlock(&mutex) == 0);
}
14 changes: 7 additions & 7 deletions starboard/shared/ffmpeg/ffmpeg_dispatch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,30 @@

#include "starboard/shared/ffmpeg/ffmpeg_dispatch.h"

#include <pthread.h>

#include "starboard/common/log.h"
#include "starboard/common/mutex.h"
#include "starboard/common/once.h"

namespace starboard {
namespace shared {
namespace ffmpeg {

namespace {
SbMutex g_codec_mutex = SB_MUTEX_INITIALIZER;
pthread_mutex_t g_codec_mutex = PTHREAD_MUTEX_INITIALIZER;
} // namespace

int FFMPEGDispatch::OpenCodec(AVCodecContext* codec_context,
const AVCodec* codec) {
SbMutexAcquire(&g_codec_mutex);
pthread_mutex_lock(&g_codec_mutex);
int result = avcodec_open2(codec_context, codec, NULL);
SbMutexRelease(&g_codec_mutex);
pthread_mutex_unlock(&g_codec_mutex);
return result;
}

void FFMPEGDispatch::CloseCodec(AVCodecContext* codec_context) {
SbMutexAcquire(&g_codec_mutex);
pthread_mutex_lock(&g_codec_mutex);
avcodec_close(codec_context);
SbMutexRelease(&g_codec_mutex);
pthread_mutex_unlock(&g_codec_mutex);
}

void FFMPEGDispatch::FreeFrame(AVFrame** frame) {
Expand Down
12 changes: 6 additions & 6 deletions starboard/shared/ffmpeg/ffmpeg_dynamic_load_dispatch_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class FFMPEGDispatchImpl {
FFMPEGDispatch* get_ffmpeg_dispatch();

private:
SbMutex mutex_;
pthread_mutex_t mutex_;
FFMPEGDispatch* ffmpeg_;
// Load the ffmpeg shared libraries, return true if successful.
bool OpenLibraries();
Expand All @@ -88,7 +88,7 @@ void construct_ffmpeg_dispatch_impl() {
}

FFMPEGDispatchImpl::FFMPEGDispatchImpl()
: mutex_(SB_MUTEX_INITIALIZER),
: mutex_(PTHREAD_MUTEX_INITIALIZER),
ffmpeg_(NULL),
avcodec_(NULL),
avformat_(NULL),
Expand Down Expand Up @@ -119,7 +119,7 @@ bool FFMPEGDispatchImpl::RegisterSpecialization(int specialization,
int avcodec,
int avformat,
int avutil) {
SbMutexAcquire(&mutex_);
pthread_mutex_lock(&mutex_);
auto result = versions_.insert(std::make_pair(
specialization, LibraryMajorVersions(avcodec, avformat, avutil)));
bool success = result.second;
Expand All @@ -131,12 +131,12 @@ bool FFMPEGDispatchImpl::RegisterSpecialization(int specialization,
existing_versions.avformat == avformat &&
existing_versions.avutil == avutil;
}
SbMutexRelease(&mutex_);
pthread_mutex_unlock(&mutex_);
return success;
}

FFMPEGDispatch* FFMPEGDispatchImpl::get_ffmpeg_dispatch() {
SbMutexAcquire(&mutex_);
pthread_mutex_lock(&mutex_);
if (!ffmpeg_) {
ffmpeg_ = new FFMPEGDispatch();
// Dynamically load the libraries and retrieve the function pointers.
Expand All @@ -147,7 +147,7 @@ FFMPEGDispatch* FFMPEGDispatchImpl::get_ffmpeg_dispatch() {
}
}
}
SbMutexRelease(&mutex_);
pthread_mutex_unlock(&mutex_);
return ffmpeg_;
}

Expand Down
20 changes: 10 additions & 10 deletions starboard/shared/uwp/xb1_media_session_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ MediaPlaybackStatus MediaSessionPlaybackStateToMediaPlaybackState(
}

pthread_once_t once_flag = PTHREAD_ONCE_INIT;
SbMutex mutex;
pthread_mutex_t mutex;

// Callbacks to the last MediaSessionClient to become active, or null.
// In practice, only one MediaSessionClient will become active at a time.
Expand All @@ -66,7 +66,7 @@ bool active = false;
bool media_playing = false;

void OnceInit() {
SbMutexCreate(&mutex);
pthread_mutex_init(&mutex, nullptr);
}

void InitButtonCallbackOnce() {
Expand Down Expand Up @@ -125,13 +125,13 @@ void OnMediaSessionStateChanged(
session_state.actual_playback_state;

pthread_once(&once_flag, OnceInit);
SbMutexAcquire(&mutex);
pthread_mutex_lock(&mutex);

InitButtonCallbackOnce();
Platform::Agile<SystemMediaTransportControls> transport_controls =
GetTransportControls();

SbMutexRelease(&mutex);
pthread_mutex_unlock(&mutex);

const bool sessionActive = kCobaltExtensionMediaSessionNone == playback_state;

Expand Down Expand Up @@ -190,36 +190,36 @@ void RegisterMediaSessionCallbacks(
CobaltExtensionMediaSessionUpdatePlatformPlaybackStateCallback
update_platform_playback_state_callback) {
pthread_once(&once_flag, OnceInit);
SbMutexAcquire(&mutex);
pthread_mutex_lock(&mutex);

g_callback_context = callback_context;
g_update_platform_playback_state_callback =
update_platform_playback_state_callback;

SbMutexRelease(&mutex);
pthread_mutex_unlock(&mutex);
}

void DestroyMediaSessionClientCallback() {
pthread_once(&once_flag, OnceInit);
SbMutexAcquire(&mutex);
pthread_mutex_lock(&mutex);

g_callback_context = NULL;
g_update_platform_playback_state_callback = NULL;

SbMutexRelease(&mutex);
pthread_mutex_unlock(&mutex);
}

void UpdateActiveSessionPlatformPlaybackState(
CobaltExtensionMediaSessionPlaybackState state) {
pthread_once(&once_flag, OnceInit);
SbMutexAcquire(&mutex);
pthread_mutex_lock(&mutex);

if (g_update_platform_playback_state_callback != NULL &&
g_callback_context != NULL) {
g_update_platform_playback_state_callback(state, g_callback_context);
}

SbMutexRelease(&mutex);
pthread_mutex_unlock(&mutex);
}
} // namespace

Expand Down
Loading

0 comments on commit bd5c2de

Please sign in to comment.