[go: up one dir, main page]

Menu

[a443d0]: / src / flom_locker.h  Maximize  Restore  History

Download this file

280 lines (210 with data), 6.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
* Copyright (c) 2013-2016, Christian Ferrari <tiian@users.sourceforge.net>
* All rights reserved.
*
* This file is part of FLoM, Free Lock Manager
*
* FLoM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2.0 as
* published by the Free Software Foundation.
*
* FLoM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with FLoM. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef FLOM_LOCKER_H
# define FLOM_LOCKER_H
#include <config.h>
#ifdef HAVE_GLIB_H
# include <glib.h>
#endif
#include "flom_conns.h"
#include "flom_rsrc.h"
/* save old FLOM_TRACE_MODULE and set a new value */
#ifdef FLOM_TRACE_MODULE
# define FLOM_TRACE_MODULE_SAVE FLOM_TRACE_MODULE
# undef FLOM_TRACE_MODULE
#else
# undef FLOM_TRACE_MODULE_SAVE
#endif /* FLOM_TRACE_MODULE */
#define FLOM_TRACE_MODULE FLOM_TRACE_MOD_LOCKER
/**
* Used to break poll loop when looking for new/old clients
* This constant will probably become useless after "ping" implementation
*/
#define FLOM_LOCKER_POLL_TIMEOUT 1000
/**
* Data structure used for a locker thread
*/
struct flom_locker_s {
/**
* Identifier of the thread running the locker
*/
GThread *thread;
/**
* Pipe file descriptor: used by main thread (listener) to send commands
*/
int write_pipe;
/**
* Pipe file descriptor: used by locker thread to receive commands
*/
int read_pipe;
/**
* Last sequence number sent by parent (listener) to locker thread:
* parent point of view
*/
int write_sequence;
/**
* Last sequence read by locker thread and sent by parent (listener):
* child point of view
*/
int read_sequence;
/**
* Number of polling periods the locker thread performed nothing (without
* any client)
*/
int idle_periods;
/**
* Minimum number of milliseconds a resource (and a locker) must be kept
* after last usage
*/
int idle_lifespan;
/**
* Resource managed by the locker
*/
flom_resource_t resource;
};
/**
* A pool of lockers
*/
struct flom_locker_array_s {
/**
* Number of available lockers
*/
guint n;
/**
* Array of lockers
*/
GPtrArray *array;
};
/**
* A shorthand to avoid "struct" verb
*/
typedef struct flom_locker_array_s flom_locker_array_t;
/**
* It's the struct passed from parent thread (listener) to child thread
* (locker) when a new client arrive
*/
struct flom_locker_token_s {
/**
* Socket domain associated to client connection
*/
int domain;
/**
* Client connection (accepted) file descriptor
*/
int client_fd;
/**
* Sequence number associated to the token
*/
int sequence;
};
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/**
* Initialize a locker struct
* @param locker IN/OUT struct to be initialized
* @return a reason code
*/
static inline void flom_locker_init(struct flom_locker_s *locker) {
locker->thread = NULL;
locker->write_pipe = locker->read_pipe = FLOM_NULL_FD;
locker->write_sequence = locker->read_sequence =
locker->idle_periods = 0;
memset(&locker->resource, 0, sizeof(flom_resource_t));
}
/**
* Destroy the objects connected to the locker and the locker object
* itself (the pointer is not anymore valid after this call)
* @param locker IN/OUT object to destroy (remove from memory)
*/
void flom_locker_destroy(struct flom_locker_s *locker);
/**
* Initialize an array of lockers
* @param lockers IN/OUT pointer to object to initialize
*/
void flom_locker_array_init(flom_locker_array_t *lockers);
/**
* Remove all objects pointed by locker array and the array itself
* @param lockers IN/OUT pointer to object to release
*/
void flom_locker_array_free(flom_locker_array_t *lockers);
/**
* Add a new locker to locker array
* @param lockers IN/OUT array of lockers
* @param locker IN new locker to add
*/
void flom_locker_array_add(flom_locker_array_t *lockers,
struct flom_locker_s *locker);
/**
* Remove a locker from locker array
* @param lockers IN/OUT array of lockers
* @param locker IN pointer to the element must be deleted
*/
void flom_locker_array_del(flom_locker_array_t *lockers,
struct flom_locker_s *locker);
/**
* Number of active lockers thread
* @param lockers IN/OUT array of lockers
* @return how many lockers are managed by the object
*/
static inline guint flom_locker_array_count(
const flom_locker_array_t *lockers) {
return lockers->n;
}
/**
* Retrieve a pointer to a locker
* @param lockers IN/OUT array of lockers
* @param i IN index of the desired element
* @return NULL if i is an invalid index, the desired locker otherwise
*/
static inline struct flom_locker_s *flom_locker_array_get(
flom_locker_array_t *lockers, guint i) {
if (i < 0 || i >= lockers->n)
return NULL;
else
return g_ptr_array_index(lockers->array, i);
}
/**
* Main loop function for locker thread
* @param data IN pointer to locker context, it must be a pointer to
* @ref flom_locker_s
*/
gpointer flom_locker_loop(gpointer data);
/**
* Manager POLLIN event received from locker thread
* @param locker IN/OUT locker context object
* @param conns IN/OUT connections object
* @param id IN connection id
* @param refresh_conns OUT the conns object must be refreshed due to
* some deletion inside it
* @return a reason code
*/
int flom_locker_loop_pollin(struct flom_locker_s *locker,
flom_conns_t *conns, guint id,
int *refresh_conns);
#ifdef __cplusplus
}
#endif /* __cplusplus */
/* restore old value of FLOM_TRACE_MODULE */
#ifdef FLOM_TRACE_MODULE_SAVE
# undef FLOM_TRACE_MODULE
# define FLOM_TRACE_MODULE FLOM_TRACE_MODULE_SAVE
# undef FLOM_TRACE_MODULE_SAVE
#endif /* FLOM_TRACE_MODULE_SAVE */
#endif /* FLOM_LOCKER_H */