summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_queue.c
blob: 7c860d11a4babc060b4c6d58dc0d6f7b50cf5007 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
 * Copyright (C) 2019 Intel Corporation.  All rights reserved.
 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 */

#include "bh_queue.h"

typedef struct bh_queue_node {
    struct bh_queue_node *next;
    struct bh_queue_node *prev;
    unsigned short tag;
    unsigned int len;
    void *body;
    bh_msg_cleaner msg_cleaner;
} bh_queue_node;

struct bh_queue {
    bh_queue_mutex queue_lock;
    bh_queue_cond queue_wait_cond;
    unsigned int cnt;
    unsigned int max;
    unsigned int drops;
    bh_queue_node *head;
    bh_queue_node *tail;

    bool exit_loop_run;
};

char *
bh_message_payload(bh_message_t message)
{
    return message->body;
}

uint32
bh_message_payload_len(bh_message_t message)
{
    return message->len;
}

int
bh_message_type(bh_message_t message)
{
    return message->tag;
}

bh_queue *
bh_queue_create()
{
    int ret;
    bh_queue *queue = bh_queue_malloc(sizeof(bh_queue));

    if (queue) {
        memset(queue, 0, sizeof(bh_queue));
        queue->max = DEFAULT_QUEUE_LENGTH;

        ret = bh_queue_mutex_init(&queue->queue_lock);
        if (ret != 0) {
            bh_queue_free(queue);
            return NULL;
        }

        ret = bh_queue_cond_init(&queue->queue_wait_cond);
        if (ret != 0) {
            bh_queue_mutex_destroy(&queue->queue_lock);
            bh_queue_free(queue);
            return NULL;
        }
    }

    return queue;
}

void
bh_queue_destroy(bh_queue *queue)
{
    bh_queue_node *node;

    if (!queue)
        return;

    bh_queue_mutex_lock(&queue->queue_lock);
    while (queue->head) {
        node = queue->head;
        queue->head = node->next;

        bh_free_msg(node);
    }
    bh_queue_mutex_unlock(&queue->queue_lock);

    bh_queue_cond_destroy(&queue->queue_wait_cond);
    bh_queue_mutex_destroy(&queue->queue_lock);
    bh_queue_free(queue);
}

bool
bh_post_msg2(bh_queue *queue, bh_queue_node *msg)
{
    if (queue->cnt >= queue->max) {
        queue->drops++;
        bh_free_msg(msg);
        return false;
    }

    bh_queue_mutex_lock(&queue->queue_lock);

    if (queue->cnt == 0) {
        bh_assert(queue->head == NULL);
        bh_assert(queue->tail == NULL);
        queue->head = queue->tail = msg;
        msg->next = msg->prev = NULL;
        queue->cnt = 1;

        bh_queue_cond_signal(&queue->queue_wait_cond);
    }
    else {
        msg->next = NULL;
        msg->prev = queue->tail;
        queue->tail->next = msg;
        queue->tail = msg;
        queue->cnt++;
    }

    bh_queue_mutex_unlock(&queue->queue_lock);

    return true;
}

bool
bh_post_msg(bh_queue *queue, unsigned short tag, void *body, unsigned int len)
{
    bh_queue_node *msg = bh_new_msg(tag, body, len, NULL);
    if (msg == NULL) {
        queue->drops++;
        if (len != 0 && body)
            BH_FREE(body);
        return false;
    }

    if (!bh_post_msg2(queue, msg)) {
        // bh_post_msg2 already freed the msg for failure
        return false;
    }

    return true;
}

bh_queue_node *
bh_new_msg(unsigned short tag, void *body, unsigned int len, void *handler)
{
    bh_queue_node *msg =
        (bh_queue_node *)bh_queue_malloc(sizeof(bh_queue_node));
    if (msg == NULL)
        return NULL;
    memset(msg, 0, sizeof(bh_queue_node));
    msg->len = len;
    msg->body = body;
    msg->tag = tag;
    msg->msg_cleaner = (bh_msg_cleaner)handler;

    return msg;
}

void
bh_free_msg(bh_queue_node *msg)
{
    if (msg->msg_cleaner) {
        msg->msg_cleaner(msg->body);
        bh_queue_free(msg);
        return;
    }

    // note: sometime we just use the payload pointer for a integer value
    //       len!=0 is the only indicator about the body is an allocated buffer.
    if (msg->body && msg->len)
        bh_queue_free(msg->body);

    bh_queue_free(msg);
}

bh_message_t
bh_get_msg(bh_queue *queue, uint64 timeout_us)
{
    bh_queue_node *msg = NULL;
    bh_queue_mutex_lock(&queue->queue_lock);

    if (queue->cnt == 0) {
        bh_assert(queue->head == NULL);
        bh_assert(queue->tail == NULL);

        if (timeout_us == 0) {
            bh_queue_mutex_unlock(&queue->queue_lock);
            return NULL;
        }

        bh_queue_cond_timedwait(&queue->queue_wait_cond, &queue->queue_lock,
                                timeout_us);
    }

    if (queue->cnt == 0) {
        bh_assert(queue->head == NULL);
        bh_assert(queue->tail == NULL);
    }
    else if (queue->cnt == 1) {
        bh_assert(queue->head == queue->tail);

        msg = queue->head;
        queue->head = queue->tail = NULL;
        queue->cnt = 0;
    }
    else {
        msg = queue->head;
        queue->head = queue->head->next;
        queue->head->prev = NULL;
        queue->cnt--;
    }

    bh_queue_mutex_unlock(&queue->queue_lock);

    return msg;
}

unsigned
bh_queue_get_message_count(bh_queue *queue)
{
    if (!queue)
        return 0;

    return queue->cnt;
}

void
bh_queue_enter_loop_run(bh_queue *queue, bh_queue_handle_msg_callback handle_cb,
                        void *arg)
{
    if (!queue)
        return;

    while (!queue->exit_loop_run) {
        bh_queue_node *message = bh_get_msg(queue, BHT_WAIT_FOREVER);

        if (message) {
            handle_cb(message, arg);
            bh_free_msg(message);
        }
    }
}

void
bh_queue_exit_loop_run(bh_queue *queue)
{
    if (queue) {
        queue->exit_loop_run = true;
        bh_queue_cond_signal(&queue->queue_wait_cond);
    }
}