51Degrees Device Detection C/C++  4.1

A device detection library that is used natively or by 51Degrees products

pool.h

1 /* *********************************************************************
2  * This Original Work is copyright of 51 Degrees Mobile Experts Limited.
3  * Copyright 2019 51 Degrees Mobile Experts Limited, 5 Charlotte Close,
4  * Caversham, Reading, Berkshire, United Kingdom RG4 7BY.
5  *
6  * This Original Work is licensed under the European Union Public Licence (EUPL)
7  * v.1.2 and is subject to its terms as set out below.
8  *
9  * If a copy of the EUPL was not distributed with this file, You can obtain
10  * one at https://opensource.org/licenses/EUPL-1.2.
11  *
12  * The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
13  * amended by the European Commission) shall be deemed incompatible for
14  * the purposes of the Work and the provisions of the compatibility
15  * clause in Article 5 of the EUPL shall not apply.
16  *
17  * If using the Work as, or as part of, a network application, by
18  * including the attribution notice(s) required under Article 5 of the EUPL
19  * in the end user terms of the application under an appropriate heading,
20  * such notice(s) shall fulfill the requirements of that article.
21  * ********************************************************************* */
22 
23 #ifndef FIFTYONE_DEGREES_POOL_H_INCLUDED
24 #define FIFTYONE_DEGREES_POOL_H_INCLUDED
25 
119 #include <stdio.h>
120 #include <errno.h>
121 #include <stdint.h>
122 #ifdef _MSC_VER
123 #include <windows.h>
124 #endif
125 #include <assert.h>
126 #include <limits.h>
127 #include "data.h"
128 #include "exceptions.h"
129 #include "memory.h"
130 #ifndef FIFTYONE_DEGREES_NO_THREADING
131 #include "threading.h"
132 #endif
133 
134 #ifdef __cplusplus
135 #define EXTERNAL extern "C"
136 #else
137 #define EXTERNAL
138 #endif
139 
141 typedef struct fiftyone_degrees_pool_item_t fiftyoneDegreesPoolItem;
142 typedef struct fiftyone_degrees_pool_t fiftyoneDegreesPool;
153 typedef void*(*fiftyoneDegreesPoolResourceCreate)(
154  fiftyoneDegreesPool *pool,
155  void *state,
156  fiftyoneDegreesException *exception);
157 
165 typedef size_t(*fiftyoneDegreesPoolResourceSize)(void *state);
166 
173  fiftyoneDegreesPool *pool,
174  void *resource);
175 
179 typedef struct fiftyone_degrees_pool_item_t {
180  void *resource;
181  uint16_t next;
184 
188 typedef union fiftyone_degrees_pool_head_t {
189  volatile long exchange;
190  struct {
191  uint16_t index;
192  uint16_t aba;
193  } values;
195 
199 typedef struct fiftyone_degrees_pool_t {
203  uint16_t count;
206 
225  fiftyoneDegreesPool *pool,
226  uint16_t concurrency,
227  void *state,
228  fiftyoneDegreesPoolResourceCreate resourceCreate,
229  fiftyoneDegreesPoolResourceFree resourceFree,
230  fiftyoneDegreesException *exception);
231 
242  fiftyoneDegreesPool *pool,
243  fiftyoneDegreesException *exception);
244 
251 
258 
264 
269 #endif
void fiftyoneDegreesPoolReset(fiftyoneDegreesPool *pool)
Resets the pool without releasing any resources.
fiftyoneDegreesPoolResourceFree resourceFree
Frees a resource.
Definition: pool.h:204
Pool item node in the stack of items.
Definition: pool.h:179
void(* fiftyoneDegreesPoolResourceFree)(fiftyoneDegreesPool *pool, void *resource)
Frees a resource previously created with fiftyoneDegreesPoolResourceCreate.
Definition: pool.h:172
uint16_t index
Index of the item in the linked list.
Definition: pool.h:191
void fiftyoneDegreesPoolFree(fiftyoneDegreesPool *pool)
Releases the items used by the pool freeing the resources used by each item by calling the resourceFr...
uint16_t count
Number of resource items that stack can hold.
Definition: pool.h:203
volatile long exchange
Number used in the compare exchange operation.
Definition: pool.h:189
Pool of resources stored as items in a stack.
Definition: pool.h:199
uint16_t aba
ABA value used to ensure proper operation.
Definition: pool.h:192
fiftyoneDegreesPoolItem * stack
Pointer to the memory used by the stack.
Definition: pool.h:200
fiftyoneDegreesPoolItem * fiftyoneDegreesPoolItemGet(fiftyoneDegreesPool *pool, fiftyoneDegreesException *exception)
Gets the next free item from the pool for exclusive use by the caller.
size_t(* fiftyoneDegreesPoolResourceSize)(void *state)
Used to determine the additional size beyond the pointer used for each resource added to the pool.
Definition: pool.h:165
The head of the stack used for pop and push operations.
Definition: pool.h:188
fiftyoneDegreesPoolHead head
Head of the stack.
Definition: pool.h:202
void fiftyoneDegreesPoolItemRelease(fiftyoneDegreesPoolItem *item)
Releases the item back to the pool it belongs ready to be reused by another operation.
void * resource
Pointer to the resource in the pool.
Definition: pool.h:180
fiftyoneDegreesPool * fiftyoneDegreesPoolInit(fiftyoneDegreesPool *pool, uint16_t concurrency, void *state, fiftyoneDegreesPoolResourceCreate resourceCreate, fiftyoneDegreesPoolResourceFree resourceFree, fiftyoneDegreesException *exception)
Initialises a pool data structure to support the number of concurrent requests that can be made to th...
void *(* fiftyoneDegreesPoolResourceCreate)(fiftyoneDegreesPool *pool, void *state, fiftyoneDegreesException *exception)
Used to create a new resource for use in the pool.
Definition: pool.h:153
Structure used to represent a 51Degrees exception and passed into methods that might generate excepti...
Definition: exceptions.h:116
uint16_t next
The next item in the stack.
Definition: pool.h:181
fiftyoneDegreesPool * pool
Reader the handle belongs to.
Definition: pool.h:182