Commit 60df1b9b authored by Pavel Vaynerman's avatar Pavel Vaynerman

retuurn old mutex

parent 1c0d68d1
......@@ -59,14 +59,13 @@ namespace UniSetTypes
{
return nm;
};
uniset_mutex (const uniset_mutex& r);
uniset_mutex &operator=(const uniset_mutex& r);
protected:
private:
friend class uniset_mutex_lock;
uniset_mutex (const uniset_mutex& r);
const uniset_mutex &operator=(const uniset_mutex& r);
omni_condition* cnd;
std::string nm;
omni_semaphore sem;
......@@ -100,10 +99,32 @@ 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();
typedef uniset_mutex uniset_spin_mutex;
typedef uniset_mutex_lock uniset_spin_lock;
private:
mutex_atomic_t m;
};
// -------------------------------------------------------------------------
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
......
......@@ -134,21 +134,18 @@ bool uniset_mutex::isRelease()
#endif // HAVE_LINUX_FUTEX_H
// -----------------------------------------------------------------------------
uniset_mutex &uniset_mutex::operator=( const uniset_mutex& r )
const uniset_mutex &uniset_mutex::operator=(const uniset_mutex& r)
{
if( this != &r )
{
nm = r.nm;
mutex_atomic_set(&locked,0);
cnd = new omni_condition(&mtx);
}
locked = r.locked;
return *this;
}
uniset_mutex::uniset_mutex( const uniset_mutex& r ):
cnd(0),
nm(r.nm)
{
mutex_atomic_set(&locked,0);
cnd = new omni_condition(&mtx);
}
......@@ -205,3 +202,107 @@ uniset_mutex_lock& uniset_mutex_lock::operator=(const uniset_mutex_lock &r)
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