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