aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/event/time.c
blob: f678f25f3f732915e9ec74f281beacb2e0945f2a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdint.h>
#include <uv.h>

#include "nvim/event/loop.h"
#include "nvim/event/time.h"
#include "nvim/func_attr.h"

#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "event/time.c.generated.h"
#endif

void time_watcher_init(Loop *loop, TimeWatcher *watcher, void *data)
  FUNC_ATTR_NONNULL_ARG(1) FUNC_ATTR_NONNULL_ARG(2)
{
  uv_timer_init(&loop->uv, &watcher->uv);
  watcher->uv.data = watcher;
  watcher->data = data;
  watcher->events = loop->fast_events;
  watcher->blockable = false;
}

void time_watcher_start(TimeWatcher *watcher, time_cb cb, uint64_t timeout, uint64_t repeat)
  FUNC_ATTR_NONNULL_ALL
{
  watcher->cb = cb;
  uv_timer_start(&watcher->uv, time_watcher_cb, timeout, repeat);
}

void time_watcher_stop(TimeWatcher *watcher)
  FUNC_ATTR_NONNULL_ALL
{
  uv_timer_stop(&watcher->uv);
}

void time_watcher_close(TimeWatcher *watcher, time_cb cb)
  FUNC_ATTR_NONNULL_ARG(1)
{
  watcher->close_cb = cb;
  uv_close((uv_handle_t *)&watcher->uv, close_cb);
}

static void time_event(void **argv)
{
  TimeWatcher *watcher = argv[0];
  watcher->cb(watcher, watcher->data);
}

static void time_watcher_cb(uv_timer_t *handle)
  FUNC_ATTR_NONNULL_ALL
{
  TimeWatcher *watcher = handle->data;
  if (watcher->blockable && !multiqueue_empty(watcher->events)) {
    // the timer blocked and there already is an unprocessed event waiting
    return;
  }
  CREATE_EVENT(watcher->events, time_event, 1, watcher);
}

static void close_event(void **argv)
{
  TimeWatcher *watcher = argv[0];
  watcher->close_cb(watcher, watcher->data);
}

static void close_cb(uv_handle_t *handle)
  FUNC_ATTR_NONNULL_ALL
{
  TimeWatcher *watcher = handle->data;
  if (watcher->close_cb) {
    CREATE_EVENT(watcher->events, close_event, 1, watcher);
  }
}