blob: 6902d6b7b5ded16024f0c647425959842d7620ac [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
Sharvil Nanavatic11b4072014-05-02 23:55:09 -070026struct fixed_queue_t;
27typedef struct fixed_queue_t fixed_queue_t;
Zach Johnsonbd522a42014-08-15 16:44:46 -070028typedef struct reactor_t reactor_t;
Sharvil Nanavatic11b4072014-05-02 23:55:09 -070029
30typedef void (*fixed_queue_free_cb)(void *data);
Zach Johnsonbd522a42014-08-15 16:44:46 -070031typedef void (*fixed_queue_cb)(fixed_queue_t *queue, void *context);
Sharvil Nanavatic11b4072014-05-02 23:55:09 -070032
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -070033// Initializes a fixed |queue| with the given |capacity|. If more elements than
34// |capacity| are added to the queue, the caller is blocked until space is
35// made available in the queue. Returns false on failure.
36bool fixed_queue_init(fixed_queue_t *queue, size_t capacity);
37
Sharvil Nanavatic5856ba2014-06-23 12:25:40 -070038// Creates a new fixed queue with the given |capacity|. If more elements than
39// |capacity| are added to the queue, the caller is blocked until space is
40// made available in the queue. Returns NULL on failure. The caller must free
41// the returned queue with |fixed_queue_free|.
Sharvil Nanavatic11b4072014-05-02 23:55:09 -070042fixed_queue_t *fixed_queue_new(size_t capacity);
43
44// Freeing a queue that is currently in use (i.e. has waiters
Sharvil Nanavatic5856ba2014-06-23 12:25:40 -070045// blocked on it) results in undefined behaviour.
Sharvil Nanavatic11b4072014-05-02 23:55:09 -070046void fixed_queue_free(fixed_queue_t *queue, fixed_queue_free_cb free_cb);
47
Zach Johnson93a1c802014-07-30 13:40:09 -070048// Returns a value indicating whether the given |queue| is empty. |queue| may
49// not be NULL.
50bool fixed_queue_is_empty(fixed_queue_t *queue);
51
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -070052// Returns the length of the |queue|. |queue| may not be NULL.
53size_t fixed_queue_length(fixed_queue_t *queue);
54
Chris Mantonc446cbe2014-08-05 11:07:23 -070055// Returns the maximum number of elements this queue may hold. |queue| may
56// not be NULL.
57size_t fixed_queue_capacity(fixed_queue_t *queue);
58
Sharvil Nanavatic5856ba2014-06-23 12:25:40 -070059// Enqueues the given |data| into the |queue|. The caller will be blocked
60// if nore more space is available in the queue. Neither |queue| nor |data|
61// may be NULL.
Sharvil Nanavatic11b4072014-05-02 23:55:09 -070062void fixed_queue_enqueue(fixed_queue_t *queue, void *data);
Sharvil Nanavatic5856ba2014-06-23 12:25:40 -070063
64// Dequeues the next element from |queue|. If the queue is currently empty,
65// this function will block the caller until an item is enqueued. This
66// function will never return NULL. |queue| may not be NULL.
Sharvil Nanavatic11b4072014-05-02 23:55:09 -070067void *fixed_queue_dequeue(fixed_queue_t *queue);
Sharvil Nanavatic5856ba2014-06-23 12:25:40 -070068
69// Tries to enqueue |data| into the |queue|. This function will never block
70// the caller. If the queue capacity would be exceeded by adding one more
71// element, this function returns false immediately. Otherwise, this function
72// returns true. Neither |queue| nor |data| may be NULL.
73bool fixed_queue_try_enqueue(fixed_queue_t *queue, void *data);
74
75// Tries to dequeue an element from |queue|. This function will never block
76// the caller. If the queue is empty, this function returns NULL immediately.
77// Otherwise, the next element in the queue is returned. |queue| may not be
78// NULL.
79void *fixed_queue_try_dequeue(fixed_queue_t *queue);
Sharvil Nanavatiab606b52014-07-04 16:33:37 -070080
Zach Johnsonbd522a42014-08-15 16:44:46 -070081// Returns the first element from |queue|, if present, without dequeuing it.
Pavlin Radoslavov1a3844f2015-09-25 11:21:15 -070082// This function will never block the caller. Returns NULL if there are no
83// elements in the queue. |queue| may not be NULL.
84void *fixed_queue_try_peek_first(fixed_queue_t *queue);
85
86// Returns the last element from |queue|, if present, without dequeuing it.
87// This function will never block the caller. Returns NULL if there are no
88// elements in the queue. |queue| may not be NULL.
89void *fixed_queue_try_peek_last(fixed_queue_t *queue);
90
91// Tries to remove a |data| element from the middle of the |queue|. This
92// function will never block the caller. If the |data| element is found
93// in the queue, a pointer to the removed data is returned, otherwise NULL.
94void *fixed_queue_try_remove_from_queue(fixed_queue_t *queue, void *data);
95
96// Returns the iterateable list with all entries in the |queue|. This function
97// will never block the caller. |queue| may not be NULL.
98//
99// NOTE: The return result of this function is not thread safe: the list could
100// be modified by another thread, and the result would be unpredictable.
101// Hence, the usage of this function is discouraged.
102list_t *fixed_queue_get_list(fixed_queue_t *queue);
Zach Johnsonbd522a42014-08-15 16:44:46 -0700103
Sharvil Nanavatiab606b52014-07-04 16:33:37 -0700104// This function returns a valid file descriptor. Callers may perform one
105// operation on the fd: select(2). If |select| indicates that the file
106// descriptor is readable, the caller may call |fixed_queue_enqueue| without
107// blocking. The caller must not close the returned file descriptor. |queue|
108// may not be NULL.
109int fixed_queue_get_enqueue_fd(const fixed_queue_t *queue);
110
111// This function returns a valid file descriptor. Callers may perform one
112// operation on the fd: select(2). If |select| indicates that the file
113// descriptor is readable, the caller may call |fixed_queue_dequeue| without
114// blocking. The caller must not close the returned file descriptor. |queue|
115// may not be NULL.
116int fixed_queue_get_dequeue_fd(const fixed_queue_t *queue);
Zach Johnsonbd522a42014-08-15 16:44:46 -0700117
118// Registers |queue| with |reactor| for dequeue operations. When there is an element
119// in the queue, ready_cb will be called. The |context| parameter is passed, untouched,
120// to the callback routine. Neither |queue|, nor |reactor|, nor |read_cb| may be NULL.
121// |context| may be NULL.
122void fixed_queue_register_dequeue(fixed_queue_t *queue, reactor_t *reactor, fixed_queue_cb ready_cb, void *context);
123
124// Unregisters the dequeue ready callback for |queue| from whichever reactor
125// it is registered with, if any. This function is idempotent.
126void fixed_queue_unregister_dequeue(fixed_queue_t *queue);