|
1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2021 Olaf Wintermann. All rights reserved. |
|
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 |
|
34 #include "config.h" |
|
35 |
|
36 #include <pthread.h> |
|
37 |
|
38 #ifdef __cplusplus |
|
39 extern "C" { |
|
40 #endif |
|
41 |
|
42 typedef struct ResourceDataPrivate ResourceDataPrivate; |
|
43 |
|
44 struct ResourceDataPrivate { |
|
45 ResourceData data; |
|
46 |
|
47 /* |
|
48 * the void* pointer returned by respool->type->createresource |
|
49 * |
|
50 * ResourceData.data contains the pointer returned by |
|
51 * respool->type->getresourcedata(resdata)) |
|
52 */ |
|
53 void *resdata; |
|
54 }; |
|
55 |
|
56 struct ResourcePool { |
|
57 /* |
|
58 * Memory pool |
|
59 */ |
|
60 pool_handle_t *pool; |
|
61 |
|
62 /* |
|
63 * Type information of this resource pool |
|
64 * The type object is stored in the registered types map and should not |
|
65 * be freed, when the resource pool is destroyed |
|
66 */ |
|
67 ResourceType *type; |
|
68 |
|
69 /* |
|
70 * Data returned by the ResourceType init function |
|
71 * When the pool is destroyed, the data should be passed to |
|
72 * ResourceType.destroy |
|
73 */ |
|
74 void *data; |
|
75 |
|
76 pthread_mutex_t lock; |
|
77 pthread_cond_t available; |
|
78 |
|
79 /* |
|
80 * Array of available resources |
|
81 * each entry is created with ResourceType.createresource |
|
82 */ |
|
83 ResourceDataPrivate **resources; |
|
84 |
|
85 /* |
|
86 * Allocated size of the resources array |
|
87 */ |
|
88 size_t resalloc; |
|
89 |
|
90 /* |
|
91 * Number of currently available resources in the array |
|
92 */ |
|
93 size_t numresources; |
|
94 |
|
95 |
|
96 /* |
|
97 * Number of created resources (in use + available) |
|
98 */ |
|
99 size_t numcreated; |
|
100 |
|
101 /* |
|
102 * resource pool min parameter |
|
103 */ |
|
104 int min; |
|
105 |
|
106 /* |
|
107 * resource pool max parameter |
|
108 */ |
|
109 int max; |
|
110 }; |
|
111 |
|
112 int init_resource_pools(void); |
|
113 |
|
114 int resourcepool_new(ServerConfiguration *cfg, scstr_t type, scstr_t name, ConfigNode *node); |
|
115 |
|
116 void resourcepool_destroy_resource(ResourceDataPrivate *res); |
|
117 |
|
118 void resourcepool_destroy(ResourcePool *respool); |
|
119 |
|
120 #ifdef __cplusplus |
|
121 } |
|
122 #endif |
|
123 |
|
124 #endif /* WS_RESOURCEPOOL_H */ |
|
125 |