producer_consumer_async.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright 2018-2019 C-SKY Microsystems Co., Ltd.
  3. * Copyright (C) 2021 Alibaba Group Holding Limited
  4. * Author: LuChongzhi <chongzhi.lcz@alibaba-inc.com>
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. */
  19. #ifndef __PRODUCER_CONSUMER_ASYNC_H__
  20. #define __PRODUCER_CONSUMER_ASYNC_H__
  21. #include "common.h"
  22. struct pca_data_info;
  23. typedef int32_t (*pca_consumed_callback)(struct pca_data_info* data_info);
  24. typedef struct pca_data_info {
  25. void* data; /* data pointer */
  26. uint32_t length; /* data length */
  27. uint32_t type; /* data type */
  28. pca_consumed_callback consumed_callback;/* data need free by dispatcher */
  29. } pca_data_info_t;
  30. typedef struct pca_data_statistics {
  31. uint32_t total_delivered; /* Data feeded in */
  32. uint32_t total_discarded; /* Data discarded because busy */
  33. uint32_t total_updated; /* New data updated in before consume */
  34. uint32_t total_consumed; /* Data consumed */
  35. } pca_data_statistics_t;
  36. typedef int32_t (*pca_consumer_process)(pca_data_info_t* data_info);
  37. typedef struct pca_thread_info {
  38. pthread_t tid; /* thread ID */
  39. char name[32]; /* thread name */
  40. sem_t sem_wakeup; /* semphore to wakeup thread */
  41. bool is_running;
  42. pca_data_info_t data_info; /* the data delivered from parent */
  43. bool has_new_data; /* indicate there is new data to handle */
  44. bool need_unlock_data_mutex; /* Just for mark flag when need unlock */
  45. pthread_mutex_t mutex_data; /* mutex to protect handling data */
  46. pca_data_statistics_t stats; /* the statistics of data */
  47. } pca_thread_info_t;
  48. typedef struct pca_consumer_processor_info {
  49. pca_consumer_process process; /* consumer processor function */
  50. int data_type; /* the data type to be consume */
  51. char name[16]; /* consumer name for debug */
  52. } pca_consumer_processor_info_t;
  53. struct pca_dispatcher_info; /* Declare here but defined below */
  54. typedef struct pca_consumer_list_info {
  55. pca_thread_info_t thread_info; /* thread info */
  56. pca_consumer_processor_info_t processor;/* post processor info */
  57. struct list_head list; /* to build list */
  58. struct pca_dispatcher_info* dispatcher; /* dispatcher's pointer */
  59. } pca_consumer_list_info_t;
  60. typedef struct {
  61. pca_data_info_t data_info; /* data_info */
  62. unsigned int reference_count; /* data using count */
  63. struct list_head list; /* to build list */
  64. } pca_data_free_list_info_t;
  65. typedef struct pca_dispatcher_info {
  66. pca_thread_info_t thread_info; /* thread info */
  67. pthread_mutex_t consumer_mutex; /* mutex to protect consumer_list */
  68. pca_consumer_list_info_t consumer_list; /* consumers list */
  69. pthread_mutex_t data_free_mutex; /* mutex to protect data_free_list */
  70. pca_data_free_list_info_t data_free_list;/* data list need to free */
  71. } pca_dispatcher_info_t;
  72. int32_t pca_dispatcher_create(pca_dispatcher_info_t** dispatcher, char* name);
  73. int32_t pca_dispatcher_run(pca_dispatcher_info_t* dispatcher);
  74. int32_t pca_dispatcher_feed(pca_dispatcher_info_t* dispatcher,
  75. void *data, int32_t data_type);
  76. int32_t pca_dispatcher_stop(pca_dispatcher_info_t* dispatcher);
  77. int32_t pca_dispatcher_destory(pca_dispatcher_info_t* dispatcher);
  78. int32_t pca_add_consumer_processor(pca_dispatcher_info_t* dispatcher,
  79. pca_consumer_processor_info_t *processor);
  80. int32_t pca_remove_consumer_processor(pca_dispatcher_info_t* dispatcher,
  81. pca_consumer_processor_info_t *processor);
  82. int32_t pca_feed_consumers(pca_dispatcher_info_t* dispatcher,
  83. pca_data_info_t* data_info);
  84. int32_t pca_feed_dispatcher(pca_dispatcher_info_t* dispatcher,
  85. pca_data_info_t* data_info);
  86. #endif /* __PRODUCER_CONSUMER_ASYNC_H__ */