Debian Codegraph @

gcc/m2/mc-boot/GRTint.cc

raw
1 /* do not edit automatically generated by mc from RTint. */
2 /* RTint.mod provides users of the COROUTINES library with the.
3
4 Copyright (C) 2009-2025 Free Software Foundation, Inc.
5 Contributed by Gaius Mulley <[email protected]>.
6
7 This file is part of GNU Modula-2.
8
9 GNU Modula-2 is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
13
14 GNU Modula-2 is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 Under Section 7 of GPL version 3, you are granted additional
20 permissions described in the GCC Runtime Library Exception, version
21 3.1, as published by the Free Software Foundation.
22
23 You should have received a copy of the GNU General Public License and
24 a copy of the GCC Runtime Library Exception along with this program;
25 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
26 <http://www.gnu.org/licenses/>. */
27
28 #include "config.h"
29 #include "system.h"
30 #include <stdbool.h>
31 # if !defined (PROC_D)
32 # define PROC_D
33 typedef void (*PROC_t) (void);
34 typedef struct { PROC_t proc; } PROC;
35 # endif
36
37 # if !defined (TRUE)
38 # define TRUE (1==1)
39 # endif
40
41 # if !defined (FALSE)
42 # define FALSE (1==0)
43 # endif
44
45 # include "GStorage.h"
46 # include "Gmcrts.h"
47 #if defined(__cplusplus)
48 # undef NULL
49 # define NULL 0
50 #endif
51 #define _RTint_C
52
53 #include "GRTint.h"
54 # include "GM2RTS.h"
55 # include "GStorage.h"
56 # include "GRTco.h"
57 # include "GCOROUTINES.h"
58 # include "Glibc.h"
59 # include "GAssertion.h"
60 # include "GSelective.h"
61
62 typedef struct RTint_DispatchVector_p RTint_DispatchVector;
63
64 # define Microseconds 1000000
65 # define DebugTime 0
66 # define Debugging false
67 typedef struct RTint__T1_r RTint__T1;
68
69 typedef RTint__T1 *RTint_Vector;
70
71 typedef struct RTint__T2_a RTint__T2;
72
73 typedef enum {RTint_input, RTint_output, RTint_time} RTint_VectorType;
74
75 struct RTint__T1_r {
76 RTint_VectorType type;
77 unsigned int priority;
78 void * arg;
79 RTint_Vector pending;
80 RTint_Vector exists;
81 unsigned int no;
82 int File;
83 Selective_Timeval rel;
84 Selective_Timeval abs_;
85 bool queued;
86 };
87
88 struct RTint__T2_a { RTint_Vector array[(7)-(COROUTINES_UnassignedPriority)+1]; };
89 static unsigned int VecNo;
90 static RTint_Vector Exists;
91 static RTint__T2 Pending;
92 static int lock;
93 static bool initialized;
94
95 /*
96 InitInputVector - returns an interrupt vector which is associated
97 with the file descriptor, fd.
98 */
99
100 extern "C" unsigned int RTint_InitInputVector (int fd, unsigned int pri);
101
102 /*
103 InitOutputVector - returns an interrupt vector which is associated
104 with the file descriptor, fd.
105 */
106
107 extern "C" unsigned int RTint_InitOutputVector (int fd, unsigned int pri);
108
109 /*
110 InitTimeVector - returns an interrupt vector associated with
111 the relative time.
112 */
113
114 extern "C" unsigned int RTint_InitTimeVector (unsigned int micro, unsigned int secs, unsigned int pri);
115
116 /*
117 ReArmTimeVector - reprimes the vector, vec, to deliver an interrupt
118 at the new relative time.
119 */
120
121 extern "C" void RTint_ReArmTimeVector (unsigned int vec, unsigned int micro, unsigned int secs);
122
123 /*
124 GetTimeVector - assigns, micro, and, secs, with the remaining
125 time before this interrupt will expire.
126 This value is only updated when a Listen
127 occurs.
128 */
129
130 extern "C" void RTint_GetTimeVector (unsigned int vec, unsigned int *micro, unsigned int *secs);
131
132 /*
133 AttachVector - adds the pointer ptr to be associated with the interrupt
134 vector. It returns the previous value attached to this
135 vector.
136 */
137
138 extern "C" void * RTint_AttachVector (unsigned int vec, void * ptr);
139
140 /*
141 IncludeVector - includes, vec, into the dispatcher list of
142 possible interrupt causes.
143 */
144
145 extern "C" void RTint_IncludeVector (unsigned int vec);
146
147 /*
148 ExcludeVector - excludes, vec, from the dispatcher list of
149 possible interrupt causes.
150 */
151
152 extern "C" void RTint_ExcludeVector (unsigned int vec);
153
154 /*
155 Listen - will either block indefinitely (until an interrupt)
156 or alteratively will test to see whether any interrupts
157 are pending.
158 If a pending interrupt was found then, call, is called
159 and then this procedure returns.
160 It only listens for interrupts > pri.
161 */
162
163 extern "C" void RTint_Listen (bool untilInterrupt, RTint_DispatchVector call, unsigned int pri);
164
165 /*
166 Init -
167 */
168
169 extern "C" void RTint_Init (void);
170
171 /*
172 Max - returns the maximum: i or j.
173 */
174
175 static int Max (int i, int j);
176 static int Min (int i, int j);
177
178 /*
179 FindVector - searches the exists list for a vector of type
180 which is associated with file descriptor, fd.
181 */
182
183 static RTint_Vector FindVector (int fd, RTint_VectorType type);
184
185 /*
186 FindVectorNo - searches the Exists list for vector vec.
187 */
188
189 static RTint_Vector FindVectorNo (unsigned int vec);
190
191 /*
192 FindPendingVector - searches the pending list for vector, vec.
193 */
194
195 static RTint_Vector FindPendingVector (unsigned int vec);
196
197 /*
198 AddFd - adds the file descriptor fd to set updating max.
199 */
200
201 static void AddFd (Selective_SetOfFd *set, int *max, int fd);
202
203 /*
204 DumpPendingQueue - displays the pending queue.
205 */
206
207 static void DumpPendingQueue (void);
208
209 /*
210 AddTime - t1 := t1 + t2
211 */
212
213 static void AddTime (Selective_Timeval t1, Selective_Timeval t2);
214
215 /*
216 IsGreaterEqual - returns TRUE if, a>=b
217 */
218
219 static bool IsGreaterEqual (Selective_Timeval a, Selective_Timeval b);
220
221 /*
222 SubTime - assigns, s and m, to a - b.
223 */
224
225 static void SubTime (unsigned int *s, unsigned int *m, Selective_Timeval a, Selective_Timeval b);
226
227 /*
228 activatePending - activates the first interrupt pending and clears it.
229 */
230
231 static bool activatePending (bool untilInterrupt, RTint_DispatchVector call, unsigned int pri, int maxFd, Selective_SetOfFd *inSet, Selective_SetOfFd *outSet, Selective_Timeval *timeval, Selective_Timeval b4, Selective_Timeval after);
232
233 /*
234 init -
235 */
236
237 static void init (void);
238
239
240 /*
241 Max - returns the maximum: i or j.
242 */
243
244 static int Max (int i, int j)
245 {
246 if (i > j)
247 {
248 return i;
249 }
250 else
251 {
252 return j;
253 }
254 /* static analysis guarentees a RETURN statement will be used before here. */
255 __builtin_unreachable ();
256 }
257
258 static int Min (int i, int j)
259 {
260 /*
261 Max - returns the minimum: i or j.
262 */
263 if (i < j)
264 {
265 return i;
266 }
267 else
268 {
269 return j;
270 }
271 /* static analysis guarentees a RETURN statement will be used before here. */
272 __builtin_unreachable ();
273 }
274
275
276 /*
277 FindVector - searches the exists list for a vector of type
278 which is associated with file descriptor, fd.
279 */
280
281 static RTint_Vector FindVector (int fd, RTint_VectorType type)
282 {
283 RTint_Vector vec;
284
285 vec = Exists;
286 while (vec != NULL)
287 {
288 if ((vec->type == type) && (vec->File == fd))
289 {
290 return vec;
291 }
292 vec = vec->exists;
293 }
294 return NULL;
295 /* static analysis guarentees a RETURN statement will be used before here. */
296 __builtin_unreachable ();
297 }
298
299
300 /*
301 FindVectorNo - searches the Exists list for vector vec.
302 */
303
304 static RTint_Vector FindVectorNo (unsigned int vec)
305 {
306 RTint_Vector vptr;
307
308 vptr = Exists;
309 while ((vptr != NULL) && (vptr->no != vec))
310 {
311 vptr = vptr->exists;
312 }
313 return vptr;
314 /* static analysis guarentees a RETURN statement will be used before here. */
315 __builtin_unreachable ();
316 }
317
318
319 /*
320 FindPendingVector - searches the pending list for vector, vec.
321 */
322
323 static RTint_Vector FindPendingVector (unsigned int vec)
324 {
325 unsigned int pri;
326 RTint_Vector vptr;
327
328 for (pri=COROUTINES_UnassignedPriority; pri<=7; pri++)
329 {
330 vptr = Pending.array[pri-(COROUTINES_UnassignedPriority)];
331 while ((vptr != NULL) && (vptr->no != vec))
332 {
333 vptr = vptr->pending;
334 }
335 if ((vptr != NULL) && (vptr->no == vec))
336 {
337 return vptr;
338 }
339 }
340 return NULL;
341 /* static analysis guarentees a RETURN statement will be used before here. */
342 __builtin_unreachable ();
343 }
344
345
346 /*
347 AddFd - adds the file descriptor fd to set updating max.
348 */
349
350 static void AddFd (Selective_SetOfFd *set, int *max, int fd)
351 {
352 if (fd < 0)
353 {
354 return;
355 }
356 (*max) = Max (fd, (*max));
357 if ((*set) == NULL)
358 {
359 (*set) = Selective_InitSet ();
360 Selective_FdZero ((*set));
361 }
362 /* printf('%d, ', fd) */
363 Selective_FdSet (fd, (*set));
364 }
365
366
367 /*
368 DumpPendingQueue - displays the pending queue.
369 */
370
371 static void DumpPendingQueue (void)
372 {
373 COROUTINES_PROTECTION pri;
374 RTint_Vector vptr;
375 unsigned int sec;
376 unsigned int micro;
377
378 libc_printf ((const char *) "Pending queue\\n", 15);
379 for (pri=COROUTINES_UnassignedPriority; pri<=7; pri++)
380 {
381 libc_printf ((const char *) "[%d] ", 6, pri);
382 vptr = Pending.array[pri-(COROUTINES_UnassignedPriority)];
383 while (vptr != NULL)
384 {
385 if ((vptr->type == RTint_input) || (vptr->type == RTint_output))
386 {
387 libc_printf ((const char *) "(fd=%d) (vec=%d)", 16, vptr->File, vptr->no);
388 }
389 else if (vptr->type == RTint_time)
390 {
391 /* avoid dangling else. */
392 Selective_GetTime (vptr->rel, &sec, &micro);
393 Assertion_Assert (micro < Microseconds);
394 libc_printf ((const char *) "time (%u.%06u secs) (arg = %p)\\n", 32, sec, micro, vptr->arg);
395 }
396 vptr = vptr->pending;
397 }
398 libc_printf ((const char *) " \\n", 3);
399 }
400 }
401
402
403 /*
404 AddTime - t1 := t1 + t2
405 */
406
407 static void AddTime (Selective_Timeval t1, Selective_Timeval t2)
408 {
409 unsigned int a;
410 unsigned int b;
411 unsigned int s;
412 unsigned int m;
413
414 Selective_GetTime (t1, &s, &m);
415 Assertion_Assert (m < Microseconds);
416 Selective_GetTime (t2, &a, &b);
417 Assertion_Assert (b < Microseconds);
418 a += s;
419 b += m;
420 if (b >= Microseconds)
421 {
422 b -= Microseconds;
423 a += 1;
424 }
425 Selective_SetTime (t1, a, b);
426 }
427
428
429 /*
430 IsGreaterEqual - returns TRUE if, a>=b
431 */
432
433 static bool IsGreaterEqual (Selective_Timeval a, Selective_Timeval b)
434 {
435 unsigned int as;
436 unsigned int am;
437 unsigned int bs;
438 unsigned int bm;
439
440 Selective_GetTime (a, &as, &am);
441 Assertion_Assert (am < Microseconds);
442 Selective_GetTime (b, &bs, &bm);
443 Assertion_Assert (bm < Microseconds);
444 return (as > bs) || ((as == bs) && (am >= bm));
445 /* static analysis guarentees a RETURN statement will be used before here. */
446 __builtin_unreachable ();
447 }
448
449
450 /*
451 SubTime - assigns, s and m, to a - b.
452 */
453
454 static void SubTime (unsigned int *s, unsigned int *m, Selective_Timeval a, Selective_Timeval b)
455 {
456 unsigned int as;
457 unsigned int am;
458 unsigned int bs;
459 unsigned int bm;
460
461 Selective_GetTime (a, &as, &am);
462 Assertion_Assert (am < Microseconds);
463 Selective_GetTime (b, &bs, &bm);
464 Assertion_Assert (bm < Microseconds);
465 if (IsGreaterEqual (a, b))
466 {
467 (*s) = as-bs;
468 if (am >= bm)
469 {
470 (*m) = am-bm;
471 Assertion_Assert ((*m) < Microseconds);
472 }
473 else
474 {
475 Assertion_Assert ((*s) > 0);
476 (*s) -= 1;
477 (*m) = (Microseconds+am)-bm;
478 Assertion_Assert ((*m) < Microseconds);
479 }
480 }
481 else
482 {
483 (*s) = 0;
484 (*m) = 0;
485 }
486 }
487
488
489 /*
490 activatePending - activates the first interrupt pending and clears it.
491 */
492
493 static bool activatePending (bool untilInterrupt, RTint_DispatchVector call, unsigned int pri, int maxFd, Selective_SetOfFd *inSet, Selective_SetOfFd *outSet, Selective_Timeval *timeval, Selective_Timeval b4, Selective_Timeval after)
494 {
495 int result;
496 unsigned int p;
497 RTint_Vector vec;
498 unsigned int b4s;
499 unsigned int b4m;
500 unsigned int afs;
501 unsigned int afm;
502 unsigned int sec;
503 unsigned int micro;
504
505 RTco_wait (lock);
506 p = static_cast<unsigned int> (7);
507 while (p > pri)
508 {
509 vec = Pending.array[p-(COROUTINES_UnassignedPriority)];
510 while (vec != NULL)
511 {
512 switch (vec->type)
513 {
514 case RTint_input:
515 if (((vec->File < maxFd) && ((*inSet) != NULL)) && (Selective_FdIsSet (vec->File, (*inSet))))
516 {
517 if (Debugging)
518 {
519 libc_printf ((const char *) "read (fd=%d) is ready (vec=%d)\\n", 32, vec->File, vec->no);
520 DumpPendingQueue ();
521 }
522 Selective_FdClr (vec->File, (*inSet)); /* so we dont activate this again from our select. */
523 RTco_signal (lock); /* so we dont activate this again from our select. */
524 (*call.proc) (vec->no, vec->priority, vec->arg);
525 return true;
526 }
527 break;
528
529 case RTint_output:
530 if (((vec->File < maxFd) && ((*outSet) != NULL)) && (Selective_FdIsSet (vec->File, (*outSet))))
531 {
532 if (Debugging)
533 {
534 libc_printf ((const char *) "write (fd=%d) is ready (vec=%d)\\n", 33, vec->File, vec->no);
535 DumpPendingQueue ();
536 }
537 Selective_FdClr (vec->File, (*outSet)); /* so we dont activate this again from our select. */
538 RTco_signal (lock); /* so we dont activate this again from our select. */
539 (*call.proc) (vec->no, vec->priority, vec->arg);
540 return true;
541 }
542 break;
543
544 case RTint_time:
545 if (untilInterrupt && ((*timeval) != NULL))
546 {
547 result = Selective_GetTimeOfDay (after);
548 Assertion_Assert (result == 0);
549 if (Debugging)
550 {
551 Selective_GetTime ((*timeval), &sec, &micro);
552 Assertion_Assert (micro < Microseconds);
553 Selective_GetTime (after, &afs, &afm);
554 Assertion_Assert (afm < Microseconds);
555 Selective_GetTime (b4, &b4s, &b4m);
556 Assertion_Assert (b4m < Microseconds);
557 libc_printf ((const char *) "waited %u.%06u + %u.%06u now is %u.%06u\\n", 41, sec, micro, b4s, b4m, afs, afm);
558 }
559 if (IsGreaterEqual (after, vec->abs_))
560 {
561 if (Debugging)
562 {
563 DumpPendingQueue ();
564 libc_printf ((const char *) "time has expired calling dispatcher\\n", 37);
565 }
566 (*timeval) = Selective_KillTime ((*timeval)); /* so we dont activate this again from our select. */
567 RTco_signal (lock); /* so we dont activate this again from our select. */
568 if (Debugging)
569 {
570 libc_printf ((const char *) "call (%d, %d, 0x%x)\\n", 21, vec->no, vec->priority, vec->arg);
571 }
572 (*call.proc) (vec->no, vec->priority, vec->arg);
573 return true;
574 }
575 else if (Debugging)
576 {
577 /* avoid dangling else. */
578 libc_printf ((const char *) "must wait longer as time has not expired\\n", 42);
579 }
580 }
581 break;
582
583
584 default:
585 CaseException ("../../gcc/m2/gm2-libs/RTint.def", 25, 1);
586 __builtin_unreachable ();
587 }
588 vec = vec->pending;
589 }
590 p -= 1;
591 }
592 RTco_signal (lock);
593 return false;
594 /* static analysis guarentees a RETURN statement will be used before here. */
595 __builtin_unreachable ();
596 }
597
598
599 /*
600 init -
601 */
602
603 static void init (void)
604 {
605 COROUTINES_PROTECTION p;
606
607 lock = RTco_initSemaphore (1);
608 RTco_wait (lock);
609 Exists = NULL;
610 for (p=COROUTINES_UnassignedPriority; p<=7; p++)
611 {
612 Pending.array[p-(COROUTINES_UnassignedPriority)] = NULL;
613 }
614 initialized = true;
615 RTco_signal (lock);
616 }
617
618
619 /*
620 InitInputVector - returns an interrupt vector which is associated
621 with the file descriptor, fd.
622 */
623
624 extern "C" unsigned int RTint_InitInputVector (int fd, unsigned int pri)
625 {
626 RTint_Vector vptr;
627
628 if (Debugging)
629 {
630 libc_printf ((const char *) "InitInputVector fd = %d priority = %d\\n", 39, fd, pri);
631 }
632 RTco_wait (lock);
633 vptr = FindVector (fd, RTint_input);
634 if (vptr == NULL)
635 {
636 Storage_ALLOCATE ((void **) &vptr, sizeof (RTint__T1));
637 VecNo += 1;
638 vptr->type = RTint_input;
639 vptr->priority = pri;
640 vptr->arg = NULL;
641 vptr->pending = NULL;
642 vptr->exists = Exists;
643 vptr->no = VecNo;
644 vptr->File = fd;
645 Exists = vptr;
646 RTco_signal (lock);
647 return VecNo;
648 }
649 else
650 {
651 RTco_signal (lock);
652 return vptr->no;
653 }
654 /* static analysis guarentees a RETURN statement will be used before here. */
655 __builtin_unreachable ();
656 }
657
658
659 /*
660 InitOutputVector - returns an interrupt vector which is associated
661 with the file descriptor, fd.
662 */
663
664 extern "C" unsigned int RTint_InitOutputVector (int fd, unsigned int pri)
665 {
666 RTint_Vector vptr;
667
668 RTco_wait (lock);
669 vptr = FindVector (fd, RTint_output);
670 if (vptr == NULL)
671 {
672 Storage_ALLOCATE ((void **) &vptr, sizeof (RTint__T1));
673 if (vptr == NULL)
674 {
675 M2RTS_HALT (-1);
676 __builtin_unreachable ();
677 }
678 else
679 {
680 VecNo += 1;
681 vptr->type = RTint_output;
682 vptr->priority = pri;
683 vptr->arg = NULL;
684 vptr->pending = NULL;
685 vptr->exists = Exists;
686 vptr->no = VecNo;
687 vptr->File = fd;
688 Exists = vptr;
689 RTco_signal (lock);
690 return VecNo;
691 }
692 }
693 else
694 {
695 RTco_signal (lock);
696 return vptr->no;
697 }
698 ReturnException ("../../gcc/m2/gm2-libs/RTint.def", 25, 1);
699 __builtin_unreachable ();
700 }
701
702
703 /*
704 InitTimeVector - returns an interrupt vector associated with
705 the relative time.
706 */
707
708 extern "C" unsigned int RTint_InitTimeVector (unsigned int micro, unsigned int secs, unsigned int pri)
709 {
710 RTint_Vector vptr;
711
712 RTco_wait (lock);
713 Storage_ALLOCATE ((void **) &vptr, sizeof (RTint__T1));
714 if (vptr == NULL)
715 {
716 M2RTS_HALT (-1);
717 __builtin_unreachable ();
718 }
719 else
720 {
721 VecNo += 1;
722 Assertion_Assert (micro < Microseconds);
723 vptr->type = RTint_time;
724 vptr->priority = pri;
725 vptr->arg = NULL;
726 vptr->pending = NULL;
727 vptr->exists = Exists;
728 vptr->no = VecNo;
729 vptr->rel = Selective_InitTime (secs+DebugTime, micro);
730 vptr->abs_ = Selective_InitTime (0, 0);
731 vptr->queued = false;
732 Exists = vptr;
733 }
734 RTco_signal (lock);
735 return VecNo;
736 /* static analysis guarentees a RETURN statement will be used before here. */
737 __builtin_unreachable ();
738 }
739
740
741 /*
742 ReArmTimeVector - reprimes the vector, vec, to deliver an interrupt
743 at the new relative time.
744 */
745
746 extern "C" void RTint_ReArmTimeVector (unsigned int vec, unsigned int micro, unsigned int secs)
747 {
748 RTint_Vector vptr;
749
750 Assertion_Assert (micro < Microseconds);
751 RTco_wait (lock);
752 vptr = FindVectorNo (vec);
753 if (vptr == NULL)
754 {
755 M2RTS_Halt ((const char *) "cannot find vector supplied", 27, (const char *) "../../gcc/m2/gm2-libs/RTint.mod", 31, (const char *) "ReArmTimeVector", 15, 287);
756 }
757 else
758 {
759 Selective_SetTime (vptr->rel, secs+DebugTime, micro);
760 }
761 RTco_signal (lock);
762 }
763
764
765 /*
766 GetTimeVector - assigns, micro, and, secs, with the remaining
767 time before this interrupt will expire.
768 This value is only updated when a Listen
769 occurs.
770 */
771
772 extern "C" void RTint_GetTimeVector (unsigned int vec, unsigned int *micro, unsigned int *secs)
773 {
774 RTint_Vector vptr;
775
776 RTco_wait (lock);
777 vptr = FindVectorNo (vec);
778 if (vptr == NULL)
779 {
780 M2RTS_Halt ((const char *) "cannot find vector supplied", 27, (const char *) "../../gcc/m2/gm2-libs/RTint.mod", 31, (const char *) "GetTimeVector", 13, 313);
781 }
782 else
783 {
784 Selective_GetTime (vptr->rel, secs, micro);
785 Assertion_Assert ((*micro) < Microseconds);
786 }
787 RTco_signal (lock);
788 }
789
790
791 /*
792 AttachVector - adds the pointer ptr to be associated with the interrupt
793 vector. It returns the previous value attached to this
794 vector.
795 */
796
797 extern "C" void * RTint_AttachVector (unsigned int vec, void * ptr)
798 {
799 RTint_Vector vptr;
800 void * prevArg;
801
802 RTco_wait (lock);
803 vptr = FindVectorNo (vec);
804 if (vptr == NULL)
805 {
806 M2RTS_Halt ((const char *) "cannot find vector supplied", 27, (const char *) "../../gcc/m2/gm2-libs/RTint.mod", 31, (const char *) "AttachVector", 12, 340);
807 }
808 else
809 {
810 prevArg = vptr->arg;
811 vptr->arg = ptr;
812 if (Debugging)
813 {
814 libc_printf ((const char *) "AttachVector %d with %p\\n", 25, vec, ptr);
815 DumpPendingQueue ();
816 }
817 RTco_signal (lock);
818 return prevArg;
819 }
820 ReturnException ("../../gcc/m2/gm2-libs/RTint.def", 25, 1);
821 __builtin_unreachable ();
822 }
823
824
825 /*
826 IncludeVector - includes, vec, into the dispatcher list of
827 possible interrupt causes.
828 */
829
830 extern "C" void RTint_IncludeVector (unsigned int vec)
831 {
832 RTint_Vector vptr;
833 unsigned int micro;
834 unsigned int sec;
835 int result;
836
837 RTco_wait (lock);
838 vptr = FindPendingVector (vec);
839 if (vptr == NULL)
840 {
841 /* avoid dangling else. */
842 vptr = FindVectorNo (vec);
843 if (vptr == NULL)
844 {
845 M2RTS_Halt ((const char *) "cannot find vector supplied", 27, (const char *) "../../gcc/m2/gm2-libs/RTint.mod", 31, (const char *) "IncludeVector", 13, 374);
846 }
847 else
848 {
849 /* printf('including vector %d (fd = %d)
850 ', vec, v^.File) ; */
851 vptr->pending = Pending.array[vptr->priority-(COROUTINES_UnassignedPriority)];
852 Pending.array[vptr->priority-(COROUTINES_UnassignedPriority)] = vptr;
853 if ((vptr->type == RTint_time) && ! vptr->queued)
854 {
855 vptr->queued = true;
856 result = Selective_GetTimeOfDay (vptr->abs_);
857 Assertion_Assert (result == 0);
858 Selective_GetTime (vptr->abs_, &sec, &micro);
859 Assertion_Assert (micro < Microseconds);
860 AddTime (vptr->abs_, vptr->rel);
861 Selective_GetTime (vptr->abs_, &sec, &micro);
862 Assertion_Assert (micro < Microseconds);
863 }
864 }
865 }
866 else
867 {
868 if (Debugging)
869 {
870 libc_printf ((const char *) "odd vector (%d) type (%d) arg (%p) is already attached to the pending queue\\n", 77, vec, vptr->type, vptr->arg);
871 }
872 }
873 RTco_signal (lock);
874 }
875
876
877 /*
878 ExcludeVector - excludes, vec, from the dispatcher list of
879 possible interrupt causes.
880 */
881
882 extern "C" void RTint_ExcludeVector (unsigned int vec)
883 {
884 RTint_Vector vptr;
885 RTint_Vector uptr;
886
887 RTco_wait (lock);
888 vptr = FindPendingVector (vec);
889 if (vptr == NULL)
890 {
891 M2RTS_Halt ((const char *) "cannot find pending vector supplied", 35, (const char *) "../../gcc/m2/gm2-libs/RTint.mod", 31, (const char *) "ExcludeVector", 13, 416);
892 }
893 else
894 {
895 /* printf('excluding vector %d
896 ', vec) ; */
897 if (Pending.array[vptr->priority-(COROUTINES_UnassignedPriority)] == vptr)
898 {
899 Pending.array[vptr->priority-(COROUTINES_UnassignedPriority)] = Pending.array[vptr->priority-(COROUTINES_UnassignedPriority)]->pending;
900 }
901 else
902 {
903 uptr = Pending.array[vptr->priority-(COROUTINES_UnassignedPriority)];
904 while (uptr->pending != vptr)
905 {
906 uptr = uptr->pending;
907 }
908 uptr->pending = vptr->pending;
909 }
910 if (vptr->type == RTint_time)
911 {
912 vptr->queued = false;
913 }
914 }
915 RTco_signal (lock);
916 }
917
918
919 /*
920 Listen - will either block indefinitely (until an interrupt)
921 or alteratively will test to see whether any interrupts
922 are pending.
923 If a pending interrupt was found then, call, is called
924 and then this procedure returns.
925 It only listens for interrupts > pri.
926 */
927
928 extern "C" void RTint_Listen (bool untilInterrupt, RTint_DispatchVector call, unsigned int pri)
929 {
930 bool found;
931 int result;
932 Selective_Timeval zero;
933 Selective_Timeval after;
934 Selective_Timeval b4;
935 Selective_Timeval timeval;
936 RTint_Vector vec;
937 Selective_SetOfFd inSet;
938 Selective_SetOfFd outSet;
939 unsigned int sec;
940 unsigned int micro;
941 int maxFd;
942 unsigned int p;
943
944 RTco_wait (lock);
945 if (pri < (7))
946 {
947 if (Debugging)
948 {
949 DumpPendingQueue ();
950 }
951 maxFd = -1;
952 timeval = NULL;
953 inSet = NULL;
954 outSet = NULL;
955 timeval = Selective_InitTime (static_cast<unsigned int> (INT_MAX), 0);
956 p = static_cast<unsigned int> (7);
957 found = false;
958 while (p > pri)
959 {
960 vec = Pending.array[p-(COROUTINES_UnassignedPriority)];
961 while (vec != NULL)
962 {
963 switch (vec->type)
964 {
965 case RTint_input:
966 AddFd (&inSet, &maxFd, vec->File);
967 break;
968
969 case RTint_output:
970 AddFd (&outSet, &maxFd, vec->File);
971 break;
972
973 case RTint_time:
974 if (IsGreaterEqual (timeval, vec->abs_))
975 {
976 Selective_GetTime (vec->abs_, &sec, &micro);
977 Assertion_Assert (micro < Microseconds);
978 if (Debugging)
979 {
980 libc_printf ((const char *) "shortest delay is %u.%06u\\n", 27, sec, micro);
981 }
982 Selective_SetTime (timeval, sec, micro);
983 found = true;
984 }
985 break;
986
987
988 default:
989 CaseException ("../../gcc/m2/gm2-libs/RTint.def", 25, 1);
990 __builtin_unreachable ();
991 }
992 vec = vec->pending;
993 }
994 p -= 1;
995 }
996 if (! untilInterrupt)
997 {
998 Selective_SetTime (timeval, 0, 0);
999 }
1000 if ((untilInterrupt && (((inSet == NULL) && (outSet == NULL)) || (maxFd == -1))) && ! found)
1001 {
1002 M2RTS_Halt ((const char *) "deadlock found, no more processes to run and no interrupts active", 65, (const char *) "../../gcc/m2/gm2-libs/RTint.mod", 31, (const char *) "Listen", 6, 733);
1003 }
1004 /* printf('}
1005 ') ; */
1006 if (! found && (maxFd == -1))
1007 {
1008 /* no file descriptors to be selected upon. */
1009 timeval = Selective_KillTime (timeval);
1010 RTco_signal (lock);
1011 return;
1012 }
1013 else
1014 {
1015 Selective_GetTime (timeval, &sec, &micro);
1016 Assertion_Assert (micro < Microseconds);
1017 zero = Selective_InitTime (0, 0);
1018 b4 = Selective_InitTime (0, 0);
1019 after = Selective_InitTime (0, 0);
1020 result = Selective_GetTimeOfDay (b4);
1021 Assertion_Assert (result == 0);
1022 SubTime (&sec, &micro, timeval, b4);
1023 Selective_SetTime (timeval, sec, micro);
1024 if (Debugging)
1025 {
1026 libc_printf ((const char *) "select waiting for %u.%06u seconds\\n", 36, sec, micro);
1027 }
1028 RTco_signal (lock);
1029 do {
1030 if (Debugging)
1031 {
1032 libc_printf ((const char *) "select (.., .., .., %u.%06u)\\n", 30, sec, micro);
1033 }
1034 if (maxFd < 0)
1035 {
1036 result = RTco_select (0, NULL, NULL, NULL, timeval);
1037 }
1038 else
1039 {
1040 result = RTco_select (maxFd+1, inSet, outSet, NULL, timeval);
1041 }
1042 if (result == -1)
1043 {
1044 if (Debugging)
1045 {
1046 libc_perror ((const char *) "select failed : ", 16);
1047 }
1048 result = RTco_select (maxFd+1, inSet, outSet, NULL, zero);
1049 if (result != -1)
1050 {
1051 Selective_GetTime (timeval, &sec, &micro);
1052 if (Debugging)
1053 {
1054 libc_printf ((const char *) "(nfds : %d timeval: %u.%06u) : \\n", 33, maxFd, sec, micro);
1055 }
1056 libc_perror ((const char *) "select timeout argument was faulty : ", 37);
1057 }
1058 else
1059 {
1060 result = RTco_select (maxFd+1, inSet, NULL, NULL, timeval);
1061 if (result != -1)
1062 {
1063 libc_perror ((const char *) "select output fd argument was faulty : ", 39);
1064 }
1065 else
1066 {
1067 result = RTco_select (maxFd+1, NULL, outSet, NULL, timeval);
1068 if (result != -1)
1069 {
1070 libc_perror ((const char *) "select input fd argument was faulty : ", 38);
1071 }
1072 else
1073 {
1074 if (maxFd == -1)
1075 {
1076 /* avoid dangling else. */
1077 result = RTco_select (0, NULL, NULL, NULL, timeval);
1078 if (result == -1)
1079 {
1080 if (Debugging)
1081 {
1082 libc_perror ((const char *) "select does not accept nfds == 0 ", 33);
1083 }
1084 result = 0;
1085 }
1086 }
1087 else
1088 {
1089 libc_perror ((const char *) "select maxFD+1 argument was faulty : ", 37);
1090 }
1091 }
1092 }
1093 }
1094 }
1095 } while (! (result != -1));
1096 }
1097 while (activatePending (untilInterrupt, call, pri, maxFd+1, &inSet, &outSet, &timeval, b4, after))
1098 {} /* empty. */
1099 if (timeval != NULL)
1100 {
1101 timeval = Selective_KillTime (timeval);
1102 }
1103 if (zero != NULL)
1104 {
1105 zero = Selective_KillTime (zero);
1106 }
1107 if (after != NULL)
1108 {
1109 after = Selective_KillTime (after);
1110 }
1111 if (b4 != NULL)
1112 {
1113 b4 = Selective_KillTime (b4);
1114 }
1115 if (inSet != NULL)
1116 {
1117 inSet = Selective_KillSet (inSet);
1118 }
1119 if (outSet != NULL)
1120 {
1121 outSet = Selective_KillSet (outSet);
1122 }
1123 }
1124 RTco_signal (lock);
1125 }
1126
1127
1128 /*
1129 Init -
1130 */
1131
1132 extern "C" void RTint_Init (void)
1133 {
1134 if (! initialized)
1135 {
1136 init ();
1137 }
1138 }
1139
1140 extern "C" void _M2_RTint_init (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])
1141 {
1142 RTint_Init ();
1143 }
1144
1145 extern "C" void _M2_RTint_fini (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])
1146 {
1147 }