stream_pool_base.c
8.07 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <scot/stream_pool.h>
#include <scot/stream_pool_fraction.h>
#include <scot/event_listener.h>
#include <scot/exception.h>
#include <scot/event.h>
#include <scot/stream.h>
#include <scot/memory.h>
#include <scot/thread.h>
#include <scot/scot_types.h>
#define GEN_LOCAL
# include <scot/list.h>
#undef GEN_LOCAL
GEN_LIST_FUNC_PROTO (scot_sp_fraction);
GEN_LIST_IMPL (scot_sp_fraction);
/*
* static functions for this file
* ----------------------------------------------------------------------
*/
static
unsigned short
scot_stream_pool_default_cb (struct scot_event * _e)
{
SSIZE_T n;
char puffer[1024];
struct scot_stream_pool_event * e = (struct scot_stream_pool_event *) _e;
if (_e->event == SCOT_EVENT_STREAM_POOL_READ)
{
if (scot_stream_eof (e->st))
{
printf ("Connection closed by foreign host\n");
scot_stream_close (e->st);
}
else
{
n = scot_stream_read (e->st, puffer, 1024);
printf ("read possible on %d (%d bytes read)\n", e->st->handle, n);
}
}
if (_e->event == SCOT_EVENT_STREAM_POOL_WRITE)
printf ("write possible on %d (%d bytes read)\n", e->st->handle, n);
if (_e->event == SCOT_EVENT_STREAM_POOL_EXCEP)
printf ("exception on %d (%d bytes read)\n", e->st->handle, n);
fflush (stdout);
return SCOT_EVENT_END;
}
static
void
main_thread_fini (void * arg)
{
struct scot_stream_pool * pool = (struct scot_stream_pool *) arg;
list_scot_sp_fraction_node_t * f_node = pool->pool;
struct scot_sp_fraction * f;
LOCK_THREAD_MUTEX (&pool->mutex);
/* this assures that the thread_fini does not try
* to wake the main thread for cleanup....
* (done because main thread is finalized here) */
((struct scot_event_listener *) arg)->thread_run_flg = 0;
while (! list_scot_sp_fraction_eol (pool->pool, f_node))
{
f_node = list_scot_sp_fraction_next (f_node);
f = list_scot_sp_fraction_retrive (f_node);
if (f->thread_run_flg != 0)
{
DETACH_THREAD (f->thread_handle);
CANCEL_THREAD (f->thread_handle);
}
}
UNLOCK_THREAD_MUTEX (&pool->mutex);
exc_end ();
}
/*
* entry point für einen stream-pool event-source thread.
*/
static
void
scot_stream_pool_main_loop (struct scot_stream_pool * sp)
{
excenv_t * ee;
LOCK_THREAD_MUTEX (&sp->mutex);
((struct scot_event_listener *) sp)->thread_id = THREAD_ID;
((struct scot_event_listener *) sp)->thread_run_flg = 1;
THREAD_CANCEL_ENABLE;
THREAD_CANCEL_DEFER;
TRY
{
list_scot_sp_fraction_node_t * f_node = sp->pool;
struct scot_sp_fraction * f;
THREAD_ATEXIT_BEGIN (main_thread_fini, sp);
/*
* starte all threads und suspende in einen cancellation point...
* Das könnte ich nochmal überdenken, immerhin kann ich mit
* diesem thread evtl. noch mehr anfangen. Z.B. Funktionen die
* in wecken, damit er alle threads stoppt (bzw. startet).
* Einzelne threads neu starten etc. Die Logik dafür könnte
* glaube ich gut in dieser Funktion liegen.....
* Vielleicht aber auch nicht.
*/
while (! list_scot_sp_fraction_eol (sp->pool, f_node))
{
f_node = list_scot_sp_fraction_next (f_node);
f = list_scot_sp_fraction_retrive (f_node);
f->thread_handle = NEW_THREAD (f->spf_entry_func, f);
f->thread_run_flg = 1;
}
UNLOCK_THREAD_MUTEX (&sp->mutex);
while (1)
{
THREAD_COND_ENTER_CS (&sp->cmd_cs);
WAIT_THREAD_COND (&sp->cmd_cond, &sp->cmd_cs, INFINITE);
switch (sp->cmd)
{
case SCOT_STREAM_POOL_RESTART_ALL_FRAC:
f_node = sp->pool;
while (! list_scot_sp_fraction_eol (sp->pool, f_node))
{
f_node = list_scot_sp_fraction_next (f_node);
f = list_scot_sp_fraction_retrive (f_node);
if (f->thread_run_flg != 0)
{
END_THREAD (f->thread_handle, INFINITE);
thread_exc_end (f->thread_handle);
}
f->thread_handle = NEW_THREAD (f->spf_entry_func, f);
f->thread_run_flg = 1;
}
break;
case SCOT_STREAM_POOL_START_ALL_FRAC:
f_node = sp->pool;
while (! list_scot_sp_fraction_eol (sp->pool, f_node))
{
f_node = list_scot_sp_fraction_next (f_node);
f = list_scot_sp_fraction_retrive (f_node);
if (f->thread_run_flg == 0)
{
f->thread_handle = NEW_THREAD (f->spf_entry_func, f);
f->thread_run_flg = 1;
}
}
break;
case SCOT_STREAM_POOL_STOP_ALL_FRAC:
f_node = sp->pool;
while (! list_scot_sp_fraction_eol (sp->pool, f_node))
{
f_node = list_scot_sp_fraction_next (f_node);
f = list_scot_sp_fraction_retrive (f_node);
if (f->thread_run_flg != 0)
{
END_THREAD (f->thread_handle, INFINITE);
thread_exc_end (f->thread_handle);
f->thread_run_flg = 0;
}
}
break;
case SCOT_STREAM_POOL_RESTART_FRACTION:
f = (struct scot_sp_fraction *) sp->cmd_arg;
if (f->thread_run_flg != 0)
{
END_THREAD (f->thread_handle, INFINITE);
thread_exc_end (f->thread_handle);
}
f->thread_handle = NEW_THREAD (f->spf_entry_func, f);
f->thread_run_flg = 1;
break;
case SCOT_STREAM_POOL_START_FRACTION:
f = (struct scot_sp_fraction *) sp->cmd_arg;
if (f->thread_run_flg == 0)
{
f->thread_handle = NEW_THREAD (f->spf_entry_func, f);
f->thread_run_flg = 1;
}
break;
case SCOT_STREAM_POOL_STOP_FRACTION:
f = (struct scot_sp_fraction *) sp->cmd_arg;
if (f->thread_run_flg != 0)
{
END_THREAD (f->thread_handle, INFINITE);
thread_exc_end (f->thread_handle);
f->thread_run_flg = 0;
}
}
/*
* now we do a cleanup. That is
* we remove empty fractions from the stream pool.
*/
f_node = sp->pool;
while (!list_scot_sp_fraction_eol (sp->pool, f_node))
{
int i;
f_node = list_scot_sp_fraction_next (f_node);
f = list_scot_sp_fraction_retrive (f_node);
if (f->s_count == 0)
{
if (f->thread_run_flg != 0)
{
END_THREAD (f->thread_handle, INFINITE);
thread_exc_end (f->thread_handle);
f->thread_run_flg = 0;
}
list_scot_sp_fraction_delete (f_node);
}
}
THREAD_COND_LEAVE_CS (&sp->cmd_cs);
}
THREAD_ATEXIT_END (0);
}
CATCH (ee)
{
print_all_exceptions (ee);
exit (EXIT_FAILURE);
}
main_thread_fini (sp);
}
/* ---------------------------------------------------------------------- */
struct scot_stream_pool *
scot_stream_pool_new (struct scot_stream_pool * sp)
{
sp = SCOT_MEM_GET (sizeof (struct scot_stream_pool));
SCOT_MEM_ZERO (sp, sizeof (struct scot_stream_pool));
sp->pool = list_scot_sp_fraction_new (sp->pool);
if (! list_scot_sp_fraction_elem_free_is_set ())
list_scot_sp_fraction_set_elem_free (scot_spf_free);
scot_event_listener_init (
(struct scot_event_listener *) sp,
SCOT_EG_STREAM_POOL,
(scot_thread_entry_fptr) scot_stream_pool_main_loop);
scot_event_listener_register_cb (
(struct scot_event_listener *) sp,
SCOT_EVENT_STREAM_POOL_READ,
scot_stream_pool_default_cb, NULL);
scot_event_listener_register_cb (
(struct scot_event_listener *) sp,
SCOT_EVENT_STREAM_POOL_WRITE,
scot_stream_pool_default_cb, NULL);
scot_event_listener_register_cb (
(struct scot_event_listener *) sp,
SCOT_EVENT_STREAM_POOL_EXCEP,
scot_stream_pool_default_cb, NULL);
NEW_THREAD_MUTEX (&sp->mutex);
NEW_THREAD_COND (&sp->cmd_cond);
NEW_THREAD_COND_CS (&sp->cmd_cs);
return sp;
}
void
scot_stream_pool_free (struct scot_stream_pool * sp)
{
scot_stream_pool_cmd (sp, SCOT_STREAM_POOL_STOP_ALL_FRAC, NULL);
scot_event_listener_fini ((struct scot_event_listener *) sp);
list_scot_sp_fraction_free (sp->pool);
FREE_THREAD_MUTEX (&sp->mutex);
FREE_THREAD_COND (&sp->cmd_cond);
FREE_THREAD_COND_CS (&sp->cmd_cs);
SCOT_MEM_FREE (sp);
}
void
scot_stream_pool_free_s (struct scot_stream_pool * sp, int free_s)
{
list_scot_sp_fraction_node_t * spf_node = sp->pool;
while (! list_scot_sp_fraction_eol (sp->pool, spf_node))
{
struct scot_sp_fraction * f;
spf_node = list_scot_sp_fraction_next (spf_node);
f = list_scot_sp_fraction_retrive (spf_node);
scot_spf_free_s (f, free_s);
}
}