CObject
CCThreadedQueue.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Brendan Bruner
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * bbruner@ualberta.ca
17  * March 22, 2017
18  */
22 #ifndef UTIL_CCTHREADEDQUEUE_H_
23 #define UTIL_CCTHREADEDQUEUE_H_
24 
25 #include <Class.h>
26 #include <CError.h>
27 #include <CIQueue.h>
28 #include <COS.h>
29 
30 #ifdef __unix__
31 #include <pthread.h>
32 #else
33 #error "Unsupported operating system. Do not use CCThreadedQueue.h/c"
34 #endif
35 
36 
37 /************************************************************************/
38 /* Error codes. */
39 /************************************************************************/
52 typedef enum
53 {
54  /* Enum values must have one to one mapping with values of
55  * CIQueueError. Add error codes with increasing values, but
56  * do not change existing values.
57  */
63 
64 
65 /************************************************************************/
66 /* Declare class and vtable. */
67 /************************************************************************/
105 {
106  /* Super class must always be first member */
107  /* of a class' struct. */
108  struct CObject cobject;
109 
110  struct
111  {
112  struct CIQueue* backbone;
113 
114  /* Three OS objects are needed for thread safety.
115  * -> mutex
116  * Synchronize access to all backbone queue functions.
117  * -> insertBlock
118  * Provides a place for calling threads to block when they
119  * insert data, but the backbone is full.
120  * -> removeBlock
121  * Provides a place for calling threads to block when they
122  * read/remove data, but the backbone is empty.
123  */
124 #ifdef __unix__
125  pthread_cond_t insertCondition;
126  pthread_cond_t removeCondition;
127  pthread_mutex_t mutex;
128 #endif
129 
130  } _;
131 };
132 
139 {
140  /* Space for a copy of the super class' virtual table must */
141  /* be the first member of a class virtual table declaration. */
142  struct CObject_VTable cobject_override;
143 
144  /* Since we are overriding the destructor, we need to keep */
145  /* keep a reference to the super class' implementation of */
146  /* the destructor. */
147  const struct CObject_VTable* cobject;
148 };
149 
157 
158 
159 /************************************************************************/
160 /* Constructor */
161 /************************************************************************/
176 CError CCThreadedQueue( struct CCThreadedQueue* self, struct CIQueue* backbone );
177 
178 
179 /************************************************************************/
180 /* Class methods. */
181 /************************************************************************/
195 CCTQueueError CCThreadedQueue_Insert( struct CCThreadedQueue* self, const void* element, COS_Timemsec blockTime );
196 
212 CCTQueueError CCThreadedQueue_Remove( struct CCThreadedQueue* self, void* element, COS_Timemsec blockTime );
213 
229 CCTQueueError CCThreadedQueue_Peek( struct CCThreadedQueue* self, void* element, COS_Timemsec blockTime );
230 
241 size_t CCThreadedQueue_Size( struct CCThreadedQueue* self );
242 
252 size_t CCThreadedQueue_MaxSize( struct CCThreadedQueue* self );
253 
254 /*
255  * TODO: clear method should allow user to enter a clean up function.
256  */
265 void CCThreadedQueue_Clear( struct CCThreadedQueue* self );
266 
267 
268 #endif /* UTIL_CCTHREADEDQUEUE_H_ */
Thread safe wrapper for queues derived from the CIQueue interface.
Definition: CCThreadedQueue.h:104
Definition: CCThreadedQueue.h:60
CCTQueueError CCThreadedQueue_Remove(struct CCThreadedQueue *self, void *element, COS_Timemsec blockTime)
Definition: CCThreadedQueue.c:209
size_t CCThreadedQueue_MaxSize(struct CCThreadedQueue *self)
Definition: CCThreadedQueue.c:248
CCTQueueError
Definition: CCThreadedQueue.h:52
void CCThreadedQueue_Clear(struct CCThreadedQueue *self)
Definition: CCThreadedQueue.c:259
Base class.
Definition: Class.h:283
Queue data structure interface.
Definition: CIQueue.h:70
Virtual table for struct CCThreadedQueue.
Definition: CCThreadedQueue.h:138
CError CCThreadedQueue(struct CCThreadedQueue *self, struct CIQueue *backbone)
Definition: CCThreadedQueue.c:311
Definition: CCThreadedQueue.h:61
Definition: CCThreadedQueue.h:59
CObject&#39;s virtual table declaration.
Definition: Class.h:306
Definition: CCThreadedQueue.h:58
CCTQueueError CCThreadedQueue_Peek(struct CCThreadedQueue *self, void *element, COS_Timemsec blockTime)
Definition: CCThreadedQueue.c:223
const struct CCThreadedQueue_VTable * CCThreadedQueue_VTable_Key()
CCTQueueError CCThreadedQueue_Insert(struct CCThreadedQueue *self, const void *element, COS_Timemsec blockTime)
Definition: CCThreadedQueue.c:195