Sat, 22 Jan 2022 11:06:11 +0100
add ResourcePool initialization
260 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
269
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
4 | * Copyright 2021 Olaf Wintermann. All rights reserved. |
260 | 5 | * |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * | |
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
26 | * POSSIBILITY OF SUCH DAMAGE. | |
27 | */ | |
28 | ||
29 | #ifndef WS_RESOURCEPOOL_H | |
30 | #define WS_RESOURCEPOOL_H | |
31 | ||
32 | #include "../public/nsapi.h" | |
33 | ||
269
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
34 | #include "config.h" |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
35 | |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
36 | #include <pthread.h> |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
37 | |
260 | 38 | #ifdef __cplusplus |
39 | extern "C" { | |
40 | #endif | |
41 | ||
42 | typedef struct ResourcePool ResourcePool; | |
43 | typedef struct ResourceDataPrivate ResourceDataPrivate; | |
44 | ||
45 | struct ResourceDataPrivate { | |
46 | ResourceData data; | |
47 | Request *rq; | |
48 | ResourceType *type; | |
49 | }; | |
50 | ||
51 | struct ResourcePool { | |
269
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
52 | /* |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
53 | * Memory pool |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
54 | */ |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
55 | pool_handle_t *pool; |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
56 | |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
57 | /* |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
58 | * Type information of this resource pool |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
59 | * The type object is stored in the registered types map and should not |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
60 | * be freed, when the resource pool is destroyed |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
61 | */ |
260 | 62 | ResourceType *type; |
63 | ||
269
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
64 | /* |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
65 | * Data returned by the ResourceType init function |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
66 | * When the pool is destroyed, the data should be passed to |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
67 | * ResourceType.destroy |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
68 | */ |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
69 | void *data; |
260 | 70 | |
269
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
71 | pthread_mutex_t lock; |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
72 | pthread_cond_t available; |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
73 | |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
74 | /* |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
75 | * Array of available resources |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
76 | * each entry is created with ResourceType.createresource |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
77 | */ |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
78 | void **resources; |
260 | 79 | |
269
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
80 | /* |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
81 | * Allocated size of the resources array |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
82 | */ |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
83 | size_t resalloc; |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
84 | |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
85 | /* |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
86 | * Number of currently available resources in the array |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
87 | */ |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
88 | size_t numresources; |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
89 | |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
90 | /* |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
91 | * resource pool min parameter |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
92 | */ |
260 | 93 | int min; |
269
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
94 | |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
95 | /* |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
96 | * resource pool max parameter |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
97 | */ |
260 | 98 | int max; |
99 | }; | |
100 | ||
269
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
101 | int init_resource_pools(void); |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
102 | |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
103 | int resourcepool_new(ServerConfiguration *cfg, scstr_t type, scstr_t name, ConfigNode *node); |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
104 | |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
105 | int resourcepool_create_resources(ResourcePool *pool, int num_res); |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
106 | |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
107 | void resourcepool_destroy(ResourcePool *respool); |
260 | 108 | |
109 | #ifdef __cplusplus | |
110 | } | |
111 | #endif | |
112 | ||
113 | #endif /* WS_RESOURCEPOOL_H */ | |
114 |