Re: [RFCv2 1/2] misc: Add timer API


Luiz Augusto von Dentz
 

Hi Tomasz,

On Wed, Apr 20, 2016 at 12:05 PM, Tomasz Bursztyka
<tomasz.bursztyka(a)linux.intel.com> wrote:
Hi Luiz,



+
+struct sys_timer_queue {
+ nano_thread_id_t __thread_id;
+ struct sys_timer *__head;
+ size_t stack_size;
+ char stack[0];
+};
+
+typedef void (*sys_timer_func)(void *user_data);

Giving the pointer on current sys_timer instead you could:

+
+struct sys_timer {
+ struct nano_timer __nano_timer;
+ sys_timer_func func;
+ void *user_data;

remove that pointer, and up to user to use CONTAINER_OF() if he wants to.
(yes I am reusing ideas from Johan ;) )
Yep, this actually saves a pointer and in fact is something I thought
about but for some reason did not implement, anyway I will change to
make use of CONTAINER_OF.

+ struct sys_timer *__next;
+};
+
+/**
+ * @brief Init timer queue
+ *
+ * @param tq Timer queue object that should be initialized
+ * @param stack_size The stack size in bytes
+ */
+int sys_timer_queue_init(struct sys_timer_queue *tq, size_t stack_size);

Are you ever going to use another queue besides the system's one (the global
one)?
It could simplify a lot the API then. Unless I miss to see some use case for
custom queue
Well a private/custom one could be useful in case the stack needs to
different, and I was actually thinking to include a priority later on.
But yes it could be simplified in the first version so later on we can
check for actual use cases.

+
+/**
+ * @brief Add timer to a timer queue
+ *
+ * Both timer and timer queue must be initialized.
+ *
+ * @param tq Timer queue object to add the timer

And continuing on that idea, is it then worth asking to which queue you want
to add a timer?

+ * @param t Timer object to be added
+ * @param ticks Amount of time in ticks to wait
+ *
+ * @return Zero on success or (negative) error code otherwise.
+ */
+int sys_timer_queue_add(struct sys_timer_queue *tq, struct sys_timer *t,
+ int ticks);

so you could remove tq parameter.

+
+/**
+ * @brief Cancel timer
+ *
+ * Remove timer from timer queue and stop it so its callback is not
called.
+ *
+ * @param tq Timer queue object where the timer was added
+ * @param t Timer object to be canceled
+ *
+ * @return Zero on success or (negative) error code otherwise.
+ */
+int sys_timer_queue_cancel(struct sys_timer_queue *tq, struct sys_timer
*t);

here as well.


+
+/**
+ * @brief Init timer
+ *
+ * @param t Timer object to be initialized
+ * @param func Function callback to be called when timer expires
+ * @param user_data Data to be passed in the callback
+ *
+ * @return Zero on success or (negative) error code otherwise.
+ */
+int sys_timer_init(struct sys_timer *t, sys_timer_func func, void
*user_data);
+
+/**
+ * @brief Add timer to the global timer queue
+ *
+ * Timer must be initialized.
+ *
+ * @param _t Timer object to be added
+ * @param _ticks Amount of time in ticks to wait
+ *
+ * @return Zero on success or (negative) error code otherwise.
+ */
+#define sys_timer_add(_t, _ticks) sys_timer_queue_add(__global_tq, _t,
_ticks)
+
+/**
+ * @brief Cancel global timer
+ *
+ * Remove timer from the global timer queue and stop it so its callback
is not
+ * called.
+ *
+ * @param _t Timer object to be canceled
+ *
+ * @return Zero on success or (negative) error code otherwise.
+ */
+#define sys_timer_cancel(_t) sys_timer_queue_cancel(__global_tq, _t)

And these 2 defines would also go away.

And struct sys_timer_queue could also be private.

Tomasz


--
Luiz Augusto von Dentz

Join devel@lists.zephyrproject.org to automatically receive all group messages.