forked from cminyard/ser2net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselector.c
1151 lines (987 loc) · 26.8 KB
/
selector.c
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* selector.c
*
* Code for abstracting select for files and timers.
*
* Author: MontaVista Software, Inc.
* Corey Minyard <[email protected]>
*
* Copyright 2002,2003 MontaVista Software Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* This file holds code to abstract the "select" call and make it
easier to use. The main thread lives here, the rest of the code
uses a callback interface. Basically, other parts of the program
can register file descriptors with this code, when interesting
things happen on those file descriptors this code will call
routines registered with it. */
#include "selector.h"
#include <sys/time.h>
#include <time.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <syslog.h>
#include <signal.h>
#include <string.h>
#ifdef HAVE_EPOLL_PWAIT
#include <sys/epoll.h>
#else
#define EPOLL_CTL_ADD 0
#define EPOLL_CTL_DEL 0
#define EPOLL_CTL_MOD 0
#endif
typedef struct fd_state_s
{
int deleted;
unsigned int use_count;
sel_fd_cleared_cb done;
} fd_state_t;
/* The control structure for each file descriptor. */
typedef struct fd_control_s
{
/* This structure is allocated when an FD is set and it holds
whether the FD has been deleted and information to handle the
deletion. */
fd_state_t *state;
void *data; /* Operation-specific data */
sel_fd_handler_t handle_read;
sel_fd_handler_t handle_write;
sel_fd_handler_t handle_except;
} fd_control_t;
typedef struct heap_val_s
{
/* Set this to the function to call when the timeout occurs. */
sel_timeout_handler_t handler;
/* Set this to whatever you like. You can use this to store your
own data. */
void *user_data;
/* Set this to the time when the timer will go off. */
struct timeval timeout;
/* Who owns me? */
selector_t *sel;
/* Am I currently running? */
int in_heap;
/* Am I currently stopped? */
int stopped;
/* Have I been freed? */
int freed;
/* Am I currently in a handler? */
int in_handler;
sel_timeout_handler_t done_handler;
void *done_cb_data;
} heap_val_t;
typedef struct theap_s theap_t;
#define heap_s theap_s
#define heap_node_s sel_timer_s
#define HEAP_EXPORT_NAME(s) theap_ ## s
#define HEAP_NAMES_LOCAL static
#define HEAP_OUTPUT_PRINTF "(%ld.%7.7ld)"
#define HEAP_OUTPUT_DATA pos->timeout.tv_sec, pos->timeout.tv_usec
static int
cmp_timeval(const struct timeval *tv1, const struct timeval *tv2)
{
if (tv1->tv_sec < tv2->tv_sec)
return -1;
if (tv1->tv_sec > tv2->tv_sec)
return 1;
if (tv1->tv_usec < tv2->tv_usec)
return -1;
if (tv1->tv_usec > tv2->tv_usec)
return 1;
return 0;
}
static int
heap_cmp_key(heap_val_t *v1, heap_val_t *v2)
{
return cmp_timeval(&v1->timeout, &v2->timeout);
}
#include "heap.h"
/* Used to build a list of threads that may need to be woken if a
timer on the top of the heap changes, or an FD is added/removed.
See wake_sel_thread() for more info. */
typedef struct sel_wait_list_s
{
/* The thread to wake up. */
long thread_id;
/* How to wake it. */
sel_send_sig_cb send_sig;
void *send_sig_cb_data;
/* This is the memory used to hold the timeout for select
operation. */
volatile struct timeval *timeout;
struct sel_wait_list_s *next, *prev;
} sel_wait_list_t;
struct sel_runner_s
{
selector_t *sel;
sel_runner_func_t func;
void *cb_data;
int in_use;
sel_runner_t *next;
};
struct selector_s
{
/* This is an array of all the file descriptors possible. This is
moderately wasteful of space, but easy to do. Hey, memory is
cheap. */
volatile fd_control_t fds[FD_SETSIZE];
/* These are the offical fd_sets used to track what file descriptors
need to be monitored. */
volatile fd_set read_set;
volatile fd_set write_set;
volatile fd_set except_set;
volatile int maxfd; /* The largest file descriptor registered with
this code. */
void *fd_lock;
/* The timer heap. */
theap_t timer_heap;
/* This is a list of items waiting to be woken up because they are
sitting in a select. See wake_sel_thread() for more info. */
sel_wait_list_t wait_list;
void *timer_lock;
sel_runner_t *runner_head;
sel_runner_t *runner_tail;
int wake_sig;
#ifdef HAVE_EPOLL_PWAIT
int epollfd;
#endif
sel_lock_t *(*sel_lock_alloc)(void *cb_data);
void (*sel_lock_free)(sel_lock_t *);
void (*sel_lock)(sel_lock_t *);
void (*sel_unlock)(sel_lock_t *);
};
static void
sel_timer_lock(selector_t *sel)
{
if (sel->sel_lock)
sel->sel_lock(sel->timer_lock);
}
static void
sel_timer_unlock(selector_t *sel)
{
if (sel->sel_lock)
sel->sel_unlock(sel->timer_lock);
}
static void
sel_fd_lock(selector_t *sel)
{
if (sel->sel_lock)
sel->sel_lock(sel->fd_lock);
}
static void
sel_fd_unlock(selector_t *sel)
{
if (sel->sel_lock)
sel->sel_unlock(sel->fd_lock);
}
/* This function will wake the SEL thread. It must be called with the
timer lock held, because it messes with timeout.
The operation is is subtle, but it does work. The timeout in the
selector is the data passed in (must be the actual data) as the
timeout to select. When we want to wake the select, we set the
timeout to zero first. That way, if the select has calculated the
timeout but has not yet called select, then this will set it to
zero (causing it to wait zero time). If select has already been
called, then the signal send should wake it up. We only need to do
this after we have calculated the timeout, but before we have
called select, thus only things in the wait list matter. */
static void
wake_sel_thread(selector_t *sel)
{
sel_wait_list_t *item;
item = sel->wait_list.next;
while (item != &sel->wait_list) {
item->timeout->tv_sec = 0;
item->timeout->tv_usec = 0;
if (item->send_sig)
item->send_sig(item->thread_id, item->send_sig_cb_data);
item = item->next;
}
}
static void
wake_fd_sel_thread(selector_t *sel)
{
wake_sel_thread(sel);
sel_fd_unlock(sel);
}
static void
wake_timer_sel_thread(selector_t *sel, volatile sel_timer_t *old_top)
{
if (old_top != theap_get_top(&sel->timer_heap))
/* If the top value changed, restart the waiting thread. */
wake_sel_thread(sel);
}
/* Wait list management. These *must* be called with the timer list
locked, and the values in the item *must not* change while in the
list. */
static void
add_sel_wait_list(selector_t *sel, sel_wait_list_t *item,
sel_send_sig_cb send_sig,
void *cb_data,
long thread_id, volatile struct timeval *timeout)
{
item->thread_id = thread_id;
item->timeout = timeout;
item->send_sig = send_sig;
item->send_sig_cb_data = cb_data;
item->next = sel->wait_list.next;
item->prev = &sel->wait_list;
sel->wait_list.next->prev = item;
sel->wait_list.next = item;
}
static void
remove_sel_wait_list(selector_t *sel, sel_wait_list_t *item)
{
item->next->prev = item->prev;
item->prev->next = item->next;
}
/* Initialize a single file descriptor. */
static void
init_fd(fd_control_t *fd)
{
fd->state = NULL;
fd->data = NULL;
fd->handle_read = NULL;
fd->handle_write = NULL;
fd->handle_except = NULL;
}
#ifdef HAVE_EPOLL_PWAIT
static int
sel_update_epoll(selector_t *sel, int fd, int op)
{
struct epoll_event event;
if (sel->epollfd < 0)
return 1;
memset(&event, 0, sizeof(event));
event.events = EPOLLONESHOT;
event.data.fd = fd;
if (FD_ISSET(fd, &sel->read_set))
event.events |= EPOLLIN | EPOLLHUP;
if (FD_ISSET(fd, &sel->write_set))
event.events |= EPOLLOUT;
if (FD_ISSET(fd, &sel->write_set))
event.events |= EPOLLERR | EPOLLPRI;
epoll_ctl(sel->epollfd, op, fd, &event);
return 0;
}
#else
static int
sel_update_epoll(selector_t *sel, int fd, int op)
{
return 1;
}
#endif
/* Set the handlers for a file descriptor. */
int
sel_set_fd_handlers(selector_t *sel,
int fd,
void *data,
sel_fd_handler_t read_handler,
sel_fd_handler_t write_handler,
sel_fd_handler_t except_handler,
sel_fd_cleared_cb done)
{
fd_control_t *fdc;
fd_state_t *state, *oldstate = NULL;
void *olddata = NULL;
int added = 1;
state = malloc(sizeof(*state));
if (!state)
return ENOMEM;
state->deleted = 0;
state->use_count = 0;
state->done = done;
sel_fd_lock(sel);
fdc = (fd_control_t *) &(sel->fds[fd]);
if (fdc->state) {
oldstate = fdc->state;
olddata = fdc->data;
added = 0;
}
fdc->state = state;
fdc->data = data;
fdc->handle_read = read_handler;
fdc->handle_write = write_handler;
fdc->handle_except = except_handler;
if (added) {
/* Move maxfd up if necessary. */
if (fd > sel->maxfd) {
sel->maxfd = fd;
}
if (sel_update_epoll(sel, fd, EPOLL_CTL_ADD)) {
wake_fd_sel_thread(sel);
goto out;
}
}
sel_fd_unlock(sel);
out:
if (oldstate) {
oldstate->deleted = 1;
if (oldstate->use_count == 0) {
if (oldstate->done)
oldstate->done(fd, olddata);
free(oldstate);
}
}
return 0;
}
/* Clear the handlers for a file descriptor and remove it from
select's monitoring. */
void
sel_clear_fd_handlers(selector_t *sel,
int fd)
{
fd_control_t *fdc;
fd_state_t *oldstate = NULL;
void *olddata = NULL;
sel_fd_lock(sel);
fdc = (fd_control_t *) &(sel->fds[fd]);
if (fdc->state) {
oldstate = fdc->state;
olddata = fdc->data;
fdc->state = NULL;
sel_update_epoll(sel, fd, EPOLL_CTL_DEL);
}
init_fd(fdc);
FD_CLR(fd, &sel->read_set);
FD_CLR(fd, &sel->write_set);
FD_CLR(fd, &sel->except_set);
/* Move maxfd down if necessary. */
if (fd == sel->maxfd) {
while ((sel->maxfd >= 0) && (! sel->fds[sel->maxfd].state)) {
sel->maxfd--;
}
}
sel_fd_unlock(sel);
if (oldstate) {
oldstate->deleted = 1;
if (oldstate->use_count == 0) {
if (oldstate->done)
oldstate->done(fd, olddata);
free(oldstate);
}
}
}
/* Set whether the file descriptor will be monitored for data ready to
read on the file descriptor. */
void
sel_set_fd_read_handler(selector_t *sel, int fd, int state)
{
fd_control_t *fdc = (fd_control_t *) &(sel->fds[fd]);
sel_fd_lock(sel);
if (!fdc->state)
goto out;
if (state == SEL_FD_HANDLER_ENABLED) {
if (FD_ISSET(fd, &sel->read_set))
goto out;
FD_SET(fd, &sel->read_set);
} else if (state == SEL_FD_HANDLER_DISABLED) {
if (!FD_ISSET(fd, &sel->read_set))
goto out;
FD_CLR(fd, &sel->read_set);
}
if (sel_update_epoll(sel, fd, EPOLL_CTL_MOD)) {
wake_fd_sel_thread(sel);
return;
}
out:
sel_fd_unlock(sel);
}
/* Set whether the file descriptor will be monitored for when the file
descriptor can be written to. */
void
sel_set_fd_write_handler(selector_t *sel, int fd, int state)
{
fd_control_t *fdc = (fd_control_t *) &(sel->fds[fd]);
sel_fd_lock(sel);
if (!fdc->state)
goto out;
if (state == SEL_FD_HANDLER_ENABLED) {
if (FD_ISSET(fd, &sel->write_set))
goto out;
FD_SET(fd, &sel->write_set);
} else if (state == SEL_FD_HANDLER_DISABLED) {
if (!FD_ISSET(fd, &sel->write_set))
goto out;
FD_CLR(fd, &sel->write_set);
}
if (sel_update_epoll(sel, fd, EPOLL_CTL_MOD)) {
wake_fd_sel_thread(sel);
return;
}
out:
sel_fd_unlock(sel);
}
/* Set whether the file descriptor will be monitored for exceptions
on the file descriptor. */
void
sel_set_fd_except_handler(selector_t *sel, int fd, int state)
{
fd_control_t *fdc = (fd_control_t *) &(sel->fds[fd]);
sel_fd_lock(sel);
if (!fdc->state)
goto out;
if (state == SEL_FD_HANDLER_ENABLED) {
if (FD_ISSET(fd, &sel->except_set))
goto out;
FD_SET(fd, &sel->except_set);
} else if (state == SEL_FD_HANDLER_DISABLED) {
if (!FD_ISSET(fd, &sel->except_set))
goto out;
FD_CLR(fd, &sel->except_set);
}
if (sel_update_epoll(sel, fd, EPOLL_CTL_MOD)) {
wake_fd_sel_thread(sel);
return;
}
out:
sel_fd_unlock(sel);
}
static void
diff_timeval(struct timeval *dest,
struct timeval *left,
struct timeval *right)
{
if ( (left->tv_sec < right->tv_sec)
|| ( (left->tv_sec == right->tv_sec)
&& (left->tv_usec < right->tv_usec)))
{
/* If left < right, just force to zero, don't allow negative
numbers. */
dest->tv_sec = 0;
dest->tv_usec = 0;
return;
}
dest->tv_sec = left->tv_sec - right->tv_sec;
dest->tv_usec = left->tv_usec - right->tv_usec;
while (dest->tv_usec < 0) {
dest->tv_usec += 1000000;
dest->tv_sec--;
}
}
int
sel_alloc_timer(selector_t *sel,
sel_timeout_handler_t handler,
void *user_data,
sel_timer_t **new_timer)
{
sel_timer_t *timer;
timer = malloc(sizeof(*timer));
if (!timer)
return ENOMEM;
memset(timer, 0, sizeof(*timer));
timer->val.handler = handler;
timer->val.user_data = user_data;
timer->val.sel = sel;
timer->val.stopped = 1;
*new_timer = timer;
return 0;
}
int
sel_free_timer(sel_timer_t *timer)
{
selector_t *sel = timer->val.sel;
int in_handler;
sel_timer_lock(sel);
if (timer->val.in_heap) {
sel_stop_timer(timer);
}
timer->val.freed = 1;
in_handler = timer->val.in_handler;
sel_timer_unlock(sel);
if (!in_handler)
free(timer);
return 0;
}
int
sel_start_timer(sel_timer_t *timer,
struct timeval *timeout)
{
selector_t *sel = timer->val.sel;
volatile sel_timer_t *top;
sel_timer_lock(sel);
if (timer->val.in_heap) {
sel_timer_unlock(sel);
return EBUSY;
}
top = theap_get_top(&sel->timer_heap);
timer->val.timeout = *timeout;
if (!timer->val.in_handler) {
/* Wait until the handler returns to start the timer. */
theap_add(&sel->timer_heap, timer);
timer->val.in_heap = 1;
}
timer->val.stopped = 0;
wake_timer_sel_thread(sel, top);
sel_timer_unlock(sel);
return 0;
}
int
sel_stop_timer(sel_timer_t *timer)
{
selector_t *sel = timer->val.sel;
sel_timer_lock(sel);
if (timer->val.stopped) {
sel_timer_unlock(sel);
return ETIMEDOUT;
}
if (timer->val.in_heap) {
volatile sel_timer_t *top = theap_get_top(&sel->timer_heap);
theap_remove(&sel->timer_heap, timer);
timer->val.in_heap = 0;
wake_timer_sel_thread(sel, top);
}
timer->val.stopped = 1;
sel_timer_unlock(sel);
return 0;
}
int
sel_stop_timer_with_done(sel_timer_t *timer,
sel_timeout_handler_t done_handler,
void *cb_data)
{
selector_t *sel = timer->val.sel;
volatile sel_timer_t *top;
sel_timer_lock(sel);
if (timer->val.stopped) {
sel_timer_unlock(sel);
goto out;
}
if (timer->val.in_handler) {
timer->val.done_handler = done_handler;
timer->val.done_cb_data = cb_data;
sel_timer_unlock(sel);
return 0;
}
if (timer->val.in_heap) {
top = theap_get_top(&sel->timer_heap);
theap_remove(&sel->timer_heap, timer);
timer->val.in_heap = 0;
wake_timer_sel_thread(sel, top);
}
timer->val.stopped = 1;
sel_timer_unlock(sel);
out:
done_handler(sel, timer, cb_data);
return 0;
}
void
sel_get_monotonic_time(struct timeval *tv)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
tv->tv_sec = ts.tv_sec;
tv->tv_usec = (ts.tv_nsec + 500) / 1000;
}
/*
* Process timers on selector. The timeout is always set, to a very
* long value if no timers are waiting. Note that this *must* be
* called with sel->timer_lock held. Note that if this processes
* any timers, the timeout will be set to { 0,0 }.
*/
static void
process_timers(selector_t *sel,
volatile struct timeval *timeout)
{
struct timeval now;
sel_timer_t *timer;
int called = 0;
timer = theap_get_top(&sel->timer_heap);
sel_get_monotonic_time(&now);
while (timer && cmp_timeval(&now, &timer->val.timeout) >= 0) {
called = 1;
theap_remove(&(sel->timer_heap), timer);
timer->val.in_heap = 0;
timer->val.stopped = 1;
timer->val.in_handler = 1;
sel_timer_unlock(sel);
timer->val.handler(sel, timer, timer->val.user_data);
sel_timer_lock(sel);
timer->val.in_handler = 0;
if (timer->val.done_handler) {
sel_timeout_handler_t done_handler = timer->val.done_handler;
void *done_cb_data = timer->val.done_cb_data;
timer->val.done_handler = NULL;
sel_timer_unlock(sel);
done_handler(sel, timer, done_cb_data);
sel_timer_lock(sel);
}
if (timer->val.freed)
free(timer);
else if (!timer->val.stopped) {
/* We were restarted while in the handler. */
theap_add(&sel->timer_heap, timer);
timer->val.in_heap = 1;
}
timer = theap_get_top(&sel->timer_heap);
}
if (called) {
/* If called, set the timeout to zero. */
timeout->tv_sec = 0;
timeout->tv_usec = 0;
} else if (timer) {
sel_get_monotonic_time(&now);
diff_timeval((struct timeval *) timeout,
(struct timeval *) &timer->val.timeout,
&now);
} else {
/* No timers, just set a long time. */
timeout->tv_sec = 100000;
timeout->tv_usec = 0;
}
}
int
sel_alloc_runner(selector_t *sel, sel_runner_t **new_runner)
{
sel_runner_t *runner;
runner = malloc(sizeof(*runner));
if (!runner)
return ENOMEM;
memset(runner, 0, sizeof(*runner));
runner->sel = sel;
*new_runner = runner;
return 0;
}
int
sel_free_runner(sel_runner_t *runner)
{
selector_t *sel = runner->sel;
sel_timer_lock(sel);
if (runner->in_use) {
sel_timer_unlock(sel);
return EBUSY;
}
sel_timer_unlock(sel);
free(runner);
return 0;
}
int
sel_run(sel_runner_t *runner, sel_runner_func_t func, void *cb_data)
{
selector_t *sel = runner->sel;
sel_timer_lock(sel);
if (runner->in_use) {
sel_timer_unlock(sel);
return EBUSY;
}
runner->func = func;
runner->cb_data = cb_data;
runner->next = NULL;
runner->in_use = 1;
if (sel->runner_tail) {
sel->runner_tail->next = runner;
sel->runner_tail = runner;
} else {
sel->runner_head = runner;
sel->runner_tail = runner;
}
sel_timer_unlock(sel);
return 0;
}
static void
process_runners(selector_t *sel)
{
while (sel->runner_head) {
sel_runner_t *runner = sel->runner_head;
sel_runner_func_t func;
void *cb_data;
sel->runner_head = sel->runner_head->next;
if (!sel->runner_head)
sel->runner_tail = NULL;
runner->in_use = 0;
func = runner->func;
cb_data = runner->cb_data;
sel_timer_unlock(sel);
func(runner, cb_data);
sel_timer_lock(sel);
}
}
static void
handle_selector_call(selector_t *sel, int i, volatile fd_set *fdset,
sel_fd_handler_t handler)
{
void *data;
fd_state_t *state;
if (handler == NULL) {
/* Somehow we don't have a handler for this.
Just shut it down. */
FD_CLR(i, fdset);
return;
}
if (!FD_ISSET(i, fdset))
/* The value was cleared, ignore it. */
return;
data = sel->fds[i].data;
state = sel->fds[i].state;
state->use_count++;
sel_fd_unlock(sel);
handler(i, data);
sel_fd_lock(sel);
state->use_count--;
if (state->deleted && state->use_count == 0) {
if (state->done) {
sel_fd_unlock(sel);
state->done(i, data);
sel_fd_lock(sel);
}
free(state);
}
}
/*
* return == 0 when timeout
* > 0 when successful
* < 0 when error
*/
static int
process_fds(selector_t *sel,
volatile struct timeval *timeout)
{
fd_set tmp_read_set;
fd_set tmp_write_set;
fd_set tmp_except_set;
int i;
int err;
int num_fds;
sel_fd_lock(sel);
memcpy(&tmp_read_set, (void *) &sel->read_set, sizeof(tmp_read_set));
memcpy(&tmp_write_set, (void *) &sel->write_set, sizeof(tmp_write_set));
memcpy(&tmp_except_set, (void *) &sel->except_set, sizeof(tmp_except_set));
num_fds = sel->maxfd + 1;
sel_fd_unlock(sel);
err = select(num_fds,
&tmp_read_set,
&tmp_write_set,
&tmp_except_set,
(struct timeval *) timeout);
if (err <= 0)
goto out;
/* We got some I/O. */
sel_fd_lock(sel);
for (i = 0; i <= sel->maxfd; i++) {
if (FD_ISSET(i, &tmp_read_set))
handle_selector_call(sel, i, &sel->read_set,
sel->fds[i].handle_read);
if (FD_ISSET(i, &tmp_write_set))
handle_selector_call(sel, i, &sel->write_set,
sel->fds[i].handle_write);
if (FD_ISSET(i, &tmp_except_set))
handle_selector_call(sel, i, &sel->except_set,
sel->fds[i].handle_except);
}
sel_fd_unlock(sel);
out:
return err;
}
#ifdef HAVE_EPOLL_PWAIT
static int
process_fds_epoll(selector_t *sel, struct timeval *tvtimeout)
{
int rv, fd;
struct epoll_event event;
int timeout;
sigset_t sigmask;
if (tvtimeout->tv_sec > 600)
/* Don't wait over 10 minutes, to work around an old epoll bug
and avoid issues with timeout overflowing on 64-bit systems,
which is much larger that 10 minutes, but who cares. */
timeout = 600 * 1000;
else
timeout = ((tvtimeout->tv_sec * 1000) +
(tvtimeout->tv_usec + 999) / 1000);
#ifdef USE_PTHREADS
pthread_sigmask(SIG_SETMASK, NULL, &sigmask);
#else
sigprocmask(SIG_SETMASK, NULL, &sigmask);
#endif
sigdelset(&sigmask, sel->wake_sig);
rv = epoll_pwait(sel->epollfd, &event, 1, timeout, &sigmask);
if (rv <= 0)
return rv;
sel_fd_lock(sel);
fd = event.data.fd;
if (event.events & (EPOLLIN | EPOLLHUP))
handle_selector_call(sel, fd, &sel->read_set,
sel->fds[fd].handle_read);
if (event.events & EPOLLOUT)
handle_selector_call(sel, fd, &sel->write_set,
sel->fds[fd].handle_write);
if (event.events & (EPOLLERR | EPOLLPRI))
handle_selector_call(sel, fd, &sel->except_set,
sel->fds[fd].handle_except);
/* Rearm the event. Remember it could have been deleted in the handler. */
if (sel->fds[fd].state)
sel_update_epoll(sel, fd, EPOLL_CTL_MOD);
sel_fd_unlock(sel);
return 0;
}
#endif
int
sel_select(selector_t *sel,
sel_send_sig_cb send_sig,
long thread_id,
void *cb_data,
struct timeval *timeout)
{
int err;
struct timeval loc_timeout;
sel_wait_list_t wait_entry;
sel_timer_lock(sel);
process_runners(sel);
process_timers(sel, (struct timeval *)(&loc_timeout));
if (timeout) {
if (cmp_timeval((struct timeval *)(&loc_timeout), timeout) >= 0)
memcpy(&loc_timeout, timeout, sizeof(loc_timeout));
}