Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1438463
  • 博文数量: 704
  • 博客积分: 10140
  • 博客等级: 上将
  • 技术积分: 6230
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-15 20:41
文章分类

全部博文(704)

文章存档

2013年(1)

2012年(16)

2011年(536)

2010年(151)

分类:

2011-11-05 20:22:43

原文地址:fifo 演示 作者:pppStar

从dvsdk中扣出来的用于线程间传送指针

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <pthread.h>
  5. #include <errno.h>
  6. #include <string.h>

  7. #define Dmai_EINVAL -5 /**< Invalid input arguments (failure). */
  8. #define Dmai_ENOMEM -4 /**< No memory available (failure). */
  9. #define Dmai_EIO -3 /**< An IO error occured (failure). */
  10. #define Dmai_ENOTIMPL -2 /**< Functionality not implemented (failure). */
  11. #define Dmai_EFAIL -1 /**< General failure code (failure). */
  12. #define Dmai_EOK 0 /**< General success code (success). */
  13. #define Dmai_EFLUSH 1 /**< The command was flushed (success). */
  14. #define Dmai_EPRIME 2 /**< The command was primed (success). */
  15. #define Dmai_EFIRSTFIELD 3 /**< Only the first field was processed (success)*/
  16. #define Dmai_EBITERROR 4 /**< There was a non fatal bit error (success). */
  17. #define Dmai_ETIMEOUT 5 /**< The operation was timed out (success). */
  18. #define Dmai_EEOF 6 /**< The operation reached end of file */
  19. #define Dmai_EAGAIN 7 /**< The command needs to be rerun (success). */


  20. typedef void *Ptr;

  21. #if defined xdc_target__
  22. #else
  23. typedef signed char Int8 ; /* 8 bit value */
  24. #endif


  25. #if defined xdc_target__
  26. #else
  27. typedef signed short int Int16 ; /* 16 bit value */
  28. #endif
  29. #if defined xdc_target__
  30. #else
  31. typedef signed long int Int32 ; /* 32 bit value */
  32. #endif

  33. #if defined xdc_target__
  34. #else
  35. typedef unsigned char Uint8 ; /* 8 bit value */
  36. #endif
  37. #if defined xdc_target__
  38. #else
  39. typedef unsigned short int Uint16 ; /* 16 bit value */
  40. #endif
  41. #if defined xdc_target__
  42. #else
  43. typedef unsigned long int Uint32 ; /* 32 bit value */
  44. #endif

  45. typedef float Real32 ; /* 32 bit value */
  46. typedef double Real64 ; /* 64 bit value */

  47. #if defined xdc_target__
  48. #else
  49. typedef short int Bool ; /* 16 bit value */
  50. #endif

  51. typedef char Char8 ; /* 8 bit value */
  52. typedef short Char16 ; /* 16 bit value */

  53. typedef unsigned char Uchar8 ; /* 8 bit value */
  54. typedef unsigned short Uchar16 ; /* 16 bit value */

  55. /* TBD resolve this with hal_psc.c */
  56. #if defined xdc_target__
  57. #else
  58. typedef int Int ;
  59. #endif

  60. #if defined xdc_target__
  61. #else
  62. #define Void void
  63. #endif
  64. typedef void * Pvoid ;

  65. typedef Char8 * Pstr ;
  66. typedef Uchar8 * Pustr ;

  67. #define assert(h) if(!h)printf("assert fail:%d\n", __LINE__)

  68. /** ============================================================================
  69.  * @const TRUE/FALSE
  70.  *
  71.  * @desc Boolean constants
  72.  * ============================================================================
  73.  */
  74. #if !defined (FALSE)
  75. #define FALSE 0u
  76. #endif

  77. #if !defined (TRUE)
  78. #define TRUE 1u
  79. #endif

  80. typedef struct Fifo_Object {
  81.     pthread_mutex_t mutex;
  82.     Int numBufs;
  83.     Int16 flush;
  84.     Int pipes[2];
  85.     void *pUserData;
  86. } Fifo_Object;

  87. /**
  88.  * @brief Handle through which to reference a Fifo.
  89.  */
  90. typedef struct Fifo_Object *Fifo_Handle;

  91. /**
  92.  * @brief Attributes used to create a Fifo.
  93.  * @see Fifo_Attrs_DEFAULT.
  94.  */
  95. typedef struct Fifo_Attrs {
  96.     /**
  97.      * @brief Maximum elements that can be put on the Fifo at once
  98.      * @remarks For Bios only, Linux ignores this attribute
  99.      */
  100.     Int maxElems;
  101. } Fifo_Attrs;

  102. const Fifo_Attrs Fifo_Attrs_DEFAULT = {
  103.     0
  104. };

  105. /******************************************************************************
  106.  * Fifo_create
  107.  ******************************************************************************/
  108. Fifo_Handle Fifo_create(Fifo_Attrs *attrs)
  109. {
  110.     Fifo_Handle hFifo;

  111.     if (attrs == NULL) {
  112.         printf("NULL attrs not supported\n");
  113.         return NULL;
  114.     }

  115.     hFifo = calloc(1, sizeof(Fifo_Object));

  116.     if (hFifo == NULL) {
  117.         printf("Failed to allocate space for Fifo Object\n");
  118.         return NULL;
  119.     }

  120.     if (pipe(hFifo->pipes)) {
  121.         free(hFifo);
  122.         return NULL;
  123.     }
  124.     hFifo->numBufs = 0;

  125.     pthread_mutex_init(&hFifo->mutex, NULL);

  126.     return hFifo;
  127. }

  128. /******************************************************************************
  129.  * Fifo_delete
  130.  ******************************************************************************/
  131. Int Fifo_delete(Fifo_Handle hFifo)
  132. {
  133.     int ret = Dmai_EOK;

  134.     if (hFifo) {
  135.         if (close(hFifo->pipes[0])) {
  136.             ret = Dmai_EIO;
  137.         }

  138.         if (close(hFifo->pipes[1])) {
  139.             ret = Dmai_EIO;
  140.         }

  141.         pthread_mutex_destroy(&hFifo->mutex);

  142.         free(hFifo);
  143.     }

  144.     return ret;
  145. }

  146. /******************************************************************************
  147.  * Fifo_get
  148.  ******************************************************************************/
  149. Int Fifo_get(Fifo_Handle hFifo, Ptr ptrPtr)
  150. {
  151.     Int flush;
  152.     Int numBytes;

  153.     assert(hFifo);
  154.     assert(ptrPtr);

  155.     pthread_mutex_lock(&hFifo->mutex);
  156.     flush = hFifo->flush;
  157.     
  158.     /* BEGIN: Added by guojun wen, 2010/8/2 PN: */
  159.     #if 1
  160.     if (hFifo->numBufs <= 0)
  161.     {
  162.         pthread_mutex_unlock(&hFifo->mutex);
  163.         return Dmai_EIO;
  164.     }
  165.     #endif
  166.     hFifo->numBufs--;
  167.     /* END: Added by guojun wen, 2010/8/2 */
  168.     
  169.     pthread_mutex_unlock(&hFifo->mutex);

  170.     if (flush) {
  171.         return Dmai_EFLUSH;
  172.     }

  173.     numBytes = read(hFifo->pipes[0], ptrPtr, sizeof(Ptr));
  174.     if (numBytes != sizeof(Ptr)) {
  175.         printf("failed to read pipe : numBytes = %d\n",
  176.                 numBytes);
  177.         pthread_mutex_lock(&hFifo->mutex);
  178.         flush = hFifo->flush;
  179.         if (flush) {
  180.             hFifo->flush = FALSE;
  181.         }
  182.         pthread_mutex_unlock(&hFifo->mutex);

  183.         if (flush) {
  184.             return Dmai_EFLUSH;
  185.         }
  186.         return Dmai_EIO;
  187.     }

  188.     /* BEGIN: Deleted by guojun wen, 2010/8/2 PN: */
  189.     #if 0
  190.     pthread_mutex_lock(&hFifo->mutex);
  191.     hFifo->numBufs--;
  192.     pthread_mutex_unlock(&hFifo->mutex);
  193.     #endif /* #if 0 */
  194.     /* END: Deleted by guojun wen, 2010/8/2 */

  195.     return Dmai_EOK;
  196. }

  197. /******************************************************************************
  198.  * Fifo_flush
  199.  ******************************************************************************/
  200. Int Fifo_flush(Fifo_Handle hFifo)
  201. {
  202.     char ch = 0xff;

  203.     assert(hFifo);

  204.     pthread_mutex_lock(&hFifo->mutex);
  205.     hFifo->flush = TRUE;
  206.     pthread_mutex_unlock(&hFifo->mutex);

  207.     /* Make sure any Fifo_get() calls are unblocked */
  208.     if (write(hFifo->pipes[1], &ch, 1) != 1) {
  209.         return Dmai_EIO;
  210.     }

  211.     return Dmai_EOK;
  212. }

  213. /******************************************************************************
  214.  * Fifo_put
  215.  ******************************************************************************/
  216. Int Fifo_put(Fifo_Handle hFifo, Ptr ptr)
  217. {
  218.     assert(hFifo);
  219.     assert(ptr);

  220.     if (write(hFifo->pipes[1], &ptr, sizeof(Ptr)) != sizeof(Ptr)) {
  221.         return Dmai_EIO;
  222.     }
  223.     pthread_mutex_lock(&hFifo->mutex);
  224.     hFifo->numBufs++;
  225.     pthread_mutex_unlock(&hFifo->mutex);

  226.     return Dmai_EOK;
  227. }

  228. /******************************************************************************
  229.  * Fifo_getNumEntries
  230.  ******************************************************************************/
  231. Int Fifo_getNumEntries(Fifo_Handle hFifo)
  232. {
  233.     Int numEntries;

  234.     assert(hFifo);

  235.     pthread_mutex_lock(&hFifo->mutex);
  236.     numEntries = hFifo->numBufs;
  237.     pthread_mutex_unlock(&hFifo->mutex);

  238.     return numEntries;
  239. }

  240. void Fifo_setUserData(Fifo_Handle hFifo,
  241.                          void *pUserData)
  242. {
  243.     /* fifo is null */
  244.     if (NULL == hFifo)
  245.     {
  246.         return;
  247.     }
  248.     pthread_mutex_lock(&hFifo->mutex);
  249.     hFifo->pUserData = pUserData;
  250.     pthread_mutex_unlock(&hFifo->mutex);

  251.     return;
  252. }

  253. void *Fifo_getUserData(Fifo_Handle hFifo)
  254. {
  255.     /* fifo is null */
  256.     if (NULL == hFifo)
  257.     {
  258.         return NULL;
  259.     }
  260.     return hFifo->pUserData;
  261. }

  262. Fifo_Handle hCap2Play ;
  263. Fifo_Handle hPlay2Cap ;
  264.    
  265. static int CreateFifo()
  266. {
  267.     Fifo_Attrs fAttrs = Fifo_Attrs_DEFAULT;
  268.     Fifo_Handle ahFiFo[2] = {0};
  269.     long lLoop = 0;
  270.     long lCount = 0;

  271.     for (lCount = 0; lCount < 2; lCount++)
  272.     {
  273.         ahFiFo[lCount] = Fifo_create(&fAttrs);
  274.         if (NULL == ahFiFo[lCount])
  275.         {
  276.             for (lLoop = 0; lLoop < lCount; lLoop++)
  277.             {
  278.                 Fifo_delete(ahFiFo[lLoop]);
  279.             }
  280.             return -1;
  281.         }
  282.     }

  283.     hCap2Play = ahFiFo[0];
  284.     hPlay2Cap = ahFiFo[1];


  285.     return 1;
  286. }

  287. unsigned char arr[5] = {'1', '2','3','4','5'};


  288. void *paly_function( void *ptr )
  289. {
  290.      int count = 0;
  291.      int fiforet = Dmai_EOK;
  292.      
  293.      while(1)
  294.      {
  295.              char* localptr = NULL;
  296.              if(count >= sizeof(arr)/sizeof(char))
  297.              {
  298.                      break;
  299.              }
  300.              
  301.              fiforet = Fifo_get(hCap2Play, (Ptr)&localptr);
  302.              if(fiforet != Dmai_EOK )
  303.              {
  304.                      printf("play:get fail\n");
  305.                      sleep(1);
  306.                      continue;
  307.              }
  308.              
  309.              printf("play get :0x%x\n", *localptr);
  310.              count++;
  311.              
  312.              fiforet = Fifo_put(hPlay2Cap, localptr);
  313.              if(fiforet != Dmai_EOK )
  314.              {
  315.                      printf("play:put fail\n");
  316.                      sleep(1);
  317.                      continue;
  318.              }         
  319.      }
  320.      
  321.      sleep(2);
  322.      printf("play thread out\n");
  323. }

  324. int main()
  325. {
  326.         pthread_t thread1;
  327.         int ret ;
  328.         int count = 0;
  329.         ret = CreateFifo();
  330.         if(ret < 0)
  331.         {
  332.       printf("create fifo fail \n");
  333.     }
  334.     
  335.     ret = pthread_create( &thread1, NULL, paly_function, (void*) NULL);
  336.     if(ret != 0)
  337.     {
  338.         printf("creat thread fail");
  339.     }
  340.     
  341.     for(count = 0; count < sizeof(arr)/sizeof(char);count++)
  342.     {
  343.                 printf("main,put count:%d\n", count);
  344.          Fifo_put(hCap2Play, (Ptr)&arr[count]);
  345.     }
  346.     
  347.     sleep(1);
  348.     
  349.     for(count = 0; count < sizeof(arr)/sizeof(char);count++)
  350.     {
  351.             char* localptr = NULL;
  352.          ret = Fifo_get(hPlay2Cap, &localptr);
  353.          if(ret == Dmai_EOK )
  354.          {
  355.              printf("main get:%c\n", *localptr);
  356.          }
  357.     }
  358.     
  359.     Fifo_delete(hCap2Play);
  360.     Fifo_delete(hPlay2Cap);

  361.     return 0;
  362. }
阅读(1142) | 评论(0) | 转发(0) |
0

上一篇:libxml学习

下一篇:使用Libxml2操作XML文档

给主人留下些什么吧!~~