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