Rizin
unix-like reverse engineering framework and cli tools
timer.c File Reference
#include "uv.h"
#include "uv-common.h"
#include "heap-inl.h"
#include <assert.h>
#include <limits.h>

Go to the source code of this file.

Functions

static struct heaptimer_heap (const uv_loop_t *loop)
 
static int timer_less_than (const struct heap_node *ha, const struct heap_node *hb)
 
int uv_timer_init (uv_loop_t *loop, uv_timer_t *handle)
 
int uv_timer_start (uv_timer_t *handle, uv_timer_cb cb, uint64_t timeout, uint64_t repeat)
 
int uv_timer_stop (uv_timer_t *handle)
 
int uv_timer_again (uv_timer_t *handle)
 
void uv_timer_set_repeat (uv_timer_t *handle, uint64_t repeat)
 
uint64_t uv_timer_get_repeat (const uv_timer_t *handle)
 
uint64_t uv_timer_get_due_in (const uv_timer_t *handle)
 
int uv__next_timeout (const uv_loop_t *loop)
 
void uv__run_timers (uv_loop_t *loop)
 
void uv__timer_close (uv_timer_t *handle)
 

Function Documentation

◆ timer_heap()

static struct heap* timer_heap ( const uv_loop_t loop)
static

Definition at line 29 of file timer.c.

29  {
30 #ifdef _WIN32
31  return (struct heap*) loop->timer_heap;
32 #else
33  return (struct heap*) &loop->timer_heap;
34 #endif
35 }
Definition: heap-inl.h:40
uv_loop_t * loop
Definition: main.c:7

References loop.

Referenced by uv__next_timeout(), uv__run_timers(), uv_loop_init(), uv_timer_start(), and uv_timer_stop().

◆ timer_less_than()

static int timer_less_than ( const struct heap_node ha,
const struct heap_node hb 
)
static

Definition at line 38 of file timer.c.

39  {
40  const uv_timer_t* a;
41  const uv_timer_t* b;
42 
45 
46  if (a->timeout < b->timeout)
47  return 1;
48  if (b->timeout < a->timeout)
49  return 0;
50 
51  /* Compare start_id when both have the same timeout. start_id is
52  * allocated with loop->timer_counter in uv_timer_start().
53  */
54  return a->start_id < b->start_id;
55 }
#define container_of(ptr, type, member)
Definition: rz_types.h:650
#define b(i)
Definition: sha256.c:42
#define a(i)
Definition: sha256.c:41
Definition: uv.h:860

References a, b, and container_of.

Referenced by uv_timer_start(), and uv_timer_stop().

◆ uv__next_timeout()

int uv__next_timeout ( const uv_loop_t loop)

Definition at line 141 of file timer.c.

141  {
142  const struct heap_node* heap_node;
143  const uv_timer_t* handle;
144  uint64_t diff;
145 
146  heap_node = heap_min(timer_heap(loop));
147  if (heap_node == NULL)
148  return -1; /* block indefinitely */
149 
151  if (handle->timeout <= loop->time)
152  return 0;
153 
154  diff = handle->timeout - loop->time;
155  if (diff > INT_MAX)
156  diff = INT_MAX;
157 
158  return (int) diff;
159 }
static mcore_handle handle
Definition: asm_mcore.c:8
#define INT_MAX
Definition: cp-demangle.c:131
#define NULL
Definition: cris-opc.c:27
unsigned long uint64_t
Definition: sftypes.h:28
static struct heap * timer_heap(const uv_loop_t *loop)
Definition: timer.c:29

References container_of, handle, INT_MAX, loop, NULL, and timer_heap().

Referenced by uv_backend_timeout().

◆ uv__run_timers()

void uv__run_timers ( uv_loop_t loop)

Definition at line 162 of file timer.c.

162  {
163  struct heap_node* heap_node;
165 
166  for (;;) {
167  heap_node = heap_min(timer_heap(loop));
168  if (heap_node == NULL)
169  break;
170 
172  if (handle->timeout > loop->time)
173  break;
174 
177  handle->timer_cb(handle);
178  }
179 }
int uv_timer_stop(uv_timer_t *handle)
Definition: timer.c:97
int uv_timer_again(uv_timer_t *handle)
Definition: timer.c:110

References container_of, handle, loop, NULL, timer_heap(), uv_timer_again(), and uv_timer_stop().

Referenced by uv_run().

◆ uv__timer_close()

void uv__timer_close ( uv_timer_t handle)

Definition at line 182 of file timer.c.

182  {
184 }

References handle, and uv_timer_stop().

Referenced by uv_close(), and uv_process_endgames().

◆ uv_timer_again()

int uv_timer_again ( uv_timer_t handle)

Definition at line 110 of file timer.c.

110  {
111  if (handle->timer_cb == NULL)
112  return UV_EINVAL;
113 
114  if (handle->repeat) {
116  uv_timer_start(handle, handle->timer_cb, handle->repeat, handle->repeat);
117  }
118 
119  return 0;
120 }
int uv_timer_start(uv_timer_t *handle, uv_timer_cb cb, uint64_t timeout, uint64_t repeat)
Definition: timer.c:66

References handle, NULL, uv_timer_start(), and uv_timer_stop().

Referenced by uv__run_timers().

◆ uv_timer_get_due_in()

uint64_t uv_timer_get_due_in ( const uv_timer_t handle)

Definition at line 133 of file timer.c.

133  {
134  if (handle->loop->time >= handle->timeout)
135  return 0;
136 
137  return handle->timeout - handle->loop->time;
138 }

References handle.

◆ uv_timer_get_repeat()

uint64_t uv_timer_get_repeat ( const uv_timer_t handle)

Definition at line 128 of file timer.c.

128  {
129  return handle->repeat;
130 }

References handle.

◆ uv_timer_init()

int uv_timer_init ( uv_loop_t loop,
uv_timer_t handle 
)

Definition at line 58 of file timer.c.

58  {
59  uv__handle_init(loop, (uv_handle_t*)handle, UV_TIMER);
60  handle->timer_cb = NULL;
61  handle->repeat = 0;
62  return 0;
63 }
#define uv__handle_init(loop_, h, type_)
Definition: uv-common.h:301

References handle, loop, NULL, and uv__handle_init.

Referenced by eof_timer_init(), main(), and uv_fs_poll_start().

◆ uv_timer_set_repeat()

void uv_timer_set_repeat ( uv_timer_t handle,
uint64_t  repeat 
)

Definition at line 123 of file timer.c.

123  {
124  handle->repeat = repeat;
125 }
static void repeat(struct parse *, sopno, int, int)
Definition: regcomp.c:1155

References handle, and repeat().

◆ uv_timer_start()

int uv_timer_start ( uv_timer_t handle,
uv_timer_cb  cb,
uint64_t  timeout,
uint64_t  repeat 
)

Definition at line 66 of file timer.c.

69  {
70  uint64_t clamped_timeout;
71 
72  if (uv__is_closing(handle) || cb == NULL)
73  return UV_EINVAL;
74 
75  if (uv__is_active(handle))
77 
78  clamped_timeout = handle->loop->time + timeout;
79  if (clamped_timeout < timeout)
80  clamped_timeout = (uint64_t) -1;
81 
82  handle->timer_cb = cb;
83  handle->timeout = clamped_timeout;
84  handle->repeat = repeat;
85  /* start_id is the second index to be compared in timer_less_than() */
86  handle->start_id = handle->loop->timer_counter++;
87 
88  heap_insert(timer_heap(handle->loop),
89  (struct heap_node*) &handle->heap_node,
92 
93  return 0;
94 }
uv_timer_t timeout
Definition: main.c:9
static int timer_less_than(const struct heap_node *ha, const struct heap_node *hb)
Definition: timer.c:38
#define uv__is_closing(h)
Definition: uv-common.h:255
#define uv__is_active(h)
Definition: uv-common.h:252
#define uv__handle_start(h)
Definition: uv-common.h:258
static const char * cb[]
Definition: z80_tab.h:176

References cb, handle, NULL, repeat(), timeout, timer_heap(), timer_less_than(), uv__handle_start, uv__is_active, uv__is_closing, and uv_timer_stop().

Referenced by eof_timer_start(), main(), poll_cb(), start_timeout(), and uv_timer_again().

◆ uv_timer_stop()

int uv_timer_stop ( uv_timer_t handle)

Definition at line 97 of file timer.c.

97  {
98  if (!uv__is_active(handle))
99  return 0;
100 
101  heap_remove(timer_heap(handle->loop),
102  (struct heap_node*) &handle->heap_node,
105 
106  return 0;
107 }
#define uv__handle_stop(h)
Definition: uv-common.h:266

References handle, timer_heap(), timer_less_than(), uv__handle_stop, and uv__is_active.

Referenced by curl_perform(), eof_timer_stop(), update(), uv__run_timers(), uv__timer_close(), uv_close(), uv_timer_again(), and uv_timer_start().