Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 1 | /* |
bo.xiao | 00387eb | 2023-02-24 18:44:29 +0800 | [diff] [blame] | 2 | * Copyright (C) 2021 Amlogic Corporation. |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 3 | * |
bo.xiao | 00387eb | 2023-02-24 18:44:29 +0800 | [diff] [blame] | 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. |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 15 | * |
| 16 | * Description: fifo implementation for single reader single writer |
| 17 | * Author: song.zhao@amlogic.com |
| 18 | */ |
| 19 | #include <stdbool.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <stdio.h> |
sheng.liu | 75a36d1 | 2023-07-28 07:54:03 +0000 | [diff] [blame] | 22 | #include <stdatomic.h> |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 23 | #include "aml_avsync.h" |
Song Zhao | 4f63295 | 2021-12-16 09:00:18 -0800 | [diff] [blame] | 24 | #include "aml_queue.h" |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 25 | |
| 26 | struct queue { |
| 27 | int max_len; |
| 28 | int ri; //read index |
| 29 | int wi; //write index |
sheng.liu | 75a36d1 | 2023-07-28 07:54:03 +0000 | [diff] [blame] | 30 | atomic_intptr_t *items; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | void* create_q(int max_len) |
| 34 | { |
| 35 | struct queue *q; |
| 36 | |
| 37 | if (max_len <= 0) { |
| 38 | printf("%s %d invalid max_len:%d\n", |
| 39 | __func__, __LINE__, max_len); |
| 40 | return NULL; |
| 41 | } |
| 42 | |
| 43 | q = (struct queue*)calloc(1, sizeof(*q)); |
| 44 | if (!q) { |
| 45 | printf("%s %d OOM\n", __func__, __LINE__); |
| 46 | return NULL; |
| 47 | } |
sheng.liu | 75a36d1 | 2023-07-28 07:54:03 +0000 | [diff] [blame] | 48 | q->items = (atomic_intptr_t *)calloc(max_len, sizeof(void *)); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 49 | if (!q->items) { |
| 50 | printf("%s %d OOM\n", __func__, __LINE__); |
| 51 | free(q); |
| 52 | return NULL; |
| 53 | } |
| 54 | |
| 55 | q->max_len = max_len; |
| 56 | q->ri = q->wi = 0; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 57 | return q; |
| 58 | } |
| 59 | |
| 60 | void destroy_q(void * queue) |
| 61 | { |
| 62 | struct queue *q = queue; |
| 63 | |
| 64 | if (!q) |
| 65 | return; |
| 66 | free(q->items); |
| 67 | free(q); |
| 68 | } |
| 69 | |
| 70 | int queue_item(void *queue, void * item) |
| 71 | { |
| 72 | struct queue *q = queue; |
yongchun.li | 0ee6e37 | 2021-08-20 04:26:04 -0700 | [diff] [blame] | 73 | int fullness; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 74 | |
yongchun.li | 0ee6e37 | 2021-08-20 04:26:04 -0700 | [diff] [blame] | 75 | if (!q) |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 76 | return -1; |
yongchun.li | 0ee6e37 | 2021-08-20 04:26:04 -0700 | [diff] [blame] | 77 | fullness = q->wi - q->ri; |
| 78 | if (fullness < 0) fullness += q->max_len; |
| 79 | if (fullness >= q->max_len - 1) |
| 80 | return -1; // not enough space |
| 81 | |
sheng.liu | 75a36d1 | 2023-07-28 07:54:03 +0000 | [diff] [blame] | 82 | atomic_store(&q->items[q->wi], (atomic_intptr_t)item); |
| 83 | |
| 84 | if (q->wi >= q->max_len - 1) |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 85 | q->wi = 0; |
| 86 | else |
| 87 | q->wi++; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | int peek_item(void *queue, void** p_item, uint32_t cnt) |
| 92 | { |
| 93 | struct queue *q = queue; |
| 94 | int32_t index; |
yongchun.li | 0ee6e37 | 2021-08-20 04:26:04 -0700 | [diff] [blame] | 95 | int fullness; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 96 | |
yongchun.li | 0ee6e37 | 2021-08-20 04:26:04 -0700 | [diff] [blame] | 97 | if (!q) |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 98 | return -1; |
| 99 | |
yongchun.li | 0ee6e37 | 2021-08-20 04:26:04 -0700 | [diff] [blame] | 100 | fullness = q->wi - q->ri; |
| 101 | if (fullness < 0) fullness += q->max_len; |
| 102 | if (fullness == 0 || fullness <= cnt) |
| 103 | return -1; //no enough to peek |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 104 | index = q->ri; |
| 105 | index += cnt; |
| 106 | if (index >= q->max_len) |
| 107 | index -= q->max_len; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 108 | |
sheng.liu | 75a36d1 | 2023-07-28 07:54:03 +0000 | [diff] [blame] | 109 | *p_item = (void *)atomic_load(&q->items[index]); |
| 110 | |
| 111 | if (*p_item != NULL) |
| 112 | return 0; |
| 113 | else |
| 114 | return -1; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | int dqueue_item(void *queue, void** p_item) |
| 118 | { |
| 119 | struct queue *q = queue; |
yongchun.li | 0ee6e37 | 2021-08-20 04:26:04 -0700 | [diff] [blame] | 120 | int fullness; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 121 | |
yongchun.li | 0ee6e37 | 2021-08-20 04:26:04 -0700 | [diff] [blame] | 122 | if (!q) |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 123 | return -1; |
yongchun.li | 0ee6e37 | 2021-08-20 04:26:04 -0700 | [diff] [blame] | 124 | fullness = q->wi - q->ri; |
| 125 | if (fullness < 0) fullness += q->max_len; |
| 126 | if (fullness == 0) |
| 127 | return -1; //empty |
sheng.liu | 75a36d1 | 2023-07-28 07:54:03 +0000 | [diff] [blame] | 128 | |
| 129 | *p_item = (void *)atomic_load(&q->items[q->ri]); |
| 130 | q->items[q->ri] = 0; |
| 131 | |
| 132 | if (q->ri >= q->max_len - 1) |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 133 | q->ri = 0; |
| 134 | else |
| 135 | q->ri++; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | int queue_size(void *queue) |
| 140 | { |
| 141 | struct queue *q = queue; |
yongchun.li | 0ee6e37 | 2021-08-20 04:26:04 -0700 | [diff] [blame] | 142 | int fullness; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 143 | |
| 144 | if (!q) |
| 145 | return -1; |
yongchun.li | 0ee6e37 | 2021-08-20 04:26:04 -0700 | [diff] [blame] | 146 | fullness = q->wi - q->ri; |
| 147 | if (fullness < 0) fullness += q->max_len; |
| 148 | return fullness; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 149 | } |