int uv_timer_start(uv_timer_t* handle, uv_timer_cb cb, uint64_t timeout, uint64_t repeat) { uint64_t clamped_timeout; if (cb == NULL) return UV_EINVAL; if (uv__is_active(handle)) uv_timer_stop(handle); clamped_timeout = handle->loop->time + timeout; if (clamped_timeout < timeout) clamped_timeout = (uint64_t) -1; handle->timer_cb = cb; handle->timeout = clamped_timeout; handle->repeat = repeat; /* start_id is the second index to be compared in uv__timer_cmp() */ handle->start_id = handle->loop->timer_counter++; heap_insert(timer_heap(handle->loop), (struct heap_node*) &handle->heap_node, timer_less_than); uv__handle_start(handle); return 0; }
// This test function was contributed with Bugzilla #2447 to test validity // of ACE_Timer_Heap timer IDs around the boundary of having to enlarge // the heap. static void test_unique_timer_heap_ids (void) { Example_Handler eh; ACE_Timer_Heap timer_heap (44); ACE_Time_Value anytime(1); ACE_Bounded_Set<long> timer_ids (max_iterations); long timer_id = -1; bool all_unique = true; for (int i = 0; i < 100; ++i) { timer_id = timer_heap.schedule (&eh, 0, anytime); if (timer_id == -1) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("Schedule timer %d %p\n"), i, ACE_TEXT ("test_unique_timer_heap_ids"))); continue; } ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Schedule timer %d. Timer id = %d\n"), i, timer_id)); if (1 == timer_ids.insert (timer_id)) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("Pass %d, id %d is not unique\n"), i, timer_id)); all_unique = false; } if (i == 0 || i == 1 || i == 47 || i == 48) { ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Free Timer %d. Timer Id = %d\n"), i, timer_id)); timer_heap.cancel (timer_id); if (timer_id == -1) ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("Failed to cancel timer"))); timer_ids.remove (timer_id); } } if (all_unique) ACE_DEBUG ((LM_INFO, ACE_TEXT ("All timer ids were unique.\n"))); return; }
int uv_timer_stop(uv_timer_t* handle) { if (!uv__is_active(handle)) return 0; heap_remove(timer_heap(handle->loop), (struct heap_node*) &handle->heap_node, timer_less_than); uv__handle_stop(handle); return 0; }
void uv__run_timers(uv_loop_t* loop) { struct heap_node* heap_node; uv_timer_t* handle; for (;;) { heap_node = heap_min(timer_heap(loop)); if (heap_node == NULL) break; handle = container_of(heap_node, uv_timer_t, heap_node); if (handle->timeout > loop->time) break; uv_timer_stop(handle); uv_timer_again(handle); handle->timer_cb(handle); } }
int uv__next_timeout(const uv_loop_t* loop) { const struct heap_node* heap_node; const uv_timer_t* handle; uint64_t diff; heap_node = heap_min(timer_heap(loop)); if (heap_node == NULL) return -1; /* block indefinitely */ handle = container_of(heap_node, uv_timer_t, heap_node); if (handle->timeout <= loop->time) return 0; diff = handle->timeout - loop->time; if (diff > INT_MAX) diff = INT_MAX; return (int) diff; }