Skip to content

Commit

Permalink
Use std::mutex instead of Glib::StaticMutex
Browse files Browse the repository at this point in the history
* Fix compile warning for missing field unused initializer
  • Loading branch information
ma8ma committed Jan 5, 2019
1 parent 0721cf2 commit 18e0bd2
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 88 deletions.
7 changes: 4 additions & 3 deletions src/article/embeddedimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include "articleadmin.h"

#include "jdlib/imgloader.h"
#include "jdlib/jdmutex.h"
#include "jdlib/miscmsg.h"

#include "dbimg/imginterface.h"
Expand All @@ -17,10 +16,12 @@

#include "message/messageadmin.h"

#include <mutex>

//
// スレッドのランチャ
//
static JDLIB::StaticMutex eimg_launcher_mutex = JDLIB_STATIC_MUTEX_INIT;
static std::mutex eimg_launcher_mutex;
int redraw_counter = 0; // 0 になったとき再描画する

void* eimg_launcher( void* dat )
Expand All @@ -29,7 +30,7 @@ void* eimg_launcher( void* dat )

// 遅いCPUの場合は同時に画像をリサイズしようとすると固まった様になるので
// mutexをかけて同時にリサイズしないようにする
JDLIB::LockGuard lock( eimg_launcher_mutex );
std::lock_guard< std::mutex > lock( eimg_launcher_mutex );

#ifdef _DEBUG
std::cout << "start eimg_launcher" << std::endl;
Expand Down
13 changes: 7 additions & 6 deletions src/dispatchmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

#include "dispatchmanager.h"

#include "jdlib/jdmutex.h"
#include "skeleton/dispatchable.h"

#include <mutex>

static JDLIB::StaticMutex dispatch_mutex = JDLIB_STATIC_MUTEX_INIT;

static std::mutex dispatch_mutex;
CORE::DispatchManager* instance_dispmanager = NULL;


Expand Down Expand Up @@ -52,7 +53,7 @@ DispatchManager::~DispatchManager()

void DispatchManager::add( SKELETON::Dispatchable* child )
{
JDLIB::LockGuard lock( dispatch_mutex );
std::lock_guard< std::mutex > lock( dispatch_mutex );

// 既にlistに登録されていたらキャンセルする
std::list< SKELETON::Dispatchable* >::iterator it = m_children.begin();
Expand All @@ -76,7 +77,7 @@ void DispatchManager::add( SKELETON::Dispatchable* child )

void DispatchManager::remove( SKELETON::Dispatchable* child )
{
JDLIB::LockGuard lock( dispatch_mutex );
std::lock_guard< std::mutex > lock( dispatch_mutex );

size_t size = m_children.size();
if( ! size ) return;
Expand All @@ -92,7 +93,7 @@ void DispatchManager::remove( SKELETON::Dispatchable* child )

void DispatchManager::slot_dispatch()
{
JDLIB::UniqueLock lock( dispatch_mutex );
std::unique_lock< std::mutex > lock( dispatch_mutex );

const size_t size = m_children.size();
if( ! size ) return;
Expand All @@ -102,7 +103,7 @@ void DispatchManager::slot_dispatch()
// child->callback_dispatch()の中で再び Dispatchable::add()が呼び出されると
// キャンセルされてしまうので callback_dispatch() を呼び出す前にremoveする
m_children.remove( child );
JDLIB::unique_unlock( lock );
lock.unlock();

if( child ) child->callback_dispatch();

Expand Down
7 changes: 4 additions & 3 deletions src/image/imageareabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@

#include "imageareabase.h"

#include "jdlib/jdmutex.h"
#include "jdlib/miscmsg.h"

#include "dbimg/imginterface.h"
#include "dbimg/img.h"

#include "config/globalconf.h"

#include <mutex>

//
// スレッドのランチャ
//
static JDLIB::StaticMutex imgarea_launcher_mutex = JDLIB_STATIC_MUTEX_INIT;
static std::mutex imgarea_launcher_mutex;

void* imgarea_launcher( void* dat )
{
JDLIB::LockGuard lock( imgarea_launcher_mutex );
std::lock_guard< std::mutex > lock( imgarea_launcher_mutex );

#ifdef _DEBUG
std::cout << "start imgarea_launcher" << std::endl;
Expand Down
10 changes: 5 additions & 5 deletions src/jdlib/imgloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ ImgLoader::~ImgLoader()
Glib::RefPtr< ImgLoader > ImgLoader::get_loader( const std::string& file )
{
ImgProvider& provider = ImgProvider::get_provider();
JDLIB::LockGuard lock( provider.m_provider_lock );
std::lock_guard< std::mutex > lock( provider.m_provider_lock );
Glib::RefPtr< ImgLoader > loader = provider.get_loader( file );
if( ! loader ) {
loader = Glib::RefPtr< ImgLoader >( new ImgLoader( file ) );
Expand All @@ -58,7 +58,7 @@ Glib::RefPtr< ImgLoader > ImgLoader::get_loader( const std::string& file )
// 画像サイズ取得
bool ImgLoader::get_size( int& width, int& height )
{
JDLIB::LockGuard lock( m_loader_lock );
std::lock_guard< std::mutex > lock( m_loader_lock );
bool ret = load_imgfile( LOADLEVEL_SIZEONLY );
width = m_width;
height = m_height;
Expand All @@ -68,7 +68,7 @@ bool ImgLoader::get_size( int& width, int& height )
Glib::RefPtr< Gdk::Pixbuf > ImgLoader::get_pixbuf( const bool pixbufonly )
{
Glib::RefPtr< Gdk::Pixbuf > ret;
JDLIB::LockGuard lock( m_loader_lock );
std::lock_guard< std::mutex > lock( m_loader_lock );
if( load_imgfile( pixbufonly ? LOADLEVEL_PIXBUFONLY : LOADLEVEL_NORMAL ) ) {
ret = m_loader->get_pixbuf();
}
Expand All @@ -78,7 +78,7 @@ Glib::RefPtr< Gdk::Pixbuf > ImgLoader::get_pixbuf( const bool pixbufonly )
Glib::RefPtr< Gdk::PixbufAnimation > ImgLoader::get_animation()
{
Glib::RefPtr< Gdk::PixbufAnimation > ret;
JDLIB::LockGuard lock( m_loader_lock );
std::lock_guard< std::mutex > lock( m_loader_lock );
if( load_imgfile( LOADLEVEL_NORMAL ) ) {
ret = m_loader->get_animation();
}
Expand All @@ -89,7 +89,7 @@ Glib::RefPtr< Gdk::PixbufAnimation > ImgLoader::get_animation()
// 動画でpixbufonly = true の時はアニメーションさせない
bool ImgLoader::load( const bool pixbufonly )
{
JDLIB::LockGuard lock( m_loader_lock );
std::lock_guard< std::mutex > lock( m_loader_lock );
return load_imgfile( pixbufonly ? LOADLEVEL_PIXBUFONLY : LOADLEVEL_NORMAL );
}

Expand Down
6 changes: 3 additions & 3 deletions src/jdlib/imgloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#include <gtkmm.h>

#include "jdmutex.h"
#include <mutex>

namespace JDLIB
{
Expand All @@ -26,7 +26,7 @@ namespace JDLIB
class ImgLoader : public Glib::Object
{
Glib::RefPtr< Gdk::PixbufLoader > m_loader;
JDLIB::Mutex m_loader_lock;
std::mutex m_loader_lock;

std::string m_file;
std::string m_errmsg;
Expand Down Expand Up @@ -66,7 +66,7 @@ namespace JDLIB
std::list< Glib::RefPtr< ImgLoader > > m_cache;

public:
JDLIB::Mutex m_provider_lock; // ImgProvider操作時の必須ロック
std::mutex m_provider_lock; // ImgProvider操作時の必須ロック

public:
virtual ~ImgProvider() noexcept {}
Expand Down
52 changes: 0 additions & 52 deletions src/jdlib/jdmutex.h

This file was deleted.

21 changes: 10 additions & 11 deletions src/jdlib/loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@

#include "httpcode.h"

#include <sstream>
#include <cstring>
#include <mutex>
#include <sstream>

#include <errno.h>
#include <fcntl.h>
Expand All @@ -44,8 +45,6 @@

#include <glibmm.h>

#include "jdmutex.h"

#ifdef _WIN32
// _soc : SOCKET (unsigned int)
#define SOC_ISVALID(_soc) ( (_soc) != INVALID_SOCKET )
Expand Down Expand Up @@ -85,8 +84,8 @@ namespace JDLIB
}


static JDLIB::StaticMutex mutex_token = JDLIB_STATIC_MUTEX_INIT;
static JDLIB::StaticMutex mutex_queue = JDLIB_STATIC_MUTEX_INIT;
static std::mutex mutex_token;
static std::mutex mutex_queue;
std::list< JDLIB::Loader* > queue_loader; // スレッド起動待ちの Loader のキュー
int token_loader = 0;
std::vector< JDLIB::Loader* > vec_loader( MAX_LOADER );
Expand All @@ -96,7 +95,7 @@ bool disable_pop = false;
// トークン取得
bool JDLIB::get_token( JDLIB::Loader* loader )
{
JDLIB::LockGuard lock( mutex_token );
std::lock_guard< std::mutex > lock( mutex_token );

#ifdef _DEBUG
std::cout << "JDLIB::get_token : url = " << loader->data().url << " token = " << token_loader << std::endl;
Expand Down Expand Up @@ -130,7 +129,7 @@ bool JDLIB::get_token( JDLIB::Loader* loader )
// トークン返す
void JDLIB::return_token( JDLIB::Loader* loader )
{
JDLIB::LockGuard lock( mutex_token );
std::lock_guard< std::mutex > lock( mutex_token );

--token_loader;
assert( token_loader >= 0 );
Expand All @@ -147,7 +146,7 @@ void JDLIB::return_token( JDLIB::Loader* loader )
// スレッド起動待ちキューに Loader を登録
void JDLIB::push_loader_queue( JDLIB::Loader* loader )
{
JDLIB::LockGuard lock( mutex_queue );
std::lock_guard< std::mutex > lock( mutex_queue );

if( ! loader ) return;

Expand All @@ -168,7 +167,7 @@ void JDLIB::push_loader_queue( JDLIB::Loader* loader )
// キューから Loader を取り除いたらtrueを返す
bool JDLIB::remove_loader_queue( JDLIB::Loader* loader )
{
JDLIB::LockGuard lock( mutex_queue );
std::lock_guard< std::mutex > lock( mutex_queue );

if( ! queue_loader.size() ) return false;
if( std::find( queue_loader.begin(), queue_loader.end(), loader ) == queue_loader.end() ) return false;
Expand All @@ -186,7 +185,7 @@ bool JDLIB::remove_loader_queue( JDLIB::Loader* loader )
// キューに登録されたスレッドを起動する
void JDLIB::pop_loader_queue()
{
JDLIB::LockGuard lock( mutex_queue );
std::lock_guard< std::mutex > lock( mutex_queue );

if( disable_pop ) return;
if( ! queue_loader.size() ) return;
Expand Down Expand Up @@ -217,7 +216,7 @@ void JDLIB::pop_loader_queue()
//
void JDLIB::disable_pop_loader_queue()
{
JDLIB::LockGuard lock( mutex_queue );
std::lock_guard< std::mutex > lock( mutex_queue );

#ifdef _DEBUG
std::cout << "JDLIB::disable_pop_loader_queue\n";
Expand Down
6 changes: 3 additions & 3 deletions src/jdlib/timeout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ using namespace JDLIB;

#ifdef _WIN32
// static
JDLIB::StaticMutex Timeout::s_lock = JDLIB_STATIC_MUTEX_INIT;
std::mutex Timeout::s_lock;
std::map< UINT_PTR, Timeout* > Timeout::s_timeouts;
#endif

Expand All @@ -38,7 +38,7 @@ Timeout::~Timeout()
{
#ifdef _WIN32
if( m_identifer != 0 ){
JDLIB::LockGuard lock( s_lock );
std::lock_guard< std::mutex > lock( s_lock );
KillTimer( NULL, m_identifer );
s_timeouts.erase( m_identifer );
m_identifer = 0;
Expand All @@ -53,7 +53,7 @@ Timeout* Timeout::connect( const sigc::slot< bool > slot_timeout, unsigned int i
{
Timeout* timeout = new Timeout( slot_timeout );
#ifdef _WIN32
JDLIB::LockGuard lock( s_lock );
std::lock_guard< std::mutex > lock( s_lock );
// use global windows timer
UINT_PTR ident = SetTimer( NULL, 0, interval, slot_timeout_win32 );
if( ident != 0 ) {
Expand Down
4 changes: 2 additions & 2 deletions src/jdlib/timeout.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#ifdef _WIN32
#include <windows.h>
#undef DELETE // conflict with Gtk::Stock::DELETE
#include "jdmutex.h"
#include <mutex>
#endif

namespace JDLIB
Expand All @@ -19,7 +19,7 @@ namespace JDLIB
Glib::RefPtr<Glib::MainContext> m_context;
UINT_PTR m_identifer;

static JDLIB::StaticMutex s_lock;
static std::mutex s_lock;
static std::map< UINT_PTR, Timeout* > s_timeouts;
#else
sigc::connection m_connection;
Expand Down

0 comments on commit 18e0bd2

Please sign in to comment.