Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
FORMUS3IC_LAS3
/
embb
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
8b03daef
authored
Nov 09, 2015
by
Christian Kern
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
work on review comments
parent
6f87ec95
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
61 additions
and
49 deletions
+61
-49
base_c/include/embb/base/c/mutex.h
+4
-2
base_c/src/mutex.c
+8
-12
base_cpp/include/embb/base/internal/mutex-inl.h
+2
-0
base_cpp/include/embb/base/mutex.h
+9
-35
base_cpp/src/mutex.cc
+38
-0
No files found.
base_c/include/embb/base/c/mutex.h
View file @
8b03daef
...
@@ -163,6 +163,7 @@ void embb_mutex_destroy(
...
@@ -163,6 +163,7 @@ void embb_mutex_destroy(
/**
/**
* Initializes a spinlock
* Initializes a spinlock
*
*
* \pre \c spinlock is uninitialized
* \post \c spinlock is initialized
* \post \c spinlock is initialized
* \return EMBB_SUCCESS if spinlock could be initialized \n
* \return EMBB_SUCCESS if spinlock could be initialized \n
* EMBB_ERROR otherwise
* EMBB_ERROR otherwise
...
@@ -206,8 +207,9 @@ int embb_spin_try_lock(
...
@@ -206,8 +207,9 @@ int embb_spin_try_lock(
embb_spinlock_t
*
spinlock
,
embb_spinlock_t
*
spinlock
,
/**< [IN/OUT] Pointer to spinlock */
/**< [IN/OUT] Pointer to spinlock */
unsigned
int
max_number_spins
unsigned
int
max_number_spins
/**< [IN] Number of attempts the locking operation is repeated if
/**< [IN] Maximal count of spins to perform, trying to acquire lock. Note that
* unsuccessful */
* passing 0 here results in not trying to obtain the lock at all.
*/
);
);
/**
/**
...
...
base_c/src/mutex.c
View file @
8b03daef
...
@@ -117,9 +117,9 @@ void embb_mutex_destroy(embb_mutex_t* mutex) {
...
@@ -117,9 +117,9 @@ void embb_mutex_destroy(embb_mutex_t* mutex) {
#endif
/* EMBB_PLATFORM_THREADING_POSIXTHREADS */
#endif
/* EMBB_PLATFORM_THREADING_POSIXTHREADS */
int
embb_spin_init
(
embb_spinlock_t
*
spinlock
)
{
int
embb_spin_init
(
embb_spinlock_t
*
spinlock
)
{
//
for now, just assign the internal struct value... in the future,
//
For now, store the initial value. In the future will use atomic init
//
we will have an atomic init function
.
//
function (as soon as available)
.
spinlock
->
atomic_spin_variable_
.
internal_variable
=
0
;
embb_atomic_store_int
(
&
spinlock
->
atomic_spin_variable_
,
0
)
;
}
}
int
embb_spin_lock
(
embb_spinlock_t
*
spinlock
)
{
int
embb_spin_lock
(
embb_spinlock_t
*
spinlock
)
{
...
@@ -128,9 +128,6 @@ int embb_spin_lock(embb_spinlock_t* spinlock) {
...
@@ -128,9 +128,6 @@ int embb_spin_lock(embb_spinlock_t* spinlock) {
// try to swap the
// try to swap the
while
(
0
==
embb_atomic_compare_and_swap_int
(
while
(
0
==
embb_atomic_compare_and_swap_int
(
&
spinlock
->
atomic_spin_variable_
,
&
expected
,
1
))
{
&
spinlock
->
atomic_spin_variable_
,
&
expected
,
1
))
{
// mtapi has a debug variable, counting spins... think about that...
// embb_atomic_fetch_and_add_int(&embb_mtapi_spinlock_spins, 1);
// reset expected, as CAS might change it...
// reset expected, as CAS might change it...
expected
=
0
;
expected
=
0
;
}
}
...
@@ -139,16 +136,15 @@ int embb_spin_lock(embb_spinlock_t* spinlock) {
...
@@ -139,16 +136,15 @@ int embb_spin_lock(embb_spinlock_t* spinlock) {
int
embb_spin_try_lock
(
embb_spinlock_t
*
spinlock
,
int
embb_spin_try_lock
(
embb_spinlock_t
*
spinlock
,
unsigned
int
max_number_spins
)
{
unsigned
int
max_number_spins
)
{
if
(
max_number_spins
==
0
)
return
EMBB_BUSY
;
int
expected
=
0
;
int
expected
=
0
;
unsigned
int
spin_count
=
max_number_spins
;
while
(
0
==
embb_atomic_compare_and_swap_int
(
while
(
0
==
embb_atomic_compare_and_swap_int
(
&
spinlock
->
atomic_spin_variable_
,
&
spinlock
->
atomic_spin_variable_
,
&
expected
,
1
))
{
&
expected
,
1
))
{
// mtapi has a debug variable, counting spins... think about that...
max_number_spins
--
;
// embb_atomic_fetch_and_add_int(&embb_mtapi_spinlock_spins, 1);
if
(
0
==
max_number_spins
)
{
spin_count
--
;
if
(
0
==
spin_count
)
{
return
EMBB_BUSY
;
return
EMBB_BUSY
;
}
}
expected
=
0
;
expected
=
0
;
...
...
base_cpp/include/embb/base/internal/mutex-inl.h
View file @
8b03daef
...
@@ -114,6 +114,8 @@ bool UniqueLock<Mutex>::OwnsLock() const {
...
@@ -114,6 +114,8 @@ bool UniqueLock<Mutex>::OwnsLock() const {
return
locked_
;
return
locked_
;
}
}
}
// namespace base
}
// namespace base
}
// namespace embb
}
// namespace embb
...
...
base_cpp/include/embb/base/mutex.h
View file @
8b03daef
...
@@ -180,18 +180,14 @@ class Spinlock {
...
@@ -180,18 +180,14 @@ class Spinlock {
*
*
* \notthreadsafe
* \notthreadsafe
*/
*/
Spinlock
()
{
Spinlock
();
embb_spin_init
(
&
spinlock_
);
}
/**
/**
* Destructs a spinlock.
* Destructs a spinlock.
*
*
* \notthreadsafe
* \notthreadsafe
*/
*/
~
Spinlock
()
{
~
Spinlock
();
embb_spin_destroy
(
&
spinlock_
);
}
/**
/**
* Waits until the spinlock can be locked and locks it.
* Waits until the spinlock can be locked and locks it.
...
@@ -201,15 +197,7 @@ class Spinlock {
...
@@ -201,15 +197,7 @@ class Spinlock {
* \threadsafe
* \threadsafe
* \see TryLock(), Unlock()
* \see TryLock(), Unlock()
*/
*/
void
Lock
()
{
void
Lock
();
int
status
=
embb_spin_lock
(
&
spinlock_
);
// Currently, embb_spin_lock will always return EMBB_SUCCESS. However,
// This might change.
if
(
status
!=
EMBB_SUCCESS
)
{
EMBB_THROW
(
ErrorException
,
"Error in embb_spin_lock"
);
}
}
/**
/**
* Tries to lock the spinlock for \c number_spins times and returns.
* Tries to lock the spinlock for \c number_spins times and returns.
...
@@ -222,19 +210,11 @@ class Spinlock {
...
@@ -222,19 +210,11 @@ class Spinlock {
*/
*/
bool
TryLock
(
bool
TryLock
(
unsigned
int
number_spins
=
1
unsigned
int
number_spins
=
1
/**< [IN] Maximal spins to perform, trying to acquire lock */
/**< [IN] Maximal count of spins to perform, trying to acquire lock.
)
{
* Note that passing 0 here results in not trying to obtain the lock at all.
int
status
=
embb_spin_try_lock
(
&
spinlock_
,
number_spins
);
* Default parameter is 1.
*/
if
(
status
==
EMBB_BUSY
){
);
return
false
;
}
else
if
(
status
!=
EMBB_SUCCESS
)
{
EMBB_THROW
(
ErrorException
,
"Error in embb_spin_try_lock"
);
}
return
true
;
}
/**
/**
* Unlocks the spinlock.
* Unlocks the spinlock.
...
@@ -244,13 +224,7 @@ class Spinlock {
...
@@ -244,13 +224,7 @@ class Spinlock {
* \threadsafe
* \threadsafe
* \see Lock(), TryLock()
* \see Lock(), TryLock()
*/
*/
void
Unlock
()
{
void
Unlock
();
int
status
=
embb_spin_unlock
(
&
spinlock_
);
if
(
status
!=
EMBB_SUCCESS
)
{
EMBB_THROW
(
ErrorException
,
"Error in embb_spin_unlock"
);
}
}
private
:
private
:
/**
/**
...
...
base_cpp/src/mutex.cc
View file @
8b03daef
...
@@ -60,5 +60,43 @@ Mutex::Mutex() : MutexBase(EMBB_MUTEX_PLAIN) {
...
@@ -60,5 +60,43 @@ Mutex::Mutex() : MutexBase(EMBB_MUTEX_PLAIN) {
RecursiveMutex
::
RecursiveMutex
()
:
MutexBase
(
EMBB_MUTEX_RECURSIVE
)
{
RecursiveMutex
::
RecursiveMutex
()
:
MutexBase
(
EMBB_MUTEX_RECURSIVE
)
{
}
}
Spinlock
::
Spinlock
()
{
embb_spin_init
(
&
spinlock_
);
}
Spinlock
::~
Spinlock
()
{
embb_spin_destroy
(
&
spinlock_
);
}
void
Spinlock
::
Lock
()
{
int
status
=
embb_spin_lock
(
&
spinlock_
);
// Currently, embb_spin_lock will always return EMBB_SUCCESS. However,
// This might change.
if
(
status
!=
EMBB_SUCCESS
)
{
EMBB_THROW
(
ErrorException
,
"Error in embb_spin_lock"
);
}
}
bool
Spinlock
::
TryLock
(
unsigned
int
number_spins
)
{
int
status
=
embb_spin_try_lock
(
&
spinlock_
,
number_spins
);
if
(
status
==
EMBB_BUSY
){
return
false
;
}
else
if
(
status
!=
EMBB_SUCCESS
)
{
EMBB_THROW
(
ErrorException
,
"Error in embb_spin_try_lock"
);
}
return
true
;
}
void
Spinlock
::
Unlock
()
{
int
status
=
embb_spin_unlock
(
&
spinlock_
);
if
(
status
!=
EMBB_SUCCESS
)
{
EMBB_THROW
(
ErrorException
,
"Error in embb_spin_unlock"
);
}
}
}
// namespace base
}
// namespace base
}
// namespace embb
}
// namespace embb
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