1/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3#ident "$Id$"
4/*======
5This file is part of PerconaFT.
6
7
8Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
9
10 PerconaFT is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License, version 2,
12 as published by the Free Software Foundation.
13
14 PerconaFT is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with PerconaFT. If not, see <http://www.gnu.org/licenses/>.
21
22----------------------------------------
23
24 PerconaFT is free software: you can redistribute it and/or modify
25 it under the terms of the GNU Affero General Public License, version 3,
26 as published by the Free Software Foundation.
27
28 PerconaFT is distributed in the hope that it will be useful,
29 but WITHOUT ANY WARRANTY; without even the implied warranty of
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 GNU Affero General Public License for more details.
32
33 You should have received a copy of the GNU Affero General Public License
34 along with PerconaFT. If not, see <http://www.gnu.org/licenses/>.
35======= */
36
37#ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
38
39#pragma once
40
41// The abstraction:
42//
43// queue.h implements a queue suitable for a producer-consumer relationship between two pthreads.
44// The enqueue/dequeue operation is fairly heavyweight (involving pthread condition variables) so it may be useful
45// to enqueue large chunks rather than small chunks.
46// It probably won't work right to have two consumer threads.
47//
48// Every item inserted into the queue has a weight. If the weight
49// gets too big, then the queue blocks on trying to insert more items.
50// The weight can be used to limit the total number of items in the
51// queue (weight of each item=1) or the total memory consumed by queue
52// items (weight of each item is its size). Or the weight's could all be
53// zero for an unlimited queue.
54
55typedef struct queue *QUEUE;
56
57int toku_queue_create (QUEUE *q, uint64_t weight_limit);
58// Effect: Create a queue with a given weight limit. The queue is initially empty.
59
60int toku_queue_enq (QUEUE q, void *item, uint64_t weight, uint64_t *total_weight_after_enq);
61// Effect: Insert ITEM of weight WEIGHT into queue. If the resulting contents weight too much then block (don't return) until the total weight is low enough.
62// If total_weight_after_enq!=NULL then return the current weight of the items in the queue (after finishing blocking on overweight, and after enqueueing the item).
63// If successful return 0.
64// If an error occurs, return the error number, and the state of the queue is undefined. The item may have been enqueued or not, and in fact the queue may be badly corrupted if the condition variables go awry. If it's just a matter of out-of-memory, then the queue is probably OK.
65// Requires: There is only a single consumer. (We wake up the consumer using a pthread_cond_signal (which is suitable only for single consumers.)
66
67int toku_queue_eof (QUEUE q);
68// Effect: Inform the queue that no more values will be inserted. After all the values that have been inserted are dequeued, further dequeue operations will return EOF.
69// Returns 0 on success. On failure, things are pretty bad (likely to be some sort of mutex failure).
70
71int toku_queue_deq (QUEUE q, void **item, uint64_t *weight, uint64_t *total_weight_after_deq);
72// Effect: Wait until the queue becomes nonempty. Then dequeue and return the oldest item. The item and its weight are returned in *ITEM.
73// If weight!=NULL then return the item's weight in *weight.
74// If total_weight_after_deq!=NULL then return the current weight of the items in the queue (after dequeuing the item).
75// Return 0 if an item is returned.
76// Return EOF is we no more items will be returned.
77// Usage note: The queue should be destroyed only after any consumers will no longer look at it (for example, they saw EOF).
78
79int toku_queue_destroy (QUEUE q);
80// Effect: Destroy the queue.
81// Requires: The queue must be empty and no consumer should try to dequeue after this (one way to do this is to make sure the consumer saw EOF).
82// Returns 0 on success. If the queue is not empty, returns EINVAL. Other errors are likely to be bad (some sort of mutex or condvar failure).
83
84