vp9_job_queue.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2018 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <assert.h>
  11. #include <string.h>
  12. #include "vpx/vpx_integer.h"
  13. #include "vp9/decoder/vp9_job_queue.h"
  14. void vp9_jobq_init(JobQueueRowMt *jobq, uint8_t *buf, size_t buf_size) {
  15. #if CONFIG_MULTITHREAD
  16. pthread_mutex_init(&jobq->mutex, NULL);
  17. pthread_cond_init(&jobq->cond, NULL);
  18. #endif
  19. jobq->buf_base = buf;
  20. jobq->buf_wr = buf;
  21. jobq->buf_rd = buf;
  22. jobq->buf_end = buf + buf_size;
  23. jobq->terminate = 0;
  24. }
  25. void vp9_jobq_reset(JobQueueRowMt *jobq) {
  26. #if CONFIG_MULTITHREAD
  27. pthread_mutex_lock(&jobq->mutex);
  28. #endif
  29. jobq->buf_wr = jobq->buf_base;
  30. jobq->buf_rd = jobq->buf_base;
  31. jobq->terminate = 0;
  32. #if CONFIG_MULTITHREAD
  33. pthread_mutex_unlock(&jobq->mutex);
  34. #endif
  35. }
  36. void vp9_jobq_deinit(JobQueueRowMt *jobq) {
  37. vp9_jobq_reset(jobq);
  38. #if CONFIG_MULTITHREAD
  39. pthread_mutex_destroy(&jobq->mutex);
  40. pthread_cond_destroy(&jobq->cond);
  41. #endif
  42. }
  43. void vp9_jobq_terminate(JobQueueRowMt *jobq) {
  44. #if CONFIG_MULTITHREAD
  45. pthread_mutex_lock(&jobq->mutex);
  46. #endif
  47. jobq->terminate = 1;
  48. #if CONFIG_MULTITHREAD
  49. pthread_cond_broadcast(&jobq->cond);
  50. pthread_mutex_unlock(&jobq->mutex);
  51. #endif
  52. }
  53. int vp9_jobq_queue(JobQueueRowMt *jobq, void *job, size_t job_size) {
  54. int ret = 0;
  55. #if CONFIG_MULTITHREAD
  56. pthread_mutex_lock(&jobq->mutex);
  57. #endif
  58. if (jobq->buf_end >= jobq->buf_wr + job_size) {
  59. memcpy(jobq->buf_wr, job, job_size);
  60. jobq->buf_wr = jobq->buf_wr + job_size;
  61. #if CONFIG_MULTITHREAD
  62. pthread_cond_signal(&jobq->cond);
  63. #endif
  64. ret = 0;
  65. } else {
  66. /* Wrap around case is not supported */
  67. assert(0);
  68. ret = 1;
  69. }
  70. #if CONFIG_MULTITHREAD
  71. pthread_mutex_unlock(&jobq->mutex);
  72. #endif
  73. return ret;
  74. }
  75. int vp9_jobq_dequeue(JobQueueRowMt *jobq, void *job, size_t job_size,
  76. int blocking) {
  77. int ret = 0;
  78. #if CONFIG_MULTITHREAD
  79. pthread_mutex_lock(&jobq->mutex);
  80. #endif
  81. if (jobq->buf_end >= jobq->buf_rd + job_size) {
  82. while (1) {
  83. if (jobq->buf_wr >= jobq->buf_rd + job_size) {
  84. memcpy(job, jobq->buf_rd, job_size);
  85. jobq->buf_rd = jobq->buf_rd + job_size;
  86. ret = 0;
  87. break;
  88. } else {
  89. /* If all the entries have been dequeued, then break and return */
  90. if (jobq->terminate == 1) {
  91. ret = 1;
  92. break;
  93. }
  94. if (blocking == 1) {
  95. #if CONFIG_MULTITHREAD
  96. pthread_cond_wait(&jobq->cond, &jobq->mutex);
  97. #endif
  98. } else {
  99. /* If there is no job available,
  100. * and this is non blocking call then return fail */
  101. ret = 1;
  102. break;
  103. }
  104. }
  105. }
  106. } else {
  107. /* Wrap around case is not supported */
  108. ret = 1;
  109. }
  110. #if CONFIG_MULTITHREAD
  111. pthread_mutex_unlock(&jobq->mutex);
  112. #endif
  113. return ret;
  114. }