summaryrefslogtreecommitdiffstats
path: root/src/core/channel.c
blob: 691ad1a30b7a2badb3f34865b52cbcc056fed225 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
 * Copyright (c) 2018-2021, OARC, Inc.
 * All rights reserved.
 *
 * This file is part of dnsjit.
 *
 * dnsjit is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * dnsjit is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with dnsjit.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include "core/channel.h"
#include "core/assert.h"

#include <sched.h>

static core_log_t     _log      = LOG_T_INIT("core.channel");
static core_channel_t _defaults = {
    LOG_T_INIT_OBJ("core.channel"),
    0, { 0 }, 0, 0,
    0, 0
};

core_log_t* core_channel_log()
{
    return &_log;
}

static inline bool _is_pow2(size_t num)
{
    while (num != 1) {
        if (num % 2 != 0)
            return false;
        num = num / 2;
    }
    return true;
}

void core_channel_init(core_channel_t* self, size_t capacity)
{
    mlassert_self();
    if (capacity < 4 || !_is_pow2(capacity)) {
        mlfatal("invalid capacity");
    }

    *self          = _defaults;
    self->capacity = capacity;

    lfatal_oom(self->ring_buf = malloc(sizeof(ck_ring_buffer_t) * capacity));
    ck_ring_init(&self->ring, capacity);
}

void core_channel_destroy(core_channel_t* self)
{
    mlassert_self();
    free(self->ring_buf);
}

void core_channel_put(core_channel_t* self, const void* obj)
{
    mlassert_self();
    lassert(self->ring_buf, "ring_buf is nil");

    while (!ck_ring_enqueue_spsc(&self->ring, self->ring_buf, (void*)obj)) {
        sched_yield();
    }
}

int core_channel_try_put(core_channel_t* self, const void* obj)
{
    mlassert_self();
    lassert(self->ring_buf, "ring_buf is nil");

    if (!ck_ring_enqueue_spsc(&self->ring, self->ring_buf, (void*)obj)) {
        return -1;
    }

    return 0;
}

void* core_channel_get(core_channel_t* self)
{
    void* obj = 0;
    mlassert_self();
    lassert(self->ring_buf, "ring_buf is nil");

    while (!ck_ring_dequeue_spsc(&self->ring, self->ring_buf, &obj)) {
        sched_yield();
        if (ck_pr_load_int(&self->closed)) {
            linfo("channel closed");
            return 0;
        }
    }

    return obj;
}

void* core_channel_try_get(core_channel_t* self)
{
    void* obj = 0;
    mlassert_self();
    lassert(self->ring_buf, "ring_buf is nil");

    if (!ck_ring_dequeue_spsc(&self->ring, self->ring_buf, &obj)) {
        return 0;
    }

    return obj;
}

int core_channel_size(core_channel_t* self)
{
    mlassert_self();
    return ck_ring_size(&self->ring);
}

bool core_channel_full(core_channel_t* self)
{
    mlassert_self();

    /* ck_ring can only hold capacity minus one enties at a time */
    if (ck_ring_size(&self->ring) < (self->capacity - 1)) {
        return false;
    }
    return true;
}

void core_channel_close(core_channel_t* self)
{
    mlassert_self();
    ck_pr_store_int(&self->closed, 1);
}

core_receiver_t core_channel_receiver()
{
    return (core_receiver_t)core_channel_put;
}

void core_channel_run(core_channel_t* self)
{
    void* obj = 0;
    mlassert_self();
    lassert(self->ring_buf, "ring_buf is nil");
    if (!self->recv) {
        lfatal("no receiver set");
    }

    for (;;) {
        while (!ck_ring_dequeue_spsc(&self->ring, self->ring_buf, &obj)) {
            sched_yield();
            if (ck_pr_load_int(&self->closed)) {
                linfo("channel closed");
                return;
            }
        }
        self->recv(self->ctx, obj);
    }
}