Sharvil Nanavati | c5856ba | 2014-06-23 12:25:40 -0700 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
| 3 | * Copyright (C) 2014 Google, Inc. |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * |
| 17 | ******************************************************************************/ |
| 18 | |
Sharvil Nanavati | c11b407 | 2014-05-02 23:55:09 -0700 | [diff] [blame] | 19 | #include <assert.h> |
| 20 | #include <pthread.h> |
Pavlin Radoslavov | 1a3844f | 2015-09-25 11:21:15 -0700 | [diff] [blame] | 21 | #include <string.h> |
Sharvil Nanavati | c11b407 | 2014-05-02 23:55:09 -0700 | [diff] [blame] | 22 | |
Sharvil Nanavati | 0f9b91e | 2015-03-12 15:42:50 -0700 | [diff] [blame] | 23 | #include "osi/include/allocator.h" |
| 24 | #include "osi/include/fixed_queue.h" |
| 25 | #include "osi/include/list.h" |
| 26 | #include "osi/include/osi.h" |
| 27 | #include "osi/include/semaphore.h" |
| 28 | #include "osi/include/reactor.h" |
Sharvil Nanavati | c11b407 | 2014-05-02 23:55:09 -0700 | [diff] [blame] | 29 | |
Etan Cohen | 3e59b5b | 2015-03-31 17:15:53 -0700 | [diff] [blame] | 30 | typedef struct fixed_queue_t { |
Sharvil Nanavati | c11b407 | 2014-05-02 23:55:09 -0700 | [diff] [blame] | 31 | list_t *list; |
| 32 | semaphore_t *enqueue_sem; |
| 33 | semaphore_t *dequeue_sem; |
| 34 | pthread_mutex_t lock; |
| 35 | size_t capacity; |
Zach Johnson | bd522a4 | 2014-08-15 16:44:46 -0700 | [diff] [blame] | 36 | |
| 37 | reactor_object_t *dequeue_object; |
| 38 | fixed_queue_cb dequeue_ready; |
| 39 | void *dequeue_context; |
Sharvil Nanavati | c11b407 | 2014-05-02 23:55:09 -0700 | [diff] [blame] | 40 | } fixed_queue_t; |
| 41 | |
Zach Johnson | bd522a4 | 2014-08-15 16:44:46 -0700 | [diff] [blame] | 42 | static void internal_dequeue_ready(void *context); |
Sharvil Nanavati | c11b407 | 2014-05-02 23:55:09 -0700 | [diff] [blame] | 43 | |
| 44 | fixed_queue_t *fixed_queue_new(size_t capacity) { |
Zach Johnson | 384f8a9 | 2014-08-25 23:22:24 -0700 | [diff] [blame] | 45 | fixed_queue_t *ret = osi_calloc(sizeof(fixed_queue_t)); |
Sharvil Nanavati | c11b407 | 2014-05-02 23:55:09 -0700 | [diff] [blame] | 46 | |
Zach Johnson | 384f8a9 | 2014-08-25 23:22:24 -0700 | [diff] [blame] | 47 | pthread_mutex_init(&ret->lock, NULL); |
| 48 | ret->capacity = capacity; |
| 49 | |
Sharvil Nanavati | c11b407 | 2014-05-02 23:55:09 -0700 | [diff] [blame] | 50 | ret->list = list_new(NULL); |
| 51 | if (!ret->list) |
| 52 | goto error; |
| 53 | |
| 54 | ret->enqueue_sem = semaphore_new(capacity); |
| 55 | if (!ret->enqueue_sem) |
| 56 | goto error; |
| 57 | |
| 58 | ret->dequeue_sem = semaphore_new(0); |
| 59 | if (!ret->dequeue_sem) |
| 60 | goto error; |
| 61 | |
Sharvil Nanavati | c11b407 | 2014-05-02 23:55:09 -0700 | [diff] [blame] | 62 | return ret; |
| 63 | |
Pavlin Radoslavov | 1a3844f | 2015-09-25 11:21:15 -0700 | [diff] [blame] | 64 | error: |
Zach Johnson | 384f8a9 | 2014-08-25 23:22:24 -0700 | [diff] [blame] | 65 | fixed_queue_free(ret, NULL); |
Sharvil Nanavati | c11b407 | 2014-05-02 23:55:09 -0700 | [diff] [blame] | 66 | return NULL; |
| 67 | } |
| 68 | |
| 69 | void fixed_queue_free(fixed_queue_t *queue, fixed_queue_free_cb free_cb) { |
| 70 | if (!queue) |
| 71 | return; |
| 72 | |
Zach Johnson | bd522a4 | 2014-08-15 16:44:46 -0700 | [diff] [blame] | 73 | fixed_queue_unregister_dequeue(queue); |
| 74 | |
Sharvil Nanavati | c11b407 | 2014-05-02 23:55:09 -0700 | [diff] [blame] | 75 | if (free_cb) |
| 76 | for (const list_node_t *node = list_begin(queue->list); node != list_end(queue->list); node = list_next(node)) |
| 77 | free_cb(list_node(node)); |
| 78 | |
| 79 | list_free(queue->list); |
| 80 | semaphore_free(queue->enqueue_sem); |
| 81 | semaphore_free(queue->dequeue_sem); |
| 82 | pthread_mutex_destroy(&queue->lock); |
Zach Johnson | 384f8a9 | 2014-08-25 23:22:24 -0700 | [diff] [blame] | 83 | osi_free(queue); |
Sharvil Nanavati | c11b407 | 2014-05-02 23:55:09 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Pavlin Radoslavov | 3335cf4 | 2016-08-19 15:32:30 -0700 | [diff] [blame^] | 86 | void fixed_queue_flush(fixed_queue_t *queue, fixed_queue_free_cb free_cb) { |
| 87 | if (!queue) |
| 88 | return; |
| 89 | |
| 90 | while (!fixed_queue_is_empty(queue)) { |
| 91 | void *data = fixed_queue_try_dequeue(queue); |
| 92 | if (free_cb != NULL) { |
| 93 | free_cb(data); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
Zach Johnson | 93a1c80 | 2014-07-30 13:40:09 -0700 | [diff] [blame] | 98 | bool fixed_queue_is_empty(fixed_queue_t *queue) { |
Pavlin Radoslavov | 577862e | 2015-10-07 18:07:48 -0700 | [diff] [blame] | 99 | if (queue == NULL) |
| 100 | return true; |
Zach Johnson | 93a1c80 | 2014-07-30 13:40:09 -0700 | [diff] [blame] | 101 | |
| 102 | pthread_mutex_lock(&queue->lock); |
| 103 | bool is_empty = list_is_empty(queue->list); |
| 104 | pthread_mutex_unlock(&queue->lock); |
| 105 | |
| 106 | return is_empty; |
| 107 | } |
| 108 | |
Pavlin Radoslavov | 1a3844f | 2015-09-25 11:21:15 -0700 | [diff] [blame] | 109 | size_t fixed_queue_length(fixed_queue_t *queue) { |
Pavlin Radoslavov | 577862e | 2015-10-07 18:07:48 -0700 | [diff] [blame] | 110 | if (queue == NULL) |
| 111 | return 0; |
Pavlin Radoslavov | 1a3844f | 2015-09-25 11:21:15 -0700 | [diff] [blame] | 112 | |
| 113 | pthread_mutex_lock(&queue->lock); |
| 114 | size_t length = list_length(queue->list); |
| 115 | pthread_mutex_unlock(&queue->lock); |
| 116 | |
| 117 | return length; |
| 118 | } |
| 119 | |
Chris Manton | c446cbe | 2014-08-05 11:07:23 -0700 | [diff] [blame] | 120 | size_t fixed_queue_capacity(fixed_queue_t *queue) { |
| 121 | assert(queue != NULL); |
| 122 | |
| 123 | return queue->capacity; |
Sharvil Nanavati | c11b407 | 2014-05-02 23:55:09 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void fixed_queue_enqueue(fixed_queue_t *queue, void *data) { |
| 127 | assert(queue != NULL); |
| 128 | assert(data != NULL); |
| 129 | |
| 130 | semaphore_wait(queue->enqueue_sem); |
| 131 | |
| 132 | pthread_mutex_lock(&queue->lock); |
| 133 | list_append(queue->list, data); |
| 134 | pthread_mutex_unlock(&queue->lock); |
| 135 | |
| 136 | semaphore_post(queue->dequeue_sem); |
| 137 | } |
| 138 | |
| 139 | void *fixed_queue_dequeue(fixed_queue_t *queue) { |
| 140 | assert(queue != NULL); |
| 141 | |
Sharvil Nanavati | c11b407 | 2014-05-02 23:55:09 -0700 | [diff] [blame] | 142 | semaphore_wait(queue->dequeue_sem); |
| 143 | |
| 144 | pthread_mutex_lock(&queue->lock); |
Sharvil Nanavati | c5856ba | 2014-06-23 12:25:40 -0700 | [diff] [blame] | 145 | void *ret = list_front(queue->list); |
| 146 | list_remove(queue->list, ret); |
| 147 | pthread_mutex_unlock(&queue->lock); |
| 148 | |
| 149 | semaphore_post(queue->enqueue_sem); |
| 150 | |
| 151 | return ret; |
| 152 | } |
| 153 | |
| 154 | bool fixed_queue_try_enqueue(fixed_queue_t *queue, void *data) { |
| 155 | assert(queue != NULL); |
| 156 | assert(data != NULL); |
| 157 | |
| 158 | if (!semaphore_try_wait(queue->enqueue_sem)) |
| 159 | return false; |
| 160 | |
| 161 | pthread_mutex_lock(&queue->lock); |
| 162 | list_append(queue->list, data); |
| 163 | pthread_mutex_unlock(&queue->lock); |
| 164 | |
| 165 | semaphore_post(queue->dequeue_sem); |
| 166 | return true; |
| 167 | } |
| 168 | |
| 169 | void *fixed_queue_try_dequeue(fixed_queue_t *queue) { |
Pavlin Radoslavov | 577862e | 2015-10-07 18:07:48 -0700 | [diff] [blame] | 170 | if (queue == NULL) |
| 171 | return NULL; |
Sharvil Nanavati | c5856ba | 2014-06-23 12:25:40 -0700 | [diff] [blame] | 172 | |
| 173 | if (!semaphore_try_wait(queue->dequeue_sem)) |
| 174 | return NULL; |
| 175 | |
| 176 | pthread_mutex_lock(&queue->lock); |
| 177 | void *ret = list_front(queue->list); |
Sharvil Nanavati | c11b407 | 2014-05-02 23:55:09 -0700 | [diff] [blame] | 178 | list_remove(queue->list, ret); |
| 179 | pthread_mutex_unlock(&queue->lock); |
| 180 | |
| 181 | semaphore_post(queue->enqueue_sem); |
| 182 | |
| 183 | return ret; |
| 184 | } |
Sharvil Nanavati | ab606b5 | 2014-07-04 16:33:37 -0700 | [diff] [blame] | 185 | |
Pavlin Radoslavov | 1a3844f | 2015-09-25 11:21:15 -0700 | [diff] [blame] | 186 | void *fixed_queue_try_peek_first(fixed_queue_t *queue) { |
Pavlin Radoslavov | 577862e | 2015-10-07 18:07:48 -0700 | [diff] [blame] | 187 | if (queue == NULL) |
| 188 | return NULL; |
Zach Johnson | bd522a4 | 2014-08-15 16:44:46 -0700 | [diff] [blame] | 189 | |
| 190 | pthread_mutex_lock(&queue->lock); |
Zach Johnson | bd522a4 | 2014-08-15 16:44:46 -0700 | [diff] [blame] | 191 | void *ret = list_is_empty(queue->list) ? NULL : list_front(queue->list); |
| 192 | pthread_mutex_unlock(&queue->lock); |
| 193 | |
| 194 | return ret; |
| 195 | } |
| 196 | |
Pavlin Radoslavov | 1a3844f | 2015-09-25 11:21:15 -0700 | [diff] [blame] | 197 | void *fixed_queue_try_peek_last(fixed_queue_t *queue) { |
Pavlin Radoslavov | 577862e | 2015-10-07 18:07:48 -0700 | [diff] [blame] | 198 | if (queue == NULL) |
| 199 | return NULL; |
Pavlin Radoslavov | 1a3844f | 2015-09-25 11:21:15 -0700 | [diff] [blame] | 200 | |
| 201 | pthread_mutex_lock(&queue->lock); |
| 202 | void *ret = list_is_empty(queue->list) ? NULL : list_back(queue->list); |
| 203 | pthread_mutex_unlock(&queue->lock); |
| 204 | |
| 205 | return ret; |
| 206 | } |
| 207 | |
| 208 | void *fixed_queue_try_remove_from_queue(fixed_queue_t *queue, void *data) { |
Pavlin Radoslavov | 577862e | 2015-10-07 18:07:48 -0700 | [diff] [blame] | 209 | if (queue == NULL) |
| 210 | return NULL; |
Pavlin Radoslavov | 1a3844f | 2015-09-25 11:21:15 -0700 | [diff] [blame] | 211 | |
Pavlin Radoslavov | 153bdfb | 2015-11-13 18:36:56 -0800 | [diff] [blame] | 212 | bool removed = false; |
Pavlin Radoslavov | 1a3844f | 2015-09-25 11:21:15 -0700 | [diff] [blame] | 213 | pthread_mutex_lock(&queue->lock); |
Pavlin Radoslavov | 153bdfb | 2015-11-13 18:36:56 -0800 | [diff] [blame] | 214 | if (list_contains(queue->list, data) && |
| 215 | semaphore_try_wait(queue->dequeue_sem)) { |
| 216 | removed = list_remove(queue->list, data); |
| 217 | assert(removed); |
| 218 | } |
Pavlin Radoslavov | 1a3844f | 2015-09-25 11:21:15 -0700 | [diff] [blame] | 219 | pthread_mutex_unlock(&queue->lock); |
| 220 | |
Pavlin Radoslavov | 153bdfb | 2015-11-13 18:36:56 -0800 | [diff] [blame] | 221 | if (removed) { |
| 222 | semaphore_post(queue->enqueue_sem); |
Pavlin Radoslavov | 1a3844f | 2015-09-25 11:21:15 -0700 | [diff] [blame] | 223 | return data; |
Pavlin Radoslavov | 153bdfb | 2015-11-13 18:36:56 -0800 | [diff] [blame] | 224 | } |
Pavlin Radoslavov | 1a3844f | 2015-09-25 11:21:15 -0700 | [diff] [blame] | 225 | return NULL; |
| 226 | } |
| 227 | |
| 228 | list_t *fixed_queue_get_list(fixed_queue_t *queue) { |
| 229 | assert(queue != NULL); |
| 230 | |
| 231 | // NOTE: This function is not thread safe, and there is no point for |
Pavlin Radoslavov | 577862e | 2015-10-07 18:07:48 -0700 | [diff] [blame] | 232 | // calling pthread_mutex_lock() / pthread_mutex_unlock() |
Pavlin Radoslavov | 1a3844f | 2015-09-25 11:21:15 -0700 | [diff] [blame] | 233 | return queue->list; |
| 234 | } |
| 235 | |
| 236 | |
Sharvil Nanavati | ab606b5 | 2014-07-04 16:33:37 -0700 | [diff] [blame] | 237 | int fixed_queue_get_dequeue_fd(const fixed_queue_t *queue) { |
| 238 | assert(queue != NULL); |
| 239 | return semaphore_get_fd(queue->dequeue_sem); |
| 240 | } |
| 241 | |
| 242 | int fixed_queue_get_enqueue_fd(const fixed_queue_t *queue) { |
| 243 | assert(queue != NULL); |
| 244 | return semaphore_get_fd(queue->enqueue_sem); |
| 245 | } |
Zach Johnson | bd522a4 | 2014-08-15 16:44:46 -0700 | [diff] [blame] | 246 | |
| 247 | void fixed_queue_register_dequeue(fixed_queue_t *queue, reactor_t *reactor, fixed_queue_cb ready_cb, void *context) { |
| 248 | assert(queue != NULL); |
| 249 | assert(reactor != NULL); |
| 250 | assert(ready_cb != NULL); |
| 251 | |
| 252 | // Make sure we're not already registered |
| 253 | fixed_queue_unregister_dequeue(queue); |
| 254 | |
| 255 | queue->dequeue_ready = ready_cb; |
| 256 | queue->dequeue_context = context; |
| 257 | queue->dequeue_object = reactor_register( |
| 258 | reactor, |
| 259 | fixed_queue_get_dequeue_fd(queue), |
| 260 | queue, |
| 261 | internal_dequeue_ready, |
| 262 | NULL |
| 263 | ); |
| 264 | } |
| 265 | |
| 266 | void fixed_queue_unregister_dequeue(fixed_queue_t *queue) { |
| 267 | assert(queue != NULL); |
| 268 | |
| 269 | if (queue->dequeue_object) { |
| 270 | reactor_unregister(queue->dequeue_object); |
| 271 | queue->dequeue_object = NULL; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | static void internal_dequeue_ready(void *context) { |
| 276 | assert(context != NULL); |
| 277 | |
| 278 | fixed_queue_t *queue = context; |
| 279 | queue->dequeue_ready(queue, queue->dequeue_context); |
| 280 | } |