1ZTIMERSET(3) CZMQ Manual ZTIMERSET(3)
2
3
4
6 ztimerset - Class for timer set
7
9 // This is a draft class, and may change without notice. It is disabled in
10 // stable builds by default. If you use this in applications, please ask
11 // for it to be pushed to stable state. Use --enable-drafts to enable.
12 #ifdef CZMQ_BUILD_DRAFT_API
13 // Callback function for timer event.
14 typedef void (ztimerset_fn) (
15 int timer_id, void *arg);
16
17 // *** Draft method, for development use, may change without warning ***
18 // Create new timer set.
19 CZMQ_EXPORT ztimerset_t *
20 ztimerset_new (void);
21
22 // *** Draft method, for development use, may change without warning ***
23 // Destroy a timer set
24 CZMQ_EXPORT void
25 ztimerset_destroy (ztimerset_t **self_p);
26
27 // *** Draft method, for development use, may change without warning ***
28 // Add a timer to the set. Returns timer id if OK, -1 on failure.
29 CZMQ_EXPORT int
30 ztimerset_add (ztimerset_t *self, size_t interval, ztimerset_fn handler, void *arg);
31
32 // *** Draft method, for development use, may change without warning ***
33 // Cancel a timer. Returns 0 if OK, -1 on failure.
34 CZMQ_EXPORT int
35 ztimerset_cancel (ztimerset_t *self, int timer_id);
36
37 // *** Draft method, for development use, may change without warning ***
38 // Set timer interval. Returns 0 if OK, -1 on failure.
39 // This method is slow, canceling the timer and adding a new one yield better performance.
40 CZMQ_EXPORT int
41 ztimerset_set_interval (ztimerset_t *self, int timer_id, size_t interval);
42
43 // *** Draft method, for development use, may change without warning ***
44 // Reset timer to start interval counting from current time. Returns 0 if OK, -1 on failure.
45 // This method is slow, canceling the timer and adding a new one yield better performance.
46 CZMQ_EXPORT int
47 ztimerset_reset (ztimerset_t *self, int timer_id);
48
49 // *** Draft method, for development use, may change without warning ***
50 // Return the time until the next interval.
51 // Should be used as timeout parameter for the zpoller wait method.
52 // The timeout is in msec.
53 CZMQ_EXPORT int
54 ztimerset_timeout (ztimerset_t *self);
55
56 // *** Draft method, for development use, may change without warning ***
57 // Invoke callback function of all timers which their interval has elapsed.
58 // Should be call after zpoller wait method.
59 // Returns 0 if OK, -1 on failure.
60 CZMQ_EXPORT int
61 ztimerset_execute (ztimerset_t *self);
62
63 // *** Draft method, for development use, may change without warning ***
64 // Self test of this class.
65 CZMQ_EXPORT void
66 ztimerset_test (bool verbose);
67
68 #endif // CZMQ_BUILD_DRAFT_API
69 Please add '@interface' section in './../src/ztimerset.c'.
70
72 ztimerset - timer set
73
74 Please add @discuss section in ./../src/ztimerset.c.
75
77 From ztimerset_test method.
78
79 // Simple create/destroy test
80 ztimerset_t *self = ztimerset_new ();
81 assert (self);
82
83 // Adding timer
84 bool timer_invoked = false;
85 int timer_id = ztimerset_add (self, 100, handler, &timer_invoked);
86 assert (timer_id != -1);
87 int rc = ztimerset_execute (self);
88 assert (rc == 0);
89 assert (!timer_invoked);
90 int timeout = ztimerset_timeout (self);
91 assert (timeout > 0);
92 zclock_sleep (timeout);
93 rc = ztimerset_execute (self);
94 assert (rc == 0);
95 assert (timer_invoked);
96
97 // Cancel timer
98 timeout = ztimerset_timeout (self);
99 assert (timeout > 0);
100 rc = ztimerset_cancel (self, timer_id);
101 assert (rc == 0);
102 timeout = ztimerset_timeout (self);
103 assert(timeout == -1);
104
105 // Reset a timer
106 timer_id = ztimerset_add (self, 100, handler, &timer_invoked);
107 assert (timer_id != -1);
108 timeout = ztimerset_timeout (self);
109 assert (timeout > 0);
110 zclock_sleep (timeout / 2);
111 timeout = ztimerset_timeout (self);
112 rc = ztimerset_reset(self, timer_id);
113 assert (rc == 0);
114 int timeout2 = ztimerset_timeout (self);
115 assert (timeout2 > timeout);
116 rc = ztimerset_cancel (self, timer_id);
117 assert (rc == 0);
118
119 // Set interval
120 timer_id = ztimerset_add (self, 100, handler, &timer_invoked);
121 assert (timer_id != -1);
122 timeout = ztimerset_timeout (self);
123 rc = ztimerset_set_interval(self, timer_id, 200);
124 timeout2 = ztimerset_timeout (self);
125 assert (timeout2 > timeout);
126
127 ztimerset_destroy (&self);
128
129 #if defined (__WINDOWS__)
130 zsys_shutdown();
131 #endif
132
133
135 The czmq manual was written by the authors in the AUTHORS file.
136
138 Main web site:
139
140 Report bugs to the email <zeromq-dev@lists.zeromq.org[1]>
141
143 Copyright (c) the Contributors as noted in the AUTHORS file. This file
144 is part of CZMQ, the high-level C binding for 0MQ:
145 http://czmq.zeromq.org. This Source Code Form is subject to the terms
146 of the Mozilla Public License, v. 2.0. If a copy of the MPL was not
147 distributed with this file, You can obtain one at
148 http://mozilla.org/MPL/2.0/. LICENSE included with the czmq
149 distribution.
150
152 1. zeromq-dev@lists.zeromq.org
153 mailto:zeromq-dev@lists.zeromq.org
154
155
156
157CZMQ 4.2.1 02/01/2021 ZTIMERSET(3)