mpp_event_trigger.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright 2017 Rockchip Electronics Co. LTD
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <assert.h>
  17. #include <errno.h>
  18. #include <malloc.h>
  19. #include <memory.h>
  20. #include <pthread.h>
  21. #include <stdio.h>
  22. #include <sys/time.h>
  23. #include <unistd.h>
  24. #include "mpp_event_trigger.h"
  25. #include "mpp_debug.h"
  26. struct event_ctx_impl {
  27. int (*notify)(void *param);
  28. event_trigger trigger;
  29. pthread_cond_t condition;
  30. pthread_mutex_t mutex;
  31. RK_U32 semval;
  32. pthread_t thr;
  33. RK_S32 flag;
  34. struct event_packet *ea;
  35. int event_idx;
  36. void *parent;
  37. };
  38. static int event_create(struct event_ctx_impl *ctx)
  39. {
  40. int ret;
  41. ret = pthread_cond_init(&ctx->condition, NULL);
  42. if (ret != 0)
  43. return -1;
  44. ret = pthread_mutex_init(&ctx->mutex, NULL);
  45. if (ret != 0)
  46. return -1;
  47. ctx->semval = ctx->ea->e[0].idx;
  48. mpp_log_f("with %u\n", ctx->semval);
  49. return 0;
  50. }
  51. static void event_destroy(struct event_ctx_impl *ctx)
  52. {
  53. pthread_cond_destroy(&ctx->condition);
  54. pthread_mutex_destroy(&ctx->mutex);
  55. }
  56. /* initialize and schedule next event */
  57. static void event_init(struct event_ctx_impl *ctx)
  58. {
  59. struct ievent *e_curr;
  60. struct ievent *e_next;
  61. pthread_mutex_lock(&ctx->mutex);
  62. e_curr = &ctx->ea->e[ctx->event_idx % ctx->ea->cnt];
  63. e_next = &ctx->ea->e[(++ctx->event_idx) % ctx->ea->cnt];
  64. mpp_log("curr %d, next %d\n",
  65. e_curr->idx, e_next->idx);
  66. if (e_next->idx > e_curr->idx)
  67. ctx->semval = e_next->idx - e_curr->idx;
  68. else if (ctx->ea->loop > 0)
  69. ctx->semval = e_next->idx + ctx->ea->loop - e_curr->idx;
  70. else
  71. ctx->flag = 0;
  72. mpp_log_f("semval %u\n", ctx->semval);
  73. pthread_mutex_unlock(&ctx->mutex);
  74. }
  75. /* wait the event trigger deadline */
  76. /*static void event_wait(struct event_ctx_impl *ctx)
  77. {
  78. pthread_mutex_lock(&ctx->mutex);
  79. while (ctx->semval > 0)
  80. pthread_cond_wait(&ctx->condition, &ctx->mutex);
  81. ctx->semval--;
  82. pthread_mutex_unlock(&ctx->mutex);
  83. }*/
  84. /* wait the event trigger deadline with timeout, avoid dead lock */
  85. static int event_timed_wait(struct event_ctx_impl *ctx, unsigned int milli_sec)
  86. {
  87. int err = 0;
  88. struct timespec final_time;
  89. struct timeval curr_time;
  90. long int microdelay;
  91. gettimeofday(&curr_time, NULL);
  92. /*
  93. * convert timeval to timespec and add delay in milliseconds
  94. * for the timeout
  95. */
  96. microdelay = ((milli_sec * 1000 + curr_time.tv_usec));
  97. final_time.tv_sec = curr_time.tv_sec + (microdelay / 1000000);
  98. final_time.tv_nsec = (microdelay % 1000000) * 1000;
  99. pthread_mutex_lock(&ctx->mutex);
  100. while (ctx->semval > 0) {
  101. err = pthread_cond_timedwait(&ctx->condition, &ctx->mutex, &final_time);
  102. if (err != 0)
  103. ctx->semval--;
  104. }
  105. ctx->semval--;
  106. pthread_mutex_unlock(&ctx->mutex);
  107. return err;
  108. }
  109. /* event heart beat */
  110. static void event_down(struct event_ctx_impl *ctx)
  111. {
  112. pthread_mutex_lock(&ctx->mutex);
  113. ctx->semval--;
  114. pthread_cond_signal(&ctx->condition);
  115. pthread_mutex_unlock(&ctx->mutex);
  116. }
  117. /* a callback to notify the event trigger that there is a event heart beat */
  118. static int event_notify(void *param)
  119. {
  120. struct event_ctx_impl *ctx = (struct event_ctx_impl *)param;
  121. if (ctx->flag)
  122. event_down(ctx);
  123. return 0;
  124. }
  125. static void *event_trigger_thread(void *param)
  126. {
  127. struct event_ctx_impl *ctx = (struct event_ctx_impl *)param;
  128. struct timeval curr_time;
  129. long start_time, curr;
  130. RK_S32 ret;
  131. gettimeofday(&curr_time, NULL);
  132. start_time = curr_time.tv_sec * 1000 + curr_time.tv_usec / 1000;
  133. event_create(ctx);
  134. while (1) {
  135. ret = event_timed_wait(ctx, ctx->semval * 10000);
  136. if (!ctx->flag)
  137. break;
  138. if (ret == ETIMEDOUT) {
  139. mpp_err("wait event timeout\n");
  140. break;
  141. }
  142. gettimeofday(&curr_time, NULL);
  143. curr = curr_time.tv_sec * 1000 + curr_time.tv_usec / 1000;
  144. /* TODO, trigger event */
  145. mpp_log("[%ld ms] event idx %d triggered\n",
  146. curr - start_time, ctx->event_idx);
  147. mpp_log("cnt %d\n", ctx->ea->cnt);
  148. ctx->trigger(ctx->parent, ctx->ea->e[ctx->event_idx % ctx->ea->cnt].event);
  149. /* schedule next event */
  150. event_init(ctx);
  151. if (!ctx->flag)
  152. break;
  153. }
  154. event_destroy(ctx);
  155. mpp_log_f("exit\n");
  156. return NULL;
  157. }
  158. struct event_ctx* event_ctx_create(struct event_packet *ea,
  159. event_trigger trigger, void *parent)
  160. {
  161. struct event_ctx_impl *ctx = (struct event_ctx_impl *)malloc(sizeof(*ctx));
  162. if (ctx == NULL) {
  163. mpp_err("allocate event ctx failed\n");
  164. return NULL;
  165. }
  166. assert(ea->cnt <= 128);
  167. ctx->event_idx = 0;
  168. ctx->ea = ea;
  169. ctx->notify = event_notify;
  170. ctx->trigger = trigger;
  171. ctx->parent = parent;
  172. ctx->flag = 1;
  173. pthread_create(&ctx->thr, NULL, event_trigger_thread, ctx);
  174. return (struct event_ctx *)ctx;
  175. }
  176. void event_ctx_release(struct event_ctx *ctx)
  177. {
  178. void *ret;
  179. struct event_ctx_impl *ictx = (struct event_ctx_impl *)ctx;
  180. assert(ctx != NULL);
  181. if (ictx->flag) {
  182. ictx->flag = 0;
  183. ictx->semval = 0;
  184. pthread_cond_signal(&ictx->condition);
  185. }
  186. pthread_join(ictx->thr, &ret);
  187. free(ictx);
  188. }
  189. #ifdef EVENT_TRIGGER_TEST
  190. /*
  191. * the following codes is the sample to use the event trigger
  192. */
  193. struct event_impl {
  194. int cnt;
  195. int loop;
  196. int idx[128];
  197. int event[128];
  198. };
  199. /* a callback to notify test that a event occur */
  200. static void event_occur(void *parent, void *event)
  201. {
  202. int *e = (int *)event;
  203. mpp_log("event %d occur\n", *e);
  204. }
  205. int main(int argc, char **argv)
  206. {
  207. struct event_ctx *ctx;
  208. int i = 0;
  209. struct event_impl ie;
  210. struct event_packet ea;
  211. ie.cnt = 4;
  212. ie.loop = 25;
  213. ie.event[0] = ie.idx[0] = 4;
  214. ie.event[1] = ie.idx[1] = 8;
  215. ie.event[2] = ie.idx[2] = 18;
  216. ie.event[3] = ie.idx[3] = 20;
  217. ea.cnt = ie.cnt;
  218. ea.loop = ie.loop;
  219. for (i = 0; i < ie.cnt; ++i) {
  220. ea.e[i].idx = ie.idx[i];
  221. ea.e[i].event = &ie.event[i];
  222. }
  223. ctx = event_ctx_create(&ea, event_occur, NULL);
  224. while (i++ < 100) {
  225. usleep(10 * 1000);
  226. mpp_log("%03d:\n", i);
  227. ctx->notify((void*)ctx);
  228. }
  229. event_ctx_release(ctx);
  230. return 0;
  231. }
  232. #endif