#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
#include <time.h>
#include "datafifo.h"
//#define CONFIG_COND_FREE 1
#define CONFIG_COND_ACTIVE 1
//#define MAX_SFIFO_NUM 32
//struct data_lists_s sfifo_des[MAX_SFIFO_NUM];
struct data_lists_s *g_data_lists;
struct data_s* data_lists_get_free_buf(struct data_lists_s *p_data_lists)
{
static long empty_count = 0;
struct data_s *p_data = NULL;
pthread_mutex_lock(&(p_data_lists->free_list.lock_mutex));
#ifdef CONFIG_COND_FREE
while (p_data_lists->free_list.head == NULL) {
pthread_cond_wait(&(p_data_lists->free_list.cond), &(p_data_lists->free_list.lock_mutex));
}
#else
if (p_data_lists->free_list.head == NULL) {
if (empty_count++ % 120 == 0) {
printf("free list empty\n");
}
goto EXIT;
}
#endif
p_data = p_data_lists->free_list.head;
p_data_lists->free_list.head = p_data->next;
EXIT:
pthread_mutex_unlock(&(p_data_lists->free_list.lock_mutex));
return p_data;
}
int data_lists_put_free_buf(struct data_s *p_data, struct data_lists_s *p_data_lists)
{
int send_cond = 0;
pthread_mutex_lock(&(p_data_lists->free_list.lock_mutex));
if (p_data_lists->free_list.head == NULL) {
p_data_lists->free_list.head = p_data;
p_data_lists->free_list.tail = p_data;
p_data_lists->free_list.tail->next = NULL;
send_cond = 1;
} else {
p_data_lists->free_list.tail->next = p_data;
p_data_lists->free_list.tail = p_data;
p_data_lists->free_list.tail->next = NULL;
}
pthread_mutex_unlock(&(p_data_lists->free_list.lock_mutex));
#ifdef CONFIG_COND_FREE
if (send_cond) {
pthread_cond_signal(&(p_data_lists->free_list.cond));
}
#endif
return 0;
}
struct data_s* data_lists_get_active_buf(struct data_lists_s *p_data_lists)
{
struct data_s *p_data = NULL;
pthread_mutex_lock(&(p_data_lists->active_list.lock_mutex));
#ifdef CONFIG_COND_ACTIVE
while (p_data_lists->active_list.head == NULL) {
//pthread_cond_timedwait(&(sfifo_des_p->active_list.cond), &(sfifo_des_p->active_list.lock_mutex), &outtime);
pthread_cond_wait(&(p_data_lists->active_list.cond), &(p_data_lists->active_list.lock_mutex));
}
#else
if (p_data_lists->active_list.head == NULL) {
printf("active list empty\n");
goto EXIT;
}
#endif
p_data = p_data_lists->active_list.head;
p_data_lists->active_list.head = p_data->next;
EXIT:
pthread_mutex_unlock(&(p_data_lists->active_list.lock_mutex));
return p_data;
}
int data_lists_put_active_buf(struct data_s *p_data, struct data_lists_s *p_data_lists)
{
int send_cond = 0;
pthread_mutex_lock(&(p_data_lists->active_list.lock_mutex));
if (p_data_lists->active_list.head == NULL) {
p_data_lists->active_list.head = p_data;
p_data_lists->active_list.tail = p_data;
p_data_lists->active_list.tail->next = NULL;
send_cond = 1;
} else {
p_data_lists->active_list.tail->next = p_data;
p_data_lists->active_list.tail = p_data;
p_data_lists->active_list.tail->next = NULL;
}
pthread_mutex_unlock(&(p_data_lists->active_list.lock_mutex));
#ifdef CONFIG_COND_ACTIVE
if (send_cond) {
pthread_cond_signal(&(p_data_lists->active_list.cond));
}
#endif
return 0;
}
//10, 4096, 5
struct data_lists_s *data_lists_init(int data_maxnum, int data_buff_size)
{
int i = 0;
struct data_s *p_data;
struct data_lists_s *sfifo_des_p;
sfifo_des_p = (struct data_lists_s *)malloc(sizeof(struct data_lists_s));
sfifo_des_p->max_num = data_maxnum;
//sfifo_des_p->sfifos_active_max_num = sfifo_active_max_num;
sfifo_des_p->free_list.data_num = 0;
sfifo_des_p->free_list.head = NULL;
sfifo_des_p->free_list.tail = NULL;
pthread_mutex_init(&sfifo_des_p->free_list.lock_mutex, NULL);
pthread_cond_init(&sfifo_des_p->free_list.cond, NULL);
sfifo_des_p->active_list.data_num = 0;
sfifo_des_p->active_list.head = NULL;
sfifo_des_p->active_list.tail = NULL;
pthread_mutex_init(&sfifo_des_p->active_list.lock_mutex, NULL);
pthread_cond_init(&sfifo_des_p->active_list.cond, NULL);
for (i = 0; i < data_maxnum; i++) {
p_data = (struct data_s *)malloc(sizeof(struct data_s));
p_data->buffer = (unsigned char *)malloc(data_buff_size);
//printf("data_lists_init: %x\n", sfifo->buffer);
memset(p_data->buffer, 0, data_buff_size);
p_data->size = data_buff_size;
p_data->next = NULL;
data_lists_put_free_buf(p_data, sfifo_des_p);
}
return sfifo_des_p;
}
void *productor_thread_func(void *arg)
{
struct data_s *sfifo;
while (1) {
sfifo = data_lists_get_free_buf(g_data_lists);
if (sfifo != NULL) {
printf("+++++++++++++++++ put : %x\n", sfifo->buffer[0]);
data_lists_put_active_buf(sfifo, g_data_lists);
}
//usleep(20*1000);
}
}
void *comsumer_thread_func(void *arg)
{
struct data_s *p_data;
int count = 0;
while (1) {
p_data = data_lists_get_active_buf(g_data_lists);
if (p_data != NULL) {
printf("---------------- get %x\n", p_data->buffer[0]);
data_lists_put_free_buf(p_data, g_data_lists);
}
//usleep(10 * 1000);
// if (count++ > 10000) {
// exit(-1);
// }
}
}
int main()
{
int ret;
static pthread_t productor_thread;
static pthread_t consumer_thread;
//struct data_s *r_sfifo;
g_data_lists = data_lists_init(10, 128);
ret = pthread_create(&productor_thread, NULL, productor_thread_func, NULL);
ret = pthread_create(&consumer_thread, NULL, comsumer_thread_func, NULL);
while (1) {
sleep(1);
}
return 0;
}
评论0