#include "uv.h"
#include "fifo.h"
#include "pthread.h"
//#include "platmacro.h"

typedef struct fifo_test_s fifo_test_t;
struct fifo_test_s {
    uint16_t id;
    uint8_t buf[32];
};

void* __test_fifo_produce_thread(void* arg)
{
    int num = 0;
    FIFO_STRU *fifo = arg;
    fifo_test_t test = {0};
    while(1){
        test.id++;
        num = Fifo_AddData(fifo, (uint8_t *)&test, 1, 0);
        if(num > 0){
            printf("produce %d \r\n", test.id);
        }
        else {
            printf("produce error %d  \r\n", test.id);
        }
        uv_sleep(10);
    }
    pthread_exit(NULL);
}

void* __test_fifo_consume_thread(void* arg)
{
    int num = 0;
    FIFO_STRU *fifo = arg;
    fifo_test_t test[2] = {0};
    while(1){
        num = Fifo_BlockDelData(fifo, (uint8_t *)&test, 2, 0);
        uv_sleep(15);
        if(num > 0)
            printf("got %d \r\n", num);
        else {
            printf("got error \r\n");
            break;
        }

    }
    pthread_exit(NULL);
}

void test_fifo(void)
{
    FIFO_STRU fifo = {0};
    pthread_t thread_id, thread_id2;

    Fifo_Init(&fifo, sizeof(fifo_test_t), 200);

    if(0!=pthread_create(&thread_id, NULL,(void *)__test_fifo_produce_thread , &fifo)){
        printf("fofo thread error\r\n");
    }

    if(0!=pthread_create(&thread_id2, NULL,(void *)__test_fifo_consume_thread , &fifo)){
        printf("fofo thread error\r\n");
    }    

    pthread_join(thread_id, NULL);
    pthread_join(thread_id2, NULL);

}

