Commit 71b0da8b authored by Alexander Morozov's avatar Alexander Morozov

use mutexes instead of spin locks

parent 6956a729
...@@ -60,12 +60,13 @@ namespace UniSetTypes ...@@ -60,12 +60,13 @@ namespace UniSetTypes
return nm; return nm;
}; };
uniset_mutex (const uniset_mutex& r);
uniset_mutex &operator=(const uniset_mutex& r);
protected: protected:
private: private:
friend class uniset_mutex_lock; friend class uniset_mutex_lock;
uniset_mutex (const uniset_mutex& r);
const uniset_mutex &operator=(const uniset_mutex& r);
omni_condition* cnd; omni_condition* cnd;
std::string nm; std::string nm;
omni_semaphore sem; omni_semaphore sem;
...@@ -99,32 +100,10 @@ namespace UniSetTypes ...@@ -99,32 +100,10 @@ namespace UniSetTypes
}; };
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
class uniset_spin_mutex
{
public:
uniset_spin_mutex();
~uniset_spin_mutex();
uniset_spin_mutex (const uniset_spin_mutex& r);
const uniset_spin_mutex &operator=(const uniset_spin_mutex& r);
void lock( int check_pause_msec=0 );
void unlock();
private: typedef uniset_mutex uniset_spin_mutex;
mutex_atomic_t m; typedef uniset_mutex_lock uniset_spin_lock;
};
// -------------------------------------------------------------------------
class uniset_spin_lock
{
public:
uniset_spin_lock( uniset_spin_mutex& m, int check_pause_msec=0 );
~uniset_spin_lock();
private:
uniset_spin_lock(const uniset_spin_lock&);
uniset_spin_lock& operator=(const uniset_spin_lock&);
uniset_spin_mutex& m;
};
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
} // end of UniSetTypes namespace } // end of UniSetTypes namespace
......
...@@ -134,18 +134,21 @@ bool uniset_mutex::isRelease() ...@@ -134,18 +134,21 @@ bool uniset_mutex::isRelease()
#endif // HAVE_LINUX_FUTEX_H #endif // HAVE_LINUX_FUTEX_H
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
const uniset_mutex &uniset_mutex::operator=(const uniset_mutex& r) uniset_mutex &uniset_mutex::operator=( const uniset_mutex& r )
{ {
if( this != &r ) if( this != &r )
locked = r.locked; {
nm = r.nm;
mutex_atomic_set(&locked,0);
cnd = new omni_condition(&mtx);
}
return *this; return *this;
} }
uniset_mutex::uniset_mutex( const uniset_mutex& r ): uniset_mutex::uniset_mutex( const uniset_mutex& r ):
cnd(0),
nm(r.nm) nm(r.nm)
{ {
mutex_atomic_set(&locked,0);
cnd = new omni_condition(&mtx); cnd = new omni_condition(&mtx);
} }
...@@ -202,107 +205,3 @@ uniset_mutex_lock& uniset_mutex_lock::operator=(const uniset_mutex_lock &r) ...@@ -202,107 +205,3 @@ uniset_mutex_lock& uniset_mutex_lock::operator=(const uniset_mutex_lock &r)
return *this; return *this;
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
#ifndef HAVE_LINUX_LIBC_HEADERS_INCLUDE_LINUX_FUTEX_H
uniset_spin_mutex::uniset_spin_mutex()
{
unlock();
}
uniset_spin_mutex::~uniset_spin_mutex()
{
unlock();
}
const uniset_spin_mutex &uniset_spin_mutex::operator=( const uniset_spin_mutex& r )
{
if( this != &r )
unlock();
return *this;
}
uniset_spin_mutex::uniset_spin_mutex( const uniset_spin_mutex& r )
{
unlock();
}
void uniset_spin_mutex::lock( int check_pause_msec )
{
while( mutex_atomic_read(&m) != 0 )
{
if( check_pause_msec > 0 )
msleep(check_pause_msec);
}
mutex_atomic_set(&m,1);
}
void uniset_spin_mutex::unlock()
{
m = 0;
}
#else // HAVE_FUTEX
// mutex futex
// http://kerneldump.110mb.com/dokuwiki/doku.php?id=wiki:futexes_are_tricky_p3
// : http://people.redhat.com/drepper/futex.pdf
void uniset_spin_mutex::lock( int check_pause_msec )
{
struct timespec tm;
tm.tv_sec = check_pause_msec / 1000;
tm.tv_nsec = check_pause_msec%1000;
int c;
if( (c = cmpxchg(val, 0, 1))!= 0 )
{
do
{
if( c==2 || cmpxchg(val, 1, 2)!=0 )
{
if( futex_wait(&val, 2,tm) == ETIMEDOUT )
return;
}
}
while( (c = cmpxchg(val, 0, 2))!=0 );
}
}
void uniset_spin_mutex::unlock()
{
if( atomic_dec(val)!=1 )
{
val = 0;
futex_wake(&val, 1);
}
}
#endif // HAVE_FUTEX
// -------------------------------------------------------------------------------------------
uniset_spin_lock::uniset_spin_lock( uniset_spin_mutex& _m, int check_pause_msec ):
m(_m)
{
m.lock(check_pause_msec);
}
uniset_spin_lock::~uniset_spin_lock()
{
m.unlock();
}
uniset_spin_lock::uniset_spin_lock( const uniset_spin_lock& r ):
m(r.m)
{
}
uniset_spin_lock& uniset_spin_lock::operator=(const uniset_spin_lock& r)
{
if( this != &r )
m = r.m;
return *this;
}
// -----------------------------------------------------------------------------
#undef MUTEX_LOCK_SLEEP_MS
// -----------------------------------------------------------------------------
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