Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members

FXSemaQueue.h
1 /********************************************************************************
2 * *
3 * S e m a p h o r e Q u e u e *
4 * *
5 *********************************************************************************
6 * Copyright (C) 2006,2024 by Jeroen van der Zijp. All Rights Reserved. *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Lesser General Public License as published by *
10 * the Free Software Foundation; either version 3 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public License *
19 * along with this program. If not, see <http://www.gnu.org/licenses/> *
20 ********************************************************************************/
21 #ifndef FXSEMAQUEUE_H
22 #define FXSEMAQUEUE_H
23 
24 namespace FX {
25 
26 
44 class FXAPI FXSemaQueue {
45 private:
46  FXPtrQueue queue; // Queue
47  FXSemaphore free; // Free cells
48  FXSemaphore used; // Used cells
49 private:
50  FXSemaQueue(const FXSemaQueue&);
51  FXSemaQueue &operator=(const FXSemaQueue&);
52 public:
53 
55  FXSemaQueue(FXival sz=32);
56 
58  FXival getSize() const { return queue.getSize(); }
59 
61  FXbool push(FXptr ptr);
62 
64  FXbool trypush(FXptr obj);
65 
68  FXbool trypush(FXptr obj,FXTime nsec);
69 
71  FXbool pop(FXptr& ptr);
72 
74  FXbool trypop(FXptr& obj);
75 
78  FXbool trypop(FXptr& obj,FXTime nsec);
79 
81  FXbool pop();
82 
84  ~FXSemaQueue();
85  };
86 
87 
88 // Specialize to pointers to TYPE
89 template<typename TYPE>
90 class FXSemaQueueOf : public FXSemaQueue {
91 public:
92 
94  FXSemaQueueOf(FXival sz=32):FXSemaQueue(sz){}
95 
97  FXbool push(TYPE* obj){ return FXSemaQueue::push((FXptr)obj); }
98 
100  FXbool trypush(TYPE* obj){ return FXSemaQueue::trypush((FXptr)obj); }
101 
103  FXbool trypush(TYPE* obj,FXTime nsec){ return FXSemaQueue::trypush((FXptr)obj,nsec); }
104 
106  FXbool pop(TYPE*& obj){ return FXSemaQueue::pop((FXptr&)obj); }
107 
109  FXbool trypop(TYPE*& obj){ return FXSemaQueue::trypop((FXptr&)obj); }
110 
112  FXbool trypop(TYPE*& obj,FXTime nsec){ return FXSemaQueue::trypop((FXptr&)obj,nsec); }
113  };
114 
115 
116 }
117 
118 #endif
FXbool push(FXptr ptr)
Add item to queue, return true if success.
Queue of void pointers.
Definition: FXPtrQueue.h:28
FXbool trypush(TYPE *obj, FXTime nsec)
Try push object into queue, return true if success.
Definition: FXSemaQueue.h:103
A semaphore allows for protection of a resource that can be accessed by a fixed number of simultaneou...
Definition: FXSemaphore.h:38
Semaphore protected queue for a single producer and a single consumer thread.
Definition: FXSemaQueue.h:44
FXSemaQueueOf(FXival sz=32)
Create initially empty queue of given size sz.
Definition: FXSemaQueue.h:94
FXival getSize() const
Return size.
Definition: FXSemaQueue.h:58
FXbool pop(TYPE *&obj)
Drop item from queue, return true if success.
Definition: FXSemaQueue.h:106
Definition: FXSemaQueue.h:90
FXbool trypush(FXptr obj)
Try push object into queue.
Definition: FX4Splitter.h:28
FXbool trypop(FXptr &obj)
Try pop object from queue.
FXbool pop()
Drop item from queue, return true if success.
FXival getSize() const
Return size.
Definition: FXPtrQueue.h:48
FXbool trypop(TYPE *&obj)
Try pop object from queue, return true if success.
Definition: FXSemaQueue.h:109
FXbool push(TYPE *obj)
Add item to queue, return true if success.
Definition: FXSemaQueue.h:97
FXbool trypush(TYPE *obj)
Try push object into queue, return true if success.
Definition: FXSemaQueue.h:100
FXbool trypop(TYPE *&obj, FXTime nsec)
Try pop object from queue, return true if success.
Definition: FXSemaQueue.h:112

Copyright © 1997-2022 Jeroen van der Zijp