Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
U
uniset2
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
1
Issues
1
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
UniSet project repositories
uniset2
Commits
5a73a16c
Commit
5a73a16c
authored
Sep 23, 2009
by
Pavel Vainerman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Mutex reingeniring
parent
3e6645e2
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
125 additions
and
185 deletions
+125
-185
Mutex.h
include/Mutex.h
+12
-18
Mutex.cc
src/Various/Mutex.cc
+46
-166
Makefile.am
tests/Makefile.am
+5
-1
umutex.cc
tests/umutex.cc
+62
-0
No files found.
include/Mutex.h
View file @
5a73a16c
...
...
@@ -34,10 +34,6 @@
// -----------------------------------------------------------------------------------------
namespace
UniSetTypes
{
typedef
sig_atomic_t
mutex_atomic_t
;
// typedef _Atomic_word mutex_atomic_t;
/*! \class uniset_mutex
* \note \a lock() \a unlock() .
...
...
@@ -48,10 +44,14 @@ namespace UniSetTypes
{
public
:
uniset_mutex
();
uniset_mutex
(
std
::
string
name
);
uniset_mutex
(
const
std
::
string
name
);
~
uniset_mutex
();
bool
isRelease
();
inline
bool
isRelease
()
const
{
return
(
bool
)
release
;
};
void
lock
();
void
unlock
();
...
...
@@ -66,18 +66,13 @@ namespace UniSetTypes
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
;
omni_mutex
mtx
;
mutex_atomic_t
locked
;
volatile
sig_atomic_t
release
;
const
std
::
string
nm
;
omni_condition
*
ocond
;
omni_mutex
omutex
;
};
/*! \class uniset_mutex_lock
* \author Pavel Vainerman
* \date $Date: 2007/11/18 19:13:35 $
* \version $Id: Mutex.h,v 1.14 2007/11/18 19:13:35 vpashka Exp $
*
* . . \ref MutexHowToPage
* \note , lock ...
* \warning !
...
...
@@ -93,13 +88,13 @@ namespace UniSetTypes
private
:
uniset_mutex
*
mutex
;
mutex_atomic_t
mlock
;
uniset_mutex_lock
(
const
uniset_mutex_lock
&
);
uniset_mutex_lock
&
operator
=
(
const
uniset_mutex_lock
&
);
};
// -------------------------------------------------------------------------
class
uniset_spin_mutex
class
uniset_spin_mutex
:
public
uniset_mutex
{
public
:
uniset_spin_mutex
();
...
...
@@ -111,7 +106,6 @@ namespace UniSetTypes
void
unlock
();
private
:
mutex_atomic_t
m
;
};
// -------------------------------------------------------------------------
class
uniset_spin_lock
...
...
src/Various/Mutex.cc
View file @
5a73a16c
...
...
@@ -33,180 +33,100 @@
using
namespace
std
;
using
namespace
UniSetTypes
;
// -----------------------------------------------------------------------------
static
mutex_atomic_t
mutex_atomic_read
(
mutex_atomic_t
*
m
){
return
(
*
m
);
}
static
mutex_atomic_t
mutex_atomic_set
(
mutex_atomic_t
*
m
,
int
val
){
return
(
*
m
)
=
val
;
}
//static void mutex_atomic_inc( mutex_atomic_t* m ){ (*m)++; }
//static void mutex_atomic_dec( mutex_atomic_t* m ){ (*m)--; }
// -----------------------------------------------------------------------------
#ifndef HAVE_LINUX_LIBC_HEADERS_INCLUDE_LINUX_FUTEX_H
uniset_mutex
::
uniset_mutex
()
:
cnd
(
0
),
#define MUTEX_LOCK_SLEEP_MS 20
// -------------------------------------------------------------------------------------------
uniset_mutex
::
uniset_mutex
()
:
release
(
1
),
nm
(
""
)
{
mutex_atomic_set
(
&
locked
,
0
);
cnd
=
new
omni_condition
(
&
mtx
);
ocond
=
new
omni_condition
(
&
omutex
);
}
uniset_mutex
::
uniset_mutex
(
string
name
)
:
uniset_mutex
::
uniset_mutex
(
const
string
name
)
:
release
(
1
),
nm
(
name
)
{
mutex_atomic_set
(
&
locked
,
0
);
cnd
=
new
omni_condition
(
&
mtx
);
ocond
=
new
omni_condition
(
&
omutex
);
}
uniset_mutex
::~
uniset_mutex
()
{
unlock
();
mutex_atomic_set
(
&
locked
,
0
);
delete
cnd
;
delete
ocond
;
}
void
uniset_mutex
::
lock
()
{
sem
.
wait
()
;
mutex_atomic_set
(
&
locked
,
1
);
release
=
0
;
omutex
.
lock
(
);
}
void
uniset_mutex
::
unlock
()
{
mutex_atomic_set
(
&
locked
,
0
);
sem
.
post
();
cnd
->
signal
();
}
bool
uniset_mutex
::
isRelease
()
{
return
(
bool
)
!
mutex_atomic_read
(
&
locked
);
}
// -----------------------------------------------------------------------------
#else // HAVE_LINUX_FUTEX_H
// -----------------------------------------------------------------------------
// mutex futex
// http://kerneldump.110mb.com/dokuwiki/doku.php?id=wiki:futexes_are_tricky_p3
// : http://people.redhat.com/drepper/futex.pdf
uniset_mutex
::
uniset_mutex
()
:
val
(
0
),
nm
(
""
)
{
}
uniset_mutex
::
uiset_mutex
(
std
::
string
name
)
val
(
0
),
nm
(
name
)
{
}
uniset_mutex
::~
uniset_mutex
()
{
unlock
();
omutex
.
unlock
();
release
=
1
;
}
void
uniset_mutex
::
lock
()
{
int
c
;
if
(
(
c
=
cmpxchg
(
val
,
0
,
1
))
!=
0
)
{
do
{
if
(
c
==
2
||
cmpxchg
(
val
,
1
,
2
)
!=
0
)
futex_wait
(
&
val
,
2
);
}
while
(
(
c
=
cmpxchg
(
val
,
0
,
2
))
!=
0
);
}
}
void
uniset_mutex
::
unlock
()
{
if
(
atomic_dec
(
val
)
!=
1
)
{
val
=
0
;
futex_wake
(
&
val
,
1
);
}
}
bool
uniset_mutex
::
isRelease
()
{
return
(
bool
)
cmpxchg
(
val
,
1
,
2
);
}
// -----------------------------------------------------------------------------
#endif // HAVE_LINUX_FUTEX_H
// -----------------------------------------------------------------------------
const
uniset_mutex
&
uniset_mutex
::
operator
=
(
const
uniset_mutex
&
r
)
{
if
(
this
!=
&
r
)
locked
=
r
.
locked
;
{
release
=
r
.
release
;
if
(
release
)
unlock
();
else
lock
();
}
return
*
this
;
}
uniset_mutex
::
uniset_mutex
(
const
uniset_mutex
&
r
)
:
cnd
(
0
),
uniset_mutex
::
uniset_mutex
(
const
uniset_mutex
&
r
)
:
release
(
r
.
release
),
nm
(
r
.
nm
)
{
cnd
=
new
omni_condition
(
&
mtx
);
}
// -----------------------------------------------------------------------------
uniset_mutex_lock
::
uniset_mutex_lock
(
uniset_mutex
&
m
,
int
t
imeMS
)
:
// -----------------------------------------------------------------------------
--------------
uniset_mutex_lock
::
uniset_mutex_lock
(
uniset_mutex
&
m
,
int
t
_msec
)
:
mutex
(
&
m
)
{
if
(
timeMS
<=
0
||
mutex
->
isRelease
()
)
if
(
m
.
isRelease
()
)
{
m
utex
->
lock
();
mutex_atomic_set
(
&
mlock
,
1
);
m
.
lock
();
// cerr << "....locked.." << endl;
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
(
t_msec
>
0
)
{
if
(
!
mutex
->
name
().
empty
()
&&
unideb
.
debugging
(
Debug
::
type
(
Debug
::
LEVEL9
|
Debug
::
WARN
))
)
m
.
lock
();
unsigned
long
sec
,
msec
;
omni_thread
::
get_time
(
&
sec
,
&
msec
,
t_msec
/
1000
,(
t_msec
%
1000
)
*
1000000
);
// cerr << "....wait mutex msec=" << t_msec << endl;
// m.ocond->timedwait(sec, msec);
if
(
!
m
.
ocond
->
timedwait
(
sec
,
msec
)
)
{
unideb
[
Debug
::
type
(
Debug
::
LEVEL9
|
Debug
::
WARN
)]
<<
"(mutex_lock): "
<<
timeMS
<<
" msec "
<<
mutex
->
name
()
<<
endl
;
m
.
unlock
();
return
;
}
mutex_atomic_set
(
&
mlock
,
0
);
mutex
->
mtx
.
unlock
();
return
;
//
}
mutex_atomic_set
(
&
mlock
,
1
);
mutex
->
lock
();
mutex
->
mtx
.
unlock
();
}
// -----------------------------------------------------------------------------
bool
uniset_mutex_lock
::
lock_ok
()
{
return
(
bool
)
mutex_atomic_read
(
&
mlock
);
// m.lock();
}
// -------------------------------------------------------------------------------------------
uniset_mutex_lock
::~
uniset_mutex_lock
()
{
if
(
mutex_atomic_read
(
&
mlock
)
)
{
mutex_atomic_set
(
&
mlock
,
0
);
mutex
->
unlock
();
}
mutex
->
unlock
();
mutex
->
ocond
->
signal
();
}
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
--------------
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
();
unlock
();
}
uniset_spin_mutex
::~
uniset_spin_mutex
()
...
...
@@ -229,54 +149,14 @@ uniset_spin_mutex::uniset_spin_mutex( const uniset_spin_mutex& r )
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
);
lock
();
}
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
);
}
unlock
();
}
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
)
:
...
...
tests/Makefile.am
View file @
5a73a16c
...
...
@@ -2,7 +2,7 @@
# This file is part of the UniSet library #
############################################################################
noinst_PROGRAMS
=
passivetimer unixml ui
noinst_PROGRAMS
=
passivetimer unixml ui
umutex
passivetimer_SOURCES
=
passivetimer.cc
passivetimer_LDADD
=
$(top_builddir)
/lib/libUniSet.la
...
...
@@ -16,6 +16,10 @@ ui_SOURCES = ui.cc
ui_LDADD
=
$(top_builddir)
/lib/libUniSet.la
ui_CPPFLAGS
=
-I
$(top_builddir)
/include
umutex_SOURCES
=
umutex.cc
umutex_LDADD
=
$(top_builddir)
/lib/libUniSet.la
umutex_CPPFLAGS
=
-I
$(top_builddir)
/include
include
$(top_builddir)/conf/setting.mk
tests/umutex.cc
0 → 100644
View file @
5a73a16c
#include <string>
#include "Mutex.h"
#include "ThreadCreator.h"
#include "UniSetTypes.h"
using
namespace
std
;
using
namespace
UniSetTypes
;
uniset_mutex
m
;
class
MyClass
{
public
:
MyClass
(
const
std
::
string
name
)
:
nm
(
name
)
{
thr
=
new
ThreadCreator
<
MyClass
>
(
this
,
&
MyClass
::
thread
);
}
~
MyClass
()
{
delete
thr
;
}
void
execute
()
{
thr
->
start
();
}
protected
:
std
::
string
nm
;
void
thread
()
{
while
(
1
)
{
{
cerr
<<
nm
<<
": before release="
<<
m
.
isRelease
()
<<
endl
;
uniset_mutex_lock
l
(
m
,
5000
);
cerr
<<
nm
<<
": after release="
<<
m
.
isRelease
()
<<
endl
;
}
msleep
(
300
);
}
}
private
:
ThreadCreator
<
MyClass
>*
thr
;
};
int
main
(
int
argc
,
const
char
**
argv
)
{
MyClass
*
mc1
=
new
MyClass
(
"t1"
);
MyClass
*
mc2
=
new
MyClass
(
"t2"
);
// m.lock();
mc1
->
execute
();
msleep
(
200
);
mc2
->
execute
();
pause
();
// m.unlock();
return
0
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment