forked from RefPerSys/RefPerSys
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheventloop_rps.cc
More file actions
1266 lines (1189 loc) · 49.4 KB
/
Copy patheventloop_rps.cc
File metadata and controls
1266 lines (1189 loc) · 49.4 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
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
/****************************************************************
* file eventloop_rps.cc
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Description:
* This file is part of the Reflective Persistent System.
*
* It has the event loop and some JSONRPC code to user-interface
* to an external process (using JSONRPC protocol)
*
* Author(s):
* Basile Starynkevitch <basile@starynkevitch.net>
* Abhishek Chakravarti <abhishek@taranjali.org>
* Nimesh Neema <nimeshneema@gmail.com>
*
* © Copyright (C) 2022 - 2025 The Reflective Persistent System Team
* team@refpersys.org & http://refpersys.org/
*
* License:
* 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 3 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 <http://www.gnu.org/licenses/>.
***************************************************************************0***/
#include "refpersys.hh"
extern "C" const char rps_eventloop_gitid[];
const char rps_eventloop_gitid[]= RPS_GITID;
extern "C" const char rps_eventloop_date[];
const char rps_eventloop_date[]= __DATE__;
extern "C" const char rps_eventloop_shortgitid[];
const char rps_eventloop_shortgitid[]= RPS_SHORTGITID;
// default or initial delay to poll(2) in milliseconds.
#define RPS_EVENT_DEFAULT_POLL_DELAY_MILLISEC 1600
enum self_pipe_code_en
{
SelfPipe__NONE=0,
SelfPipe_Dump = 'D',
SelfPipe_GarbColl = 'G',
SelfPipe_Process = 'P',
SelfPipe_Quit = 'Q',
SelfPipe_Exit = 'X',
};
#define RPS_MAXPOLL_FD 128
//extern "C" std::atomic<bool> rps_stop_event_loop_flag;
std::atomic<bool> rps_stop_event_loop_flag;
int rps_poll_delay_millisec;
#define RPS_EVENTLOOPDATA_MAGIC 814538509 /*0x308cdf0d*/
struct event_loop_data_st
{
unsigned eld_magic; // should be RPS_EVENTLOOPDATA_MAGIC
int eld_polldelaymillisec;
unsigned eld_lastix;
std::recursive_mutex eld_mtx;
double eld_startelapsedtime; // start real time of event loop
double eld_startcputime; // start CPU time of event loop
Rps_EventHandler_sigt* eld_handlarr[RPS_MAXPOLL_FD+1];
const char*eld_explarr[RPS_MAXPOLL_FD+1];
struct pollfd eld_pollarr[RPS_MAXPOLL_FD+1];
void* eld_datarr[RPS_MAXPOLL_FD+1];
int eld_sigfd; // file descriptor from signalfd(2)
int eld_timfd; // file descriptor from timerfd_create(2)
int eld_selfpipereadfd; // self pipe, reading end
int eld_selfpipewritefd; // self pipe, writing end
std::deque<unsigned char> eld_selfpipefifo;
std::atomic<bool> eld_eventloopisactive;
std::atomic<long> eld_nbloops;
std::vector<std::function<void(struct pollfd*, int& npoll, Rps_CallFrame*)>> eld_prepollvect;
};
extern "C" struct event_loop_data_st rps_eventloopdata;
struct event_loop_data_st rps_eventloopdata;
/// for debugging purposes, a string explaining the rps_eventloopdata
extern "C" std::string rps_eventloop_explstring(void);
static std::mutex rps_jsonrpc_mtx; /// common mutex for below buffers
#warning TODO: maybe command and response should use std::stringstream?
static std::stringbuf rps_jsonrpc_cmdbuf; /// buffer for commands written to JSONRPC GUI process
static std::stringbuf rps_jsonrpc_rspbuf; /// buffer for responses read from JSONRPC GUI process
static std::stringstream rps_jsonrpc_rspstream; // should be used
#warning use rps_jsonrpc_rspstream below
extern "C" void rps_sigfd_read_handler(Rps_CallFrame*cf, int fd, void* data);
extern "C" void rps_timerfd_read_handler(Rps_CallFrame*cf, int fd, void* data);
extern "C" void rps_fifo_read_handler(Rps_CallFrame*cf, int fd, void* data);
extern "C" void rps_fifo_write_handler(Rps_CallFrame*cf, int fd, void* data);
extern "C" void rps_self_pipe_read_handler(Rps_CallFrame*cf, int fd, void* data);
extern "C" void rps_self_pipe_write_handler(Rps_CallFrame*cf, int fd, void* data);
/**
* We probably want to use the pipe to self trick.
* https://www.sitepoint.com/the-self-pipe-trick-explained/
*
* in cooperation with Rps_PayloadUnixProcess::start_process
**/
static std::atomic<bool> event_loop_is_active;
static std::atomic<long> event_nbloops;
extern "C" void rps_self_pipe_write_byte(unsigned char b);
static void handle_self_pipe_byte_rps(unsigned char b);
void
rps_do_stop_event_loop(void)
{
RPS_DEBUG_LOG(REPL, "rps_do_stop_event_loop thread:"
<< rps_current_pthread_name()
<< RPS_FULL_BACKTRACE_HERE(1, "rps_do_stop_event_loop"));
RPS_ASSERT(rps_eventloopdata.eld_magic == RPS_EVENTLOOPDATA_MAGIC);
rps_stop_event_loop_flag.store(true);
if (rps_fltk_enabled ())
{
rps_fltk_stop();
}
} // end rps_do_stop_event_loop
extern "C" void rps_jsonrpc_initialize(void);
void
rps_self_pipe_write_byte(unsigned char b)
{
RPS_ASSERT(b != (char)0);
std::lock_guard<std::recursive_mutex> gu(rps_eventloopdata.eld_mtx);
RPS_ASSERT(rps_eventloopdata.eld_magic == RPS_EVENTLOOPDATA_MAGIC);
rps_eventloopdata.eld_selfpipefifo.push_back(b);
} // end rps_self_pipe_write_byte
int
rps_register_event_loop_prepoller(std::function<void (struct pollfd*, int npoll, Rps_CallFrame*)> fun)
{
std::lock_guard<std::recursive_mutex> gu(rps_eventloopdata.eld_mtx);
RPS_ASSERT(rps_eventloopdata.eld_magic == RPS_EVENTLOOPDATA_MAGIC);
int ln = (int) rps_eventloopdata.eld_prepollvect.size();
if (ln > 1000)
RPS_FATALOUT("too many event loop prepoller " << ln);
for (int i=0; i<ln; i++)
{
if (!rps_eventloopdata.eld_prepollvect[i])
{
rps_eventloopdata.eld_prepollvect[i] = fun;
return i;
}
}
rps_eventloopdata.eld_prepollvect.push_back(fun);
return rps_eventloopdata.eld_prepollvect.size();
} // end rps_register_event_loop_prepoller
void
rps_unregister_event_loop_prepoller(int rank)
{
std::lock_guard<std::recursive_mutex> gu(rps_eventloopdata.eld_mtx);
RPS_ASSERT(rps_eventloopdata.eld_magic == RPS_EVENTLOOPDATA_MAGIC);
if (rank<0 || rank>(int) rps_eventloopdata.eld_prepollvect.size())
{
RPS_WARNOUT("invalid rank to rps_unregister_event_loop_prepoller " << rank);
return;
};
rps_eventloopdata.eld_prepollvect[rank] = nullptr;
} // end rps_unregister_event_loop_prepoller
bool rps_event_loop_get_entry(int ix,
Rps_EventHandler_sigt**pfun,
struct pollfd*po, const char**pexpl, void**pdata)
{
std::lock_guard<std::recursive_mutex> gu(rps_eventloopdata.eld_mtx);
RPS_ASSERT(rps_eventloopdata.eld_magic == RPS_EVENTLOOPDATA_MAGIC);
if (pfun)
*pfun = nullptr;
if (po)
{
*po = pollfd {};
};
if (pexpl)
*pexpl = nullptr;
if (pdata)
*pdata = nullptr;
if (ix<0 || ix>(int) rps_eventloopdata.eld_lastix)
return false;
if (pfun)
*pfun = rps_eventloopdata.eld_handlarr[ix];
if (po)
*po = rps_eventloopdata.eld_pollarr[ix];
if (pexpl)
*pexpl = rps_eventloopdata.eld_explarr[ix];
if (pdata)
*pdata = rps_eventloopdata.eld_datarr[ix];
return true;
} // end rps_event_loop_get_entry
void
rps_event_loop_add_input_fd_handler (int fd,
Rps_EventHandler_sigt*f,
const char* explanation,
void*data)
{
std::lock_guard<std::recursive_mutex> gu(rps_eventloopdata.eld_mtx);
RPS_ASSERT(rps_eventloopdata.eld_magic == RPS_EVENTLOOPDATA_MAGIC);
unsigned lastfd = rps_eventloopdata.eld_lastix;
RPS_ASSERT(lastfd < RPS_MAXPOLL_FD);
rps_eventloopdata.eld_pollarr[lastfd].fd = fd;
rps_eventloopdata.eld_pollarr[lastfd].events = POLLIN;
rps_eventloopdata.eld_explarr[lastfd] = explanation;
rps_eventloopdata.eld_datarr[lastfd] = data;
rps_eventloopdata.eld_lastix = lastfd+1;
if (rps_fltk_enabled())
{
rps_fltk_add_input_fd(fd, f, explanation, (int)lastfd);
};
RPS_DEBUG_LOG(REPL, "rps_event_loop_add_input_fd_handler fd#" << fd
<< " f@" << (void*)f
<< " expl:" << explanation
<< " data@" << (void*)data);
} // end rps_event_loop_add_input_fd_handler
void
rps_event_loop_add_output_fd_handler (int fd,
Rps_EventHandler_sigt*f,
const char* explanation,
void*data)
{
std::lock_guard<std::recursive_mutex> gu(rps_eventloopdata.eld_mtx);
RPS_ASSERT(rps_eventloopdata.eld_magic == RPS_EVENTLOOPDATA_MAGIC);
unsigned lastfd = rps_eventloopdata.eld_lastix;
RPS_ASSERT(lastfd < RPS_MAXPOLL_FD);
rps_eventloopdata.eld_pollarr[lastfd].fd = fd;
rps_eventloopdata.eld_pollarr[lastfd].events = POLLOUT;
rps_eventloopdata.eld_explarr[lastfd] = explanation;
rps_eventloopdata.eld_datarr[lastfd] = data;
rps_eventloopdata.eld_lastix = lastfd+1;
if (rps_fltk_enabled())
{
rps_fltk_add_output_fd(fd, f, explanation, (int)lastfd);
}
RPS_DEBUG_LOG(REPL, "rps_event_loop_add_output_fd_handler fd#" << fd
<< " f@" << (void*)f
<< " expl:" << explanation
<< " data@" << (void*)data);
} // end rps_event_loop_add_output_fd_handler
void
rps_event_loop_remove_input_fd_handler(int fd)
{
Rps_EventHandler_sigt* new_handlarr[RPS_MAXPOLL_FD+1];
const char*new_explarr[RPS_MAXPOLL_FD+1];
struct pollfd new_pollarr[RPS_MAXPOLL_FD+1];
void* new_datarr[RPS_MAXPOLL_FD+1];
memset (new_handlarr, 0, sizeof(new_handlarr));
memset (new_explarr, 0, sizeof(new_explarr));
memset (new_pollarr, 0, sizeof(new_pollarr));
memset (new_datarr, 0, sizeof(new_datarr));
std::lock_guard<std::recursive_mutex> gu(rps_eventloopdata.eld_mtx);
RPS_ASSERT(rps_eventloopdata.eld_magic == RPS_EVENTLOOPDATA_MAGIC);
unsigned lastix = rps_eventloopdata.eld_lastix;
unsigned newlastix = 0;
for (int ix=0; ix<(int)lastix; ix++)
{
Rps_EventHandler_sigt* curphdlr=nullptr;
struct pollfd curpfd= {};
const char*curexpl=nullptr;
void*curdata = nullptr;
if (rps_event_loop_get_entry(ix, &curphdlr, &curpfd, &curexpl, &curdata))
{
if (curpfd.fd == fd && curpfd.events == POLLIN)
continue;
new_handlarr[newlastix] = curphdlr;
new_pollarr[newlastix] = curpfd;
new_explarr[newlastix] = curexpl;
new_datarr[newlastix] = curdata;
newlastix++;
}
else
continue;
};
// the erasing below is in theory useless...
memset (rps_eventloopdata.eld_handlarr, 0, sizeof(rps_eventloopdata.eld_handlarr));
memset (rps_eventloopdata.eld_explarr, 0, sizeof(rps_eventloopdata.eld_explarr));
memset (rps_eventloopdata.eld_pollarr, 0, sizeof(rps_eventloopdata.eld_pollarr));
memset (rps_eventloopdata.eld_datarr, 0, sizeof(rps_eventloopdata.eld_datarr));
// copy the new things
memcpy (rps_eventloopdata.eld_handlarr, new_handlarr, newlastix*sizeof(new_handlarr[0]));
memcpy (rps_eventloopdata.eld_explarr, new_explarr, newlastix*sizeof(new_explarr[0]));
memcpy (rps_eventloopdata.eld_pollarr, new_pollarr, newlastix*sizeof(new_pollarr[0]));
memcpy (rps_eventloopdata.eld_datarr, new_datarr, newlastix*sizeof(new_datarr[0]));
rps_eventloopdata.eld_lastix = newlastix;
RPS_DEBUG_LOG(REPL, "rps_event_loop_remove_input_fd_handler fd#" << fd);
if (rps_fltk_enabled())
rps_fltk_remove_input_fd(fd);
} // end rps_event_loop_remove_input_fd_handler
void
rps_self_pipe_read_handler(Rps_CallFrame*cf, int fd, void* data)
{
RPS_ASSERT(rps_is_main_thread());
unsigned char buf[128];
RPS_ASSERT(fd == rps_eventloopdata.eld_selfpipereadfd);
RPS_ASSERT(cf != nullptr && cf->is_good_call_frame());
memset(buf, 0, sizeof(buf));
int nbr = read(fd, buf, sizeof(buf));
RPS_DEBUG_LOG(REPL, "rps_self_pipe_read_handler fd#" << fd << " nbr=" << nbr
<< " buf=" << buf << "." << std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "rps_self_pipe_read_handler"));
if (nbr > 0)
{
std::function<void(Rps_GarbageCollector*)> gcmarker
= [&] (Rps_GarbageCollector*gc)
{
RPS_ASSERT(gc->is_valid_garbcoll());
gc->mark_call_stack(cf);
};
for (int i = 0; i<nbr; i++)
{
unsigned char b=buf[i];
switch (b)
{
case SelfPipe_Dump:
rps_dump_into (rps_get_loaddir());
break;
case SelfPipe_GarbColl:
rps_garbage_collect(&gcmarker);
break;
case SelfPipe_Quit:
rps_stop_event_loop_flag.store(true);
break;
case SelfPipe_Exit:
rps_dump_into (rps_get_loaddir());
rps_stop_event_loop_flag.store(true);
break;
case SelfPipe_Process:
#warning should call something from transientobj_rps.cc to perhaps fork a process related to some Rps_PayloadUnixProcess
/* TODO: see Rps_PayloadUnixProcess::queue_of_runnable_processes
in transientobj_rps.cc; the dormant processes should somehow
be started by fork/exec */
default:
RPS_FATALOUT("unexpected byte " << (char)b << "#" << (unsigned)b << " on self pipe");
};
};
}
} // end rps_self_pipe_read_handler
void
rps_self_pipe_write_handler(Rps_CallFrame*cf, int fd, void* data)
{
RPS_ASSERT(rps_is_main_thread());
RPS_DEBUG_LOG(REPL, "rps_self_pipe_write_handler fd#" << fd
<< RPS_FULL_BACKTRACE_HERE(1, "rps_self_pipe_write_handler"));
std::lock_guard<std::recursive_mutex> gu(rps_eventloopdata.eld_mtx);
RPS_ASSERT(rps_eventloopdata.eld_magic == RPS_EVENTLOOPDATA_MAGIC);
RPS_ASSERT(fd == rps_eventloopdata.eld_selfpipewritefd);
while (!rps_eventloopdata.eld_selfpipefifo.empty())
{
char buf[4] = {0,0,0,0};
char c = rps_eventloopdata.eld_selfpipefifo.back();
buf[0] = c;
errno = 0;
ssize_t nw = write(fd, buf, 1);
if (nw>0)
{
RPS_DEBUG_LOG(REPL, "rps_self_pipe_write_handler wrote " << buf << " to fd#" << fd);
rps_eventloopdata.eld_selfpipefifo.pop_back();
}
else
{
RPS_DEBUG_LOG(REPL, "rps_self_pipe_write_handler write-fail fd#" << fd << ":" << strerror(errno));
break;
}
}
} // end rps_self_pipe_write_handler
void
rps_event_loop_remove_output_fd_handler(int fd)
{
std::lock_guard<std::recursive_mutex> gu(rps_eventloopdata.eld_mtx);
RPS_ASSERT(rps_eventloopdata.eld_magic == RPS_EVENTLOOPDATA_MAGIC);
unsigned lastfd = rps_eventloopdata.eld_lastix;
Rps_EventHandler_sigt* new_handlarr[RPS_MAXPOLL_FD+1];
const char*new_explarr[RPS_MAXPOLL_FD+1];
struct pollfd new_pollarr[RPS_MAXPOLL_FD+1];
void* new_datarr[RPS_MAXPOLL_FD+1];
memset (new_handlarr, 0, sizeof(new_handlarr));
memset (new_explarr, 0, sizeof(new_explarr));
memset (new_pollarr, 0, sizeof(new_pollarr));
memset (new_datarr, 0, sizeof(new_datarr));
unsigned new_lastfd=0;
unsigned lastix = rps_eventloopdata.eld_lastix;
unsigned newlastix = 0;
for (int ix=0; ix<(int)lastix; ix++)
{
Rps_EventHandler_sigt* curphdlr=nullptr;
struct pollfd curpfd= {0};
const char*curexpl=nullptr;
void*curdata = nullptr;
if (rps_event_loop_get_entry(ix, &curphdlr, &curpfd, &curexpl, &curdata))
{
if (curpfd.fd == fd && curpfd.events == POLLOUT)
continue;
new_handlarr[newlastix] = curphdlr;
new_pollarr[newlastix] = curpfd;
new_explarr[newlastix] = curexpl;
new_datarr[newlastix] = curdata;
newlastix++;
}
else
continue;
};
// the erasing below is in theory useless...
memset (rps_eventloopdata.eld_handlarr, 0,
sizeof(rps_eventloopdata.eld_handlarr));
memset (rps_eventloopdata.eld_explarr, 0,
sizeof(rps_eventloopdata.eld_explarr));
memset (rps_eventloopdata.eld_pollarr, 0,
sizeof(rps_eventloopdata.eld_pollarr));
memset (rps_eventloopdata.eld_datarr, 0,
sizeof(rps_eventloopdata.eld_datarr));
// copy the new things
memcpy (rps_eventloopdata.eld_handlarr, new_handlarr,
newlastix*sizeof(new_handlarr[0]));
memcpy (rps_eventloopdata.eld_explarr, new_explarr,
newlastix*sizeof(new_explarr[0]));
memcpy (rps_eventloopdata.eld_pollarr, new_pollarr,
newlastix*sizeof(new_pollarr[0]));
memcpy (rps_eventloopdata.eld_datarr, new_datarr,
newlastix*sizeof(new_datarr[0]));
rps_eventloopdata.eld_lastix = newlastix;
RPS_DEBUG_LOG(REPL, "rps_event_loop_remove_output_fd_handler fd#" << fd);
if (rps_fltk_enabled())
rps_fltk_remove_output_fd(fd);
} // end rps_event_loop_remove_output_fd_handler
bool
rps_is_fifo(std::string path)
{
struct stat s = {};
if (!stat(path.c_str(), &s))
return (s.st_mode & S_IFMT) == S_IFIFO;
return false;
} // end rps_is_fifo
void
rps_initialize_pipe_to_self_in_event_loop(void)
{
std::lock_guard<std::recursive_mutex> gu(rps_eventloopdata.eld_mtx);
RPS_ASSERT(rps_eventloopdata.eld_magic == RPS_EVENTLOOPDATA_MAGIC);
/**
* create the pipe to self and install handlers for it
**/
{
int pipefdarr[2] = {-1, -1};
if (pipe2(pipefdarr, O_CLOEXEC) <0)
RPS_FATALOUT("rps_initialize_event_loop failed to create pipe to self:"
<< strerror(errno));
rps_eventloopdata.eld_selfpipereadfd = pipefdarr[0];
RPS_ASSERT(rps_eventloopdata.eld_selfpipereadfd > 0);
rps_eventloopdata.eld_selfpipewritefd = pipefdarr[1];
RPS_ASSERT(rps_eventloopdata.eld_selfpipewritefd > 0);
rps_event_loop_add_input_fd_handler(rps_eventloopdata.eld_selfpipereadfd,
rps_self_pipe_read_handler,
"selfpiperead",
nullptr);;
rps_event_loop_add_input_fd_handler(rps_eventloopdata.eld_selfpipewritefd,
rps_self_pipe_write_handler,
"selfpipewrite",
nullptr);
RPS_DEBUG_LOG(REPL, "rps_initialize_pipe_to_self_in_event_loop eld_selfpipereadfd#"
<< (rps_eventloopdata.eld_selfpipereadfd)
<< " eld_selfpipewritefd#"
<< (rps_eventloopdata.eld_selfpipewritefd)
<< " rps_poll_delay_millisec=" << rps_poll_delay_millisec
<< " in thread " << rps_current_pthread_name()
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "rps_initialize_pipe_to_self_event_loop"));
}
} // end rps_initialize_pipe_to_self_in_event_loop
void
rps_initialize_signalfd_in_event_loop(void)
{
std::lock_guard<std::recursive_mutex> gu(rps_eventloopdata.eld_mtx);
RPS_ASSERT(rps_eventloopdata.eld_magic == RPS_EVENTLOOPDATA_MAGIC);
sigset_t msk= {};
sigemptyset(&msk);
sigaddset(&msk, SIGCHLD);
sigaddset(&msk, SIGINT);
sigaddset(&msk, SIGTERM);
sigaddset(&msk, SIGXCPU);
sigaddset(&msk, SIGALRM);
sigaddset(&msk, SIGVTALRM);
rps_eventloopdata.eld_sigfd = signalfd(-1, &msk, SFD_CLOEXEC);
if (rps_eventloopdata.eld_sigfd<=0)
RPS_FATALOUT("failed to call signalfd:" << strerror(errno));
RPS_DEBUG_LOG(REPL, "rps_initialize_signalfd_in_event_loop thread "
<< rps_current_pthread_name()
<< " sigfd#" << rps_eventloopdata.eld_sigfd
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "rps_initialize_signalfd_in_event_loop")
);
rps_event_loop_add_input_fd_handler(rps_eventloopdata.eld_sigfd, rps_sigfd_read_handler,
"signalfd", nullptr);
} // end rps_initialize_signalfd_in_event_loop
void
rps_initialize_timerfd_in_event_loop(void)
{
std::lock_guard<std::recursive_mutex> gu(rps_eventloopdata.eld_mtx);
RPS_ASSERT(rps_eventloopdata.eld_magic == RPS_EVENTLOOPDATA_MAGIC);
rps_eventloopdata.eld_timfd = timerfd_create(CLOCK_REALTIME, TFD_CLOEXEC);
if (rps_eventloopdata.eld_timfd<=0)
RPS_FATALOUT("failed to call timerfd:" << strerror(errno));
RPS_DEBUG_LOG(REPL, "rps_initialize_timerfd_in_event_loop thread "
<< rps_current_pthread_name()
<< " timerfd#" << rps_eventloopdata.eld_timfd
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "rps_initialize_timerfd_in_event_loop")
);
rps_event_loop_add_input_fd_handler(rps_eventloopdata.eld_sigfd, rps_timerfd_read_handler,
"timerfd", nullptr);
} // end rps_initialize_timerfd_in_event_loop
void
rps_initialize_jsonfifo_in_event_loop(void)
{
struct rps_fifo_fdpair_st fdp = rps_get_gui_fifo_fds();
if (fdp.fifo_ui_wcmd <= 0)
RPS_FATALOUT("invalid command FIFO fd " << fdp.fifo_ui_wcmd
<< " with FIFO prefix " << rps_get_fifo_prefix());
if (fdp.fifo_ui_rout <= 0)
RPS_FATALOUT("invalid output FIFO fd " << fdp.fifo_ui_rout
<< " with FIFO prefix " << rps_get_fifo_prefix());
} // end rps_initialize_jsonfifo_in_event_loop
/////////// the toplevel event loop initialization
void
rps_initialize_event_loop(void)
{
/// rps_initialize_event_loop should be called once, early, from the
/// main thread. It is called after the persistent heap load.
std::lock_guard<std::recursive_mutex> gu(rps_eventloopdata.eld_mtx);
static int count;
if (!rps_is_main_thread())
RPS_FATALOUT("rps_initialize_event_loop should be called only from the main thread");
if (count++ > 0)
RPS_FATALOUT("rps_initialize_event_loop should be called once");
rps_eventloopdata.eld_magic = RPS_EVENTLOOPDATA_MAGIC;
rps_eventloopdata.eld_polldelaymillisec = (rps_debug_flags != 0)?666:111;
rps_eventloopdata.eld_lastix = 0;
rps_eventloopdata.eld_startelapsedtime = -1.0/1024;
rps_eventloopdata.eld_startcputime = -1.0/1024;
memset (rps_eventloopdata.eld_handlarr, 0,
sizeof(rps_eventloopdata.eld_handlarr));
memset (rps_eventloopdata.eld_explarr, 0,
sizeof(rps_eventloopdata.eld_explarr));
memset (rps_eventloopdata.eld_pollarr, 0,
sizeof(rps_eventloopdata.eld_pollarr));
memset (rps_eventloopdata.eld_datarr, 0,
sizeof(rps_eventloopdata.eld_datarr));
rps_eventloopdata.eld_sigfd = -1;
rps_eventloopdata.eld_timfd = -1;
rps_eventloopdata.eld_selfpipereadfd = -1;
rps_eventloopdata.eld_selfpipewritefd = -1;
rps_eventloopdata.eld_selfpipefifo.clear();
rps_eventloopdata.eld_eventloopisactive = false;
rps_eventloopdata.eld_nbloops = 0;
rps_eventloopdata.eld_prepollvect.clear();
///
RPS_DEBUG_LOG(REPL, "rps_initialize_event_loop starting "
<< (rps_fltk_enabled()?"with FLTK":"without-fltk")
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "rps_initialize_event_loop*start"));
rps_initialize_pipe_to_self_in_event_loop();
rps_initialize_signalfd_in_event_loop();
rps_initialize_timerfd_in_event_loop();
if (!rps_get_fifo_prefix().empty())
rps_initialize_jsonfifo_in_event_loop();
if (rps_poll_delay_millisec==0)
rps_poll_delay_millisec = RPS_EVENT_DEFAULT_POLL_DELAY_MILLISEC;
RPS_DEBUG_LOG(REPL, "rps_initialize_event_loop ended "
<< (rps_fltk_enabled()?"with FLTK":"without-fltk")
<< " polldelay=" << rps_poll_delay_millisec << " ms"
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "rps_initialize_event_loop*ended"));
} // end rps_initialize_event_loop
/**
Function rps_jsonrpc_initialize is called once from main, when
rps_fifo_prefix is not empty. The FIFOs have been created in
main_rps.cc...
***/
void
rps_jsonrpc_initialize(void)
{
RPS_ASSERT(!rps_get_fifo_prefix().empty());
struct rps_fifo_fdpair_st fdp = rps_get_gui_fifo_fds();
if (fdp.fifo_ui_wcmd <= 0)
RPS_FATALOUT("invalid command FIFO fd " << fdp.fifo_ui_wcmd
<< " with FIFO prefix " << rps_get_fifo_prefix());
if (fdp.fifo_ui_rout <= 0)
RPS_FATALOUT("invalid output FIFO fd " << fdp.fifo_ui_rout
<< " with FIFO prefix " << rps_get_fifo_prefix());
RPS_DEBUG_LOG(REPL, "rps_jsonrpc_initialize FIFO prefix "
<< rps_get_fifo_prefix()
<< " wcmdfd#" << fdp.fifo_ui_wcmd
<< " routfd#" << fdp.fifo_ui_rout
<< RPS_FULL_BACKTRACE_HERE(1, "rps_jsonrpc_initialize")
<< std::endl << " in thread " << rps_current_pthread_name());
#warning maybe incomplete rps_jsonrpc_initialize
/**
* TODO: we need to document the JSONRPC protocol between the GUI
* software and RefPerSys
*
* A needed request is the _VERSION one....
*
**/
RPS_DEBUG_LOG(REPL, "ending rps_jsonrpc_initialize with fifo prefix "
<< rps_get_fifo_prefix() //
<< " and wcmd.fd#" << fdp.fifo_ui_wcmd //
<< " and rout.fd#" << fdp.fifo_ui_rout
<< " thread:" << rps_current_pthread_name());
} // end rps_jsonrpc_initialize
std::string
rps_eventloop_explstring(void)
{
std::lock_guard<std::recursive_mutex> gu(rps_eventloopdata.eld_mtx);
char numbuf[32];
memset(numbuf, 0, sizeof(numbuf));
RPS_ASSERT(rps_eventloopdata.eld_magic == RPS_EVENTLOOPDATA_MAGIC);
std::ostringstream out;
double elapsed_age = rps_elapsed_real_time() - rps_eventloopdata.eld_startelapsedtime;
double cpu_age = rps_process_cpu_time() - rps_eventloopdata.eld_startcputime;
snprintf(numbuf, sizeof(numbuf), "%.2f", elapsed_age);
out << numbuf << " elap";
snprintf(numbuf, sizeof(numbuf), "%.3f", cpu_age);
out << "," << numbuf << " cpu time";
#warning rps_eventloop_explstring very incomplete
out << std::flush;
return out.str();
} // end rps_eventloop_explstring
////////////////////////////////////////////////////////////////
/* TODO: an event loop using poll(2) and also handling SIGCHLD using
https://man7.org/linux/man-pages/man2/signalfd.2.html
*/
void
rps_event_loop(void)
{
/// see https://man7.org/linux/man-pages/man2/poll.2.html
int nbfdpoll = 0; /// number of polled file descriptors, second argument to poll(2)
long pollcount = 0;
double startelapsedtime = rps_elapsed_real_time();
double startcputime = rps_process_cpu_time();
RPS_DEBUG_LOG(REPL, "rps_event_loop starting elapsedtime=" << startelapsedtime
<< " cputime=" << startcputime
<< " thread:" << rps_current_pthread_name() << std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "rps_event_loop/start"));
std::array<std::function<void(Rps_CallFrame*, int/*fd*/, short /*revents*/)>,RPS_MAXPOLL_FD+1> handlarr;
///
/// check that rps_event_loop is called exactly once from main
/// thread, and after rps_initialize_event_loop...
{
static int nbcall;
if (!rps_is_main_thread())
RPS_FATALOUT("rps_event_loop should be called only once, and from the main thread");
if (nbcall++>0)
RPS_FATALOUT("rps_event_loop has already been called " << nbcall << " times");
/// The rps_poll_delay_millisec should have been set by a prior call
/// ... to rps_initialize_event_loop above.
if (rps_poll_delay_millisec<=0)
RPS_FATALOUT("the poll event loop has not being properly initialized");
};
if (rps_fltk_enabled ())
RPS_FATALOUT("rps_event_loop incompatible with FLTK");
RPS_LOCALFRAME(RPS_CALL_FRAME_UNDESCRIBED,
/*callerframe:*/rps_curthread_callframe, //
/** locals **/
Rps_Value valarr[RPS_MAXPOLL_FD+1];
Rps_ClosureValue closarr[RPS_MAXPOLL_FD+1];
);
#warning incomplete rps_event_loop
const char*explarr[RPS_MAXPOLL_FD+1];
memset (explarr, 0, sizeof(explarr));
#warning TODO: consider using rps_timer ...?
/*** give output
***/
RPS_INFORMOUT("starting rps_event_loop in pid " << (long)getpid() << std::endl
<< "… on " << rps_hostname() << " thread " << rps_current_pthread_name()
<< " git " << rps_shortgitid << std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "rps_event_loop")
);
while (!rps_stop_event_loop_flag.load())
{
char elapsbuf[32];
memset(elapsbuf, 0, sizeof(elapsbuf));
struct pollfd pollarr[RPS_MAXPOLL_FD+1];
memset ((void*)&pollarr, 0, sizeof(pollarr));
#define EXPLAIN_EVFD_AT(Fil,Lin,Ix,Expl) do { explarr[Ix] = Fil ":" #Lin " " Expl; } while(0)
#define EXPLAIN_EVFD_ATBIS(Fil,Lin,Ix,Expl) EXPLAIN_EVFD_AT(Fil,Lin,Ix,Expl)
#define EXPLAIN_EVFD_RPS(Ix,Expl) EXPLAIN_EVFD_ATBIS(__FILE__,__LINE__,(Ix),Expl)
int loopcnt=1+ event_nbloops.fetch_add(1);
if ((loopcnt-1) % 16 == 0)
{
RPS_DEBUG_LOG(REPL, "looping rps_event_loop #" << loopcnt
<< " elapsed:" << rps_elapsed_real_time()
<< " thread:" << rps_current_pthread_name()
<< ((rps_fltk_enabled())?" with FLTK": " without-fltk")
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "rps_event_loop/looping"));
}
else
RPS_DEBUG_LOG(REPL, "looping rps_event_loop #" << loopcnt
<< " elapsed:" << rps_elapsed_real_time());
memset ((void*)&pollarr, 0, sizeof(pollarr));
nbfdpoll=0;
struct rps_fifo_fdpair_st fdp = rps_get_gui_fifo_fds();
if (fdp.fifo_ui_wcmd >0)
{
/// JSONRPC commands written from RefPerSys to the GUI process...
RPS_ASSERT(nbfdpoll<RPS_MAXPOLL_FD);
int pix = nbfdpoll++;
pollarr[pix].fd = fdp.fifo_ui_wcmd;
pollarr[pix].events = POLLOUT;
EXPLAIN_EVFD_RPS(pix, "JsonRpc commands to GUI");
handlarr[pix] = [&](Rps_CallFrame* cf, int fd, short rev)
{
RPS_ASSERT(fd == pollarr[pix].fd);
RPS_ASSERT(rev == POLLOUT);
RPS_ASSERT(cf != nullptr && cf->is_good_call_frame());
RPS_DEBUG_LOG(REPL, "should write JSONRPC to GUI on cmdfd#" << fd);
RPS_FATALOUT("missing code to handle JSONRPC output commands to fd#"
<< fd << " pix#" << pix);
/* TODO: write the bytes that are in rps_jsonrpc_cmdbuf */
#warning missing code to handle JsonRpc output to the GUI process
};
};
if (fdp.fifo_ui_rout>0)
{
/// JSONRPC responses/events sent by the GUI process to RefPerSys
RPS_ASSERT(nbfdpoll<RPS_MAXPOLL_FD);
int pix = nbfdpoll++;
pollarr[pix].fd = fdp.fifo_ui_rout;
pollarr[pix].events = POLLIN;
EXPLAIN_EVFD_RPS(pix, "JsonRpc responses from GUI");
handlarr[pix] = [&](Rps_CallFrame* cf, int fd, short rev)
{
constexpr unsigned bufusefulen = 1024;
char buf[bufusefulen+8];
RPS_ASSERT(fd == pollarr[pix].fd);
RPS_ASSERT(rev == POLLIN);
RPS_ASSERT(cf != nullptr && cf->is_good_call_frame());
/* TODO: should read(2) */
memset(buf, 0, sizeof(buf));
RPS_DEBUG_LOG(REPL, "reading JSONRPC from GUI on outfd#" << fd);
errno = 0;
int nbr = read(fd, buf, bufusefulen);
if (nbr < 0)
return;
if (nbr == 0)
{
RPS_FATALOUT("missing code to handle JSONRPC input EOF from fd#"
<< fd << " pix#" << pix);
#warning missing code to handle JsonRpc EOF from GUI process
};
buf[nbr] = (char)0;
RPS_DEBUG_LOG(REPL, "got " << nbr << " bytes from JSONRPC fd#" << fd << std::endl
<< buf << std::endl);
/* TODO: append the bytes we did read to
rps_jsonrpc_rspbuf; by convention a double newline or a
formfeed is ending the JSON message. */
{
std::lock_guard<std::mutex> gu(rps_jsonrpc_mtx);
#warning oldbufsiz need a code review
std::size_t oldbufsiz= rps_jsonrpc_rspbuf.in_avail();
std::size_t oldprevix = (oldbufsiz>0)?(oldbufsiz-1):0;
auto newsiz = rps_jsonrpc_rspbuf.sputn(buf, nbr);
bool again = false;
char* curp = buf;
do
{
again = false;
/// Find a double newline or formfeed at index. If there
/// is one (or more) it is terminating a message...
/* Invariant: there cannot be any double newline of
formfeed before, since it would have been already
processed. */
{
char*ffbuf = strchr(curp, '\f');
char*nl2buf = strstr(curp, "\n\n");
char*eombuf = nullptr;
if (ffbuf && nl2buf)
eombuf = (ffbuf>nl2buf)?ffbuf:nl2buf;
else if (ffbuf) eombuf = ffbuf;
else if (nl2buf) eombuf = nl2buf+1;
if (eombuf)
{
int eombufoff = eombuf - buf;
again = true;
RPS_FATALOUT("missing code JSONRPC input eombufoff:" << eombufoff);
#warning should build a string with the JSON message, then decode and process that JSON
}
}
}
while (again);
/// oldprevix
#warning missing code to handle JsonRpc input from the GUI process
}
RPS_FATALOUT("missing code to handle JSONRPC input responses from fd#"
<< fd << " pix#" << pix
<< " did read " << nbr << " bytes");
};
};
if (rps_eventloopdata.eld_timfd>0)
{
/// timers transformed to data with timerfd_create(2)
RPS_ASSERT(nbfdpoll<RPS_MAXPOLL_FD);
int pix = nbfdpoll++;
pollarr[pix].fd = rps_eventloopdata.eld_timfd;
pollarr[pix].events = POLLIN;
EXPLAIN_EVFD_RPS(pix, "timerfd");
handlarr[pix] = [&](Rps_CallFrame*cf, int fd, short rev)
{
RPS_ASSERT(fd == pollarr[pix].fd);
RPS_ASSERT(cf != nullptr && cf->is_good_call_frame());
RPS_ASSERT(rev == POLLIN);
uint64_t timbuf[16];
memset (&timbuf, 0, sizeof(timbuf));
int nbr = read(fd, (void*)&timbuf, sizeof(timbuf));
if (nbr>0)
{
RPS_DEBUG_LOG(REPL, "eventloop read " << nbr << " bytes on timerfd, so " << (nbr/sizeof(std::uint64_t)) << " int64s: "
<< Rps_Do_Output([=](std::ostream& out)
{
for (int ix=0; ix< (int) (nbr/sizeof(std::uint64_t)); ix++)
out << ' ' << timbuf[ix];
}
)
<< std::flush);
}
#warning some missing code to handle timerfd...
};
};
if (rps_eventloopdata.eld_selfpipereadfd>0)
{
RPS_ASSERT(nbfdpoll<RPS_MAXPOLL_FD);
int pix = nbfdpoll++;
pollarr[pix].fd = rps_eventloopdata.eld_selfpipereadfd;
pollarr[pix].events = POLLIN;
EXPLAIN_EVFD_RPS(pix, "self_pipe_read_fd");
handlarr[pix] = [&](Rps_CallFrame* cf, int fd, short rev)
{
unsigned char buf[128];
RPS_ASSERT(fd == rps_eventloopdata.eld_selfpipereadfd);
RPS_ASSERT(cf != nullptr && cf->is_good_call_frame());
RPS_ASSERT(rev == POLLIN);
/* TODO: should read(2) */
memset(buf, 0, sizeof(buf));
int nbr = read(fd, buf, sizeof(buf));
if (nbr > 0)
for (int i = 0; i<nbr; i++)
handle_self_pipe_byte_rps(buf[i]);
};
};
bool wantselfwrite = false;
{
std::lock_guard<std::recursive_mutex> gu(rps_eventloopdata.eld_mtx);
wantselfwrite = !rps_eventloopdata.eld_selfpipefifo.empty();
}
if (rps_eventloopdata.eld_selfpipewritefd>0 && wantselfwrite)
{
RPS_ASSERT(nbfdpoll<RPS_MAXPOLL_FD);
int pix = nbfdpoll++;
pollarr[pix].fd = rps_eventloopdata.eld_selfpipewritefd;
pollarr[pix].events = POLLOUT;
EXPLAIN_EVFD_RPS(pix, "self_pipe_write_fd");
handlarr[pix] = [&](Rps_CallFrame* cf, int fd, short rev)
{
RPS_ASSERT(fd == rps_eventloopdata.eld_selfpipewritefd);
RPS_ASSERT(rev == POLLOUT);
RPS_ASSERT(cf != nullptr && cf->is_good_call_frame());
std::lock_guard<std::recursive_mutex> gu(rps_eventloopdata.eld_mtx);
while (!rps_eventloopdata.eld_selfpipefifo.empty())
{
unsigned char b = rps_eventloopdata.eld_selfpipefifo.back();
if (write(fd, &b, 1) > 0)
{
RPS_DEBUG_LOG(REPL, "eventloop wrote self pipe " << b
<< " to fd#" << fd);
rps_eventloopdata.eld_selfpipefifo.pop_back();
}
else
{
RPS_DEBUG_LOG(REPL, "eventloop failed to write self pipe " << b
<< " to fd#" << fd << ":" << strerror(errno));
break;
}
};
};
};
bool debugpoll = RPS_DEBUG_ENABLED(REPL) //
|| RPS_DEBUG_ENABLED(EVENT_LOOP) //
|| RPS_DEBUG_ENABLED(GUI);
if (debugpoll)
{
char pidbuf[16];
memset(pidbuf, 0, sizeof(pidbuf));
if (event_nbloops.load() % 10)
snprintf(pidbuf, sizeof(pidbuf)-1, " pid %d", (int)getpid());
for (int pix=0; pix<nbfdpoll; pix++)
{
std::string evstr;
if (pollarr[pix].events & POLLIN)
evstr += " POLLIN";
if (pollarr[pix].events & POLLOUT)
evstr += " POLLOUT";
if (pollarr[pix].events & POLLPRI)
evstr += " POLLPRI";
if (pollarr[pix].events & POLLRDHUP)
evstr += " POLLRDHUP";
if (pollarr[pix].events & POLLERR)
evstr += " POLLERR";
if (pollarr[pix].events & POLLHUP)
evstr += " POLLHUP";
if (pollarr[pix].events & POLLNVAL)
evstr += " POLLNVAL";
rps_debug_printf_at(__FILE__,__LINE__,__FUNCTION__,RPS_DEBUG__EVERYTHING,
"poll[%d] loop%ld:fd#%d:%s,%s%s\n",
pix, event_nbloops.load(), pollarr[pix].fd,
explarr[pix], evstr.c_str(), pidbuf);
}
};
errno = 0;
if (Rps_Agenda::agenda_timeout > 0
&& rps_elapsed_real_time() >= Rps_Agenda::agenda_timeout)
{
RPS_INFORMOUT("stopping agenda mechanism because of agenda timeoutafter "
<< pollcount << " polling." << std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "rps_event_loop/timeout"));
rps_stop_agenda_mechanism();
break;
};
fflush(nullptr);
// call the registered event prepoller
{
std::lock_guard<std::recursive_mutex> gu(rps_eventloopdata.eld_mtx);
for (auto fun : rps_eventloopdata.eld_prepollvect)
{
if (fun)
fun(pollarr, nbfdpoll, &_);
}
}
errno = 0;
int respoll = poll(pollarr, nbfdpoll, 20 + (rps_poll_delay_millisec*(debugpoll?4:1)));
pollcount++;
if (pollcount %2 && debugpoll)
snprintf(elapsbuf, sizeof(elapsbuf), " elti: %.3fs", rps_elapsed_real_time());
RPS_DEBUG_LOG(REPL, "rps_event_loop pollcount#" << pollcount ///
<< " respoll=" << respoll
<< " nbfdpoll=" << nbfdpoll
<< " elapsed time:" << rps_elapsed_real_time());
if (respoll>0)
{
if (debugpoll)
rps_debug_printf_at(__FILE__,__LINE__,__FUNCTION__,RPS_DEBUG__EVERYTHING,
"respoll=%d loop%ld%s\n", respoll, event_nbloops.load(), elapsbuf);
int nbrev=0;
for (int pix=0; pix<nbfdpoll; pix++)
{