summaryrefslogtreecommitdiff
path: root/examples/redis-unstable/src/eventnotifier.c
blob: 0a6c145c13e0f11bb1dc44b50dc1a729997b0c52 (plain)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* eventnotifier.c -- An event notifier based on eventfd or pipe.
 *
 * Copyright (c) 2024-Present, Redis Ltd.
 * All rights reserved.
 *
 * Licensed under your choice of (a) the Redis Source Available License 2.0
 * (RSALv2); or (b) the Server Side Public License v1 (SSPLv1); or (c) the
 * GNU Affero General Public License v3 (AGPLv3).
 */

#include "eventnotifier.h"

#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
#ifdef HAVE_EVENT_FD
#include <sys/eventfd.h>
#endif

#include "anet.h"
#include "zmalloc.h"

eventNotifier* createEventNotifier(void) {
    eventNotifier *en = zmalloc(sizeof(eventNotifier));
    if (!en) return NULL;

#ifdef HAVE_EVENT_FD
    if ((en->efd = eventfd(0, EFD_NONBLOCK| EFD_CLOEXEC)) != -1) {
        return en;
    }
#else
    if (anetPipe(en->pipefd, O_CLOEXEC|O_NONBLOCK, O_CLOEXEC|O_NONBLOCK) != -1) {
        return en;
    }
#endif

    /* Clean up if error. */
    zfree(en);
    return NULL;
}

int getReadEventFd(struct eventNotifier *en) {
#ifdef HAVE_EVENT_FD
    return en->efd;
#else
    return en->pipefd[0];
#endif
}

int getWriteEventFd(struct eventNotifier *en) {
#ifdef HAVE_EVENT_FD
    return en->efd;
#else
    return en->pipefd[1];
#endif
}

int triggerEventNotifier(struct eventNotifier *en) {
#ifdef HAVE_EVENT_FD
    uint64_t u = 1;
    if (write(en->efd, &u, sizeof(uint64_t)) == -1) {
        return EN_ERR;
    }
#else
    char buf[1] = {'R'};
    if (write(en->pipefd[1], buf, 1) == -1) {
        return EN_ERR;
    }
#endif
    return EN_OK;
}

int handleEventNotifier(struct eventNotifier *en) {
#ifdef HAVE_EVENT_FD
    uint64_t u;
    if (read(en->efd, &u, sizeof(uint64_t)) == -1) {
        return EN_ERR;
    }
#else
    char buf[1];
    if (read(en->pipefd[0], buf, 1) == -1) {
        return EN_ERR;
    }
#endif
    return EN_OK;
}

void freeEventNotifier(struct eventNotifier *en) {
#ifdef HAVE_EVENT_FD
    close(en->efd);
#else
    close(en->pipefd[0]);
    close(en->pipefd[1]);
#endif

    /* Free memory */
    zfree(en);
}