blob: 54f0093bb70c829a74b82d1c1e026713af8b4f87 [file] [log] [blame]
Sharvil Nanavatic5856ba2014-06-23 12:25:40 -07001/******************************************************************************
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 Nanavatic11b4072014-05-02 23:55:09 -070019#pragma once
20
Sharvil Nanavatic5856ba2014-06-23 12:25:40 -070021#include <stdbool.h>
Marie Janssen49a86702015-07-08 11:48:57 -070022#include <stdlib.h>
Sharvil Nanavatic11b4072014-05-02 23:55:09 -070023
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -070024#include "osi/include/list.h"
25
Jakub Pawlowski713993d2016-04-21 13:16:45 -070026#ifdef __cplusplus
27extern "C" {
28#endif
29
Sharvil Nanavatic11b4072014-05-02 23:55:09 -070030struct fixed_queue_t;
31typedef struct fixed_queue_t fixed_queue_t;
Zach Johnsonbd522a42014-08-15 16:44:46 -070032typedef struct reactor_t reactor_t;
Sharvil Nanavatic11b4072014-05-02 23:55:09 -070033
34typedef void (*fixed_queue_free_cb)(void *data);
Zach Johnsonbd522a42014-08-15 16:44:46 -070035typedef void (*fixed_queue_cb)(fixed_queue_t *queue, void *context);
Sharvil Nanavatic11b4072014-05-02 23:55:09 -070036
Sharvil Nanavatic5856ba2014-06-23 12:25:40 -070037// Creates a new fixed queue with the given |capacity|. If more elements than
38// |capacity| are added to the queue, the caller is blocked until space is
39// made available in the queue. Returns NULL on failure. The caller must free
40// the returned queue with |fixed_queue_free|.
Sharvil Nanavatic11b4072014-05-02 23:55:09 -070041fixed_queue_t *fixed_queue_new(size_t capacity);
42
Pavlin Radoslavov3335cf42016-08-19 15:32:30 -070043// Frees a queue and (optionally) the enqueued elements.
44// |queue| is the queue to free. If the |free_cb| callback is not null,
45// it is called on each queue element to free it.
Sharvil Nanavatic11b4072014-05-02 23:55:09 -070046// Freeing a queue that is currently in use (i.e. has waiters
Sharvil Nanavatic5856ba2014-06-23 12:25:40 -070047// blocked on it) results in undefined behaviour.
Sharvil Nanavatic11b4072014-05-02 23:55:09 -070048void fixed_queue_free(fixed_queue_t *queue, fixed_queue_free_cb free_cb);
49
Pavlin Radoslavov3335cf42016-08-19 15:32:30 -070050// Flushes a queue and (optionally) frees the enqueued elements.
51// |queue| is the queue to flush. If the |free_cb| callback is not null,
52// it is called on each queue element to free it.
53void fixed_queue_flush(fixed_queue_t *queue, fixed_queue_free_cb free_cb);
54
Pavlin Radoslavov577862e2015-10-07 18:07:48 -070055// Returns a value indicating whether the given |queue| is empty. If |queue|
56// is NULL, the return value is true.
Zach Johnson93a1c802014-07-30 13:40:09 -070057bool fixed_queue_is_empty(fixed_queue_t *queue);
58
Pavlin Radoslavov577862e2015-10-07 18:07:48 -070059// Returns the length of the |queue|. If |queue| is NULL, the return value
60// is 0.
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -070061size_t fixed_queue_length(fixed_queue_t *queue);
62
Chris Mantonc446cbe2014-08-05 11:07:23 -070063// Returns the maximum number of elements this queue may hold. |queue| may
64// not be NULL.
65size_t fixed_queue_capacity(fixed_queue_t *queue);
66
Sharvil Nanavatic5856ba2014-06-23 12:25:40 -070067// Enqueues the given |data| into the |queue|. The caller will be blocked
Pavlin Radoslavov153bdfb2015-11-13 18:36:56 -080068// if no more space is available in the queue. Neither |queue| nor |data|
Sharvil Nanavatic5856ba2014-06-23 12:25:40 -070069// may be NULL.
Sharvil Nanavatic11b4072014-05-02 23:55:09 -070070void fixed_queue_enqueue(fixed_queue_t *queue, void *data);
Sharvil Nanavatic5856ba2014-06-23 12:25:40 -070071
72// Dequeues the next element from |queue|. If the queue is currently empty,
73// this function will block the caller until an item is enqueued. This
74// function will never return NULL. |queue| may not be NULL.
Sharvil Nanavatic11b4072014-05-02 23:55:09 -070075void *fixed_queue_dequeue(fixed_queue_t *queue);
Sharvil Nanavatic5856ba2014-06-23 12:25:40 -070076
77// Tries to enqueue |data| into the |queue|. This function will never block
78// the caller. If the queue capacity would be exceeded by adding one more
79// element, this function returns false immediately. Otherwise, this function
80// returns true. Neither |queue| nor |data| may be NULL.
81bool fixed_queue_try_enqueue(fixed_queue_t *queue, void *data);
82
83// Tries to dequeue an element from |queue|. This function will never block
Pavlin Radoslavov577862e2015-10-07 18:07:48 -070084// the caller. If the queue is empty or NULL, this function returns NULL
85// immediately. Otherwise, the next element in the queue is returned.
Sharvil Nanavatic5856ba2014-06-23 12:25:40 -070086void *fixed_queue_try_dequeue(fixed_queue_t *queue);
Sharvil Nanavatiab606b52014-07-04 16:33:37 -070087
Zach Johnsonbd522a42014-08-15 16:44:46 -070088// Returns the first element from |queue|, if present, without dequeuing it.
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -070089// This function will never block the caller. Returns NULL if there are no
Pavlin Radoslavov577862e2015-10-07 18:07:48 -070090// elements in the queue or |queue| is NULL.
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -070091void *fixed_queue_try_peek_first(fixed_queue_t *queue);
92
93// Returns the last element from |queue|, if present, without dequeuing it.
94// This function will never block the caller. Returns NULL if there are no
Pavlin Radoslavov577862e2015-10-07 18:07:48 -070095// elements in the queue or |queue| is NULL.
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -070096void *fixed_queue_try_peek_last(fixed_queue_t *queue);
97
98// Tries to remove a |data| element from the middle of the |queue|. This
Pavlin Radoslavov153bdfb2015-11-13 18:36:56 -080099// function will never block the caller. If the queue is empty or NULL, this
100// function returns NULL immediately. |data| may not be NULL. If the |data|
101// element is found in the queue, a pointer to the removed data is returned,
102// otherwise NULL.
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700103void *fixed_queue_try_remove_from_queue(fixed_queue_t *queue, void *data);
104
105// Returns the iterateable list with all entries in the |queue|. This function
106// will never block the caller. |queue| may not be NULL.
107//
108// NOTE: The return result of this function is not thread safe: the list could
109// be modified by another thread, and the result would be unpredictable.
Pavlin Radoslavov153bdfb2015-11-13 18:36:56 -0800110// TODO: The usage of this function should be refactored, and the function
111// itself should be removed.
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -0700112list_t *fixed_queue_get_list(fixed_queue_t *queue);
Zach Johnsonbd522a42014-08-15 16:44:46 -0700113
Sharvil Nanavatiab606b52014-07-04 16:33:37 -0700114// This function returns a valid file descriptor. Callers may perform one
115// operation on the fd: select(2). If |select| indicates that the file
116// descriptor is readable, the caller may call |fixed_queue_enqueue| without
117// blocking. The caller must not close the returned file descriptor. |queue|
118// may not be NULL.
119int fixed_queue_get_enqueue_fd(const fixed_queue_t *queue);
120
121// This function returns a valid file descriptor. Callers may perform one
122// operation on the fd: select(2). If |select| indicates that the file
123// descriptor is readable, the caller may call |fixed_queue_dequeue| without
124// blocking. The caller must not close the returned file descriptor. |queue|
125// may not be NULL.
126int fixed_queue_get_dequeue_fd(const fixed_queue_t *queue);
Zach Johnsonbd522a42014-08-15 16:44:46 -0700127
128// Registers |queue| with |reactor| for dequeue operations. When there is an element
129// in the queue, ready_cb will be called. The |context| parameter is passed, untouched,
130// to the callback routine. Neither |queue|, nor |reactor|, nor |read_cb| may be NULL.
131// |context| may be NULL.
132void fixed_queue_register_dequeue(fixed_queue_t *queue, reactor_t *reactor, fixed_queue_cb ready_cb, void *context);
133
134// Unregisters the dequeue ready callback for |queue| from whichever reactor
135// it is registered with, if any. This function is idempotent.
136void fixed_queue_unregister_dequeue(fixed_queue_t *queue);
Jakub Pawlowski713993d2016-04-21 13:16:45 -0700137
138#ifdef __cplusplus
139}
140#endif