1 #pragma once
2 
3 /** @file the_Foundation/queue.h  Thread-safe queue of objects.
4 
5 Queue is derived from ObjectList, so it keeps a reference to each object in the queue.
6 When objects are taken from the queue the reference is passed to the caller, so they are
7 responsible for releasing taken objects.
8 
9 @authors Copyright (c) 2017 Jaakko Keränen <jaakko.keranen@iki.fi>
10 
11 @par License
12 
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are met:
15 
16 1. Redistributions of source code must retain the above copyright notice, this
17    list of conditions and the following disclaimer.
18 2. Redistributions in binary form must reproduce the above copyright notice,
19    this list of conditions and the following disclaimer in the documentation
20    and/or other materials provided with the distribution.
21 
22 <small>THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
26 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</small>
32 */
33 
34 #include "defs.h"
35 #include "mutex.h"
36 #include "objectlist.h"
37 #include "stdthreads.h"
38 
39 iBeginPublic
40 
41 iDeclareClass(Queue)
42 
43 iDeclareObjectConstruction(Queue)
44 
45 struct Impl_Queue {
46     iObjectList items;
47     iMutex mutex;
48     iCondition cond;
49 };
50 
51 typedef iAnyObject iQueueItem;
52 
53 void        init_Queue          (iQueue *);
54 void        deinit_Queue        (iQueue *);
55 
56 void        put_Queue           (iQueue *, iQueueItem *item);
57 
58 iQueueItem *take_Queue          (iQueue *);
59 iQueueItem *takeTimeout_Queue   (iQueue *, double timeoutSeconds);
60 iQueueItem *tryTake_Queue       (iQueue *);
61 void        waitForItems_Queue  (iQueue *);
62 
63 size_t      size_Queue          (const iQueue *d);
64 
isEmpty_Queue(const iQueue * d)65 iLocalDef iBool isEmpty_Queue(const iQueue *d) {
66     return size_Queue(d) == 0;
67 }
68 
69 iEndPublic
70