51Degrees Common C/C++  4.1

A shared functionality library that is used by 51Degrees products

pool.h

1 /* *********************************************************************
2  * This Source Code Form 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 Source Code Form is subject to the terms of the Mozilla Public
7  * License, v. 2.0.
8  *
9  * If a copy of the MPL was not distributed with this file, You can obtain
10  * one at http://mozilla.org/MPL/2.0/.
11  *
12  * This Source Code Form is "Incompatible With Secondary Licenses", as
13  * defined by the Mozilla Public License, v. 2.0.
14  * ******************************************************************** */
15 
16 #ifndef FIFTYONE_DEGREES_POOL_H_INCLUDED
17 #define FIFTYONE_DEGREES_POOL_H_INCLUDED
18 
112 #include <stdio.h>
113 #include <errno.h>
114 #include <stdint.h>
115 #ifdef _MSC_VER
116 #include <windows.h>
117 #endif
118 #include <assert.h>
119 #include <limits.h>
120 #include "data.h"
121 #include "exceptions.h"
122 #include "memory.h"
123 #ifndef FIFTYONE_DEGREES_NO_THREADING
124 #include "threading.h"
125 #endif
126 
127 #ifdef __cplusplus
128 #define EXTERNAL extern "C"
129 #else
130 #define EXTERNAL
131 #endif
132 
134 typedef struct fiftyone_degrees_pool_item_t fiftyoneDegreesPoolItem;
135 typedef struct fiftyone_degrees_pool_t fiftyoneDegreesPool;
146 typedef void*(*fiftyoneDegreesPoolResourceCreate)(
147  fiftyoneDegreesPool *pool,
148  void *state,
149  fiftyoneDegreesException *exception);
150 
158 typedef size_t(*fiftyoneDegreesPoolResourceSize)(void *state);
159 
166  fiftyoneDegreesPool *pool,
167  void *resource);
168 
172 typedef struct fiftyone_degrees_pool_item_t {
173  void *resource;
174  uint16_t next;
177 
181 typedef union fiftyone_degrees_pool_head_t {
182  volatile long exchange;
183  struct {
184  uint16_t index;
185  uint16_t aba;
186  } values;
188 
192 typedef struct fiftyone_degrees_pool_t {
196  uint16_t count;
199 
218  fiftyoneDegreesPool *pool,
219  uint16_t concurrency,
220  void *state,
221  fiftyoneDegreesPoolResourceCreate resourceCreate,
222  fiftyoneDegreesPoolResourceFree resourceFree,
223  fiftyoneDegreesException *exception);
224 
235  fiftyoneDegreesPool *pool,
236  fiftyoneDegreesException *exception);
237 
244 
251 
257 
262 #endif
void fiftyoneDegreesPoolReset(fiftyoneDegreesPool *pool)
Resets the pool without releasing any resources.
Definition: pool.c:119
fiftyoneDegreesPoolResourceFree resourceFree
Frees a resource.
Definition: pool.h:197
Pool item node in the stack of items.
Definition: pool.h:172
void(* fiftyoneDegreesPoolResourceFree)(fiftyoneDegreesPool *pool, void *resource)
Frees a resource previously created with fiftyoneDegreesPoolResourceCreate.
Definition: pool.h:165
uint16_t index
Index of the item in the linked list.
Definition: pool.h:184
void fiftyoneDegreesPoolFree(fiftyoneDegreesPool *pool)
Releases the items used by the pool freeing the resources used by each item by calling the resourceFr...
Definition: pool.c:127
uint16_t count
Number of resource items that stack can hold.
Definition: pool.h:196
volatile long exchange
Number used in the compare exchange operation.
Definition: pool.h:182
Pool of resources stored as items in a stack.
Definition: pool.h:192
uint16_t aba
ABA value used to ensure proper operation.
Definition: pool.h:185
fiftyoneDegreesPoolItem * stack
Pointer to the memory used by the stack.
Definition: pool.h:193
fiftyoneDegreesPoolItem * fiftyoneDegreesPoolItemGet(fiftyoneDegreesPool *pool, fiftyoneDegreesException *exception)
Gets the next free item from the pool for exclusive use by the caller.
Definition: pool.c:69
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:158
The head of the stack used for pop and push operations.
Definition: pool.h:181
fiftyoneDegreesPoolHead head
Head of the stack.
Definition: pool.h:195
void fiftyoneDegreesPoolItemRelease(fiftyoneDegreesPoolItem *item)
Releases the item back to the pool it belongs ready to be reused by another operation.
Definition: pool.c:100
void * resource
Pointer to the resource in the pool.
Definition: pool.h:173
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...
Definition: pool.c:20
void *(* fiftyoneDegreesPoolResourceCreate)(fiftyoneDegreesPool *pool, void *state, fiftyoneDegreesException *exception)
Used to create a new resource for use in the pool.
Definition: pool.h:146
Structure used to represent a 51Degrees exception and passed into methods that might generate excepti...
Definition: exceptions.h:109
uint16_t next
The next item in the stack.
Definition: pool.h:174
fiftyoneDegreesPool * pool
Reader the handle belongs to.
Definition: pool.h:175