diff --git a/synctable/synctable.cpp b/synctable/synctable.cpp index dfcbec1..d270c41 100644 --- a/synctable/synctable.cpp +++ b/synctable/synctable.cpp @@ -1,9 +1,6 @@ #include "synctable.h" namespace synclock { - - // SyncTable - SyncTable::~SyncTable() { for (auto iter = this->locks_table.begin(), @@ -31,18 +28,6 @@ namespace synclock { return var_lock; } - - // _Table_Locker - - _Table_Locker::_Table_Locker(SyncTable & sync_table, void * addr) - : var_lock_holder(*sync_table.get_lock_address(addr)), - finished(false) - { } - - - _Table_Locker::~_Table_Locker() { } - - // The Global SyncTable for synchronized() blocks SyncTable globalsynctable; } diff --git a/synctable/synctable.h b/synctable/synctable.h index f97165a..c412103 100644 --- a/synctable/synctable.h +++ b/synctable/synctable.h @@ -3,14 +3,13 @@ #include #include +#include namespace synclock{ class SyncTable{ - friend class _Table_Locker; private: std::unordered_map locks_table; std::mutex table_lock; - std::mutex * get_lock_address(void *addr); public: SyncTable(){} @@ -21,29 +20,13 @@ namespace synclock{ SyncTable & operator=(const SyncTable &) = delete; ~SyncTable(); + std::mutex * get_lock_address(void *addr); }; - // This class is only for use by the synchronized/tablesynchronized blocks - // and should not be used directly. The name of the class is - // intentionlly poorly formed. - class _Table_Locker{ - private: - std::lock_guard var_lock_holder; - - public: - bool finished; - _Table_Locker(SyncTable &sync_table, void * addr); - _Table_Locker(const _Table_Locker &) = delete; - _Table_Locker & operator=(const _Table_Locker &) = delete; - ~_Table_Locker(); - }; - // global table for use in synchronized blocks extern SyncTable globalsynctable; } -// the _Table_Lockers have a bunch of capital letters on the end of them -// to (try to) ensure there are no collisions // tablesynchronized(synctable, &var) { critical section } // @@ -52,24 +35,34 @@ namespace synclock{ // this is provided so that groups of unrelated threads do not result in a // large, slow, globalsynctable. // using a value in a local SyncTable will NOT add it to tho global synctable -// this is exception safe since the _Table_Locker releases the lock on +// this is exception safe since the lock_guard releases the lock on // destruction #define tablesynchronized(TABLE, ADDR) \ -for(synclock::_Table_Locker _table_locker_obj_ABCDEFAOEUI(TABLE, (void*)(ADDR)); \ - !_table_locker_obj_ABCDEFAOEUI.finished; \ - _table_locker_obj_ABCDEFAOEUI.finished = true) +for(std::pair, bool> pairwithREALLYlongname( \ + std::piecewise_construct, \ + std::make_tuple(std::ref( \ + *TABLE.get_lock_address( \ + static_cast(ADDR)))), \ + std::make_tuple(false)); \ + !pairwithREALLYlongname.second; \ + pairwithREALLYlongname.second = true) // synchronized(&var) { critical section } // -// synchronized blocks construct a _Table_Locker on entry and destroy it +// synchronized blocks construct a lock_guard on entry and destroy it // on exit. This results in a locking of var for the body of the block. -// It is also exception safe since destructon occurs when an exception +// It is also exception safe since destruction occurs when an exception // causes the block to exit #define synchronized(ADDR) \ -for(synclock::_Table_Locker _table_locker_obj_ABCDEFAOEUI(synclock::globalsynctable, static_cast(ADDR)); \ - !_table_locker_obj_ABCDEFAOEUI.finished; \ - _table_locker_obj_ABCDEFAOEUI.finished = true) + for (class : public std::lock_guard { \ + public: \ + bool finished = false; \ + using std::lock_guard::lock_guard; \ + } lgwithREALLYlongname(*synclock::globalsynctable.get_lock_address( \ + static_cast(ADDR))); \ + !lgwithREALLYlongname.finished; \ + lgwithREALLYlongname.finished = true) #endif // __SYNCHRONIZER__H__