Add signed integer parser to eliminate compiler errors
[trust_router.git] / common / tr_mq.c
index 6606be3..6676880 100644 (file)
@@ -34,6 +34,8 @@
 
 #include <talloc.h>
 #include <pthread.h>
+#include <time.h>
+#include <errno.h>
 
 #include <tr_mq.h>
 #include <tr_debug.h>
@@ -47,12 +49,11 @@ static int tr_mq_msg_destructor(void *object)
   return 0;
 }
 
-TR_MQ_MSG *tr_mq_msg_new(TALLOC_CTX *mem_ctx, const char *message, TR_MQ_PRIORITY prio)
+TR_MQ_MSG *tr_mq_msg_new(TALLOC_CTX *mem_ctx, const char *message)
 {
   TR_MQ_MSG *msg=talloc(mem_ctx, TR_MQ_MSG);
   if (msg!=NULL) {
     msg->next=NULL;
-    msg->prio=prio;
     msg->message=talloc_strdup(msg, message);
     if (msg->message==NULL) {
       talloc_free(msg);
@@ -70,11 +71,6 @@ void tr_mq_msg_free(TR_MQ_MSG *msg)
     talloc_free(msg);
 }
 
-TR_MQ_PRIORITY tr_mq_msg_get_prio(TR_MQ_MSG *msg)
-{
-  return msg->prio;
-}
-
 const char *tr_mq_msg_get_message(TR_MQ_MSG *msg)
 {
   return msg->message;
@@ -107,12 +103,21 @@ static void tr_mq_msg_set_next(TR_MQ_MSG *msg, TR_MQ_MSG *next)
 TR_MQ *tr_mq_new(TALLOC_CTX *mem_ctx)
 {
   TR_MQ *mq=talloc(mem_ctx, TR_MQ);
+  pthread_condattr_t cattr;
+
   if (mq!=NULL) {
     pthread_mutex_init(&(mq->mutex), 0);
-    pthread_cond_init(&(mq->have_msg_cond), 0);
+    pthread_condattr_init(&cattr);
+
+    pthread_condattr_setclock(&cattr, CLOCK_MONOTONIC); /* use the monotonic clock for timeouts */
+    pthread_cond_init(&(mq->have_msg_cond), &cattr);
+    pthread_condattr_destroy(&cattr);
+
     mq->head=NULL;
     mq->tail=NULL;
-    mq->last_hi_prio=NULL;
+
+    mq->notify_cb=NULL;
+    mq->notify_cb_arg=NULL;
   }
   return mq;
 }
@@ -196,22 +201,6 @@ static void tr_mq_append(TR_MQ *mq, TR_MQ_MSG *msg)
   talloc_steal(mq, msg);
 }
 
-static void tr_mq_append_high_prio(TR_MQ *mq, TR_MQ_MSG *new)
-{
-  if (tr_mq_get_head(mq)==NULL) {
-    tr_mq_set_head(mq, new);
-    tr_mq_set_tail(mq, new);
-  } else if (mq->last_hi_prio==NULL) {
-    tr_mq_msg_set_next(new, tr_mq_get_head(mq)); /* add to front of list */
-    tr_mq_set_head(mq, new); /* update head of list */
-  } else {
-    tr_mq_msg_set_next(new, tr_mq_msg_get_next(mq->last_hi_prio));
-    tr_mq_msg_set_next(mq->last_hi_prio, new); /* add to end of hi prio msgs */
-  }
-  mq->last_hi_prio=new; /* in any case, this is now the last high priority msg */
-  talloc_steal(mq,new);
-}
-
 #define DEBUG_TR_MQ 0
 #if DEBUG_TR_MQ
 static void tr_mq_print(TR_MQ *mq)
@@ -222,8 +211,8 @@ static void tr_mq_print(TR_MQ *mq)
   tr_debug("tr_mq_print: mq contents:");
   while(m!=NULL) {
     ii++;
-    tr_debug("tr_mq_print: Entry %02d: %-15s (prio %d)",
-             ii, tr_mq_msg_get_message(m), tr_mq_msg_get_prio(m));
+    tr_debug("tr_mq_print: Entry %02d: %-15s",
+             ii, tr_mq_msg_get_message(m));
     m=tr_mq_msg_get_next(m);
   }
 }
@@ -237,14 +226,8 @@ void tr_mq_add(TR_MQ *mq, TR_MQ_MSG *msg)
   tr_mq_lock(mq);
   
   was_empty=tr_mq_empty(mq);
-  switch (tr_mq_msg_get_prio(msg)) {
-  case TR_MQ_PRIO_HIGH:
-    tr_mq_append_high_prio(mq, msg);
-    break;
-  default:
-    tr_mq_append(mq, msg);
-    break;
-  }
+  tr_mq_append(mq, msg);
+
   /* before releasing the mutex, get notify_cb data out of mq */
   notify_cb=mq->notify_cb;
   notify_cb_arg=mq->notify_cb_arg;
@@ -266,27 +249,40 @@ void tr_mq_add(TR_MQ *mq, TR_MQ_MSG *msg)
     notify_cb(mq, notify_cb_arg);
 }
 
-/* Caller must free msg via tr_mq_msg_free, waiting up to maxwait seconds (0 for non-blocking).
- * Not guaranteed to wait if an error occurs - immediately returns without a message. */
-TR_MQ_MSG *tr_mq_pop(TR_MQ *mq, time_t maxwait)
+/* Compute an absolute time from a desired timeout interval for use with tr_mq_pop().
+ * Fills in *ts and returns 0 on success. */
+int tr_mq_pop_timeout(time_t seconds, struct timespec *ts)
+{
+  if (0!=clock_gettime(CLOCK_MONOTONIC, ts))
+    return -1;
+
+  ts->tv_sec+=seconds;
+  return 0;
+}
+
+/* Retrieves a message from the queue, waiting until absolute
+ * time ts_abort before giving up (using CLOCK_MONOTONIC). If ts_abort
+ * has passed, returns an existing message but will not wait if one is
+ * not already available. If ts_abort is null, no blocking.  Not
+ * guaranteed to wait if an error occurs - immediately returns without
+ * a message. Use tr_mq_pop_timeout() to get an absolute time that
+ * is guaranteed compatible with this function.
+ *
+ * Caller should free msg via tr_mq_msg_free when done with it. It stays
+ * in the TR_MQ's context, though, so use talloc_steal() if you want to do
+ * something clever with it. */
+TR_MQ_MSG *tr_mq_pop(TR_MQ *mq, struct timespec *ts_abort)
 {
   TR_MQ_MSG *popped=NULL;
   int wait_err=0;
-  struct timespec expiration={0};
-
+  
   tr_mq_lock(mq);
-  if ((tr_mq_get_head(mq)==NULL) && (maxwait>0)) {
+  if ((tr_mq_get_head(mq)==NULL) && (ts_abort!=NULL)) {
     /* No msgs yet, and blocking was requested */
-    if (0!=clock_gettime(CLOCK_REALTIME, &expiration)) {
-      tr_notice("tr_mq_pop: error reading realtime clock.");
-      return NULL;
-    }
-    expiration.tv_sec+=maxwait;
-
     while ((wait_err==0) && (NULL==tr_mq_get_head(mq)))
       wait_err=pthread_cond_timedwait(&(mq->have_msg_cond),
                                      &(mq->mutex),
-                                     &expiration);
+                                     ts_abort);
     
     if ((wait_err!=0) && (wait_err!=ETIMEDOUT)) {
       tr_notice("tr_mq_pop: error waiting for message.");
@@ -299,9 +295,6 @@ TR_MQ_MSG *tr_mq_pop(TR_MQ *mq, time_t maxwait)
     popped=tr_mq_get_head(mq);
     tr_mq_set_head(mq, tr_mq_msg_get_next(popped)); /* popped is the old head */
 
-    if (popped==mq->last_hi_prio)
-      mq->last_hi_prio=NULL;
-
     if (tr_mq_get_head(mq)==NULL)
       tr_mq_set_tail(mq, NULL); /* just popped the last element */
   }