Skip to content
Snippets Groups Projects
Commit 7f972e43 authored by Guillaume Roguez's avatar Guillaume Roguez
Browse files

daemon: use C++11 std thread than pthread

Refs #61640

Change-Id: Ib33e915fbdd7596c4c17320659bfd8abbd4515b0
parent cac91e03
Branches
Tags
No related merge requests found
...@@ -38,6 +38,9 @@ ...@@ -38,6 +38,9 @@
#include <algorithm> #include <algorithm>
#include <string> #include <string>
#include <iostream> #include <iostream>
#include <thread>
#include <mutex>
#include <exception>
std::map<std::string, std::string> encoders_; std::map<std::string, std::string> encoders_;
std::vector<std::string> installed_video_codecs_; std::vector<std::string> installed_video_codecs_;
...@@ -68,28 +71,29 @@ std::vector<std::string> getVideoCodecList() ...@@ -68,28 +71,29 @@ std::vector<std::string> getVideoCodecList()
return installed_video_codecs_; return installed_video_codecs_;
} }
// protect libav/ffmpeg access with pthreads // protect libav/ffmpeg access
static int static int
avcodecManageMutex(void **data, enum AVLockOp op) avcodecManageMutex(void **data, enum AVLockOp op)
{ {
pthread_mutex_t **mutex = reinterpret_cast<pthread_mutex_t**>(data); auto mutex = reinterpret_cast<std::mutex**>(data);
int ret = 0; int ret = 0;
switch (op) { switch (op) {
case AV_LOCK_CREATE: case AV_LOCK_CREATE:
*mutex = static_cast<pthread_mutex_t*>(av_malloc(sizeof(pthread_mutex_t))); try {
if (!*mutex) *mutex = new std::mutex;
} catch (const std::bad_alloc& e) {
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
ret = pthread_mutex_init(*mutex, NULL); }
break; break;
case AV_LOCK_OBTAIN: case AV_LOCK_OBTAIN:
ret = pthread_mutex_lock(*mutex); (*mutex)->lock();
break; break;
case AV_LOCK_RELEASE: case AV_LOCK_RELEASE:
ret = pthread_mutex_unlock(*mutex); (*mutex)->unlock();
break; break;
case AV_LOCK_DESTROY: case AV_LOCK_DESTROY:
ret = pthread_mutex_destroy(*mutex); delete *mutex;
av_freep(mutex); *mutex = nullptr;
break; break;
default: default:
#ifdef AVERROR_BUG #ifdef AVERROR_BUG
...@@ -145,11 +149,11 @@ static void init_once() ...@@ -145,11 +149,11 @@ static void init_once()
findInstalledVideoCodecs(); findInstalledVideoCodecs();
} }
static pthread_once_t already_called = PTHREAD_ONCE_INIT; static std::once_flag already_called;
void sfl_avcodec_init() void sfl_avcodec_init()
{ {
(void) pthread_once(&already_called, init_once); std::call_once(already_called, init_once);
} }
std::vector<std::map<std::string, std::string> > std::vector<std::map<std::string, std::string> >
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment