-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.c
3732 lines (2760 loc) · 102 KB
/
agent.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
/*
* libslack - https://libslack.org
*
* Copyright (C) 1999-2004, 2010, 2020-2023 raf <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, see <https://www.gnu.org/licenses/>.
*
* 20230824 raf <[email protected]>
*/
/*
=head1 NAME
I<libslack(agent)> - agent module
=head1 SYNOPSIS
#include <slack/std.h>
#include <slack/agent.h>
typedef struct Agent Agent;
typedef int agent_action_t(Agent *agent, void *arg);
typedef int agent_reaction_t(Agent *agent, int fd, int revents, void *arg);
Agent *agent_create(void);
Agent *agent_create_with_locker(Locker *locker);
Agent *agent_create_measured(void);
Agent *agent_create_measured_with_locker(Locker *locker);
Agent *agent_create_using_select(void);
Agent *agent_create_using_select_with_locker(Locker *locker);
void agent_release(Agent *agent);
void *agent_destroy(Agent **agent);
int agent_rdlock(const Agent *agent);
int agent_wrlock(const Agent *agent);
int agent_unlock(const Agent *agent);
int agent_connect(Agent *agent, int fd, int events, agent_reaction_t *reaction, void *arg);
int agent_connect_unlocked(Agent *agent, int fd, int events, agent_reaction_t *reaction, void *arg);
int agent_disconnect(Agent *agent, int fd);
int agent_disconnect_unlocked(Agent *agent, int fd);
int agent_transfer(Agent *agent, int fd, Agent *dst);
int agent_transfer_unlocked(Agent *agent, int fd, Agent *dst);
int agent_send(Agent *agent, int fd, int sockfd);
int agent_send_unlocked(Agent *agent, int fd, int sockfd);
int agent_recv(Agent *agent, int sockfd, agent_reaction_t *reaction, void *arg);
int agent_recv_unlocked(Agent *agent, int sockfd, agent_reaction_t *reaction, void *arg);
int agent_detail(Agent *agent, int fd);
int agent_detail_unlocked(Agent *agent, int fd);
const struct timeval * const agent_last(Agent *agent, int fd);
const struct timeval * const agent_last_unlocked(Agent *agent, int fd);
int agent_velocity(Agent *agent, int fd);
int agent_velocity_unlocked(Agent *agent, int fd);
int agent_acceleration(Agent *agent, int fd);
int agent_acceleration_unlocked(Agent *agent, int fd);
int agent_dadt(Agent *agent, int fd);
int agent_dadt_unlocked(Agent *agent, int fd);
void *agent_schedule(Agent *agent, long sec, long usec, agent_action_t *action, void *arg);
void *agent_schedule_unlocked(Agent *agent, long sec, long usec, agent_action_t *action, void *arg);
int agent_cancel(Agent *agent, void *action_id);
int agent_cancel_unlocked(Agent *agent, void *action_id);
int agent_start(Agent *agent);
int agent_stop(Agent *agent);
=head1 DESCRIPTION
This module provides support for a generic agent programming model. Agents
are like event loops, except that while event loops only react to input
events, agents can also take independent actions at specific times.
Unlike event loops, which are typically GUI-specific and receive input
events by calling some concrete event retrieval function, input events for
agents take the form of data transfers across file descriptors. This means
that input events can come from any source, and have any semantics. For
example, to implement an event loop for a specific GUI using an agent, you'd
have a separate thread or process that calls the GUI's event retrieval
function, and then sends each event to the agent across a pipe or socket.
Agents multiplex input sources using I<poll(2)> (or I<select(2)> if
unavoidable) and multiplex timers for scheduled actions over I<poll(2)>'s
timeout facility using hierarchical timing wheels. If timers are not used,
agents are just an alternate interface to I<poll(2)>. If input sources are
not used, agents are just a multi-purpose timer that doesn't use any
signals.
Multiple agents can be connected to each other via pipes and sockets in
arbitrary networks (in multiple threads or multiple processes on the same
host or multiple hosts) and these connections may change over time.
It is expected that agents will generally be used to build highly scalable
internet servers because connecting and disconnecting file descriptors and
scheduling and cancelling timed actions are all O(1) operations and managing
timers has constant average time. If two or more agents cooperate (on a
system that has I<poll(2)>), responding to input events can also be highly
scalable (See the SCALABILITY section for details).
=over 4
=cut
*/
#ifndef _BSD_SOURCE
#define _BSD_SOURCE /* For timercmp() in glibc and snprintf() on OpenBSD-4.7 */
#endif
#ifndef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE /* New name for _BSD_SOURCE */
#endif
#ifndef _NETBSD_SOURCE
#define _NETBSD_SOURCE /* For timercmp() on NetBSD-5.0.2 */
#endif
#include "config.h"
#include "std.h"
#include "mem.h"
#include "err.h"
#include "link.h"
#include "net.h"
#include "agent.h"
#include <sys/time.h>
#include <sys/types.h>
#ifndef TEST
#if HAVE_POLL_H
#include <poll.h>
#elif HAVE_SYS_POLL_H
#include <sys/poll.h>
#elif HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
typedef struct timewheel_t timewheel_t;
typedef struct action_t action_t;
typedef struct reaction_t reaction_t;
typedef struct activity_t activity_t;
typedef struct timeval timeval;
#define POLL_SIZE 16
enum { IDLE = 0, START = 1, STOP = 2 }; /* Agent states */
enum { POLL = 0, SELECT = 1 }; /* Agent implementations */
struct Agent
{
int state; /* idle, start, stop */
ssize_t *ids; /* map fd to array indexes */
size_t ids_size; /* size of ids */
int method; /* implementation method: poll() or select() */
union
{
#ifdef HAVE_POLL
struct pollfd *pfds; /* for poll() */
#endif
struct { fd_set *rfds, *xfds, *wfds; } s; /* for select() */
} u;
reaction_t *reactions; /* reactions to input events */
activity_t *tempo; /* activity of the agent itself */
activity_t *activity; /* activity of each connection */
size_t size; /* number of elements allocated in pollfd */
size_t length; /* number of elements used in pollfd */
timewheel_t *timewheel; /* hierarchical timing wheel for scheduling */
size_t timers; /* number of timers in the timewheel */
Locker *locker; /* locking strategy for this agent */
};
#ifdef HAVE_POLL
#define pollfds u.pfds
#endif
#define readfds u.s.rfds
#define writefds u.s.wfds
#define exceptfds u.s.xfds
struct action_t
{
action_t *next; /* link to next action */
action_t *prev; /* link to previous action */
action_t **parent; /* the list in which this action is stored */
timeval when; /* absolute time to act */
agent_action_t *action; /* function to call */
void *arg; /* argument to pass to function */
size_t day; /* index into days */
size_t hour; /* index into hours */
size_t minute; /* index into minutes */
size_t second; /* index into seconds */
size_t jiffy; /* index into jiffies */
};
struct reaction_t
{
int fd; /* file descriptor */
int events; /* read/write/exception */
agent_reaction_t *reaction; /* function to call */
void *arg; /* argument to pass to function */
};
struct activity_t
{
timeval since; /* when the last event occurred */
size_t detail; /* how much detail do we have? */
int dt; /* the event rate */
int ddt; /* the rate of change of the event rate */
int dddt; /* the rate of change of the rate of change of the event rate */
};
enum
{
DAYS = 10,
HOURS = 24,
MINUTES = 60,
SECONDS = 60,
JIFFIES = 100
};
struct timewheel_t
{
timeval now[1]; /* current time */
size_t day; /* current index into days */
size_t hour; /* current index into hours */
size_t minute; /* current index into minutes */
size_t second; /* current index into seconds */
size_t jiffy; /* current index into jiffies */
action_t *days[DAYS]; /* timers for subsequent days */
action_t *hours[HOURS]; /* timers for subsequent hours */
action_t *minutes[MINUTES]; /* timers for subsequent minutes */
action_t *seconds[SECONDS]; /* timers for subsequent seconds */
action_t *jiffies[JIFFIES]; /* timers for this second */
};
/*
C<static timewheel_t *timewheel_create()>
Creates a I<timewheel_t> object. It is the caller's responsibility to
deallocate the timewheel object with I<timewheel_release(3)>. On error,
returns C<null> with C<errno> set appropriately.
*/
static timewheel_t *timewheel_create()
{
timewheel_t *timewheel = mem_new(timewheel_t); /* XXX decouple */
if (!timewheel)
return NULL;
memset(timewheel, 0, sizeof(timewheel_t));
if (gettimeofday(timewheel->now, NULL) == -1)
return NULL;
return timewheel;
}
/*
C<static void timewheel_release(timewheel_t *timewheel)>
Releases (deallocates) C<timewheel>.
*/
static action_t *release_action(action_t *action)
{
action_t *next = dlink_next(action);
mem_release(action);
return next;
}
static void release_actions(action_t *action)
{
while (action)
action = release_action(action);
}
static void timewheel_release(timewheel_t *timewheel)
{
size_t i;
if (!timewheel)
return;
for (i = 0; i < DAYS; ++i)
release_actions(timewheel->days[i]);
for (i = 0; i < HOURS; ++i)
release_actions(timewheel->hours[i]);
for (i = 0; i < MINUTES; ++i)
release_actions(timewheel->minutes[i]);
for (i = 0; i < SECONDS; ++i)
release_actions(timewheel->seconds[i]);
for (i = 0; i < JIFFIES; ++i)
release_actions(timewheel->jiffies[i]);
mem_release(timewheel);
}
/*
=item C<Agent *agent_create(void)>
Creates an I<Agent> object. On error, returns C<null> with C<errno> set
appropriately. It is the caller's responsibility to deallocate the new agent
with I<agent_release(3)> or I<agent_destroy(3)>. It is strongly recommended
to use I<agent_destroy(3)>, because it also sets the pointer variable to
C<null>.
=cut
*/
Agent *agent_create(void)
{
return agent_create_with_locker(NULL);
}
/*
=item C<Agent *agent_create_with_locker(Locker *locker)>
Equivalent to I<agent_create(3)> except that multiple threads accessing the
new agent will be synchronised by C<locker>.
=cut
*/
Agent *agent_create_with_locker(Locker *locker)
{
Agent *agent = mem_new(Agent); /* XXX decouple */
if (!agent)
return NULL;
memset(agent, 0, sizeof(Agent));
#ifdef HAVE_POLL
agent->method = POLL;
#else
agent->method = SELECT;
#endif
agent->locker = locker;
return agent;
}
/*
=item C<Agent *agent_create_measured(void)>
Creates an I<Agent> object that measures I/O activity. Such agents can be
passed to the following functions to determine the level of I/O activity
handled by the agent: I<agent_detail(3)> which returns the level of detail
available (this determines which of the subsequent functions may be called);
I<agent_last(3)> which returns the time that the most recent I/O event
occurred; I<agent_velocity(3)> which returns the rate of I/O events;
I<agent_acceleration(3)> which returns the rate of change of the I/O event
rate; and I<agent_dadt(3)> which returns the rate of change of the rate of
change of the I/O event rate. These functions can be applied to individual
file descriptors, or to the agent as a whole. These agents can be combined
to produce a fast/slow lane structure that improves scalability of I/O with
respect to the number of connected file descriptors. See the SCALABILITY
section below for more details. On error, returns C<null> with C<errno> set
appropriately. It is the caller's responsibility to deallocate the new agent
with I<agent_release(3)> or I<agent_destroy(3)>. It is strongly recommended
to use I<agent_destroy(3)>, because it also sets the pointer variable to
C<null>. Note, if this system does not have I<poll(2)>, this function is not
very useful.
=cut
*/
Agent *agent_create_measured(void)
{
return agent_create_measured_with_locker(NULL);
}
/*
=item C<Agent *agent_create_measured_with_locker(Locker *locker)>
Equivalent to I<agent_create_measured(3)> except that multiple threads
accessing the new agent will be synchronised by C<locker>.
=cut
*/
Agent *agent_create_measured_with_locker(Locker *locker)
{
Agent *agent = mem_new(Agent); /* XXX decouple */
if (!agent)
return NULL;
memset(agent, 0, sizeof(Agent));
#ifdef HAVE_POLL
agent->method = POLL;
#else
agent->method = SELECT;
#endif
if (!(agent->tempo = mem_new(activity_t))) /* XXX decouple */
{
mem_release(agent);
return NULL;
}
memset(agent->tempo, 0, sizeof(activity_t));
agent->locker = locker;
return agent;
}
/*
=item C<Agent *agent_create_using_select(void)>
Equivalent to I<agent_create(3)> except that the agent created will use
I<select(2)> instead of I<poll(2)>. This should only be used under I<Linux>
when accurate 10ms timers are required (see the BUGS section for details).
It should not be used for I/O (see the SCALABILITY section for details).
=cut
*/
Agent *agent_create_using_select(void)
{
return agent_create_using_select_with_locker(NULL);
}
/*
=item C<Agent *agent_create_using_select_with_locker(Locker *locker)>
Equivalent to I<agent_create_using_select(3)> except that multiple threads
accessing the new agent will be synchronised by C<locker>.
=cut
*/
Agent *agent_create_using_select_with_locker(Locker *locker)
{
Agent *agent = mem_new(Agent); /* XXX decouple */
if (!agent)
return NULL;
memset(agent, 0, sizeof(Agent));
agent->method = SELECT;
agent->locker = locker;
return agent;
}
/*
=item C<void agent_release(Agent *agent)>
Releases (deallocates) C<agent>.
=cut
*/
void agent_release(Agent *agent)
{
Locker *locker;
if (!agent)
return;
if (agent_wrlock(agent))
return;
locker = agent->locker;
mem_release(agent->ids);
#ifdef HAVE_POLL
if (agent->method == POLL)
mem_release(agent->pollfds);
else
#endif
{
mem_release(agent->readfds);
mem_release(agent->writefds);
mem_release(agent->exceptfds);
}
mem_release(agent->reactions);
mem_release(agent->tempo);
mem_release(agent->activity);
timewheel_release(agent->timewheel);
mem_release(agent);
locker_unlock(locker);
}
/*
=item C<void *agent_destroy(Agent **agent)>
Destroys (deallocates and sets to C<null>) C<*agent>. Returns C<null>.
B<Note:> agents that are shared by multiple threads must not be destroyed
until after all threads have finished with it.
=cut
*/
void *agent_destroy(Agent **agent)
{
if (agent && *agent)
{
agent_release(*agent);
*agent = NULL;
}
return NULL;
}
/*
=item C<int agent_rdlock(const Agent *agent)>
Claims a read lock on C<agent> (if C<agent> was created with a I<Locker>).
This is needed when multiple read-only I<agent(3)> module functions need to
be called atomically. It is the caller's responsibility to call
I<agent_unlock(3)> after the atomic operation. The only functions that may
be called on C<agent> between calls to I<agent_rdlock(3)> and
I<agent_unlock(3)> are any read-only I<agent(3)> module functions whose name
ends with C<_unlocked>. On success, returns C<0>. On error, returns an error
code.
=cut
*/
#define agent_rdlock(agent) locker_rdlock((agent)->locker)
#define agent_wrlock(agent) locker_wrlock((agent)->locker)
#define agent_unlock(agent) locker_unlock((agent)->locker)
int (agent_rdlock)(const Agent *agent)
{
return agent_rdlock(agent);
}
/*
=item C<int agent_wrlock(const Agent *agent)>
Claims a write lock on C<agent> (if C<agent> was created with a I<Locker>).
This is needed when multiple read/write I<agent(3)> module functions need to
be called atomically. It is the caller's responsibility to call
I<agent_unlock(3)> after the atomic operation. The only functions that may
be called on C<agent> between calls to I<agent_wrlock(3)> and
I<agent_unlock(3)> are any I<agent(3)> module functions whose name ends with
C<_unlocked>. On success, returns C<0>. On error, returns an error code.
=cut
*/
int (agent_wrlock)(const Agent *agent)
{
return agent_wrlock(agent);
}
/*
=item C<int agent_unlock(const Agent *agent)>
Unlocks a read or write lock on C<agent> obtained with I<agent_rdlock(3)> or
I<agent_wrlock(3)> (if C<agent> was created with a C<locker>). On success,
returns C<0>. On error, returns an error code.
=cut
*/
int (agent_unlock)(const Agent *agent)
{
return agent_unlock(agent);
}
/*
=item C<int agent_connect(Agent *agent, int fd, int events, agent_reaction_t *reaction, void *arg)>
Connect the file descriptor, C<fd>, to C<agent>. C<events> specifies the
input/output events of interest. It is a bitmask of the following values:
C<R_OK>, C<W_OK> and C<X_OK> indicating, respectively, readability,
writability and exceptional condition (i.e. arrival of out of band data).
When any of the specified events occur, the function, C<reaction>, will be
called with four arguments: C<agent>, C<fd>, C<revents> (the bitmask of the
events that occurred), and C<arg>. If C<fd> is already connected, the
existing C<events>, C<reaction> and C<arg> are replaced with the new values.
On success, returns C<0>. On error, returns C<-1> with C<errno> set
appropriately.
=cut
*/
int agent_connect(Agent *agent, int fd, int events, agent_reaction_t *reaction, void *arg)
{
int ret, err;
if (!agent)
return set_errno(EINVAL);
if ((err = agent_wrlock(agent)))
return set_errno(err);
ret = agent_connect_unlocked(agent, fd, events, reaction, arg);
if ((err = agent_unlock(agent)))
return set_errno(err);
return ret;
}
/*
=item C<int agent_connect_unlocked(Agent *agent, int fd, int events, agent_reaction_t *reaction, void *arg)>
Equivalent to I<agent_connect(3)> except that C<agent> is not write-locked.
=cut
*/
int agent_connect_unlocked(Agent *agent, int fd, int events, agent_reaction_t *reaction, void *arg)
{
/* Check the arguments */
if (!agent || fd < 0 || !reaction || !(events & (R_OK | W_OK | X_OK)) || (events & ~(R_OK | W_OK | X_OK)))
return set_errno(EINVAL);
/* Allocate or extend ids if necessary */
if (!agent->ids)
{
if (!(agent->ids = mem_create(fd + 1, ssize_t)))
return -1;
memset(agent->ids, 0xff, (fd + 1) * sizeof(ssize_t));
agent->ids_size = fd + 1;
}
else if (agent->ids_size <= fd)
{
if (!(agent->ids = mem_resize(&agent->ids, agent->ids_size << 1)))
return -1;
memset(agent->ids + agent->ids_size, 0xff, agent->ids_size * sizeof(ssize_t));
agent->ids_size <<= 1;
}
/* Allocate or extend pollfds, reactions and activity, if necessary */
if (!agent->reactions)
{
#if HAVE_POLL
if (agent->method == POLL)
{
if (!(agent->pollfds = mem_create(POLL_SIZE, struct pollfd)))
return -1;
memset(agent->pollfds, 0, POLL_SIZE * sizeof(struct pollfd));
}
#endif
if (!(agent->reactions = mem_create(POLL_SIZE, reaction_t)))
return -1;
memset(agent->reactions, 0, POLL_SIZE * sizeof(reaction_t));
if (agent->tempo)
{
if (!(agent->activity = mem_create(POLL_SIZE, activity_t)))
return -1;
memset(agent->activity, 0, POLL_SIZE * sizeof(activity_t));
}
agent->size = POLL_SIZE;
}
else if (agent->length == agent->size)
{
#if HAVE_POLL
if (agent->method == POLL)
{
if (!mem_resize(&agent->pollfds, agent->size << 1))
return -1;
memset(agent->pollfds + agent->size, 0, agent->size * sizeof(struct pollfd));
}
#endif
if (!mem_resize(&agent->reactions, agent->size << 1))
return -1;
memset(agent->reactions + agent->size, 0, agent->size * sizeof(reaction_t));
if (agent->tempo)
{
if (!mem_resize(&agent->activity, agent->size << 1))
return -1;
memset(agent->activity + agent->size, 0, agent->size * sizeof(activity_t));
}
agent->size <<= 1;
}
/* Claim a new pollfd structure if not already connected */
if (agent->ids[fd] == -1)
agent->ids[fd] = agent->length++;
/* Fill in the pollfd structure and its reaction */
#if HAVE_POLL
if (agent->method == POLL)
{
agent->pollfds[agent->ids[fd]].fd = fd;
agent->pollfds[agent->ids[fd]].events = 0;
agent->pollfds[agent->ids[fd]].revents = 0;
if (events & R_OK)
agent->pollfds[agent->ids[fd]].events |= POLLIN;
if (events & X_OK)
agent->pollfds[agent->ids[fd]].events |= POLLPRI;
if (events & W_OK)
agent->pollfds[agent->ids[fd]].events |= POLLOUT;
}
else
#endif
{
if (events & R_OK && !agent->readfds)
{
if (!(agent->readfds = mem_new(fd_set)))
return -1;
FD_ZERO(agent->readfds);
}
if (events & X_OK && !agent->exceptfds)
{
if (!(agent->exceptfds = mem_new(fd_set)))
return -1;
FD_ZERO(agent->exceptfds);
}
if (events & W_OK && !agent->writefds)
{
if (!(agent->writefds = mem_new(fd_set)))
return -1;
FD_ZERO(agent->writefds);
}
if (events & R_OK)
FD_SET(fd, agent->readfds);
if (events & X_OK)
FD_SET(fd, agent->exceptfds);
if (events & W_OK)
FD_SET(fd, agent->writefds);
}
agent->reactions[agent->ids[fd]].fd = fd;
agent->reactions[agent->ids[fd]].events = events;
agent->reactions[agent->ids[fd]].reaction = reaction;
agent->reactions[agent->ids[fd]].arg = arg;
return 0;
}
/*
=item C<int agent_disconnect(Agent *agent, int fd)>
Disconnect the file descriptor, C<fd>, from C<agent>. C<agent> will no
longer respond to input/output events that occur on C<fd>. On success,
returns C<0>. On error, returns C<-1> with C<errno> set appropriately.
=cut
*/
int agent_disconnect(Agent *agent, int fd)
{
int ret, err;
if (!agent)
return set_errno(EINVAL);
if ((err = agent_wrlock(agent)))
return set_errno(err);
ret = agent_disconnect_unlocked(agent, fd);
if ((err = agent_unlock(agent)))
return set_errno(err);
return ret;
}
/*
=item C<int agent_disconnect_unlocked(Agent *agent, int fd)>
Equivalent to I<agent_disconnect(3)> except that C<agent> is not
write-locked.
=cut
*/
int agent_disconnect_unlocked(Agent *agent, int fd)
{
ssize_t id, last_id;
int last_fd;
/* Check the arguments */
if (!agent || fd < 0)
return set_errno(EINVAL);
/* Check the agent */
#if HAVE_POLL
if (agent->method == POLL)
{
if (!agent->pollfds)
return set_errno(EINVAL);
}
else
#endif
if (!agent->readfds && !agent->writefds && !agent->exceptfds)
return set_errno(EINVAL);
if (!agent->ids || !agent->reactions || (agent->tempo && !agent->activity) || agent->ids_size <= fd)
return set_errno(EINVAL);
/* Remove the agent from pollfds, reactions, activity and ids */
if ((id = agent->ids[fd]) == -1)
return set_errno(EINVAL);
last_fd = agent->reactions[agent->length - 1].fd;
last_id = agent->ids[last_fd];
#if HAVE_POLL
if (agent->method == POLL)
{
agent->pollfds[id] = agent->pollfds[last_id];
memset(&agent->pollfds[last_id], 0, sizeof(struct pollfd));
}
else
#endif
{
if (agent->readfds)
FD_CLR(fd, agent->readfds);
if (agent->exceptfds)
FD_CLR(fd, agent->exceptfds);
if (agent->writefds)
FD_CLR(fd, agent->writefds);
}
agent->reactions[id] = agent->reactions[last_id];
if (agent->tempo)
agent->activity[id] = agent->activity[last_id];
agent->ids[last_fd] = id;
--agent->length;
memset(&agent->reactions[last_id], 0, sizeof(reaction_t));
if (agent->tempo)
memset(&agent->activity[last_id], 0, sizeof(activity_t));
agent->ids[fd] = -1;
return 0;
}
/*
=item C<int agent_transfer(Agent *agent, int fd, Agent *dst)>
Transfers the connected file descriptor, C<fd>, from C<agent> to C<dst>. The
activity data for C<fd> (i.e. time of last event, velocity, acceleration and
dadt) are transferred as well. Both C<agent> and C<dst> must be agents
created using I<agent_create_measured(3)>. On success, returns C<0>. On
error, returns C<-1> with C<errno> set appropriately. Note this only works
for agents in separate threads. To transfer a file descriptor to another
agent in another process on the same host, use I<agent_send(3)> and
I<agent_recv(3)>. It is not possible to transfer a file descriptor to
another agent on another host.
=cut
*/
int agent_transfer(Agent *agent, int fd, Agent *dst)
{
int ret, err;
if (!agent)
return set_errno(EINVAL);
if ((err = agent_wrlock(agent)))
return set_errno(err);
ret = agent_transfer_unlocked(agent, fd, dst);
if ((err = agent_unlock(agent)))
return set_errno(err);
return ret;
}
/*
=item C<int agent_transfer_unlocked(Agent *agent, int fd, Agent *dst)>
Equivalent to I<agent_transfer(3)> except that C<agent> is not write-locked.
Note that C<dst> is still write-locked.
=cut
*/
int agent_transfer_unlocked(Agent *agent, int fd, Agent *dst)
{
reaction_t reaction;
activity_t activity;
ssize_t id;
/* Check the arguments */
if (!agent || fd < 0 || !dst)
return set_errno(EINVAL);
/* Check agent */
if (!agent->ids || !agent->reactions || !agent->tempo || !agent->activity || agent->ids_size <= fd)
return set_errno(EINVAL);
/* Check dst */
if (!dst->tempo)
return set_errno(EINVAL);
/* Transfer the connection and its activity data */
if ((id = agent->ids[fd]) == -1)
return set_errno(EINVAL);
reaction = agent->reactions[id];
activity = agent->activity[id];
if (agent_connect(dst, reaction.fd, reaction.events, reaction.reaction, reaction.arg) == -1)
return -1;
dst->activity[dst->ids[fd]] = activity;
if (agent_disconnect_unlocked(agent, fd) == -1)
{