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 2026 51 Degrees Mobile Experts Limited, Davidson House,
4 * Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU.
5 *
6 * This Original Work is licensed under the European Union Public Licence
7 * (EUPL) 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
26#ifdef FIFTYONE_DEGREES_IPI_SYMBOLS
27#include "ipiSymbols.h"
28#endif
29
125
126#include <stdio.h>
127#include <errno.h>
128#include <stdint.h>
129#ifdef _MSC_VER
130#pragma warning (push)
131#pragma warning (disable: 5105)
132#include <windows.h>
133#pragma warning (default: 5105)
134#pragma warning (pop)
135#endif
136#include <assert.h>
137#include <limits.h>
138#include "data.h"
139#include "exceptions.h"
140#include "memory.h"
141#ifndef FIFTYONE_DEGREES_NO_THREADING
142#include "threading.h"
143#endif
144#include "common.h"
145
147typedef struct fiftyone_degrees_pool_item_t fiftyoneDegreesPoolItem;
148typedef struct fiftyone_degrees_pool_t fiftyoneDegreesPool;
150
159typedef void*(*fiftyoneDegreesPoolResourceCreate)(
161 void *state,
162 fiftyoneDegreesException *exception);
163
171typedef size_t(*fiftyoneDegreesPoolResourceSize)(void *state);
172
179 fiftyoneDegreesPool *pool,
180 void *resource);
181
185typedef struct fiftyone_degrees_pool_item_t {
186 void *resource;
187 uint16_t next;
190
194typedef union fiftyone_degrees_pool_head_t {
195 volatile long exchange;
196 struct {
197 uint16_t index;
198 uint16_t aba;
199 } values;
201
205typedef struct fiftyone_degrees_pool_t {
209 uint16_t count;
211#ifndef FIFTYONE_DEGREES_NO_THREADING
212 volatile long waiting;
213 long waits;
215#ifdef _MSC_VER
216 CONDITION_VARIABLE released;
217#else
218 pthread_cond_t released;
219#endif
220#endif
222
243 uint16_t concurrency,
244 void *state,
247 fiftyoneDegreesException *exception);
248
263 fiftyoneDegreesException *exception);
264
271
278
284
288
289#endif
void fiftyoneDegreesPoolFree(fiftyoneDegreesPool *pool)
Releases the items used by the pool freeing the resources used by each item by calling the resourceFr...
fiftyoneDegreesPoolItem * fiftyoneDegreesPoolItemGet(fiftyoneDegreesPool *pool, fiftyoneDegreesException *exception)
Gets the next free item from the pool for exclusive use by the caller.
void fiftyoneDegreesPoolItemRelease(fiftyoneDegreesPoolItem *item)
Releases the item back to the pool it belongs ready to be reused by another operation.
void(* fiftyoneDegreesPoolResourceFree)(fiftyoneDegreesPool *pool, void *resource)
Frees a resource previously created with fiftyoneDegreesPoolResourceCreate.
Definition pool.h:178
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:171
void *(* fiftyoneDegreesPoolResourceCreate)(fiftyoneDegreesPool *pool, void *state, fiftyoneDegreesException *exception)
Used to create a new resource for use in the pool.
Definition pool.h:159
void fiftyoneDegreesPoolReset(fiftyoneDegreesPool *pool)
Resets the pool without releasing any resources.
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...
pthread_mutex_t fiftyoneDegreesMutex
MUTEX AND THREADING MACROS.
Definition threading.h:112
Structure used to represent a 51Degrees exception and passed into methods that might generate excepti...
Definition exceptions.h:115
Pool of resources stored as items in a stack.
Definition pool.h:205
pthread_cond_t released
Wakes a waiting thread.
Definition pool.h:218
fiftyoneDegreesMutex lock
Guards the count and the wait for an item.
Definition pool.h:214
fiftyoneDegreesPoolItem * stack
Pointer to the memory used by the stack.
Definition pool.h:206
fiftyoneDegreesPoolResourceFree resourceFree
Frees a resource.
Definition pool.h:210
long waits
Counts waits for an item, for the tests.
Definition pool.h:213
volatile long waiting
Number of threads waiting for an item.
Definition pool.h:212
uint16_t count
Number of resource items that stack can hold.
Definition pool.h:209
fiftyoneDegreesPoolHead head
Head of the stack.
Definition pool.h:208
Pool item node in the stack of items.
Definition pool.h:185
void * resource
Pointer to the resource in the pool.
Definition pool.h:186
uint16_t next
The next item in the stack.
Definition pool.h:187
fiftyoneDegreesPool * pool
Reader the handle belongs to.
Definition pool.h:188
The head of the stack used for pop and push operations.
Definition pool.h:194
volatile long exchange
Number used in the compare exchange operation.
Definition pool.h:195
uint16_t aba
ABA value used to ensure proper operation.
Definition pool.h:198
uint16_t index
Index of the item in the linked list.
Definition pool.h:197