diff mbox

[v2,09/16] api: timer: Use odp_event_t instead of odp_buffer_t

Message ID 1421752692-24439-10-git-send-email-petri.savolainen@linaro.org
State New
Headers show

Commit Message

Petri Savolainen Jan. 20, 2015, 11:18 a.m. UTC
Changed timer API to use odp_event_t instead of odp_buffer_t.

Signed-off-by: Petri Savolainen <petri.savolainen@linaro.org>
---
 example/timer/odp_timer_test.c                 | 28 ++++++------
 platform/linux-generic/include/api/odp_timer.h | 63 ++++++++++----------------
 platform/linux-generic/odp_timer.c             | 28 ++++--------
 test/validation/odp_timer.c                    | 55 +++++++++++-----------
 4 files changed, 77 insertions(+), 97 deletions(-)
diff mbox

Patch

diff --git a/example/timer/odp_timer_test.c b/example/timer/odp_timer_test.c
index 69cde3e..f4fa9a2 100644
--- a/example/timer/odp_timer_test.c
+++ b/example/timer/odp_timer_test.c
@@ -62,8 +62,8 @@  static const char *timerset2str(odp_timer_set_t val)
 		return "too early";
 	case ODP_TIMER_TOOLATE:
 		return "too late";
-	case ODP_TIMER_NOBUF:
-		return "no buffer";
+	case ODP_TIMER_NOEVENT:
+		return "no event";
 	default:
 		return "?";
 	}
@@ -72,7 +72,7 @@  static const char *timerset2str(odp_timer_set_t val)
 /** @private Helper struct for timers */
 struct test_timer {
 	odp_timer_t tim;
-	odp_buffer_t buf;
+	odp_event_t ev;
 };
 
 /** @private Array of all timer helper structs */
@@ -86,6 +86,7 @@  static void test_abs_timeouts(int thr, test_args_t *args)
 	odp_queue_t queue;
 	uint64_t tick;
 	struct test_timer *ttp;
+	odp_buffer_t buf;
 
 	EXAMPLE_DBG("  [%i] test_timeouts\n", thr);
 
@@ -106,11 +107,12 @@  static void test_abs_timeouts(int thr, test_args_t *args)
 		EXAMPLE_ERR("Failed to allocate timer\n");
 		return;
 	}
-	ttp->buf = odp_buffer_alloc(pool);
-	if (ttp->buf == ODP_BUFFER_INVALID) {
+	buf = odp_buffer_alloc(pool);
+	if (buf == ODP_BUFFER_INVALID) {
 		EXAMPLE_ERR("Failed to allocate buffer\n");
 		return;
 	}
+	ttp->ev = odp_buffer_to_event(buf);
 	tick = odp_timer_current_tick(tp);
 
 	while ((int)odp_atomic_load_u32(&remain) > 0) {
@@ -118,7 +120,7 @@  static void test_abs_timeouts(int thr, test_args_t *args)
 		odp_timer_set_t rc;
 
 		tick += period;
-		rc = odp_timer_set_abs(ttp->tim, tick, &ttp->buf);
+		rc = odp_timer_set_abs(ttp->tim, tick, &ttp->ev);
 		if (odp_unlikely(rc != ODP_TIMER_SUCCESS)) {
 			/* Too early or too late timeout requested */
 			EXAMPLE_ABORT("odp_timer_set_abs() failed: %s\n",
@@ -134,14 +136,14 @@  static void test_abs_timeouts(int thr, test_args_t *args)
 		if (ev == ODP_EVENT_INVALID)
 			continue; /* Re-check the remain counter */
 		if (odp_event_type(ev) != ODP_EVENT_TIMEOUT) {
-			/* Not a default timeout buffer */
+			/* Not a default timeout event */
 			EXAMPLE_ABORT("Unexpected event type (%u) received\n",
 				      odp_event_type(ev));
 		}
 		odp_timeout_t tmo = odp_timeout_from_event(ev);
 		tick = odp_timeout_tick(tmo);
 		ttp = odp_timeout_user_ptr(tmo);
-		ttp->buf = odp_buffer_from_event(ev);
+		ttp->ev = ev;
 		if (!odp_timeout_fresh(tmo)) {
 			/* Not the expected expiration tick, timer has
 			 * been reset or cancelled or freed */
@@ -154,12 +156,12 @@  static void test_abs_timeouts(int thr, test_args_t *args)
 	}
 
 	/* Cancel and free last timer used */
-	(void)odp_timer_cancel(ttp->tim, &ttp->buf);
-	if (ttp->buf != ODP_BUFFER_INVALID)
-		odp_buffer_free(ttp->buf);
+	(void)odp_timer_cancel(ttp->tim, &ttp->ev);
+	if (ttp->ev != ODP_EVENT_INVALID)
+		odp_buffer_free(odp_buffer_from_event(ttp->ev));
 	else
-		EXAMPLE_ERR("Lost timeout buffer at timer cancel\n");
-	/* Since we have cancelled the timer, there is no timeout buffer to
+		EXAMPLE_ERR("Lost timeout event at timer cancel\n");
+	/* Since we have cancelled the timer, there is no timeout event to
 	 * return from odp_timer_free() */
 	(void)odp_timer_free(ttp->tim);
 }
diff --git a/platform/linux-generic/include/api/odp_timer.h b/platform/linux-generic/include/api/odp_timer.h
index cb17b7b..075263a 100644
--- a/platform/linux-generic/include/api/odp_timer.h
+++ b/platform/linux-generic/include/api/odp_timer.h
@@ -20,7 +20,6 @@  extern "C" {
 
 #include <stdlib.h>
 #include <odp_std_types.h>
-#include <odp_buffer.h>
 #include <odp_event.h>
 #include <odp_queue.h>
 
@@ -86,10 +85,10 @@  typedef enum {
  * timer pool. */
 	ODP_TIMER_TOOLATE = -2,
 /**
- * Timer set operation failed because no timeout buffer specified and no
- * timeout buffer present in the timer (timer inactive/expired).
+ * Timer set operation failed because no timeout event specified and no
+ * timeout event present in the timer (timer inactive/expired).
  */
-	ODP_TIMER_NOBUF = -3
+	ODP_TIMER_NOEVENT = -3
 } odp_timer_set_t;
 
 /** Maximum timer pool name length in chars (including null char) */
@@ -214,29 +213,29 @@  odp_timer_t odp_timer_alloc(odp_timer_pool_t tpid,
  * Free a timer
  *
  * Free (destroy) a timer, reclaiming associated resources.
- * The timeout buffer for an active timer will be returned.
- * The timeout buffer for an expired timer will not be returned. It is the
+ * The timeout event for an active timer will be returned.
+ * The timeout event for an expired timer will not be returned. It is the
  * responsibility of the application to handle this timeout when it is received.
  *
  * @param tim      Timer handle
- * @return Buffer handle of timeout buffer or ODP_BUFFER_INVALID
+ * @return Event handle of timeout event or ODP_EVENT_INVALID
  */
-odp_buffer_t odp_timer_free(odp_timer_t tim);
+odp_event_t odp_timer_free(odp_timer_t tim);
 
 /**
- * Set a timer (absolute time) with a user-provided timeout buffer
+ * Set a timer (absolute time) with a user-provided timeout event
  *
  * Set (arm) the timer to expire at specific time. The timeout
- * buffer will be enqueued when the timer expires.
+ * event will be enqueued when the timer expires.
  *
  * Note: any invalid parameters will be treated as programming errors and will
  * cause the application to abort.
  *
  * @param tim      Timer
  * @param abs_tck  Expiration time in absolute timer ticks
- * @param[in,out] tmo_buf  Reference to a buffer variable that points to
- * timeout buffer or NULL to reuse the existing timeout buffer. Any existing
- * timeout buffer that is replaced by a successful set operation will be
+ * @param[in,out] tmo_ev  Reference to an event variable that points to
+ * timeout event or NULL to reuse the existing timeout event. Any existing
+ * timeout event that is replaced by a successful set operation will be
  * returned here.
  *
  * @retval ODP_TIMER_SUCCESS Operation succeeded
@@ -244,15 +243,15 @@  odp_buffer_t odp_timer_free(odp_timer_t tim);
  * early
  * @retval ODP_TIMER_TOOLATE Operation failed because expiration tick too
  * late
- * @retval ODP_TIMER_NOBUF Operation failed because timeout buffer not
+ * @retval ODP_TIMER_NOEVENT Operation failed because timeout event not
  * specified in odp_timer_set call and not present in timer
  */
 int odp_timer_set_abs(odp_timer_t tim,
 		      uint64_t abs_tck,
-		      odp_buffer_t *tmo_buf);
+		      odp_event_t *tmo_ev);
 
 /**
- * Set a timer with a relative expiration time and user-provided buffer.
+ * Set a timer with a relative expiration time and user-provided event.
  *
  * Set (arm) the timer to expire at a relative future time.
  *
@@ -262,9 +261,9 @@  int odp_timer_set_abs(odp_timer_t tim,
  * @param tim      Timer
  * @param rel_tck  Expiration time in timer ticks relative to current time of
  *		   the timer pool the timer belongs to
- * @param[in,out] tmo_buf  Reference to a buffer variable that points to
- * timeout buffer or NULL to reuse the existing timeout buffer. Any existing
- * timeout buffer that is replaced by a successful set operation will be
+ * @param[in,out] tmo_ev  Reference to an event variable that points to
+ * timeout event or NULL to reuse the existing timeout event. Any existing
+ * timeout event that is replaced by a successful set operation will be
  * returned here.
  *
  * @retval ODP_TIMER_SUCCESS Operation succeeded
@@ -272,18 +271,18 @@  int odp_timer_set_abs(odp_timer_t tim,
  * early
  * @retval ODP_TIMER_TOOLATE Operation failed because expiration tick too
  * late
- * @retval ODP_TIMER_NOBUF Operation failed because timeout buffer not
+ * @retval ODP_TIMER_NOEVENT Operation failed because timeout event not
  * specified in call and not present in timer
  */
 int odp_timer_set_rel(odp_timer_t tim,
 		      uint64_t rel_tck,
-		      odp_buffer_t *tmo_buf);
+		      odp_event_t *tmo_ev);
 
 /**
  * Cancel a timer
  *
  * Cancel a timer, preventing future expiration and delivery. Return any
- * present timeout buffer.
+ * present timeout event.
  *
  * A timer that has already expired may be impossible to cancel and the timeout
  * will instead be delivered to the destination queue.
@@ -292,23 +291,11 @@  int odp_timer_set_rel(odp_timer_t tim,
  * cause the application to abort.
  *
  * @param tim     Timer
- * @param[out] tmo_buf Pointer to a buffer variable
- * @retval 0  Success, active timer cancelled, timeout returned in '*tmo_buf'
+ * @param[out] tmo_ev Pointer to an event variable
+ * @retval 0  Success, active timer cancelled, timeout returned in '*tmo_ev'
  * @retval -1 Failure, timer already expired (or inactive)
  */
-int odp_timer_cancel(odp_timer_t tim, odp_buffer_t *tmo_buf);
-
-/**
- * Return timeout handle that is associated with timeout buffer
- *
- * Note: any invalid parameters will cause undefined behavior and may cause
- * the application to abort or crash.
- *
- * @param buf A buffer of type ODP_BUFFER_TYPE_TIMEOUT
- *
- * @return timeout handle
- */
-odp_timeout_t odp_timeout_from_buf(odp_buffer_t buf);
+int odp_timer_cancel(odp_timer_t tim, odp_event_t *tmo_ev);
 
 /**
  * Return timeout handle that is associated with timeout event
@@ -316,7 +303,7 @@  odp_timeout_t odp_timeout_from_buf(odp_buffer_t buf);
  * Note: any invalid parameters will cause undefined behavior and may cause
  * the application to abort or crash.
  *
- * @param buf An event of type ODP_EVENT_TIMEOUT
+ * @param ev An event of type ODP_EVENT_TIMEOUT
  *
  * @return timeout handle
  */
diff --git a/platform/linux-generic/odp_timer.c b/platform/linux-generic/odp_timer.c
index 5d462ba..a32e240 100644
--- a/platform/linux-generic/odp_timer.c
+++ b/platform/linux-generic/odp_timer.c
@@ -739,17 +739,17 @@  odp_timer_t odp_timer_alloc(odp_timer_pool_t tpid,
 	return ODP_TIMER_INVALID;
 }
 
-odp_buffer_t odp_timer_free(odp_timer_t hdl)
+odp_event_t odp_timer_free(odp_timer_t hdl)
 {
 	odp_timer_pool *tp = handle_to_tp(hdl);
 	uint32_t idx = handle_to_idx(hdl, tp);
 	odp_buffer_t old_buf = timer_free(tp, idx);
-	return old_buf;
+	return odp_buffer_to_event(old_buf);
 }
 
 int odp_timer_set_abs(odp_timer_t hdl,
 		      uint64_t abs_tck,
-		      odp_buffer_t *tmo_buf)
+		      odp_event_t *tmo_ev)
 {
 	odp_timer_pool *tp = handle_to_tp(hdl);
 	uint32_t idx = handle_to_idx(hdl, tp);
@@ -758,15 +758,15 @@  int odp_timer_set_abs(odp_timer_t hdl,
 		return ODP_TIMER_TOOEARLY;
 	if (odp_unlikely(abs_tck > cur_tick + tp->max_rel_tck))
 		return ODP_TIMER_TOOLATE;
-	if (timer_reset(idx, abs_tck, tmo_buf, tp))
+	if (timer_reset(idx, abs_tck, (odp_buffer_t *)tmo_ev, tp))
 		return ODP_TIMER_SUCCESS;
 	else
-		return ODP_TIMER_NOBUF;
+		return ODP_TIMER_NOEVENT;
 }
 
 int odp_timer_set_rel(odp_timer_t hdl,
 		      uint64_t rel_tck,
-		      odp_buffer_t *tmo_buf)
+		      odp_event_t *tmo_ev)
 {
 	odp_timer_pool *tp = handle_to_tp(hdl);
 	uint32_t idx = handle_to_idx(hdl, tp);
@@ -775,34 +775,26 @@  int odp_timer_set_rel(odp_timer_t hdl,
 		return ODP_TIMER_TOOEARLY;
 	if (odp_unlikely(rel_tck > tp->max_rel_tck))
 		return ODP_TIMER_TOOLATE;
-	if (timer_reset(idx, abs_tck, tmo_buf, tp))
+	if (timer_reset(idx, abs_tck, (odp_buffer_t *)tmo_ev, tp))
 		return ODP_TIMER_SUCCESS;
 	else
-		return ODP_TIMER_NOBUF;
+		return ODP_TIMER_NOEVENT;
 }
 
-int odp_timer_cancel(odp_timer_t hdl, odp_buffer_t *tmo_buf)
+int odp_timer_cancel(odp_timer_t hdl, odp_event_t *tmo_ev)
 {
 	odp_timer_pool *tp = handle_to_tp(hdl);
 	uint32_t idx = handle_to_idx(hdl, tp);
 	/* Set the expiration tick of the timer to TMO_INACTIVE */
 	odp_buffer_t old_buf = timer_cancel(tp, idx, TMO_INACTIVE);
 	if (old_buf != ODP_BUFFER_INVALID) {
-		*tmo_buf = old_buf;
+		*tmo_ev = odp_buffer_to_event(old_buf);
 		return 0; /* Active timer cancelled, timeout returned */
 	} else {
 		return -1; /* Timer already expired, no timeout returned */
 	}
 }
 
-odp_timeout_t odp_timeout_from_buf(odp_buffer_t buf)
-{
-	/* This check not mandated by the API specification */
-	if (_odp_buffer_type(buf) != ODP_BUFFER_TYPE_TIMEOUT)
-		ODP_ABORT("Buffer not a timeout");
-	return (odp_timeout_t)timeout_hdr_from_buf(buf);
-}
-
 odp_timeout_t odp_timeout_from_event(odp_event_t ev)
 {
 	/* This check not mandated by the API specification */
diff --git a/test/validation/odp_timer.c b/test/validation/odp_timer.c
index adc1554..4e4cc5b 100644
--- a/test/validation/odp_timer.c
+++ b/test/validation/odp_timer.c
@@ -37,25 +37,25 @@  static int min(int a, int b)
 /* @private Timer helper structure */
 struct test_timer {
 	odp_timer_t tim; /* Timer handle */
-	odp_buffer_t buf; /* Timeout buffer */
-	odp_buffer_t buf2; /* Copy of buffer handle */
+	odp_event_t ev;  /* Timeout event */
+	odp_event_t ev2; /* Copy of event handle */
 	uint64_t tick; /* Expiration tick or TICK_INVALID */
 };
 
 #define TICK_INVALID (~(uint64_t)0)
 
-/* @private Handle a received (timeout) buffer */
-static void handle_tmo(odp_buffer_t buf, bool stale, uint64_t prev_tick)
+/* @private Handle a received (timeout) event */
+static void handle_tmo(odp_event_t ev, bool stale, uint64_t prev_tick)
 {
 	/* Use assert() for internal correctness checks of test program */
-	assert(buf != ODP_BUFFER_INVALID);
-	if (odp_event_type(odp_buffer_to_event(buf)) != ODP_EVENT_TIMEOUT) {
-		/* Not a timeout buffer */
-		CU_FAIL("Unexpected buffer type received");
+	assert(ev != ODP_EVENT_INVALID);
+	if (odp_event_type(ev) != ODP_EVENT_TIMEOUT) {
+		/* Not a timeout event */
+		CU_FAIL("Unexpected event type received");
 		return;
 	}
 	/* Read the metadata from the timeout */
-	odp_timeout_t tmo = odp_timeout_from_buf(buf);
+	odp_timeout_t tmo = odp_timeout_from_event(ev);
 	odp_timer_t tim = odp_timeout_timer(tmo);
 	uint64_t tick = odp_timeout_tick(tmo);
 	struct test_timer *ttp = odp_timeout_user_ptr(tmo);
@@ -65,7 +65,7 @@  static void handle_tmo(odp_buffer_t buf, bool stale, uint64_t prev_tick)
 	if (ttp == NULL)
 		CU_FAIL("odp_timeout_user_ptr() null user ptr");
 
-	if (ttp->buf2 != buf)
+	if (ttp->ev2 != ev)
 		CU_FAIL("odp_timeout_user_ptr() wrong user ptr");
 	if (ttp->tim != tim)
 		CU_FAIL("odp_timeout_timer() wrong timer");
@@ -95,8 +95,8 @@  static void handle_tmo(odp_buffer_t buf, bool stale, uint64_t prev_tick)
 	}
 
 	/* Use assert() for correctness check of test program itself */
-	assert(ttp->buf == ODP_BUFFER_INVALID);
-	ttp->buf = buf;
+	assert(ttp->ev == ODP_EVENT_INVALID);
+	ttp->ev = ev;
 }
 
 /* @private Worker thread entrypoint which performs timer alloc/set/cancel/free
@@ -123,10 +123,12 @@  static void *worker_entrypoint(void *arg)
 		tt[i].tim = odp_timer_alloc(tp, queue, &tt[i]);
 		if (tt[i].tim == ODP_TIMER_INVALID)
 			CU_FAIL_FATAL("Failed to allocate timer");
-		tt[i].buf = odp_buffer_alloc(tbp);
-		if (tt[i].buf == ODP_BUFFER_INVALID)
+		/* Timeout alloc is needed.
+		 * With this alloc call pool/event type should be buffer. */
+		tt[i].ev = odp_buffer_to_event(odp_buffer_alloc(tbp));
+		if (tt[i].ev == ODP_EVENT_INVALID)
 			CU_FAIL_FATAL("Failed to allocate timeout buffer");
-		tt[i].buf2 = tt[i].buf;
+		tt[i].ev2 = tt[i].ev;
 		tt[i].tick = TICK_INVALID;
 	}
 
@@ -140,7 +142,7 @@  static void *worker_entrypoint(void *arg)
 						    (rand_r(&seed) % RANGE_MS)
 						    * 1000000ULL);
 		odp_timer_set_t rc;
-		rc = odp_timer_set_abs(tt[i].tim, tck, &tt[i].buf);
+		rc = odp_timer_set_abs(tt[i].tim, tck, &tt[i].ev);
 		if (rc != ODP_TIMER_SUCCESS) {
 			CU_FAIL("Failed to set timer");
 		} else {
@@ -159,26 +161,24 @@  static void *worker_entrypoint(void *arg)
 	for (ms = 0; ms < 7 * RANGE_MS / 10; ms++) {
 		odp_event_t ev;
 		while ((ev = odp_queue_deq(queue)) != ODP_EVENT_INVALID) {
-			odp_buffer_t buf;
-			buf = odp_buffer_from_event(ev);
 			/* Subtract one from prev_tick to allow for timeouts
 			 * to be delivered a tick late */
-			handle_tmo(buf, false, prev_tick - 1);
+			handle_tmo(ev, false, prev_tick - 1);
 			nrcv++;
 		}
 		prev_tick = odp_timer_current_tick(tp);
 		i = rand_r(&seed) % NTIMERS;
-		if (tt[i].buf == ODP_BUFFER_INVALID &&
+		if (tt[i].ev == ODP_EVENT_INVALID &&
 		    (rand_r(&seed) % 2 == 0)) {
 			/* Timer active, cancel it */
-			int rc = odp_timer_cancel(tt[i].tim, &tt[i].buf);
+			int rc = odp_timer_cancel(tt[i].tim, &tt[i].ev);
 			if (rc != 0)
 				/* Cancel failed, timer already expired */
 				ntoolate++;
 			tt[i].tick = TICK_INVALID;
 			ncancel++;
 		} else {
-			if (tt[i].buf != ODP_BUFFER_INVALID)
+			if (tt[i].ev != ODP_EVENT_INVALID)
 				/* Timer inactive => set */
 				nset++;
 			else
@@ -193,7 +193,7 @@  static void *worker_entrypoint(void *arg)
 			do {
 				cur_tick = odp_timer_current_tick(tp);
 				rc = odp_timer_set_rel(tt[i].tim,
-						       tck, &tt[i].buf);
+						       tck, &tt[i].ev);
 			} while (cur_tick != odp_timer_current_tick(tp));
 			if (rc == ODP_TIMER_TOOEARLY ||
 			    rc == ODP_TIMER_TOOLATE) {
@@ -212,13 +212,13 @@  static void *worker_entrypoint(void *arg)
 	/* Cancel and free all timers */
 	uint32_t nstale = 0;
 	for (i = 0; i < NTIMERS; i++) {
-		(void)odp_timer_cancel(tt[i].tim, &tt[i].buf);
+		(void)odp_timer_cancel(tt[i].tim, &tt[i].ev);
 		tt[i].tick = TICK_INVALID;
-		if (tt[i].buf == ODP_BUFFER_INVALID)
+		if (tt[i].ev == ODP_EVENT_INVALID)
 			/* Cancel too late, timer already expired and
 			 * timoeut buffer enqueued */
 			nstale++;
-		if (odp_timer_free(tt[i].tim) != ODP_BUFFER_INVALID)
+		if (odp_timer_free(tt[i].tim) != ODP_EVENT_INVALID)
 			CU_FAIL("odp_timer_free");
 	}
 
@@ -237,8 +237,7 @@  static void *worker_entrypoint(void *arg)
 	while (nstale != 0) {
 		odp_event_t ev = odp_queue_deq(queue);
 		if (ev != ODP_EVENT_INVALID) {
-			odp_buffer_t buf = odp_buffer_from_event(ev);
-			handle_tmo(buf, true, 0/*Dont' care for stale tmo's*/);
+			handle_tmo(ev, true, 0/*Dont' care for stale tmo's*/);
 			nstale--;
 		} else {
 			CU_FAIL("Failed to receive stale timeout");