common_blocked_queue.h 1.4 KB
Newer Older
Y
yuqing 已提交
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
/**
* 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.
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
**/

//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;
	pthread_mutex_t lock;
	pthread_cond_t cond;
};

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

static inline void common_blocked_queue_terminate(struct common_blocked_queue *queue)
{
     pthread_cond_signal(&(queue->cond));
}

int common_blocked_queue_push(struct common_blocked_queue *queue, void *data);

void *common_blocked_queue_pop(struct common_blocked_queue *queue);

#ifdef __cplusplus
}
#endif

#endif