24 #include "rmh_monitor.h"
29 eventHandle=malloc(
sizeof(*eventHandle));
30 if (!eventHandle)
return NULL;
31 memset(eventHandle, 0,
sizeof(*eventHandle));
33 if (pthread_mutex_init(&eventHandle->lock, NULL) != 0) {
37 if (pthread_cond_init(&eventHandle->cond, NULL) != 0) {
38 pthread_mutex_destroy (&eventHandle->lock);
42 eventHandle->signal =
false;
48 pthread_mutex_destroy (&eventHandle->lock);
49 pthread_cond_destroy (&eventHandle->cond);
55 pthread_mutex_lock(&eventHandle->lock);
56 eventHandle->signal = false ;
57 pthread_mutex_unlock(&eventHandle->lock);
62 pthread_mutex_lock(&eventHandle->lock);
63 eventHandle->signal =
true;
64 pthread_cond_signal(&eventHandle->cond);
65 pthread_mutex_unlock(&eventHandle->lock);
69 RMH_Result RMHMonitor_Semaphore_WaitTimeout(
RMHMonitor_hSemaphore eventHandle,
const int timeoutMsec) {
71 struct timespec timeout;
72 RMH_Result result=RMH_FAILURE;
74 pthread_mutex_lock(&eventHandle->lock);
75 if (eventHandle->signal) {
77 eventHandle->signal =
false;
81 gettimeofday(&now, NULL);
82 timeout.tv_nsec = now.tv_usec * 1000 + (timeoutMsec%1000)*1000000;
83 timeout.tv_sec = now.tv_sec + (timeoutMsec/1000);
84 if (timeout.tv_nsec >= 1000000000) {
85 timeout.tv_nsec -= 1000000000;
89 int rc = pthread_cond_timedwait(&eventHandle->cond, &eventHandle->lock, &timeout);
90 if (eventHandle->signal) {
104 result = RMH_FAILURE;
107 }
while(!eventHandle->signal);
109 eventHandle->signal =
false;
111 pthread_mutex_unlock(&eventHandle->lock);
119 void RMHMonitor_Queue_Enqueue(
RMHMonitor *app,
const enum RMH_Event event,
const struct RMH_EventData *eventData) {
125 gettimeofday(&cbE->eventTime, NULL);
127 cbE->eventData = *eventData;
130 pthread_mutex_lock(&app->eventQueueProtect);
131 TAILQ_INSERT_HEAD(&app->eventQueue, cbE, entries);
132 RMH_PrintDbg(
"%s[%u] ENQUEUED event '%s' in %p\n", __FUNCTION__, __LINE__,
RMH_EventToString(cbE->event, printBuff,
sizeof(printBuff)/
sizeof(printBuff[0])), cbE);
133 pthread_mutex_unlock(&app->eventQueueProtect);
134 RMHMonitor_Semaphore_Signal(app->eventNotify);
142 void RMHMonitor_Queue_Dequeue(
RMHMonitor *app) {
147 pthread_mutex_lock(&app->eventQueueProtect);
148 cbE=app->eventQueue.tqh_first;
150 TAILQ_REMOVE(&app->eventQueue, app->eventQueue.tqh_first, entries);
151 RMH_PrintDbg(
"%s[%u] DEQUEUED event '%s' in %p\n", __FUNCTION__, __LINE__,
RMH_EventToString(cbE->event, printBuff,
sizeof(printBuff)/
sizeof(printBuff[0])), cbE);
154 pthread_mutex_unlock(&app->eventQueueProtect);