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 *)