Commit 8f741e7b authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

Add a std::condition_variable compatibility layer

parent 82a099af
......@@ -6,6 +6,9 @@ AM_CPPFLAGS = -Wall -Wsign-compare -Wextra -Wstrict-aliasing -Wstrict-overflow \
-pipe
MEDIALIB_CPPFLAGS = $(AM_CPPFLAGS) -I$(top_srcdir)/include -I$(top_srcdir)/src
if HAVE_WIN32
MEDIALIB_CPPFLAGS += -D_WIN32_WINNT=0x600
endif
libmedialibrary_ladir = $(includedir)/medialibrary
......@@ -147,6 +150,7 @@ noinst_HEADERS = \
src/VideoTrack.h \
src/compat/Thread.h \
src/compat/Mutex.h \
src/compat/ConditionVariable.h \
$(NULL)
......
/*****************************************************************************
* Media Library
*****************************************************************************
* Copyright (C) 2016 Hugo Beauzée-Luyssen, Videolabs
*
* Authors: Hugo Beauzée-Luyssen<hugo@beauzee.fr>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#pragma once
// Unconditionaly include <mutex> for std::unique_lock
#include <mutex>
#include "compat/Mutex.h"
#if CXX11_THREADS
#include <condition_variable>
namespace medialibrary
{
namespace compat
{
using ConditionVariable = std::condition_variable;
}
}
#else
#include <windows.h>
namespace medialibrary
{
namespace compat
{
class ConditionVariable
{
public:
using native_handle_type = PCONDITION_VARIABLE;
ConditionVariable()
{
InitializeConditionVariable( &m_cond );
}
void notify_one() noexcept
{
WakeConditionVariable( &m_cond );
}
void notify_all() noexcept
{
WakeAllConditionVariable( &m_cond );
}
void wait( std::unique_lock<Mutex>& lock )
{
SleepConditionVariableCS( &m_cond, lock.mutex()->native_handle(), INFINITE );
}
template <typename Pred>
void wait( std::unique_lock<Mutex>& lock, Pred pred )
{
while ( pred() == false )
SleepConditionVariableCS( &m_cond, lock.mutex()->native_handle(), INFINITE );
}
template <typename Rep, typename Period, typename Pred>
bool wait_for( std::unique_lock<Mutex>& lock, const std::chrono::duration<Rep, Period>& relTime, Pred pred )
{
auto timeout = std::chrono::duration_cast<std::chrono::milliseconds>( relTime );
while ( pred() == false )
{
auto now = std::chrono::system_clock::now();
if ( SleepConditionVariableCS( &m_cond, lock.mutex()->native_handle(), timeout.count() ) != 0 )
{
auto res = GetLastError();
if ( res == ERROR_TIMEOUT )
return false;
throw std::system_error{ std::make_error_code( std::errc::resource_unavailable_try_again ) };
}
timeout -= std::chrono::duration_cast<std::chrono::milliseconds>( std::chrono::system_clock::now() - now );
}
return true;
}
template <typename Clock, typename Period, typename Pred>
bool wait_until( std::unique_lock<Mutex>& lock, const std::chrono::time_point<Clock, Period>& relTime, Pred&& pred )
{
auto timeout = relTime - std::chrono::steady_clock::now();
return wait_for( lock, timeout, std::forward<Pred>( pred ) );
}
native_handle_type native_handle()
{
return &m_cond;
}
private:
CONDITION_VARIABLE m_cond;
};
}
}
#endif
......@@ -26,7 +26,7 @@
#include <functional>
#include <memory>
#include <sqlite3.h>
#include <condition_variable>
#include "compat/ConditionVariable.h"
#include <unordered_map>
#include <string>
......
......@@ -23,7 +23,7 @@
#pragma once
#include <atomic>
#include <condition_variable>
#include "compat/ConditionVariable.h"
#include <memory>
#include <queue>
#include <string>
......@@ -65,7 +65,7 @@ private:
compat::Thread m_thread;
std::queue<Task> m_tasks;
compat::Mutex m_mutex;
std::condition_variable m_cond;
compat::ConditionVariable m_cond;
std::atomic_bool m_run;
std::vector<std::unique_ptr<IDiscoverer>> m_discoverers;
IMediaLibraryCb* m_cb;
......
......@@ -23,7 +23,7 @@
#ifndef VLCMETADATASERVICE_H
#define VLCMETADATASERVICE_H
#include <condition_variable>
#include "compat/ConditionVariable.h"
#include <vlcpp/vlc.hpp>
#include <mutex>
......@@ -48,7 +48,7 @@ private:
private:
VLC::Instance m_instance;
compat::Mutex m_mutex;
std::condition_variable m_cond;
compat::ConditionVariable m_cond;
};
}
......
......@@ -22,7 +22,7 @@
#pragma once
#include <condition_variable>
#include "compat/ConditionVariable.h"
#include <vlcpp/vlc.hpp>
......@@ -58,7 +58,7 @@ private:
private:
VLC::Instance m_instance;
compat::Mutex m_mutex;
std::condition_variable m_cond;
compat::ConditionVariable m_cond;
std::unique_ptr<IImageCompressor> m_compressor;
// Per thumbnail variables
std::unique_ptr<uint8_t[]> m_buff;
......
......@@ -23,7 +23,7 @@
#pragma once
#include <atomic>
#include <condition_variable>
#include "compat/ConditionVariable.h"
#include <queue>
#include "Task.h"
......@@ -83,7 +83,7 @@ private:
IParserCb* m_parserCb;
bool m_stopParser;
bool m_paused;
std::condition_variable m_cond;
compat::ConditionVariable m_cond;
std::queue<std::unique_ptr<parser::Task>> m_tasks;
std::vector<compat::Thread> m_threads;
compat::Mutex m_lock;
......
......@@ -23,7 +23,7 @@
#pragma once
#include <atomic>
#include <condition_variable>
#include "compat/ConditionVariable.h"
#include <functional>
#include <vector>
#include <chrono>
......@@ -154,7 +154,7 @@ private:
// Notifier thread
compat::Mutex m_lock;
std::condition_variable m_cond;
compat::ConditionVariable m_cond;
compat::Thread m_notifierThread;
std::atomic_bool m_stop;
std::chrono::time_point<std::chrono::steady_clock> m_timeout;
......
......@@ -22,7 +22,7 @@
#pragma once
#include <condition_variable>
#include "compat/ConditionVariable.h"
#include "compat/Mutex.h"
#include <atomic>
......@@ -85,7 +85,7 @@ public:
}
private:
std::condition_variable m_writeDoneCond;
compat::ConditionVariable m_writeDoneCond;
compat::Mutex m_lock;
unsigned int m_nbReader;
unsigned int m_nbReaderWaiting;
......
......@@ -75,7 +75,7 @@ public:
private:
std::atomic_bool m_done;
std::atomic_bool m_waitingReload;
std::condition_variable m_cond;
compat::ConditionVariable m_cond;
compat::Mutex m_mutex;
};
......
......@@ -48,7 +48,7 @@ private:
virtual void onDiscoveryCompleted( const std::string& ) override;
virtual void onParsingStatsUpdated(uint32_t percent) override;
std::condition_variable m_parsingCompleteVar;
compat::ConditionVariable m_parsingCompleteVar;
compat::Mutex m_parsingMutex;
bool m_done;
bool m_discoveryCompleted;
......
......@@ -55,7 +55,7 @@ public:
private:
std::mutex m_lock;
std::condition_variable m_cond;
compat::ConditionVariable m_cond;
uint32_t m_nbMedia;
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment