common_blocked_queue.h 3.7 KB
Newer Older
Y
YuQing 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright (c) 2020 YuQing <384681@qq.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the Lesser GNU General Public License, version 3
 * or later ("LGPL"), as published by the Free Software Foundation.
 *
 * This program 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.
 *
 * You should have received a copy of the Lesser GNU General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 */
Y
yuqing 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

//common_blocked_queue.h

#ifndef _COMMON_BLOCKED_QUEUE_H
#define _COMMON_BLOCKED_QUEUE_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include "common_define.h"
#include "fast_mblock.h"

struct common_blocked_node
{
    void *data;
    struct common_blocked_node *next;
};

struct common_blocked_queue
{
	struct common_blocked_node *head;
	struct common_blocked_node *tail;
    struct fast_mblock_man mblock;
Y
YuQing 已提交
39
    pthread_lock_cond_pair_t lc_pair;
Y
yuqing 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53
};

#ifdef __cplusplus
extern "C" {
#endif

int common_blocked_queue_init_ex(struct common_blocked_queue *queue,
        const int alloc_elements_once);

#define common_blocked_queue_init(queue)  \
        common_blocked_queue_init_ex(queue, 1024)

void common_blocked_queue_destroy(struct common_blocked_queue *queue);

54 55
static inline void common_blocked_queue_terminate(
        struct common_blocked_queue *queue)
56
{
Y
YuQing 已提交
57
    pthread_cond_signal(&(queue->lc_pair.cond));
58 59 60 61
}

static inline void common_blocked_queue_terminate_all(
        struct common_blocked_queue *queue, const int count)
Y
yuqing 已提交
62
{
63 64 65
    int i;
    for (i=0; i<count; i++)
    {
Y
YuQing 已提交
66
        pthread_cond_signal(&(queue->lc_pair.cond));
67
    }
Y
yuqing 已提交
68 69
}

70 71 72 73 74 75 76 77 78 79 80 81 82 83
//notify by the caller
int common_blocked_queue_push_ex(struct common_blocked_queue *queue,
        void *data, bool *notify);

static inline int common_blocked_queue_push(struct common_blocked_queue
        *queue, void *data)
{
    bool notify;
    int result;

    if ((result=common_blocked_queue_push_ex(queue, data, &notify)) == 0)
    {
        if (notify)
        {
Y
YuQing 已提交
84
            pthread_cond_signal(&(queue->lc_pair.cond));
85 86 87 88 89 90
        }
    }

    return result;
}

Y
yuqing 已提交
91

Y
YuQing 已提交
92 93 94
void common_blocked_queue_return_nodes(struct common_blocked_queue *queue,
        struct common_blocked_node *node);

95 96 97
void *common_blocked_queue_pop_ex(struct common_blocked_queue *queue,
        const bool blocked);

98 99
#define common_blocked_queue_pop(queue) \
    common_blocked_queue_pop_ex(queue, true)
100

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
#define common_blocked_queue_try_pop(queue) \
    common_blocked_queue_pop_ex(queue, false)

struct common_blocked_node *common_blocked_queue_pop_all_nodes_ex(
        struct common_blocked_queue *queue, const bool blocked);

#define common_blocked_queue_pop_all_nodes(queue)  \
    common_blocked_queue_pop_all_nodes_ex(queue, true)

#define common_blocked_queue_try_pop_all_nodes(queue)  \
    common_blocked_queue_pop_all_nodes_ex(queue, false)

#define common_blocked_queue_free_one_node(queue, node) \
    fast_mblock_free_object(&queue->mblock, node)

void common_blocked_queue_free_all_nodes(struct common_blocked_queue *queue,
        struct common_blocked_node *node);
Y
yuqing 已提交
118

119 120 121 122 123 124 125 126 127 128 129 130
void *common_blocked_queue_timedpop(struct common_blocked_queue *queue,
        const int timeout, const int time_unit);

#define common_blocked_queue_timedpop_sec(queue, timeout) \
    common_blocked_queue_timedpop(queue, timeout, FC_TIME_UNIT_SECOND)

#define common_blocked_queue_timedpop_ms(queue, timeout) \
    common_blocked_queue_timedpop(queue, timeout, FC_TIME_UNIT_MSECOND)

#define common_blocked_queue_timedpop_us(queue, timeout) \
    common_blocked_queue_timedpop(queue, timeout, FC_TIME_UNIT_USECOND)

Y
yuqing 已提交
131 132 133 134 135
#ifdef __cplusplus
}
#endif

#endif