dpdk_ring剥离(单线程版)

本文介绍DPDK_RING的单线程剥离过程,探讨其无锁设计的线程安全特性,以及如何减少比较和减法操作以提高性能。已提供相关源代码:daq_ring.h 和 daq_ring.c。
摘要由CSDN通过智能技术生成

DPDK_RING 剥离(sc)

dpdk中的ring结构的原理在官方的doc中有。详细的介绍了单线程和多线程下的ring的结构的实现。其中线程安全的ring的出入队中没有用到锁,这个结构是比较巧妙的。
此外,和一般的ring设计相比,dpdk的ring中减少了比较的次数和减法的次数。考虑了很多性能方面的东西。
目前就剥离了单线程版的。
代码如下:
- daq_ring.h

#ifndef DAQ_RING_H
#define DAQ_RING_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include <pthread.h>




typedef struct _daq_ring{
  //  pthread_mutex_t mutex;
    int size;
    int mask;
    int cons_head;
    int cons_tail;
    int prod_head;
    int prod_tail;
    void * ring[0];
};
typedef struct _daq_ring daq_ring;
//create a ring which can contain size's objects.
daq_ring * daq_ring_create(int num);
//fre a ring.
int daq_ring_free(daq_ring * r);
//see whether a ring is empty.
int daq_ring_isEmpty(daq_ring * r);
//see whether a ring is full.
int daq_ring_isFull(daq_ring * r);
//see the number of objects in ring now.
int daq_ring_count(daq_ring * r);
//see the size of a ring.
int daq_ring_getSize(daq_ring *r);
//see the number of objects can be inputed into the ring now.
int daq_ring_freeCount(daq_ring * r);
//enqueue objects into a ring.
int daq_ring_enqueue(daq_ring * r, void * const *obj_table, int num,int mc);
//dequeue objects from a ring.
int daq_ring_dequeue(daq_ring * r, void **obj_table, int num,int mc);

#endif
//end of daq_ring.h

-daq_ring.c

#include "daq_ring.h"

/*typedef struct _daq_ring{
    pthread_mutex_t mutex;
    unsigned int size;
    void * head;
    void * tail;
    void * memhead;
}daq_ring;*/

daq_ring * daq_ring_create(int num){
    daq_ring * r = NULL;
    if(num <=0)return NULL;
    r = (daq_ring *)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值