-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselector.c
More file actions
591 lines (513 loc) · 15.2 KB
/
Copy pathselector.c
File metadata and controls
591 lines (513 loc) · 15.2 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/**
* selector.c - un muliplexor de entrada salida
*/
#include <stdio.h> // perror
#include <stdlib.h> // malloc
#include <string.h> // memset
#include <assert.h> // :)
#include <errno.h> // :)
#include <pthread.h>
#include <stdint.h> // SIZE_MAX
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/signal.h>
#include "include/selector.h"
#define N(x) (sizeof(x)/sizeof((x)[0]))
#define ERROR_DEFAULT_MSG "something failed"
/** retorna una descripción humana del fallo */
const char *
selector_error(const selector_status status) {
const char *msg;
switch(status) {
case SELECTOR_SUCCESS:
msg = "Success";
break;
case SELECTOR_ENOMEM:
msg = "Not enough memory";
break;
case SELECTOR_MAXFD:
msg = "Can't handle any more file descriptors";
break;
case SELECTOR_IARGS:
msg = "Illegal argument";
break;
case SELECTOR_IO:
msg = "I/O error";
break;
default:
msg = ERROR_DEFAULT_MSG;
}
return msg;
}
static void
wake_handler(const int signal) {
// nada que hacer. está solo para interrumpir el select
}
// señal a usar para las notificaciones de resolución
struct selector_init conf;
static sigset_t emptyset, blockset;
selector_status
selector_init(const struct selector_init *c) {
memcpy(&conf, c, sizeof(conf));
// inicializamos el sistema de comunicación entre threads y el selector
// principal. La técnica se encuentra descripta en
// "The new pselect() system call" <https://lwn.net/Articles/176911/>
// March 24, 2006
selector_status ret = SELECTOR_SUCCESS;
struct sigaction act = {
.sa_handler = wake_handler,
};
// 0. calculamos mascara para evitar que se interrumpa antes de llegar al
// select
sigemptyset(&blockset);
sigaddset (&blockset, conf.signal);
if(-1 == sigprocmask(SIG_BLOCK, &blockset, NULL)) {
ret = SELECTOR_IO;
goto finally;
}
// 1. Registramos una función que atenderá la señal de interrupción
// del selector.
// Esta interrupción es útil en entornos multi-hilos.
if (sigaction(conf.signal, &act, 0)) {
ret = SELECTOR_IO;
goto finally;
}
sigemptyset(&emptyset);
finally:
return ret;
}
selector_status
selector_close(void) {
// Nada para liberar.
// TODO(juan): podriamos reestablecer el handler de la señal.
return SELECTOR_SUCCESS;
}
// estructuras internas
struct item {
int fd;
fd_interest interest;
const fd_handler *handler;
void * data;
};
/* tarea bloqueante */
struct blocking_job {
/** selector dueño de la resolucion */
fd_selector s;
/** file descriptor dueño de la resolucion */
int fd;
/** datos del trabajo provisto por el usuario */
void *data;
/** el siguiente en la lista */
struct blocking_job *next;
};
/** marca para usar en item->fd para saber que no está en uso */
static const int FD_UNUSED = -1;
/** verifica si el item está usado */
#define ITEM_USED(i) ( ( FD_UNUSED != (i)->fd) )
struct fdselector {
// almacenamos en una jump table donde la entrada es el file descriptor.
// Asumimos que el espacio de file descriptors no va a ser esparso; pero
// esto podría mejorarse utilizando otra estructura de datos
struct item *fds;
size_t fd_size; // cantidad de elementos posibles de fds
/** fd maximo para usar en select() */
int max_fd; // max(.fds[].fd)
/** descriptores prototipicos ser usados en select */
fd_set master_r, master_w;
/** para ser usado en el select() (recordar que select cambia el valor) */
fd_set slave_r, slave_w;
/** timeout prototipico para usar en select() */
struct timespec master_t;
/** tambien select() puede cambiar el valor */
struct timespec slave_t;
// notificaciónes entre blocking jobs y el selector
volatile pthread_t selector_thread;
/** protege el acceso a resolutions jobs */
pthread_mutex_t resolution_mutex;
/**
* lista de trabajos blockeantes que finalizaron y que pueden ser
* notificados.
*/
struct blocking_job *resolution_jobs;
};
/** cantidad máxima de file descriptors que la plataforma puede manejar */
#define ITEMS_MAX_SIZE FD_SETSIZE
// en esta implementación el máximo está dado por el límite natural de select(2).
/**
* determina el tamaño a crecer, generando algo de slack para no tener
* que realocar constantemente.
*/
static
size_t next_capacity(const size_t n) {
unsigned bits = 0;
size_t tmp = n;
while(tmp != 0) {
tmp >>= 1;
bits++;
}
tmp = 1UL << bits;
assert(tmp >= n);
if(tmp > ITEMS_MAX_SIZE) {
tmp = ITEMS_MAX_SIZE;
}
return tmp + 1;
}
static inline void
item_init(struct item *item) {
item->fd = FD_UNUSED;
}
/**
* inicializa los nuevos items. `last' es el indice anterior.
* asume que ya está blanqueada la memoria.
*/
static void
items_init(fd_selector s, const size_t last) {
assert(last <= s->fd_size);
for(size_t i = last; i < s->fd_size; i++) {
item_init(s->fds + i);
}
}
/**
* calcula el fd maximo para ser utilizado en select()
*/
static int
items_max_fd(fd_selector s) {
int max = 0;
for(int i = 0; i <= s->max_fd; i++) {
struct item *item = s->fds + i;
if(ITEM_USED(item)) {
if(item->fd > max) {
max = item->fd;
}
}
}
return max;
}
static void
items_update_fdset_for_fd(fd_selector s, const struct item * item) {
FD_CLR(item->fd, &s->master_r);
FD_CLR(item->fd, &s->master_w);
if(ITEM_USED(item)) {
if(item->interest & OP_READ) {
FD_SET(item->fd, &(s->master_r));
}
if(item->interest & OP_WRITE) {
FD_SET(item->fd, &(s->master_w));
}
}
}
/**
* garantizar cierta cantidad de elemenos en `fds'.
* Se asegura de que `n' sea un número que la plataforma donde corremos lo
* soporta
*/
static selector_status
ensure_capacity(fd_selector s, const size_t n) {
selector_status ret = SELECTOR_SUCCESS;
const size_t element_size = sizeof(*s->fds);
if(n < s->fd_size) {
// nada para hacer, entra...
ret = SELECTOR_SUCCESS;
} else if(n > ITEMS_MAX_SIZE) {
// me estás pidiendo más de lo que se puede.
ret = SELECTOR_MAXFD;
} else if(NULL == s->fds) {
// primera vez.. alocamos
const size_t new_size = next_capacity(n);
s->fds = calloc(new_size, element_size);
if(NULL == s->fds) {
ret = SELECTOR_ENOMEM;
} else {
s->fd_size = new_size;
items_init(s, 0);
}
} else {
// hay que agrandar...
const size_t new_size = next_capacity(n);
if (new_size > SIZE_MAX/element_size) { // ver MEM07-C
ret = SELECTOR_ENOMEM;
} else {
struct item *tmp = realloc(s->fds, new_size * element_size);
if(NULL == tmp) {
ret = SELECTOR_ENOMEM;
} else {
s->fds = tmp;
const size_t old_size = s->fd_size;
s->fd_size = new_size;
items_init(s, old_size);
}
}
}
return ret;
}
fd_selector
selector_new(const size_t initial_elements) {
size_t size = sizeof(struct fdselector);
fd_selector ret = malloc(size);
if(ret != NULL) {
memset(ret, 0x00, size);
ret->master_t.tv_sec = conf.select_timeout.tv_sec;
ret->master_t.tv_nsec = conf.select_timeout.tv_nsec;
assert(ret->max_fd == 0);
ret->resolution_jobs = 0;
pthread_mutex_init(&ret->resolution_mutex, 0);
if(0 != ensure_capacity(ret, initial_elements)) {
selector_destroy(ret);
ret = NULL;
}
}
return ret;
}
void
selector_destroy(fd_selector s) {
// lean ya que se llama desde los casos fallidos de _new.
if(s != NULL) {
if(s->fds != NULL) {
for(size_t i = 0; i < s->fd_size ; i++) {
if(ITEM_USED(s->fds + i)) {
selector_unregister_fd(s, i);
}
}
pthread_mutex_destroy(&s->resolution_mutex);
struct blocking_job *j, *next;
for(j = s->resolution_jobs; j != NULL; j = next) {
next = j->next;
free(j);
}
free(s->fds);
s->fds = NULL;
s->fd_size = 0;
}
free(s);
}
}
#define INVALID_FD(fd) ((fd) < 0 || (fd) >= ITEMS_MAX_SIZE)
selector_status
selector_register(fd_selector s,
const int fd,
const fd_handler *handler,
const fd_interest interest,
void *data) {
selector_status ret = SELECTOR_SUCCESS;
// 0. validación de argumentos
if(s == NULL || INVALID_FD(fd) || handler == NULL) {
ret = SELECTOR_IARGS;
goto finally;
}
// 1. tenemos espacio?
size_t ufd = (size_t)fd;
if(ufd > s->fd_size) {
ret = ensure_capacity(s, ufd);
if(SELECTOR_SUCCESS != ret) {
goto finally;
}
}
// 2. registración
struct item * item = s->fds + ufd;
if(ITEM_USED(item)) {
ret = SELECTOR_FDINUSE;
goto finally;
} else {
item->fd = fd;
item->handler = handler;
item->interest = interest;
item->data = data;
// actualizo colaterales
if(fd > s->max_fd) {
s->max_fd = fd;
}
items_update_fdset_for_fd(s, item);
}
finally:
return ret;
}
selector_status
selector_unregister_fd(fd_selector s,
const int fd) {
selector_status ret = SELECTOR_SUCCESS;
if(NULL == s || INVALID_FD(fd)) {
ret = SELECTOR_IARGS;
goto finally;
}
struct item *item = s->fds + fd;
if(!ITEM_USED(item)) {
ret = SELECTOR_IARGS;
goto finally;
}
if(item->handler->handle_close != NULL) {
struct selector_key key = {
.s = s,
.fd = item->fd,
.data = item->data,
};
item->handler->handle_close(&key);
}
item->interest = OP_NOOP;
items_update_fdset_for_fd(s, item);
memset(item, 0x00, sizeof(*item));
item_init(item);
s->max_fd = items_max_fd(s);
finally:
return ret;
}
selector_status
selector_set_interest(fd_selector s, int fd, fd_interest i) {
selector_status ret = SELECTOR_SUCCESS;
if(NULL == s || INVALID_FD(fd)) {
ret = SELECTOR_IARGS;
goto finally;
}
struct item *item = s->fds + fd;
if(!ITEM_USED(item)) {
ret = SELECTOR_IARGS;
goto finally;
}
item->interest = i;
items_update_fdset_for_fd(s, item);
finally:
return ret;
}
selector_status
selector_set_interest_key(struct selector_key *key, fd_interest i) {
selector_status ret;
if(NULL == key || NULL == key->s || INVALID_FD(key->fd)) {
ret = SELECTOR_IARGS;
} else {
ret = selector_set_interest(key->s, key->fd, i);
}
return ret;
}
/**
* se encarga de manejar los resultados del select.
* se encuentra separado para facilitar el testing
*/
static void
handle_iteration(fd_selector s) {
int n = s->max_fd;
struct selector_key key = {
.s = s,
};
for (int i = 0; i <= n; i++) {
struct item *item = s->fds + i;
if(ITEM_USED(item)) {
key.fd = item->fd;
key.data = item->data;
if(FD_ISSET(item->fd, &s->slave_r)) {
if(OP_READ & item->interest) {
if(0 == item->handler->handle_read) {
assert(("OP_READ arrived but no handler. bug!" == 0));
} else {
item->handler->handle_read(&key);
}
}
}
if(FD_ISSET(i, &s->slave_w)) {
if(OP_WRITE & item->interest) {
if(0 == item->handler->handle_write) {
assert(("OP_WRITE arrived but no handler. bug!" == 0));
} else {
item->handler->handle_write(&key);
}
}
}
}
}
}
static void
handle_block_notifications(fd_selector s) {
struct selector_key key = {
.s = s,
};
pthread_mutex_lock(&s->resolution_mutex);
struct blocking_job *j, *next;
for(j = s->resolution_jobs; j != NULL ; j = next) {
struct item *item = s->fds + j->fd;
if(ITEM_USED(item)) {
key.fd = item->fd;
key.data = item->data;
item->handler->handle_block(&key);
}
next = j->next;
free(j);
}
s->resolution_jobs = 0;
pthread_mutex_unlock(&s->resolution_mutex);
}
selector_status
selector_notify_block(fd_selector s,
const int fd) {
selector_status ret = SELECTOR_SUCCESS;
// TODO(juan): usar un pool
struct blocking_job *job = malloc(sizeof(*job));
if(job == NULL) {
ret = SELECTOR_ENOMEM;
goto finally;
}
job->s = s;
job->fd = fd;
// encolamos en el selector los resultados
pthread_mutex_lock(&s->resolution_mutex);
job->next = s->resolution_jobs;
s->resolution_jobs = job;
pthread_mutex_unlock(&s->resolution_mutex);
// notificamos al hilo principal
pthread_kill(s->selector_thread, conf.signal);
finally:
return ret;
}
selector_status
selector_select(fd_selector s) {
selector_status ret = SELECTOR_SUCCESS;
memcpy(&s->slave_r, &s->master_r, sizeof(s->slave_r));
memcpy(&s->slave_w, &s->master_w, sizeof(s->slave_w));
memcpy(&s->slave_t, &s->master_t, sizeof(s->slave_t));
s->selector_thread = pthread_self();
int fds = pselect(s->max_fd + 1, &s->slave_r, &s->slave_w, 0, &s->slave_t,
&emptyset);
if(-1 == fds) {
switch(errno) {
case EAGAIN:
case EINTR:
// si una señal nos interrumpio. ok!
break;
case EBADF:
// ayuda a encontrar casos donde se cierran los fd pero no
// se desregistraron
for(int i = 0 ; i < s->max_fd; i++) {
if(FD_ISSET(i, &s->master_r)|| FD_ISSET(i, &s->master_w)) {
if(-1 == fcntl(i, F_GETFD, 0)) {
fprintf(stderr, "Bad descriptor detected: %d\n", i);
}
}
}
ret = SELECTOR_IO;
break;
default:
ret = SELECTOR_IO;
goto finally;
}
} else {
handle_iteration(s);
}
if(ret == SELECTOR_SUCCESS) {
handle_block_notifications(s);
}
finally:
return ret;
}
int
selector_fd_set_nio(const int fd) {
int ret = 0;
int flags = fcntl(fd, F_GETFD, 0);
if(flags == -1) {
ret = -1;
} else {
if(fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
ret = -1;
}
}
return ret;
}