Commit 4f4b01cd authored by Pavel Vainerman's avatar Pavel Vainerman

Переписал mutex-ы на стандартные появившееся в c++11

parent bee92874
...@@ -58,7 +58,7 @@ AM_CONDITIONAL(DISABLE_SQLITE, test ${buildsqlite} = false) ...@@ -58,7 +58,7 @@ AM_CONDITIONAL(DISABLE_SQLITE, test ${buildsqlite} = false)
# export # export
LDFLAGS="${OMNI_LIBS} ${XML_LIBS}" LDFLAGS="${OMNI_LIBS} ${XML_LIBS}"
CXXFLAGS="-pedantic -Wall -funsigned-char -std=c++0x -g -D_GNU_SOURCE ${OMNI_CFLAGS} ${XML_CFLAGS} -I\$(top_builddir)/include" CXXFLAGS="-pedantic -Wall -funsigned-char -std=c++11 -g -D_GNU_SOURCE ${OMNI_CFLAGS} ${XML_CFLAGS} -I\$(top_builddir)/include"
AC_SUBST(LDFLAGS) AC_SUBST(LDFLAGS)
AC_SUBST(CXXFLAGS) AC_SUBST(CXXFLAGS)
......
../../../Utilities/scripts/uniset-stop.sh ../../../Utilities/scripts/uniset2-stop.sh
\ No newline at end of file \ No newline at end of file
../../../Utilities/scripts/uniset-functions.sh ../../../Utilities/scripts/uniset2-functions.sh
\ No newline at end of file \ No newline at end of file
../../../Utilities/scripts/uniset-start.sh ../../../Utilities/scripts/uniset2-start.sh
\ No newline at end of file \ No newline at end of file
...@@ -25,20 +25,15 @@ ...@@ -25,20 +25,15 @@
#define UniSet_MUTEX_H_ #define UniSet_MUTEX_H_
// ----------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------
#include <string> #include <string>
#include <omnithread.h> #include <atomic>
#include <signal.h> #include <chrono>
#include <mutex>
#include <cc++/thread.h> #include <cc++/thread.h>
// ----------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------
namespace UniSetTypes namespace UniSetTypes
{ {
typedef ost::AtomicCounter mutex_atomic_t; typedef std::atomic_int mutex_atomic_t;
/*! \class uniset_mutex
* \note Напрямую функциями \a lock() и \a unlock() лучше не пользоваться.
* Как пользоваться см. \ref MutexHowToPage
* \todo Проверить правильность конструкторов копирования и operator=
*/
class uniset_mutex class uniset_mutex
{ {
public: public:
...@@ -46,9 +41,9 @@ namespace UniSetTypes ...@@ -46,9 +41,9 @@ namespace UniSetTypes
uniset_mutex( const std::string& name ); uniset_mutex( const std::string& name );
~uniset_mutex(); ~uniset_mutex();
bool isRelease();
void lock(); void lock();
void unlock(); void unlock();
bool try_lock_for( const time_t& msec );
inline std::string name(){ return nm; } inline std::string name(){ return nm; }
inline void setName( const std::string& name ){ nm = name; } inline void setName( const std::string& name ){ nm = name; }
...@@ -57,13 +52,10 @@ namespace UniSetTypes ...@@ -57,13 +52,10 @@ namespace UniSetTypes
private: private:
friend class uniset_mutex_lock; friend class uniset_mutex_lock;
uniset_mutex (const uniset_mutex& r); uniset_mutex(const uniset_mutex& r);
const uniset_mutex &operator=(const uniset_mutex& r); const uniset_mutex &operator=(const uniset_mutex& r);
omni_condition* cnd;
std::string nm; std::string nm;
omni_semaphore sem; std::timed_mutex m_lock;
omni_mutex mtx;
mutex_atomic_t locked;
}; };
std::ostream& operator<<(std::ostream& os, uniset_mutex& m ); std::ostream& operator<<(std::ostream& os, uniset_mutex& m );
...@@ -79,14 +71,15 @@ namespace UniSetTypes ...@@ -79,14 +71,15 @@ namespace UniSetTypes
class uniset_mutex_lock class uniset_mutex_lock
{ {
public: public:
uniset_mutex_lock( uniset_mutex& m, int timeoutMS=0 ); uniset_mutex_lock( uniset_mutex& m, const time_t timeoutMS=0 );
~uniset_mutex_lock(); ~uniset_mutex_lock();
bool lock_ok(); bool lock_ok();
private: private:
uniset_mutex* mutex; uniset_mutex* mutex;
mutex_atomic_t mlock; std::atomic<int> locked;
uniset_mutex_lock(const uniset_mutex_lock&); uniset_mutex_lock(const uniset_mutex_lock&);
uniset_mutex_lock& operator=(const uniset_mutex_lock&); uniset_mutex_lock& operator=(const uniset_mutex_lock&);
}; };
...@@ -120,8 +113,8 @@ namespace UniSetTypes ...@@ -120,8 +113,8 @@ namespace UniSetTypes
std::string nm; std::string nm;
friend class uniset_rwmutex_lock; friend class uniset_rwmutex_lock;
ost::ThreadLock m; ost::ThreadLock m;
ost::AtomicCounter wr_wait; std::atomic<int> wr_wait;
static ost::AtomicCounter num; static std::atomic<int> num;
}; };
std::ostream& operator<<(std::ostream& os, uniset_rwmutex& m ); std::ostream& operator<<(std::ostream& os, uniset_rwmutex& m );
......
...@@ -445,9 +445,7 @@ bool IONotifyController::activateObject() ...@@ -445,9 +445,7 @@ bool IONotifyController::activateObject()
// сперва вычитаем датчиков и заказчиков.. // сперва вычитаем датчиков и заказчиков..
readDump(); readDump();
// а потом уже собственно активация.. // а потом уже собственно активация..
IOController::activateObject(); return IOController::activateObject();
return true;
} }
// -------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------
void IONotifyController::readDump() void IONotifyController::readDump()
......
...@@ -22,6 +22,8 @@ ...@@ -22,6 +22,8 @@
*/ */
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
#include <chrono>
#include <thread>
#include <unistd.h> #include <unistd.h>
#include "UniSetTypes.h" #include "UniSetTypes.h"
#include "Mutex.h" #include "Mutex.h"
...@@ -34,24 +36,17 @@ using namespace UniSetTypes; ...@@ -34,24 +36,17 @@ using namespace UniSetTypes;
#define MUTEX_DEBUG(m) {} #define MUTEX_DEBUG(m) {}
uniset_mutex::uniset_mutex(): uniset_mutex::uniset_mutex():
cnd(0), nm("")
nm(""),
locked(0)
{ {
cnd = new omni_condition(&mtx);
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
uniset_mutex::uniset_mutex( const string& name ): uniset_mutex::uniset_mutex( const string& name ):
cnd(0), nm(name)
nm(name),
locked(0)
{ {
cnd = new omni_condition(&mtx);
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
uniset_mutex::~uniset_mutex() uniset_mutex::~uniset_mutex()
{ {
delete cnd;
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
std::ostream& UniSetTypes::operator<<(std::ostream& os, uniset_mutex& m ) std::ostream& UniSetTypes::operator<<(std::ostream& os, uniset_mutex& m )
...@@ -61,100 +56,63 @@ std::ostream& UniSetTypes::operator<<(std::ostream& os, uniset_mutex& m ) ...@@ -61,100 +56,63 @@ std::ostream& UniSetTypes::operator<<(std::ostream& os, uniset_mutex& m )
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
void uniset_mutex::lock() void uniset_mutex::lock()
{ {
sem.wait(); m_lock.lock();
locked = 1;
MUTEX_DEBUG(cerr << nm << " Locked.." << endl;) MUTEX_DEBUG(cerr << nm << " Locked.." << endl;)
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
void uniset_mutex::unlock() void uniset_mutex::unlock()
{ {
locked = 0; m_lock.unlock();
MUTEX_DEBUG(cerr << nm << " Unlocked.." << endl;) MUTEX_DEBUG(cerr << nm << " Unlocked.." << endl;)
sem.post();
cnd->signal();
}
// -----------------------------------------------------------------------------
bool uniset_mutex::isRelease()
{
return !locked;
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
const uniset_mutex &uniset_mutex::operator=(const uniset_mutex& r) bool uniset_mutex::try_lock_for( const time_t& msec )
{ {
// if( this != &r ) return m_lock.try_lock_for( std::chrono::milliseconds(msec) );
// locked = r.locked;
return *this;
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
uniset_mutex::uniset_mutex( const uniset_mutex& r ): uniset_mutex_lock::uniset_mutex_lock( uniset_mutex& m, const time_t timeMS ):
cnd(0), mutex(&m),
nm(r.nm), locked(0)
locked(r.locked)
{ {
cnd = new omni_condition(&mtx);
}
// ----------------------------------------------------------------------------- if( timeMS == 0 )
uniset_mutex_lock::uniset_mutex_lock( uniset_mutex& m, int timeMS ):
mutex(&m)
{
if( timeMS <= 0 || mutex->isRelease() )
{ {
mutex->lock(); mutex->lock();
mlock = 1; locked = 1;
return; return;
} }
unsigned long sec, msec; if( !mutex->try_lock_for(timeMS) )
omni_thread::get_time(&sec,&msec, timeMS/1000, (timeMS%1000)*1000000 );
mutex->mtx.lock();
if( !mutex->cnd->timedwait(sec, msec) )
{ {
if( !mutex->name().empty() ) if( !mutex->name().empty() )
{ {
ulog9 << "(mutex_lock): вышло заданное время ожидания " ulog9 << "(mutex_lock): вышло заданное время ожидания "
<< timeMS << " msec для " << mutex->name() << endl; << timeMS << " msec для " << mutex->name() << endl;
} }
return;
mlock = 0;
mutex->mtx.unlock();
return; // ресурс не захватываем
} }
mlock = 1; locked = 1;
mutex->lock();
mutex->mtx.unlock();
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
bool uniset_mutex_lock::lock_ok() bool uniset_mutex_lock::lock_ok()
{ {
return mlock; return (locked == 1);
}
uniset_mutex_lock::~uniset_mutex_lock()
{
if( mlock )
{
mlock = 0;
mutex->unlock();
}
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
uniset_mutex_lock& uniset_mutex_lock::operator=(const uniset_mutex_lock &r) uniset_mutex_lock::~uniset_mutex_lock()
{ {
return *this; mutex->unlock();
locked = 0;
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
uniset_rwmutex::uniset_rwmutex( const std::string& name ): uniset_rwmutex::uniset_rwmutex( const std::string& name ):
nm(name), nm(name),
wr_wait(0) wr_wait(0)
{ {
} }
ost::AtomicCounter uniset_rwmutex::num = 0;
uniset_rwmutex::uniset_rwmutex(): uniset_rwmutex::uniset_rwmutex():
nm(""), nm(""),
wr_wait(0) wr_wait(0)
...@@ -170,6 +128,8 @@ std::ostream& UniSetTypes::operator<<(std::ostream& os, uniset_rwmutex& m ) ...@@ -170,6 +128,8 @@ std::ostream& UniSetTypes::operator<<(std::ostream& os, uniset_rwmutex& m )
return os << m.name(); return os << m.name();
} }
std::atomic<int> uniset_rwmutex::num(0);
const uniset_rwmutex &uniset_rwmutex::operator=( const uniset_rwmutex& r ) const uniset_rwmutex &uniset_rwmutex::operator=( const uniset_rwmutex& r )
{ {
if( this != &r ) if( this != &r )
...@@ -178,6 +138,7 @@ const uniset_rwmutex &uniset_rwmutex::operator=( const uniset_rwmutex& r ) ...@@ -178,6 +138,7 @@ const uniset_rwmutex &uniset_rwmutex::operator=( const uniset_rwmutex& r )
ostringstream s; ostringstream s;
s << r.nm << "." << (++num); s << r.nm << "." << (++num);
nm = s.str(); nm = s.str();
wr_wait = 0;
unlock(); unlock();
MUTEX_DEBUG(cerr << "...copy mutex..." << nm << endl;) MUTEX_DEBUG(cerr << "...copy mutex..." << nm << endl;)
} }
...@@ -186,24 +147,25 @@ const uniset_rwmutex &uniset_rwmutex::operator=( const uniset_rwmutex& r ) ...@@ -186,24 +147,25 @@ const uniset_rwmutex &uniset_rwmutex::operator=( const uniset_rwmutex& r )
} }
uniset_rwmutex::uniset_rwmutex( const uniset_rwmutex& r ): uniset_rwmutex::uniset_rwmutex( const uniset_rwmutex& r ):
nm(r.nm) nm(r.nm),
wr_wait(0)
{ {
} }
void uniset_rwmutex::lock() void uniset_rwmutex::lock()
{ {
MUTEX_DEBUG(cerr << nm << " prepare Locked.." << endl;) MUTEX_DEBUG(cerr << nm << " prepare Locked.." << endl;)
wr_wait +=1; wr_wait++;
m.writeLock(); m.writeLock();
wr_wait -=1; wr_wait--;
MUTEX_DEBUG(cerr << nm << " Locked.." << endl;) MUTEX_DEBUG(cerr << nm << " Locked.." << endl;)
} }
void uniset_rwmutex::wrlock() void uniset_rwmutex::wrlock()
{ {
MUTEX_DEBUG(cerr << nm << " prepare WRLocked.." << endl;) MUTEX_DEBUG(cerr << nm << " prepare WRLocked.." << endl;)
wr_wait +=1; wr_wait++;
m.writeLock(); m.writeLock();
wr_wait -=1; wr_wait--;
MUTEX_DEBUG(cerr << nm << " WRLocked.." << endl;) MUTEX_DEBUG(cerr << nm << " WRLocked.." << endl;)
} }
void uniset_rwmutex::rlock() void uniset_rwmutex::rlock()
...@@ -211,7 +173,10 @@ void uniset_rwmutex::rlock() ...@@ -211,7 +173,10 @@ void uniset_rwmutex::rlock()
MUTEX_DEBUG(cerr << nm << " prepare RLocked.." << endl;) MUTEX_DEBUG(cerr << nm << " prepare RLocked.." << endl;)
while( wr_wait ) while( wr_wait )
msleep(2); {
MUTEX_DEBUG( cerr << nm << " whait... WR=" << wr_wait << endl; )
msleep(2);
}
m.readLock(); m.readLock();
MUTEX_DEBUG(cerr << nm << " RLocked.." << endl;) MUTEX_DEBUG(cerr << nm << " RLocked.." << endl;)
......
...@@ -30,7 +30,7 @@ ui_CPPFLAGS = -I$(top_builddir)/include ...@@ -30,7 +30,7 @@ ui_CPPFLAGS = -I$(top_builddir)/include
umutex_SOURCES = umutex.cc umutex_SOURCES = umutex.cc
umutex_LDADD = $(top_builddir)/lib/libUniSet2.la $(COMCPP_LIBS) umutex_LDADD = $(top_builddir)/lib/libUniSet2.la $(COMCPP_LIBS)
umutex_CPPFLAGS = -S -O2 -I$(top_builddir)/include $(COMCPP_CFLAGS) umutex_CPPFLAGS = -I$(top_builddir)/include $(COMCPP_CFLAGS)
conftest_SOURCES = conftest.cc conftest_SOURCES = conftest.cc
conftest_LDADD = $(top_builddir)/lib/libUniSet2.la conftest_LDADD = $(top_builddir)/lib/libUniSet2.la
......
...@@ -140,7 +140,7 @@ int main( int argc, const char **argv ) ...@@ -140,7 +140,7 @@ int main( int argc, const char **argv )
try try
{ {
#if 0 #if 1
{ {
uniset_rwmutex m1("mutex1"); uniset_rwmutex m1("mutex1");
uniset_rwmutex m2("mutex2"); uniset_rwmutex m2("mutex2");
...@@ -161,7 +161,7 @@ int main( int argc, const char **argv ) ...@@ -161,7 +161,7 @@ int main( int argc, const char **argv )
cout << "m3.lock: wrlock OK" << endl; cout << "m3.lock: wrlock OK" << endl;
} }
return 0; // return 0;
#endif #endif
#if 0 #if 0
......
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