从dvsdk中扣出来的用于线程间传送指针
- #include <stdio.h>
-
#include <stdlib.h>
-
#include <unistd.h>
-
#include <pthread.h>
-
#include <errno.h>
-
#include <string.h>
-
-
#define Dmai_EINVAL -5 /**< Invalid input arguments (failure). */
-
#define Dmai_ENOMEM -4 /**< No memory available (failure). */
-
#define Dmai_EIO -3 /**< An IO error occured (failure). */
-
#define Dmai_ENOTIMPL -2 /**< Functionality not implemented (failure). */
-
#define Dmai_EFAIL -1 /**< General failure code (failure). */
-
#define Dmai_EOK 0 /**< General success code (success). */
-
#define Dmai_EFLUSH 1 /**< The command was flushed (success). */
-
#define Dmai_EPRIME 2 /**< The command was primed (success). */
-
#define Dmai_EFIRSTFIELD 3 /**< Only the first field was processed (success)*/
-
#define Dmai_EBITERROR 4 /**< There was a non fatal bit error (success). */
-
#define Dmai_ETIMEOUT 5 /**< The operation was timed out (success). */
-
#define Dmai_EEOF 6 /**< The operation reached end of file */
-
#define Dmai_EAGAIN 7 /**< The command needs to be rerun (success). */
-
-
-
typedef void *Ptr;
-
-
#if defined xdc_target__
-
#else
-
typedef signed char Int8 ; /* 8 bit value */
-
#endif
-
-
-
#if defined xdc_target__
-
#else
-
typedef signed short int Int16 ; /* 16 bit value */
-
#endif
-
#if defined xdc_target__
-
#else
-
typedef signed long int Int32 ; /* 32 bit value */
-
#endif
-
-
#if defined xdc_target__
-
#else
-
typedef unsigned char Uint8 ; /* 8 bit value */
-
#endif
-
#if defined xdc_target__
-
#else
-
typedef unsigned short int Uint16 ; /* 16 bit value */
-
#endif
-
#if defined xdc_target__
-
#else
-
typedef unsigned long int Uint32 ; /* 32 bit value */
-
#endif
-
-
typedef float Real32 ; /* 32 bit value */
-
typedef double Real64 ; /* 64 bit value */
-
-
#if defined xdc_target__
-
#else
-
typedef short int Bool ; /* 16 bit value */
-
#endif
-
-
typedef char Char8 ; /* 8 bit value */
-
typedef short Char16 ; /* 16 bit value */
-
-
typedef unsigned char Uchar8 ; /* 8 bit value */
-
typedef unsigned short Uchar16 ; /* 16 bit value */
-
-
/* TBD resolve this with hal_psc.c */
-
#if defined xdc_target__
-
#else
-
typedef int Int ;
-
#endif
-
-
#if defined xdc_target__
-
#else
-
#define Void void
-
#endif
-
typedef void * Pvoid ;
-
-
typedef Char8 * Pstr ;
-
typedef Uchar8 * Pustr ;
-
-
#define assert(h) if(!h)printf("assert fail:%d\n", __LINE__)
-
-
/** ============================================================================
-
* @const TRUE/FALSE
-
*
-
* @desc Boolean constants
-
* ============================================================================
-
*/
-
#if !defined (FALSE)
-
#define FALSE 0u
-
#endif
-
-
#if !defined (TRUE)
-
#define TRUE 1u
-
#endif
-
-
typedef struct Fifo_Object {
-
pthread_mutex_t mutex;
-
Int numBufs;
-
Int16 flush;
-
Int pipes[2];
-
void *pUserData;
-
} Fifo_Object;
-
-
/**
-
* @brief Handle through which to reference a Fifo.
-
*/
-
typedef struct Fifo_Object *Fifo_Handle;
-
-
/**
-
* @brief Attributes used to create a Fifo.
-
* @see Fifo_Attrs_DEFAULT.
-
*/
-
typedef struct Fifo_Attrs {
-
/**
-
* @brief Maximum elements that can be put on the Fifo at once
-
* @remarks For Bios only, Linux ignores this attribute
-
*/
-
Int maxElems;
-
} Fifo_Attrs;
-
-
const Fifo_Attrs Fifo_Attrs_DEFAULT = {
-
0
-
};
-
-
/******************************************************************************
-
* Fifo_create
-
******************************************************************************/
-
Fifo_Handle Fifo_create(Fifo_Attrs *attrs)
-
{
-
Fifo_Handle hFifo;
-
-
if (attrs == NULL) {
-
printf("NULL attrs not supported\n");
-
return NULL;
-
}
-
-
hFifo = calloc(1, sizeof(Fifo_Object));
-
-
if (hFifo == NULL) {
-
printf("Failed to allocate space for Fifo Object\n");
-
return NULL;
-
}
-
-
if (pipe(hFifo->pipes)) {
-
free(hFifo);
-
return NULL;
-
}
-
hFifo->numBufs = 0;
-
-
pthread_mutex_init(&hFifo->mutex, NULL);
-
-
return hFifo;
-
}
-
-
/******************************************************************************
-
* Fifo_delete
-
******************************************************************************/
-
Int Fifo_delete(Fifo_Handle hFifo)
-
{
-
int ret = Dmai_EOK;
-
-
if (hFifo) {
-
if (close(hFifo->pipes[0])) {
-
ret = Dmai_EIO;
-
}
-
-
if (close(hFifo->pipes[1])) {
-
ret = Dmai_EIO;
-
}
-
-
pthread_mutex_destroy(&hFifo->mutex);
-
-
free(hFifo);
-
}
-
-
return ret;
-
}
-
-
/******************************************************************************
-
* Fifo_get
-
******************************************************************************/
-
Int Fifo_get(Fifo_Handle hFifo, Ptr ptrPtr)
-
{
-
Int flush;
-
Int numBytes;
-
-
assert(hFifo);
-
assert(ptrPtr);
-
-
pthread_mutex_lock(&hFifo->mutex);
-
flush = hFifo->flush;
-
-
/* BEGIN: Added by guojun wen, 2010/8/2 PN: */
-
#if 1
-
if (hFifo->numBufs <= 0)
-
{
-
pthread_mutex_unlock(&hFifo->mutex);
-
return Dmai_EIO;
-
}
-
#endif
-
hFifo->numBufs--;
-
/* END: Added by guojun wen, 2010/8/2 */
-
-
pthread_mutex_unlock(&hFifo->mutex);
-
-
if (flush) {
-
return Dmai_EFLUSH;
-
}
-
-
numBytes = read(hFifo->pipes[0], ptrPtr, sizeof(Ptr));
-
if (numBytes != sizeof(Ptr)) {
-
printf("failed to read pipe : numBytes = %d\n",
-
numBytes);
-
pthread_mutex_lock(&hFifo->mutex);
-
flush = hFifo->flush;
-
if (flush) {
-
hFifo->flush = FALSE;
-
}
-
pthread_mutex_unlock(&hFifo->mutex);
-
-
if (flush) {
-
return Dmai_EFLUSH;
-
}
-
return Dmai_EIO;
-
}
-
-
/* BEGIN: Deleted by guojun wen, 2010/8/2 PN: */
-
#if 0
-
pthread_mutex_lock(&hFifo->mutex);
-
hFifo->numBufs--;
-
pthread_mutex_unlock(&hFifo->mutex);
-
#endif /* #if 0 */
-
/* END: Deleted by guojun wen, 2010/8/2 */
-
-
return Dmai_EOK;
-
}
-
-
/******************************************************************************
-
* Fifo_flush
-
******************************************************************************/
-
Int Fifo_flush(Fifo_Handle hFifo)
-
{
-
char ch = 0xff;
-
-
assert(hFifo);
-
-
pthread_mutex_lock(&hFifo->mutex);
-
hFifo->flush = TRUE;
-
pthread_mutex_unlock(&hFifo->mutex);
-
-
/* Make sure any Fifo_get() calls are unblocked */
-
if (write(hFifo->pipes[1], &ch, 1) != 1) {
-
return Dmai_EIO;
-
}
-
-
return Dmai_EOK;
-
}
-
-
/******************************************************************************
-
* Fifo_put
-
******************************************************************************/
-
Int Fifo_put(Fifo_Handle hFifo, Ptr ptr)
-
{
-
assert(hFifo);
-
assert(ptr);
-
-
if (write(hFifo->pipes[1], &ptr, sizeof(Ptr)) != sizeof(Ptr)) {
-
return Dmai_EIO;
-
}
-
pthread_mutex_lock(&hFifo->mutex);
-
hFifo->numBufs++;
-
pthread_mutex_unlock(&hFifo->mutex);
-
-
return Dmai_EOK;
-
}
-
-
/******************************************************************************
-
* Fifo_getNumEntries
-
******************************************************************************/
-
Int Fifo_getNumEntries(Fifo_Handle hFifo)
-
{
-
Int numEntries;
-
-
assert(hFifo);
-
-
pthread_mutex_lock(&hFifo->mutex);
-
numEntries = hFifo->numBufs;
-
pthread_mutex_unlock(&hFifo->mutex);
-
-
return numEntries;
-
}
-
-
void Fifo_setUserData(Fifo_Handle hFifo,
-
void *pUserData)
-
{
-
/* fifo is null */
-
if (NULL == hFifo)
-
{
-
return;
-
}
-
pthread_mutex_lock(&hFifo->mutex);
-
hFifo->pUserData = pUserData;
-
pthread_mutex_unlock(&hFifo->mutex);
-
-
return;
-
}
-
-
void *Fifo_getUserData(Fifo_Handle hFifo)
-
{
-
/* fifo is null */
-
if (NULL == hFifo)
-
{
-
return NULL;
-
}
-
return hFifo->pUserData;
-
}
-
-
Fifo_Handle hCap2Play ;
-
Fifo_Handle hPlay2Cap ;
-
-
static int CreateFifo()
-
{
-
Fifo_Attrs fAttrs = Fifo_Attrs_DEFAULT;
-
Fifo_Handle ahFiFo[2] = {0};
-
long lLoop = 0;
-
long lCount = 0;
-
-
for (lCount = 0; lCount < 2; lCount++)
-
{
-
ahFiFo[lCount] = Fifo_create(&fAttrs);
-
if (NULL == ahFiFo[lCount])
-
{
-
for (lLoop = 0; lLoop < lCount; lLoop++)
-
{
-
Fifo_delete(ahFiFo[lLoop]);
-
}
-
return -1;
-
}
-
}
-
-
hCap2Play = ahFiFo[0];
-
hPlay2Cap = ahFiFo[1];
-
-
-
return 1;
-
}
-
-
unsigned char arr[5] = {'1', '2','3','4','5'};
-
-
-
void *paly_function( void *ptr )
-
{
-
int count = 0;
-
int fiforet = Dmai_EOK;
-
-
while(1)
-
{
-
char* localptr = NULL;
-
if(count >= sizeof(arr)/sizeof(char))
-
{
-
break;
-
}
-
-
fiforet = Fifo_get(hCap2Play, (Ptr)&localptr);
-
if(fiforet != Dmai_EOK )
-
{
-
printf("play:get fail\n");
-
sleep(1);
-
continue;
-
}
-
-
printf("play get :0x%x\n", *localptr);
-
count++;
-
-
fiforet = Fifo_put(hPlay2Cap, localptr);
-
if(fiforet != Dmai_EOK )
-
{
-
printf("play:put fail\n");
-
sleep(1);
-
continue;
-
}
-
}
-
-
sleep(2);
-
printf("play thread out\n");
-
}
-
-
int main()
-
{
-
pthread_t thread1;
-
int ret ;
-
int count = 0;
-
ret = CreateFifo();
-
if(ret < 0)
-
{
-
printf("create fifo fail \n");
-
}
-
-
ret = pthread_create( &thread1, NULL, paly_function, (void*) NULL);
-
if(ret != 0)
-
{
-
printf("creat thread fail");
-
}
-
-
for(count = 0; count < sizeof(arr)/sizeof(char);count++)
-
{
-
printf("main,put count:%d\n", count);
-
Fifo_put(hCap2Play, (Ptr)&arr[count]);
-
}
-
-
sleep(1);
-
-
for(count = 0; count < sizeof(arr)/sizeof(char);count++)
-
{
-
char* localptr = NULL;
-
ret = Fifo_get(hPlay2Cap, &localptr);
-
if(ret == Dmai_EOK )
-
{
-
printf("main get:%c\n", *localptr);
-
}
-
}
-
-
Fifo_delete(hCap2Play);
-
Fifo_delete(hPlay2Cap);
-
-
return 0;
- }