Use std::atomic and std::mutex where applicable (nw)

This commit is contained in:
Miodrag Milanovic 2016-03-01 10:05:59 +01:00
parent 599570bf7f
commit 91c910e73c
9 changed files with 44 additions and 78 deletions

View File

@ -38,6 +38,7 @@
#include "emu.h" #include "emu.h"
#include "sound/wavwrite.h" #include "sound/wavwrite.h"
#include "discrete.h" #include "discrete.h"
#include <atomic>
/* for_each collides with c++ standard libraries - include it here */ /* for_each collides with c++ standard libraries - include it here */
#define for_each(_T, _e, _l) for (_T _e = (_l)->begin_ptr() ; _e <= (_l)->end_ptr(); _e++) #define for_each(_T, _e, _l) for (_T _e = (_l)->begin_ptr() ; _e <= (_l)->end_ptr(); _e++)
@ -110,9 +111,8 @@ public:
inline void step_nodes(void); inline void step_nodes(void);
inline bool lock_threadid(INT32 threadid) inline bool lock_threadid(INT32 threadid)
{ {
INT32 prev_id; int expected = -1;
prev_id = compare_exchange32(&m_threadid, -1, threadid); return m_threadid.compare_exchange_weak(expected, threadid, std::memory_order_release,std::memory_order_relaxed);
return (prev_id == -1 && m_threadid == threadid);
} }
inline void unlock(void) { m_threadid = -1; } inline void unlock(void) { m_threadid = -1; }
@ -144,7 +144,7 @@ protected:
discrete_device & m_device; discrete_device & m_device;
private: private:
volatile INT32 m_threadid; std::atomic<INT32> m_threadid;
volatile int m_samples; volatile int m_samples;
}; };

View File

@ -38,6 +38,7 @@
#define __POLY_H__ #define __POLY_H__
#include <limits.h> #include <limits.h>
#include <atomic>
//************************************************************************** //**************************************************************************
// DEBUGGING // DEBUGGING
@ -165,7 +166,7 @@ private:
// internal unit of work // internal unit of work
struct work_unit struct work_unit
{ {
volatile UINT32 count_next; // number of scanlines and index of next item to process std::atomic<UINT32> count_next; // number of scanlines and index of next item to process
polygon_info * polygon; // pointer to polygon polygon_info * polygon; // pointer to polygon
INT16 scanline; // starting scanline INT16 scanline; // starting scanline
UINT16 previtem; // index of previous item in the same bucket UINT16 previtem; // index of previous item in the same bucket
@ -405,7 +406,7 @@ void *poly_manager<_BaseType, _ObjectData, _MaxParams, _MaxPolys>::work_item_cal
{ {
orig_count_next = prevunit.count_next; orig_count_next = prevunit.count_next;
new_count_next = orig_count_next | (unitnum << 16); new_count_next = orig_count_next | (unitnum << 16);
} while (compare_exchange32((volatile INT32 *)&prevunit.count_next, orig_count_next, new_count_next) != orig_count_next); } while (!prevunit.count_next.compare_exchange_weak(orig_count_next, new_count_next, std::memory_order_release, std::memory_order_relaxed));
#if KEEP_POLY_STATISTICS #if KEEP_POLY_STATISTICS
// track resolved conflicts // track resolved conflicts
@ -427,7 +428,7 @@ void *poly_manager<_BaseType, _ObjectData, _MaxParams, _MaxPolys>::work_item_cal
do do
{ {
orig_count_next = unit.count_next; orig_count_next = unit.count_next;
} while (compare_exchange32((volatile INT32 *)&unit.count_next, orig_count_next, 0) != orig_count_next); } while (!unit.count_next.compare_exchange_weak(orig_count_next, 0, std::memory_order_release, std::memory_order_relaxed));
// if we have no more work to do, do nothing // if we have no more work to do, do nothing
orig_count_next >>= 16; orig_count_next >>= 16;

View File

@ -10,7 +10,7 @@
#include "emu.h" #include "emu.h"
#include "polylgcy.h" #include "polylgcy.h"
#include <atomic>
/*************************************************************************** /***************************************************************************
DEBUGGING DEBUGGING
@ -88,7 +88,7 @@ struct poly_section
struct work_unit_shared struct work_unit_shared
{ {
polygon_info * polygon; /* pointer to polygon */ polygon_info * polygon; /* pointer to polygon */
volatile UINT32 count_next; /* number of scanlines and index of next item to process */ std::atomic<UINT32> count_next; /* number of scanlines and index of next item to process */
INT16 scanline; /* starting scanline and count */ INT16 scanline; /* starting scanline and count */
UINT16 previtem; /* index of previous item in the same bucket */ UINT16 previtem; /* index of previous item in the same bucket */
#ifndef PTR64 #ifndef PTR64
@ -1305,7 +1305,7 @@ static void *poly_item_callback(void *param, int threadid)
{ {
orig_count_next = prevunit->shared.count_next; orig_count_next = prevunit->shared.count_next;
new_count_next = orig_count_next | (unitnum << 16); new_count_next = orig_count_next | (unitnum << 16);
} while (compare_exchange32((volatile INT32 *)&prevunit->shared.count_next, orig_count_next, new_count_next) != orig_count_next); } while (!prevunit->shared.count_next.compare_exchange_weak(orig_count_next, new_count_next, std::memory_order_release, std::memory_order_relaxed));
#if KEEP_STATISTICS #if KEEP_STATISTICS
/* track resolved conflicts */ /* track resolved conflicts */
@ -1336,7 +1336,7 @@ static void *poly_item_callback(void *param, int threadid)
do do
{ {
orig_count_next = unit->shared.count_next; orig_count_next = unit->shared.count_next;
} while (compare_exchange32((volatile INT32 *)&unit->shared.count_next, orig_count_next, 0) != orig_count_next); } while (!unit->shared.count_next.compare_exchange_weak(orig_count_next, 0, std::memory_order_release, std::memory_order_relaxed));
/* if we have no more work to do, do nothing */ /* if we have no more work to do, do nothing */
orig_count_next >>= 16; orig_count_next >>= 16;

View File

@ -154,22 +154,6 @@ static void *osd_sharedmem_ptr(osd_shared_mem *os_shmem)
} }
#endif #endif
/***************************************************************************
INLINE FUNCTIONS
***************************************************************************/
static inline void shmem_lock(shmem_t *shmem)
{
while (atomic_exchange32(&shmem->lock,1) == 0)
;
}
static inline void shmem_unlock(shmem_t *shmem)
{
atomic_exchange32(&shmem->lock,0);
}
/*************************************************************************** /***************************************************************************
DEVICE INTERFACE DEVICE INTERFACE
***************************************************************************/ ***************************************************************************/
@ -248,10 +232,9 @@ void gaelco_serial_device::device_reset()
m_last_in_msg_cnt = -1; m_last_in_msg_cnt = -1;
m_slack_cnt = LINK_SLACK_B; m_slack_cnt = LINK_SLACK_B;
shmem_lock(m_shmem); std::lock_guard<std::mutex> guard(m_mutex);
buf_reset(m_out_ptr); buf_reset(m_out_ptr);
buf_reset(m_in_ptr); buf_reset(m_in_ptr);
shmem_unlock(m_shmem);
} }
//------------------------------------------------- //-------------------------------------------------
@ -260,11 +243,11 @@ void gaelco_serial_device::device_reset()
void gaelco_serial_device::device_stop() void gaelco_serial_device::device_stop()
{ {
shmem_lock(m_shmem); {
std::lock_guard<std::mutex> guard(m_mutex);
buf_reset(m_out_ptr); buf_reset(m_out_ptr);
buf_reset(m_in_ptr); buf_reset(m_in_ptr);
shmem_unlock(m_shmem); }
osd_sharedmem_free(m_os_shmem); osd_sharedmem_free(m_os_shmem);
} }
@ -313,7 +296,8 @@ void gaelco_serial_device::sync_link()
int breakme = 1; int breakme = 1;
do do
{ {
shmem_lock(m_shmem); std::lock_guard<std::mutex> guard(m_mutex);
process_in(); process_in();
/* HACK: put some timing noise on the line */ /* HACK: put some timing noise on the line */
if (buf->cnt + m_slack_cnt > m_out_ptr->cnt) if (buf->cnt + m_slack_cnt > m_out_ptr->cnt)
@ -321,23 +305,23 @@ void gaelco_serial_device::sync_link()
/* stop if not connected .. */ /* stop if not connected .. */
if ((m_out_ptr->stat & GAELCOSER_STATUS_RESET) != 0) if ((m_out_ptr->stat & GAELCOSER_STATUS_RESET) != 0)
breakme = 0; breakme = 0;
shmem_unlock(m_shmem);
} while (breakme); } while (breakme);
m_slack_cnt++; m_slack_cnt++;
m_slack_cnt = (m_slack_cnt % LINK_SLACK) + LINK_SLACK_B; m_slack_cnt = (m_slack_cnt % LINK_SLACK) + LINK_SLACK_B;
shmem_lock(m_shmem); {
std::lock_guard<std::mutex> guard(m_mutex);
m_out_ptr->stat &= ~GAELCOSER_STATUS_RESET; m_out_ptr->stat &= ~GAELCOSER_STATUS_RESET;
shmem_unlock(m_shmem); }
} }
TIMER_CALLBACK_MEMBER( gaelco_serial_device::link_cb ) TIMER_CALLBACK_MEMBER( gaelco_serial_device::link_cb )
{ {
shmem_lock(m_shmem); std::lock_guard<std::mutex> guard(m_mutex);
m_out_ptr->cnt++; m_out_ptr->cnt++;
sync_link(); sync_link();
shmem_unlock(m_shmem);
} }
@ -356,19 +340,20 @@ READ8_MEMBER( gaelco_serial_device::status_r)
{ {
UINT8 ret = 0; UINT8 ret = 0;
shmem_lock(m_shmem); std::lock_guard<std::mutex> guard(m_mutex);
process_in(); process_in();
if ((m_status & GAELCOSER_STATUS_READY) != 0) if ((m_status & GAELCOSER_STATUS_READY) != 0)
ret |= 0x01; ret |= 0x01;
if ((m_in_ptr->stat & GAELCOSER_STATUS_RTS) != 0) if ((m_in_ptr->stat & GAELCOSER_STATUS_RTS) != 0)
ret |= 0x02; ret |= 0x02;
shmem_unlock(m_shmem);
return ret; return ret;
} }
WRITE8_MEMBER( gaelco_serial_device::data_w) WRITE8_MEMBER( gaelco_serial_device::data_w)
{ {
shmem_lock(m_shmem); std::lock_guard<std::mutex> guard(m_mutex);
m_out_ptr->data = data; m_out_ptr->data = data;
m_status &= ~GAELCOSER_STATUS_READY; m_status &= ~GAELCOSER_STATUS_READY;
@ -376,7 +361,6 @@ WRITE8_MEMBER( gaelco_serial_device::data_w)
set_status( ~GAELCOSER_STATUS_READY, GAELCOSER_STATUS_READY, LINK_FREQ ); set_status( ~GAELCOSER_STATUS_READY, GAELCOSER_STATUS_READY, LINK_FREQ );
shmem_unlock(m_shmem);
LOGMSG(("command send %02x at %d\n", data, m_out_ptr->cnt)); LOGMSG(("command send %02x at %d\n", data, m_out_ptr->cnt));
} }
@ -384,7 +368,7 @@ READ8_MEMBER( gaelco_serial_device::data_r)
{ {
UINT8 ret; UINT8 ret;
shmem_lock(m_shmem); std::lock_guard<std::mutex> guard(m_mutex);
process_in(); process_in();
ret = (m_in_ptr->data & 0xff); ret = (m_in_ptr->data & 0xff);
@ -395,21 +379,19 @@ READ8_MEMBER( gaelco_serial_device::data_r)
if ((m_status & GAELCOSER_STATUS_SEND) == 0) if ((m_status & GAELCOSER_STATUS_SEND) == 0)
m_status |= GAELCOSER_STATUS_READY; m_status |= GAELCOSER_STATUS_READY;
shmem_unlock(m_shmem);
return ret; return ret;
} }
WRITE8_MEMBER( gaelco_serial_device::unknown_w) WRITE8_MEMBER( gaelco_serial_device::unknown_w)
{ {
shmem_lock(m_shmem); std::lock_guard<std::mutex> guard(m_mutex);
LOGMSG(("???? unknown serial access %d\n", data)); LOGMSG(("???? unknown serial access %d\n", data));
shmem_unlock(m_shmem);
} }
WRITE8_MEMBER( gaelco_serial_device::rts_w ) WRITE8_MEMBER( gaelco_serial_device::rts_w )
{ {
shmem_lock(m_shmem); std::lock_guard<std::mutex> guard(m_mutex);
if (data == 0) if (data == 0)
m_out_ptr->stat |= GAELCOSER_STATUS_RTS; m_out_ptr->stat |= GAELCOSER_STATUS_RTS;
@ -419,18 +401,14 @@ WRITE8_MEMBER( gaelco_serial_device::rts_w )
//m_status |= GAELCOSER_STATUS_READY; //m_status |= GAELCOSER_STATUS_READY;
m_out_ptr->stat &= ~GAELCOSER_STATUS_RTS; m_out_ptr->stat &= ~GAELCOSER_STATUS_RTS;
} }
shmem_unlock(m_shmem);
} }
WRITE8_MEMBER( gaelco_serial_device::tr_w) WRITE8_MEMBER( gaelco_serial_device::tr_w)
{ {
LOGMSG(("set transmit %d\n", data)); LOGMSG(("set transmit %d\n", data));
shmem_lock(m_shmem); std::lock_guard<std::mutex> guard(m_mutex);
if ((data & 0x01) != 0) if ((data & 0x01) != 0)
m_status |= GAELCOSER_STATUS_SEND; m_status |= GAELCOSER_STATUS_SEND;
else else
m_status &= ~GAELCOSER_STATUS_SEND; m_status &= ~GAELCOSER_STATUS_SEND;
shmem_unlock(m_shmem);
} }

View File

@ -7,7 +7,6 @@
***************************************************************************/ ***************************************************************************/
/*************************************************************************** /***************************************************************************
DEVICE CONFIGURATION MACROS DEVICE CONFIGURATION MACROS
***************************************************************************/ ***************************************************************************/
@ -47,7 +46,6 @@ struct buf_t
struct shmem_t struct shmem_t
{ {
volatile INT32 lock;
buf_t buf[2]; buf_t buf[2];
}; };
@ -106,6 +104,7 @@ private:
buf_t *m_out_ptr; buf_t *m_out_ptr;
osd_shared_mem *m_os_shmem; osd_shared_mem *m_os_shmem;
shmem_t *m_shmem; shmem_t *m_shmem;
std::mutex m_mutex;
TIMER_CALLBACK_MEMBER( set_status_cb ); TIMER_CALLBACK_MEMBER( set_status_cb );
TIMER_CALLBACK_MEMBER( link_cb ); TIMER_CALLBACK_MEMBER( link_cb );

View File

@ -18,14 +18,6 @@
#if defined(__i386__) || defined(__x86_64__) #if defined(__i386__) || defined(__x86_64__)
static inline void ATTR_FORCE_INLINE
osd_yield_processor(void)
{
__asm__ __volatile__ ( " rep ; nop ;" );
}
#if defined(__x86_64__) #if defined(__x86_64__)
//============================================================ //============================================================
@ -52,14 +44,6 @@ _osd_exchange64(INT64 volatile *ptr, INT64 exchange)
#elif defined(__ppc__) || defined (__PPC__) || defined(__ppc64__) || defined(__PPC64__) #elif defined(__ppc__) || defined (__PPC__) || defined(__ppc64__) || defined(__PPC64__)
static inline void ATTR_FORCE_INLINE
osd_yield_processor(void)
{
__asm__ __volatile__ ( " nop \n nop \n" );
}
#if defined(__ppc64__) || defined(__PPC64__) #if defined(__ppc64__) || defined(__PPC64__)
//============================================================ //============================================================

View File

@ -51,7 +51,7 @@ watchdog::watchdog(void)
watchdog::~watchdog(void) watchdog::~watchdog(void)
{ {
atomic_exchange32(&m_do_exit, 1); m_do_exit = 1;
osd_event_set(m_event); osd_event_set(m_event);
osd_thread_wait_free(m_thread); osd_thread_wait_free(m_thread);
osd_event_free(m_event); osd_event_free(m_event);

View File

@ -11,6 +11,7 @@
//============================================================ //============================================================
#include "modules/sync/osdsync.h" #include "modules/sync/osdsync.h"
#include <atomic>
class watchdog class watchdog
{ {
@ -27,7 +28,7 @@ public:
private: private:
osd_event * m_event; osd_event * m_event;
osd_thread * m_thread; osd_thread * m_thread;
volatile INT32 m_do_exit; std::atomic<INT32> m_do_exit;
osd_ticks_t m_timeout; osd_ticks_t m_timeout;
}; };

View File

@ -12,9 +12,12 @@
// Needed for RAW Input // Needed for RAW Input
#define WM_INPUT 0x00FF #define WM_INPUT 0x00FF
#include <stdio.h> // must be here otherwise issues with I64FMT in MINGW
// standard C headers // standard C headers
#include <process.h> #include <process.h>
#include <atomic>
// MAME headers // MAME headers
#include "emu.h" #include "emu.h"
#include "uiinput.h" #include "uiinput.h"
@ -127,11 +130,11 @@ struct mtlog
}; };
static mtlog mtlog[100000]; static mtlog mtlog[100000];
static volatile INT32 mtlogindex; static std::atomic<INT32> mtlogindex;
void mtlog_add(const char *event) void mtlog_add(const char *event)
{ {
int index = atomic_increment32((INT32 *) &mtlogindex) - 1; int index = mtlogindex++;
if (index < ARRAY_LENGTH(mtlog)) if (index < ARRAY_LENGTH(mtlog))
{ {
mtlog[index].timestamp = osd_ticks(); mtlog[index].timestamp = osd_ticks();
@ -149,7 +152,7 @@ static void mtlog_dump(void)
for (i = 0; i < mtlogindex; i++) for (i = 0; i < mtlogindex; i++)
{ {
osd_ticks_t curr = mtlog[i].timestamp * 1000000 / cps; osd_ticks_t curr = mtlog[i].timestamp * 1000000 / cps;
fprintf(f, "%20I64d %10I64d %s\n", curr, curr - last, mtlog[i].event); fprintf(f, "%20" I64FMT "d %10" I64FMT "d %s\n", (UINT64)curr, (UINT64)(curr - last), mtlog[i].event);
last = curr; last = curr;
} }
fclose(f); fclose(f);