/src/libvpx/vp9/decoder/vp9_job_queue.c
Line | Count | Source (jump to first uncovered line) |
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 | | |
11 | | #include <assert.h> |
12 | | #include <string.h> |
13 | | |
14 | | #include "vpx/vpx_integer.h" |
15 | | #include "vpx_util/vpx_pthread.h" |
16 | | |
17 | | #include "vp9/decoder/vp9_job_queue.h" |
18 | | |
19 | 0 | void vp9_jobq_init(JobQueueRowMt *jobq, uint8_t *buf, size_t buf_size) { |
20 | 0 | #if CONFIG_MULTITHREAD |
21 | 0 | pthread_mutex_init(&jobq->mutex, NULL); |
22 | 0 | pthread_cond_init(&jobq->cond, NULL); |
23 | 0 | #endif |
24 | 0 | jobq->buf_base = buf; |
25 | 0 | jobq->buf_wr = buf; |
26 | 0 | jobq->buf_rd = buf; |
27 | 0 | jobq->buf_end = buf + buf_size; |
28 | 0 | jobq->terminate = 0; |
29 | 0 | } |
30 | | |
31 | 0 | void vp9_jobq_reset(JobQueueRowMt *jobq) { |
32 | 0 | #if CONFIG_MULTITHREAD |
33 | 0 | pthread_mutex_lock(&jobq->mutex); |
34 | 0 | #endif |
35 | 0 | jobq->buf_wr = jobq->buf_base; |
36 | 0 | jobq->buf_rd = jobq->buf_base; |
37 | 0 | jobq->terminate = 0; |
38 | 0 | #if CONFIG_MULTITHREAD |
39 | 0 | pthread_mutex_unlock(&jobq->mutex); |
40 | 0 | #endif |
41 | 0 | } |
42 | | |
43 | 0 | void vp9_jobq_deinit(JobQueueRowMt *jobq) { |
44 | 0 | vp9_jobq_reset(jobq); |
45 | 0 | #if CONFIG_MULTITHREAD |
46 | 0 | pthread_mutex_destroy(&jobq->mutex); |
47 | 0 | pthread_cond_destroy(&jobq->cond); |
48 | 0 | #endif |
49 | 0 | } |
50 | | |
51 | 0 | void vp9_jobq_terminate(JobQueueRowMt *jobq) { |
52 | 0 | #if CONFIG_MULTITHREAD |
53 | 0 | pthread_mutex_lock(&jobq->mutex); |
54 | 0 | #endif |
55 | 0 | jobq->terminate = 1; |
56 | 0 | #if CONFIG_MULTITHREAD |
57 | 0 | pthread_cond_broadcast(&jobq->cond); |
58 | 0 | pthread_mutex_unlock(&jobq->mutex); |
59 | 0 | #endif |
60 | 0 | } |
61 | | |
62 | 0 | int vp9_jobq_queue(JobQueueRowMt *jobq, void *job, size_t job_size) { |
63 | 0 | int ret = 0; |
64 | 0 | #if CONFIG_MULTITHREAD |
65 | 0 | pthread_mutex_lock(&jobq->mutex); |
66 | 0 | #endif |
67 | 0 | if (jobq->buf_end >= jobq->buf_wr + job_size) { |
68 | 0 | memcpy(jobq->buf_wr, job, job_size); |
69 | 0 | jobq->buf_wr = jobq->buf_wr + job_size; |
70 | 0 | #if CONFIG_MULTITHREAD |
71 | 0 | pthread_cond_signal(&jobq->cond); |
72 | 0 | #endif |
73 | 0 | ret = 0; |
74 | 0 | } else { |
75 | | /* Wrap around case is not supported */ |
76 | 0 | assert(0); |
77 | 0 | ret = 1; |
78 | 0 | } |
79 | 0 | #if CONFIG_MULTITHREAD |
80 | 0 | pthread_mutex_unlock(&jobq->mutex); |
81 | 0 | #endif |
82 | 0 | return ret; |
83 | 0 | } |
84 | | |
85 | | int vp9_jobq_dequeue(JobQueueRowMt *jobq, void *job, size_t job_size, |
86 | 0 | int blocking) { |
87 | 0 | int ret = 0; |
88 | 0 | #if CONFIG_MULTITHREAD |
89 | 0 | pthread_mutex_lock(&jobq->mutex); |
90 | 0 | #endif |
91 | 0 | if (jobq->buf_end >= jobq->buf_rd + job_size) { |
92 | 0 | while (1) { |
93 | 0 | if (jobq->buf_wr >= jobq->buf_rd + job_size) { |
94 | 0 | memcpy(job, jobq->buf_rd, job_size); |
95 | 0 | jobq->buf_rd = jobq->buf_rd + job_size; |
96 | 0 | ret = 0; |
97 | 0 | break; |
98 | 0 | } else { |
99 | | /* If all the entries have been dequeued, then break and return */ |
100 | 0 | if (jobq->terminate == 1) { |
101 | 0 | ret = 1; |
102 | 0 | break; |
103 | 0 | } |
104 | 0 | if (blocking == 1) { |
105 | 0 | #if CONFIG_MULTITHREAD |
106 | 0 | pthread_cond_wait(&jobq->cond, &jobq->mutex); |
107 | 0 | #endif |
108 | 0 | } else { |
109 | | /* If there is no job available, |
110 | | * and this is non blocking call then return fail */ |
111 | 0 | ret = 1; |
112 | 0 | break; |
113 | 0 | } |
114 | 0 | } |
115 | 0 | } |
116 | 0 | } else { |
117 | | /* Wrap around case is not supported */ |
118 | 0 | ret = 1; |
119 | 0 | } |
120 | 0 | #if CONFIG_MULTITHREAD |
121 | 0 | pthread_mutex_unlock(&jobq->mutex); |
122 | 0 | #endif |
123 | |
|
124 | 0 | return ret; |
125 | 0 | } |