common_blocked_queue.h 2.9 KB
Newer Older
Y
yuqing 已提交
1 2 3 4 5
/**
* Copyright (C) 2008 Happy Fish / YuQing
*
* FastDFS may be copied only under the terms of the GNU General
* Public License V3, which may be found in the FastDFS source kit.
Y
YuQing 已提交
6
* Please visit the FastDFS Home Page http://www.fastken.com/ for more detail.
Y
yuqing 已提交
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
**/

//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 已提交
32
    pthread_lock_cond_pair_t lc_pair;
Y
yuqing 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46
};

#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);

47 48
static inline void common_blocked_queue_terminate(
        struct common_blocked_queue *queue)
49
{
Y
YuQing 已提交
50
    pthread_cond_signal(&(queue->lc_pair.cond));
51 52 53 54
}

static inline void common_blocked_queue_terminate_all(
        struct common_blocked_queue *queue, const int count)
Y
yuqing 已提交
55
{
56 57 58
    int i;
    for (i=0; i<count; i++)
    {
Y
YuQing 已提交
59
        pthread_cond_signal(&(queue->lc_pair.cond));
60
    }
Y
yuqing 已提交
61 62
}

63 64 65 66 67 68 69 70 71 72 73 74 75 76
//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 已提交
77
            pthread_cond_signal(&(queue->lc_pair.cond));
78 79 80 81 82 83
        }
    }

    return result;
}

Y
yuqing 已提交
84

Y
YuQing 已提交
85 86 87
void common_blocked_queue_return_nodes(struct common_blocked_queue *queue,
        struct common_blocked_node *node);

88 89 90
void *common_blocked_queue_pop_ex(struct common_blocked_queue *queue,
        const bool blocked);

91 92
#define common_blocked_queue_pop(queue) \
    common_blocked_queue_pop_ex(queue, true)
93

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
#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 已提交
111 112 113 114 115 116

#ifdef __cplusplus
}
#endif

#endif