Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
LimaGroup
Lima
Commits
091b2201
Commit
091b2201
authored
Mar 05, 2021
by
Alejandro Homs Puron
Committed by
operator for beamline
Mar 06, 2021
Browse files
ThreadUtils: add OS error code information when throwing exceptions
parent
b2dd9e87
Changes
4
Hide whitespace changes
Inline
Side-by-side
common/src/Exceptions.cpp
View file @
091b2201
...
...
@@ -84,7 +84,7 @@ Exception::Exception(Layer layer, ErrorType err_type, const string& err_desc,
<<
getErrDesc
();
else
std
::
cerr
<<
"********* Exception("
<<
getErrType
()
<<
"): "
<<
getErrDesc
();
<<
getErrDesc
()
<<
" *********"
<<
std
::
endl
;
}
Layer
Exception
::
getLayer
()
const
...
...
common/src/ThreadUtils.cpp
View file @
091b2201
...
...
@@ -36,10 +36,20 @@
using
namespace
lima
;
inline
void
check_error
(
int
ret
,
const
char
*
desc
)
{
if
(
ret
==
0
)
return
;
std
::
ostringstream
os
;
os
<<
desc
<<
": "
<<
strerror
(
ret
)
<<
" ("
<<
ret
<<
")"
;
throw
LIMA_COM_EXC
(
Error
,
os
.
str
());
}
MutexAttr
::
MutexAttr
(
Type
type
)
{
i
f
(
pthread_mutexattr_init
(
&
m_mutex_attr
)
!=
0
)
throw
LIMA_COM_EXC
(
Error
,
"Error initializing mutex attr"
);
i
nt
ret
=
pthread_mutexattr_init
(
&
m_mutex_attr
)
;
check_error
(
ret
,
"Error initializing mutex attr"
);
try
{
setType
(
type
);
...
...
@@ -76,15 +86,15 @@ void MutexAttr::setType(Type type)
throw
LIMA_COM_EXC
(
InvalidValue
,
"Invalid MutexAttr type"
);
}
i
f
(
pthread_mutexattr_settype
(
&
m_mutex_attr
,
kind
)
!=
0
)
throw
LIMA_COM_EXC
(
Error
,
"Error setting mutex attr"
);
i
nt
ret
=
pthread_mutexattr_settype
(
&
m_mutex_attr
,
kind
)
;
check_error
(
ret
,
"Error setting mutex attr"
);
}
MutexAttr
::
Type
MutexAttr
::
getType
()
const
{
int
kind
;
i
f
(
pthread_mutexattr_gettype
(
&
m_mutex_attr
,
&
kind
)
!=
0
)
throw
LIMA_COM_EXC
(
Error
,
"Error getting mutex attr"
);
i
nt
ret
=
pthread_mutexattr_gettype
(
&
m_mutex_attr
,
&
kind
)
;
check_error
(
ret
,
"Error getting mutex attr"
);
switch
(
kind
)
{
case
PTHREAD_MUTEX_NORMAL
:
...
...
@@ -120,8 +130,8 @@ Mutex::Mutex(MutexAttr mutex_attr)
:
m_mutex_attr
(
mutex_attr
)
{
pthread_mutexattr_t
&
attr
=
m_mutex_attr
.
m_mutex_attr
;
i
f
(
pthread_mutex_init
(
&
m_mutex
,
&
attr
)
!=
0
)
throw
LIMA_COM_EXC
(
Error
,
"Error initializing mutex"
);
i
nt
ret
=
pthread_mutex_init
(
&
m_mutex
,
&
attr
)
;
check_error
(
ret
,
"Error initializing mutex"
);
}
Mutex
::~
Mutex
()
...
...
@@ -131,14 +141,14 @@ Mutex::~Mutex()
void
Mutex
::
lock
()
{
i
f
(
pthread_mutex_lock
(
&
m_mutex
)
!=
0
)
throw
LIMA_COM_EXC
(
Error
,
"Error locking mutex"
);
i
nt
ret
=
pthread_mutex_lock
(
&
m_mutex
)
;
check_error
(
ret
,
"Error locking mutex"
);
}
void
Mutex
::
unlock
()
{
i
f
(
pthread_mutex_unlock
(
&
m_mutex
)
!=
0
)
throw
LIMA_COM_EXC
(
Error
,
"Error unlocking mutex"
);
i
nt
ret
=
pthread_mutex_unlock
(
&
m_mutex
)
;
check_error
(
ret
,
"Error unlocking mutex"
);
}
bool
Mutex
::
tryLock
()
...
...
@@ -162,8 +172,8 @@ MutexAttr Mutex::getAttr()
Cond
::
Cond
()
:
m_mutex
(
MutexAttr
::
Normal
)
{
i
f
(
pthread_cond_init
(
&
m_cond
,
NULL
)
!=
0
)
throw
LIMA_COM_EXC
(
Error
,
"Error initializing condition"
);
i
nt
ret
=
pthread_cond_init
(
&
m_cond
,
NULL
)
;
check_error
(
ret
,
"Error initializing condition"
);
}
Cond
::~
Cond
()
...
...
@@ -194,15 +204,15 @@ bool Cond::wait(double timeout)
else
retcode
=
pthread_cond_wait
(
&
m_cond
,
&
m_mutex
.
m_mutex
);
if
(
retcode
&&
retcode
!=
ETIMEDOUT
)
throw
LIMA_COM_EXC
(
Error
,
"Error waiting for condition"
);
if
(
retcode
!=
ETIMEDOUT
)
check_error
(
retcode
,
"Error waiting for condition"
);
return
!
retcode
;
}
void
Cond
::
signal
()
{
i
f
(
pthread_cond_signal
(
&
m_cond
)
!=
0
)
throw
LIMA_COM_EXC
(
Error
,
"Error signaling condition"
);
i
nt
ret
=
pthread_cond_signal
(
&
m_cond
)
;
check_error
(
ret
,
"Error signaling condition"
);
}
void
Cond
::
acquire
()
...
...
@@ -217,8 +227,8 @@ void Cond::release()
void
Cond
::
broadcast
()
{
if
(
pthread_cond_broadcast
(
&
m_cond
)
!=
0
)
throw
LIMA_COM_EXC
(
Error
,
"Error broadcast condition"
);
int
ret
=
pthread_cond_broadcast
(
&
m_cond
)
;
check_error
(
ret
,
"Error broadcast condition"
);
}
pid_t
lima
::
GetThreadID
()
{
...
...
@@ -241,7 +251,9 @@ Thread::ExceptionCleanUp::~ExceptionCleanUp()
m_thread
.
m_exception_handled
=
true
;
}
Thread
::
Thread
()
:
m_thread
(
NULL
),
m_started
(
false
),
m_finished
(
false
),
m_exception_handled
(
false
),
m_tid
(
0
)
Thread
::
Thread
()
:
m_thread
(
0
),
m_started
(
false
),
m_finished
(
false
),
m_exception_handled
(
false
),
m_tid
(
0
)
{
pthread_attr_init
(
&
m_thread_attr
);
}
...
...
@@ -259,8 +271,9 @@ void Thread::start()
throw
LIMA_COM_EXC
(
Error
,
"Thread already started"
);
m_finished
=
false
;
if
(
pthread_create
(
&
m_thread
,
&
m_thread_attr
,
staticThreadFunction
,
this
)
!=
0
)
throw
LIMA_HW_EXC
(
Error
,
"Error creating thread"
);
int
ret
=
pthread_create
(
&
m_thread
,
&
m_thread_attr
,
staticThreadFunction
,
this
);
check_error
(
ret
,
"Error creating thread"
);
m_started
=
true
;
}
...
...
@@ -418,7 +431,7 @@ void CmdThread::sendCmdIf(int cmd, bool (*if_test)(int,int))
void
CmdThread
::
doSendCmd
(
int
cmd
)
{
if
(
m_status
==
Finished
)
throw
LIMA_
HW
_EXC
(
Error
,
"Thread has Finished"
);
throw
LIMA_
COM
_EXC
(
Error
,
"Thread has Finished"
);
// Assume that we will have a call to waitStatus somewhere after the new command
m_status_history
.
reset
();
...
...
common/test/CMakeLists.txt
View file @
091b2201
...
...
@@ -22,7 +22,7 @@
set
(
test_src test_membuffer test_ordered_map
)
if
(
NOT WIN32
)
list
(
APPEND test_src test_regex
)
list
(
APPEND test_src test_regex
test_mutex
)
endif
()
limatools_run_camera_tests
(
"
${
test_src
}
"
${
NAME
}
)
common/test/test_mutex.cpp
0 → 100644
View file @
091b2201
#include
"lima/ThreadUtils.h"
#include
"lima/Debug.h"
#include
"lima/Exceptions.h"
using
namespace
lima
;
DEB_GLOBAL
(
DebModTest
);
class
TestThread
:
public
Thread
{
DEB_CLASS
(
DebModTest
,
"TestThread"
);
public:
enum
Type
{
Tester
,
Locker
,
Releaser
};
TestThread
(
Type
type
)
:
m_type
(
type
),
m_started
(
false
)
{
DEB_CONSTRUCTOR
();
DEB_PARAM
()
<<
DEB_VAR1
(
type
);
}
void
start
()
{
DEB_MEMBER_FUNCT
();
Thread
::
start
();
AutoMutex
l
(
m_cond
.
mutex
());
while
(
!
m_started
)
m_cond
.
wait
();
DEB_TRACE
()
<<
"Main: started"
;
}
protected:
void
threadFunction
()
{
DEB_MEMBER_FUNCT
();
CleanUp
cleanup
(
*
this
);
if
(
m_type
==
Tester
)
THROW_COM_ERROR
(
Error
)
<<
"Testing cleanup"
;
if
(
m_type
==
Locker
)
{
AutoMutex
l
(
m_mutex
);
l
.
leaveLocked
();
DEB_TRACE
()
<<
"Leave locked"
;
}
else
{
AutoMutex
l
(
m_mutex
,
AutoMutex
::
PrevLocked
);
DEB_TRACE
()
<<
"Previously unlocked"
;
}
}
private:
class
CleanUp
:
public
ExceptionCleanUp
{
public:
using
ExceptionCleanUp
::
ExceptionCleanUp
;
virtual
~
CleanUp
()
{
static_cast
<
TestThread
&>
(
m_thread
).
signalStarted
();
}
};
void
signalStarted
()
{
DEB_MEMBER_FUNCT
();
AutoMutex
l
(
m_cond
.
mutex
());
m_started
=
true
;
m_cond
.
signal
();
DEB_TRACE
()
<<
"Thread: started"
;
}
static
Mutex
m_mutex
;
Type
m_type
;
Cond
m_cond
;
bool
m_started
;
};
Mutex
TestThread
::
m_mutex
;
int
main
(
int
argc
,
char
*
argv
[])
{
DEB_GLOBAL_FUNCT
();
DebParams
::
setTypeFlags
(
DebParams
::
AllFlags
);
DEB_TRACE
()
<<
"Starting test"
;
TestThread
tester
(
TestThread
::
Tester
);
tester
.
start
();
tester
.
join
();
TestThread
locker
(
TestThread
::
Locker
);
TestThread
releaser
(
TestThread
::
Releaser
);
locker
.
start
();
releaser
.
start
();
locker
.
join
();
releaser
.
join
();
return
0
;
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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