API
semUtilsDerived.hpp
Go to the documentation of this file.
1 /** \file semUtilsDerived.hpp
2  * \brief XWC app semaphore Utilities to use CRTP derived classes
3  * \author Jared R. Males (jaredmales@gmail.com)
4  *
5  *
6  * \ingroup app_files
7  */
8 
9 #ifndef app_semUtilsDerived_hpp
10 #define app_semUtilsDerived_hpp
11 
12 /// Add the wait time to a timespec for a sem_timedwait call, with no value returned on error, using the derived class
13 /** An error would be generated by clock_gettime.
14  *
15  * \param ts is the timespec to modify, should be set to current time
16  * \param sec is the number of seconds to add to ts
17  * \param nsec is the number of nanoseconds to add to ts
18  *
19  */
20 #define XWC_SEM_WAIT_TS_RETVOID_DERIVED( ts, sec, nsec ) \
21  if(clock_gettime(CLOCK_REALTIME, &ts) < 0) \
22  { \
23  derivedT::template log<software_critical>({__FILE__,__LINE__,errno,0,"clock_gettime"}); \
24  return; /*will trigger a shutdown*/ \
25  } \
26  ts.tv_sec += sec; \
27  mx::sys::timespecAddNsec(ts, nsec);
28 
29 /// Add the wait time to a timespec for a sem_timedwait call, with -1 returned on error.
30 /** An error would be generated by clock_gettime
31  *
32  * \param ts is the timespec to modify, should be set to current time
33  * \param sec is the number of seconds to add to ts
34  * \param nsec is the number of nanoseconds to add to ts
35  *
36  */
37 #define XWC_SEM_WAIT_TS_DERIVED( ts, sec, nsec ) \
38  if(clock_gettime(CLOCK_REALTIME, &ts) < 0) \
39  { \
40  derivedT::template log<software_critical>({__FILE__,__LINE__,errno,0,"clock_gettime"}); \
41  return -1; \
42  } \
43  ts.tv_sec += sec; \
44  mx::sys::timespecAddNsec(ts, nsec);
45 
46 /// Perform a sem_timedwait in the context of a standard loop in MagAO-X code using the derived class
47 /**
48  * \param sem the semaphore
49  * \param ts the timespec with the time to wait until
50  *
51  */
52 #define XWC_SEM_TIMEDWAIT_LOOP_DERIVED( sem, ts ) \
53  if(sem_timedwait(&sem, &ts) != 0) \
54  { \
55  /* Check for why we timed out */ \
56  /* EINTER probably indicates time to shutdown, loop wil exit if m_shutdown is set */ \
57  /* ETIMEDOUT just means keep waiting */ \
58  if(errno == EINTR || errno == ETIMEDOUT) continue; \
59  \
60  /*Otherwise, report an error.*/ \
61  derivedT::template log<software_error>({__FILE__, __LINE__,errno, "sem_timedwait"}); \
62  break; \
63  }
64 
65 #define XWC_SEM_FLUSH_DERIVED( sem ) \
66 { \
67  int semval; \
68  if(sem_getvalue( &sem, &semval)<0) \
69  { \
70  return derivedT::template log<software_error,-1>({__FILE__, __LINE__,errno, "sem_getvalue"}); \
71  } \
72  for(int i = 0; i < semval; ++i) \
73  { \
74  if(sem_trywait(&sem) != 0) \
75  { \
76  if(errno == EAGAIN) break; \
77  return derivedT::template log<software_error,-1>({__FILE__, __LINE__,errno, "sem_trywait"}); \
78  } \
79  } \
80 }
81 
82 
83 #endif //app_semUtilsDerived_hpp