Ejemplo n.º 1
0
/******************************************************************************
 **函数名称: queue_creat
 **功    能: 初始化加锁队列
 **输入参数:
 **     max: 队列长度(必须为2的次方)
 **     size: 内存单元SIZE
 **输出参数: NONE
 **返    回: 加锁队列对象
 **实现描述:
 **注意事项: 内存池中的块数必须与队列长度一致, 否则可能出现PUSH失败的情况出现!
 **作    者: # Qifeng.zou # 2014.10.12 #
 ******************************************************************************/
queue_t *queue_creat(int max, int size)
{
    queue_t *queue;

    /* > 新建对象 */
    queue = (queue_t *)calloc(1, sizeof(queue_t));
    if (NULL == queue) {
        return NULL;
    }

    /* > 创建队列 */
    queue->ring = ring_creat(max);
    if (NULL == queue->ring) {
        FREE(queue);
        return NULL;
    }

    /* > 创建内存池 */
    queue->slot = slot_creat(max, size);
    if (NULL == queue->slot) {
        ring_destroy(queue->ring);
        FREE(queue);
        return NULL;
    }

    return queue;
}
Ejemplo n.º 2
0
/******************************************************************************
 **函数名称: slot_creat
 **功    能: 创建内存池
 **输入参数: 
 **     num: 内存块数
 **     size: 内存块大小
 **输出参数: NONE
 **返    回: 内存池对象
 **实现描述: 
 **注意事项: 
 **作    者: # Qifeng.zou # 2015.05.05 #
 ******************************************************************************/
slot_t *slot_creat(int num, size_t size)
{
    int i;
    slot_t *slot;
    void *addr, *ptr;

    /* > 创建对象 */
    slot = (slot_t *)calloc(1, sizeof(slot_t));
    if (NULL == slot) {
        return NULL;
    }

    slot->max = num;
    slot->size = size;

    slot->ring = ring_creat(num);
    if (NULL == slot->ring) {
        free(slot);
        return NULL;
    }

    /* > 申请内存池空间 */
    addr = (void *)calloc(num, size);
    if (NULL == addr) {
        ring_destroy(slot->ring);
        free(slot);
        return NULL;
    }

    /* > 插入管理队列 */
    ptr = addr;
    for (i=0; i<num; ++i, ptr += size) {
        if (ring_push(slot->ring, ptr)) {
            ring_destroy(slot->ring);
            free(slot);
            free(addr);
            return NULL;
        }
    }

    return slot;
}
Ejemplo n.º 3
0
int main() {
    srand(time(NULL));
    ring *buf = ring_create(1 << 14);
    pthread_t w, w1, r, r1;

    // first we do a test with a single writer, single reader
    // in this test, the reader will ensure the sequence appears
    // strictly in the same order it is written
    arg_t args = {
        .buf = buf,
        .write_len = 1 << 22,
        .multi = false,
    };
    pthread_create(&w, NULL, write_sequence, &args);
    pthread_create(&r, NULL, read_sequence, &args);

    pthread_join(w, NULL);
    int res;
    void *res_p;
    pthread_join(r, &res_p);
    res = *(int*)res_p;
    free(res_p);

    printf("single reader, single writer test passed: %s\n", res ? "FALSE" : "TRUE");

    // now do 2 writers, 2 readers
    // we relax the sequence restriction and now just look for the same sums
    args.multi = true;

    pthread_create(&w, NULL, write_sequence, &args);
    pthread_create(&w1, NULL, write_sequence, &args);
    pthread_create(&r, NULL, read_sequence, &args);
    pthread_create(&r1, NULL, read_sequence, &args);

    int write_sum = 0;
    pthread_join(w, &res_p);
    write_sum += *(int*)res_p;
    free(res_p);
    pthread_join(w1, &res_p);
    write_sum += *(int*)res_p;
    free(res_p);
    int read_sum = 0;
    pthread_join(r, &res_p);
    read_sum += *(int*)res_p;
    free(res_p);
    pthread_join(r1, &res_p);
    read_sum += *(int*)res_p;
    free(res_p);

    printf("2 reader, 2 writer test passed: %s\n", (read_sum != write_sum) ? "FALSE" : "TRUE");

    ring_destroy(buf);
    return res;
}
Ejemplo n.º 4
0
/******************************************************************************
 **函数名称: slot_destroy
 **功    能: 销毁内存块
 **输入参数: 
 **     slot: 内存块对象
 **输出参数: NONE
 **返    回: VOID
 **实现描述: 
 **注意事项: 
 **作    者: # Qifeng.zou # 2015.05.05 #
 ******************************************************************************/
void slot_destroy(slot_t *slot)
{
    ring_destroy(slot->ring);
    free(slot->addr);
    free(slot);
}
Ejemplo n.º 5
0
/******************************************************************************
 **函数名称: queue_destroy
 **功    能: 销毁加锁队列
 **输入参数:
 **     queue: 队列
 **输出参数: NONE
 **返    回: VOID
 **实现描述:
 **注意事项:
 **作    者: # Qifeng.zou # 2014.10.12 #
 ******************************************************************************/
void queue_destroy(queue_t *queue)
{
    ring_destroy(queue->ring);
    slot_destroy(queue->slot);
    free(queue);
}