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
71b0da8b
Commit
71b0da8b
authored
Oct 01, 2009
by
Alexander Morozov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use mutexes instead of spin locks
parent
6956a729
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
12 additions
and
134 deletions
+12
-134
Mutex.h
include/Mutex.h
+5
-26
Mutex.cc
src/Various/Mutex.cc
+7
-108
No files found.
include/Mutex.h
View file @
71b0da8b
...
@@ -59,13 +59,14 @@ namespace UniSetTypes
...
@@ -59,13 +59,14 @@ 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
...
...
src/Various/Mutex.cc
View file @
71b0da8b
...
@@ -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
// -----------------------------------------------------------------------------
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