ztimerset(3)

ztimerset(3)

CZMQ Manual - CZMQ/2.2.1

Name

ztimerset - Class for timer set

Synopsis

//  This is a draft class, and may change without notice. It is disabled in
//  stable builds by default. If you use this in applications, please ask
//  for it to be pushed to stable state. Use --enable-drafts to enable.
#ifdef CZMQ_BUILD_DRAFT_API
// Callback function for timer event.
typedef void (ztimerset_fn) (
    int timer_id, void *arg);

//  *** Draft method, for development use, may change without warning ***
//  Create new timer set.
CZMQ_EXPORT ztimerset_t *
    ztimerset_new (void);

//  *** Draft method, for development use, may change without warning ***
//  Destroy a timer set
CZMQ_EXPORT void
    ztimerset_destroy (ztimerset_t **self_p);

//  *** Draft method, for development use, may change without warning ***
//  Add a timer to the set. Returns timer id if OK, -1 on failure.
CZMQ_EXPORT int
    ztimerset_add (ztimerset_t *self, size_t interval, ztimerset_fn handler, void *arg);

//  *** Draft method, for development use, may change without warning ***
//  Cancel a timer. Returns 0 if OK, -1 on failure.
CZMQ_EXPORT int
    ztimerset_cancel (ztimerset_t *self, int timer_id);

//  *** Draft method, for development use, may change without warning ***
//  Set timer interval. Returns 0 if OK, -1 on failure.
//  This method is slow, canceling the timer and adding a new one yield better performance.
CZMQ_EXPORT int
    ztimerset_set_interval (ztimerset_t *self, int timer_id, size_t interval);

//  *** Draft method, for development use, may change without warning ***
//  Reset timer to start interval counting from current time. Returns 0 if OK, -1 on failure.
//  This method is slow, canceling the timer and adding a new one yield better performance.
CZMQ_EXPORT int
    ztimerset_reset (ztimerset_t *self, int timer_id);

//  *** Draft method, for development use, may change without warning ***
//  Return the time until the next interval.
//  Should be used as timeout parameter for the zpoller wait method.
//  The timeout is in msec.
CZMQ_EXPORT int
    ztimerset_timeout (ztimerset_t *self);

//  *** Draft method, for development use, may change without warning ***
//  Invoke callback function of all timers which their interval has elapsed.
//  Should be call after zpoller wait method.
//  Returns 0 if OK, -1 on failure.
CZMQ_EXPORT int
    ztimerset_execute (ztimerset_t *self);

//  *** Draft method, for development use, may change without warning ***
//  Self test of this class.
CZMQ_EXPORT void
    ztimerset_test (bool verbose);

#endif // CZMQ_BUILD_DRAFT_API
Please add '@interface' section in './../src/ztimerset.c'.

Description

ztimerset - timer set

Please add @discuss section in ./../src/ztimerset.c.

Example

From ztimerset_test method

// Simple create/destroy test
ztimerset_t *self = ztimerset_new ();
assert (self);

// Adding timer
bool timer_invoked = false;
int timer_id = ztimerset_add (self, 100, handler, &timer_invoked);
assert (timer_id != -1);
int rc = ztimerset_execute (self);
assert (rc == 0);
assert (!timer_invoked);
int timeout = ztimerset_timeout (self);
assert (timeout > 0);
zclock_sleep (timeout);
rc = ztimerset_execute (self);
assert (rc == 0);
assert (timer_invoked);

// Cancel timer
timeout = ztimerset_timeout (self);
assert (timeout > 0);
rc = ztimerset_cancel (self, timer_id);
assert (rc == 0);
timeout = ztimerset_timeout (self);
assert(timeout == -1);

// Reset a timer
timer_id = ztimerset_add (self, 100, handler, &timer_invoked);
assert (timer_id != -1);
timeout = ztimerset_timeout (self);
assert (timeout > 0);
zclock_sleep (timeout / 2);
timeout = ztimerset_timeout (self);
rc = ztimerset_reset(self, timer_id);
assert (rc == 0);
int timeout2 = ztimerset_timeout (self);
assert (timeout2 > timeout);
rc = ztimerset_cancel (self, timer_id);
assert (rc == 0);

// Set interval
timer_id = ztimerset_add (self, 100, handler, &timer_invoked);
assert (timer_id != -1);
timeout = ztimerset_timeout (self);
rc = ztimerset_set_interval(self, timer_id, 200);
timeout2 = ztimerset_timeout (self);
assert (timeout2 > timeout);

ztimerset_destroy (&self);

#if defined (__WINDOWS__)
zsys_shutdown(); #endif

Authors

The CZMQ manual was written by Pieter Hintjens<moc.xitami|hp#moc.xitami|hp>.

Resources

Main web site: http://czmq.zeromq.org/

Report bugs to the ØMQ development mailing list: <gro.qmorez.stsil|ved-qmorez#gro.qmorez.stsil|ved-qmorez>

Copyright

Copyright (c) 1991-2014 iMatix and Contributors. License LGPLv3+: GNU LGPL 3 or later <http://gnu.org/licenses/lgpl.html>. This is free software: you are free to change it and redistribute it. There is NO WARRANTY, to the extent permitted by law. For details see the files COPYING and COPYING.LESSER included with the CZMQ distribution.