blob: 12e8e7527407090f00f4abac591662e8aa5c6e02 [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>
Sharvil Nanavatic11b4072014-05-02 23:55:09 -070022
23struct fixed_queue_t;
24typedef struct fixed_queue_t fixed_queue_t;
25
26typedef void (*fixed_queue_free_cb)(void *data);
27
Sharvil Nanavatic5856ba2014-06-23 12:25:40 -070028// Creates a new fixed queue with the given |capacity|. If more elements than
29// |capacity| are added to the queue, the caller is blocked until space is
30// made available in the queue. Returns NULL on failure. The caller must free
31// the returned queue with |fixed_queue_free|.
Sharvil Nanavatic11b4072014-05-02 23:55:09 -070032fixed_queue_t *fixed_queue_new(size_t capacity);
33
34// Freeing a queue that is currently in use (i.e. has waiters
Sharvil Nanavatic5856ba2014-06-23 12:25:40 -070035// blocked on it) results in undefined behaviour.
Sharvil Nanavatic11b4072014-05-02 23:55:09 -070036void fixed_queue_free(fixed_queue_t *queue, fixed_queue_free_cb free_cb);
37
Zach Johnson93a1c802014-07-30 13:40:09 -070038// Returns a value indicating whether the given |queue| is empty. |queue| may
39// not be NULL.
40bool fixed_queue_is_empty(fixed_queue_t *queue);
41
Chris Mantonc446cbe2014-08-05 11:07:23 -070042// Returns the maximum number of elements this queue may hold. |queue| may
43// not be NULL.
44size_t fixed_queue_capacity(fixed_queue_t *queue);
45
Sharvil Nanavatic5856ba2014-06-23 12:25:40 -070046// Enqueues the given |data| into the |queue|. The caller will be blocked
47// if nore more space is available in the queue. Neither |queue| nor |data|
48// may be NULL.
Sharvil Nanavatic11b4072014-05-02 23:55:09 -070049void fixed_queue_enqueue(fixed_queue_t *queue, void *data);
Sharvil Nanavatic5856ba2014-06-23 12:25:40 -070050
51// Dequeues the next element from |queue|. If the queue is currently empty,
52// this function will block the caller until an item is enqueued. This
53// function will never return NULL. |queue| may not be NULL.
Sharvil Nanavatic11b4072014-05-02 23:55:09 -070054void *fixed_queue_dequeue(fixed_queue_t *queue);
Sharvil Nanavatic5856ba2014-06-23 12:25:40 -070055
56// Tries to enqueue |data| into the |queue|. This function will never block
57// the caller. If the queue capacity would be exceeded by adding one more
58// element, this function returns false immediately. Otherwise, this function
59// returns true. Neither |queue| nor |data| may be NULL.
60bool fixed_queue_try_enqueue(fixed_queue_t *queue, void *data);
61
62// Tries to dequeue an element from |queue|. This function will never block
63// the caller. If the queue is empty, this function returns NULL immediately.
64// Otherwise, the next element in the queue is returned. |queue| may not be
65// NULL.
66void *fixed_queue_try_dequeue(fixed_queue_t *queue);
Sharvil Nanavatiab606b52014-07-04 16:33:37 -070067
68// This function returns a valid file descriptor. Callers may perform one
69// operation on the fd: select(2). If |select| indicates that the file
70// descriptor is readable, the caller may call |fixed_queue_enqueue| without
71// blocking. The caller must not close the returned file descriptor. |queue|
72// may not be NULL.
73int fixed_queue_get_enqueue_fd(const fixed_queue_t *queue);
74
75// This function returns a valid file descriptor. Callers may perform one
76// operation on the fd: select(2). If |select| indicates that the file
77// descriptor is readable, the caller may call |fixed_queue_dequeue| without
78// blocking. The caller must not close the returned file descriptor. |queue|
79// may not be NULL.
80int fixed_queue_get_dequeue_fd(const fixed_queue_t *queue);