Check-in [1e3df2a217]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Corrected a problem in removing items from the delayed event queue where expired events were not taken into account.
Timelines: family | ancestors | trunk
Files: files | file ages | folders
SHA1:1e3df2a2175bd64870b5520fe07f1543f4652c37
User & Date: andrewm 2017-05-09 23:30:14
Context
2017-05-09
23:30
Corrected a problem in removing items from the delayed event queue where expired events were not taken into account. Leaf check-in: 1e3df2a217 user: andrewm tags: trunk
2016-11-06
20:09
Build of pycca and tack for macosx. check-in: 15694b418d user: andrewm tags: trunk
Changes

Changes to mechs/code/cortex-m3/mechs.c.

1
2
3
4
5
6
7
8
9
10
...
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
/*
 * THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT IT.
 * This file corresponds to Version 1.7 of the STSA literate
 * program.
 */
/*
 * This software is copyrighted 2007 - 2014 by G. Andrew
 * Mangogna.  The following terms apply to all files associated
 * with the software unless explicitly disclaimed in individual
 * files.
................................................................................
    eventQueueInsert(ecb, iter) ;
    /*
     * Since we have stored a reference to the ECB we do
     * the bookkeeping.
     */
    mechEventIncrRef(ecb) ;
}

static void
removeFromDelayedQueue(
    MechEcb ecb)
{
    /*
     * If we are not at the end of the queue, all the delay
     * from the removed entry is accumulated on the next
     * entry in the queue.
     */

    if (ecb->next != eventQueueEnd(&delayedEventQueue)) {
        ecb->next->delay += ecb->delay ;
    }
    /*
     * Remove the ECB from the delayed queue.
     */
    eventQueueRemove(ecb) ;
    /*
     * Return the ECB back to the pool.
     */
    mechEventDelete(ecb) ;
}
#define MECH_DELAY_EXPIRED  UINT32_MAX
static MechEcb
expireDelayedEvents(void)
{
    /*
     * Iterate along the delayed event queue.
     */
    for (MechEcb iter = eventQueueBegin(&delayedEventQueue) ;


|







 







>









>
|











<







1
2
3
4
5
6
7
8
9
10
...
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
/*
 * THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT IT.
 * This file corresponds to Version 1.8 of the STSA literate
 * program.
 */
/*
 * This software is copyrighted 2007 - 2014 by G. Andrew
 * Mangogna.  The following terms apply to all files associated
 * with the software unless explicitly disclaimed in individual
 * files.
................................................................................
    eventQueueInsert(ecb, iter) ;
    /*
     * Since we have stored a reference to the ECB we do
     * the bookkeeping.
     */
    mechEventIncrRef(ecb) ;
}
#define MECH_DELAY_EXPIRED  UINT32_MAX
static void
removeFromDelayedQueue(
    MechEcb ecb)
{
    /*
     * If we are not at the end of the queue, all the delay
     * from the removed entry is accumulated on the next
     * entry in the queue.
     */
    if (!(ecb->delay == MECH_DELAY_EXPIRED ||
            ecb->next == eventQueueEnd(&delayedEventQueue))) {
        ecb->next->delay += ecb->delay ;
    }
    /*
     * Remove the ECB from the delayed queue.
     */
    eventQueueRemove(ecb) ;
    /*
     * Return the ECB back to the pool.
     */
    mechEventDelete(ecb) ;
}

static MechEcb
expireDelayedEvents(void)
{
    /*
     * Iterate along the delayed event queue.
     */
    for (MechEcb iter = eventQueueBegin(&delayedEventQueue) ;

Changes to mechs/code/cortex-m3/mechs.h.

1
2
3
4
5
6
7
8
9
10
/*
 * THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT IT.
 * This file corresponds to Version 1.7 of the STSA literate
 * program.
 */
/*
 * This software is copyrighted 2007 - 2014 by G. Andrew
 * Mangogna.  The following terms apply to all files associated
 * with the software unless explicitly disclaimed in individual
 * files.


|







1
2
3
4
5
6
7
8
9
10
/*
 * THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT IT.
 * This file corresponds to Version 1.8 of the STSA literate
 * program.
 */
/*
 * This software is copyrighted 2007 - 2014 by G. Andrew
 * Mangogna.  The following terms apply to all files associated
 * with the software unless explicitly disclaimed in individual
 * files.

Changes to mechs/code/msp430/mechs.c.

1
2
3
4
5
6
7
8
9
10
...
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
/*
 * THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT IT.
 * This file corresponds to Version 1.7 of the STSA literate
 * program.
 */
/*
 * This software is copyrighted 2007 - 2014 by G. Andrew
 * Mangogna.  The following terms apply to all files associated
 * with the software unless explicitly disclaimed in individual
 * files.
................................................................................
    eventQueueInsert(ecb, iter) ;
    /*
     * Since we have stored a reference to the ECB we do
     * the bookkeeping.
     */
    mechEventIncrRef(ecb) ;
}

static void
removeFromDelayedQueue(
    MechEcb ecb)
{
    /*
     * If we are not at the end of the queue, all the delay
     * from the removed entry is accumulated on the next
     * entry in the queue.
     */

    if (ecb->next != eventQueueEnd(&delayedEventQueue)) {
        ecb->next->delay += ecb->delay ;
    }
    /*
     * Remove the ECB from the delayed queue.
     */
    eventQueueRemove(ecb) ;
    /*
     * Return the ECB back to the pool.
     */
    mechEventDelete(ecb) ;
}
#define MECH_DELAY_EXPIRED  UINT32_MAX
static MechEcb
expireDelayedEvents(void)
{
    /*
     * Iterate along the delayed event queue.
     */
    for (MechEcb iter = eventQueueBegin(&delayedEventQueue) ;


|







 







>









>
|











<







1
2
3
4
5
6
7
8
9
10
...
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
/*
 * THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT IT.
 * This file corresponds to Version 1.8 of the STSA literate
 * program.
 */
/*
 * This software is copyrighted 2007 - 2014 by G. Andrew
 * Mangogna.  The following terms apply to all files associated
 * with the software unless explicitly disclaimed in individual
 * files.
................................................................................
    eventQueueInsert(ecb, iter) ;
    /*
     * Since we have stored a reference to the ECB we do
     * the bookkeeping.
     */
    mechEventIncrRef(ecb) ;
}
#define MECH_DELAY_EXPIRED  UINT32_MAX
static void
removeFromDelayedQueue(
    MechEcb ecb)
{
    /*
     * If we are not at the end of the queue, all the delay
     * from the removed entry is accumulated on the next
     * entry in the queue.
     */
    if (!(ecb->delay == MECH_DELAY_EXPIRED ||
            ecb->next == eventQueueEnd(&delayedEventQueue))) {
        ecb->next->delay += ecb->delay ;
    }
    /*
     * Remove the ECB from the delayed queue.
     */
    eventQueueRemove(ecb) ;
    /*
     * Return the ECB back to the pool.
     */
    mechEventDelete(ecb) ;
}

static MechEcb
expireDelayedEvents(void)
{
    /*
     * Iterate along the delayed event queue.
     */
    for (MechEcb iter = eventQueueBegin(&delayedEventQueue) ;

Changes to mechs/code/msp430/mechs.h.

1
2
3
4
5
6
7
8
9
10
/*
 * THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT IT.
 * This file corresponds to Version 1.7 of the STSA literate
 * program.
 */
/*
 * This software is copyrighted 2007 - 2014 by G. Andrew
 * Mangogna.  The following terms apply to all files associated
 * with the software unless explicitly disclaimed in individual
 * files.


|







1
2
3
4
5
6
7
8
9
10
/*
 * THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT IT.
 * This file corresponds to Version 1.8 of the STSA literate
 * program.
 */
/*
 * This software is copyrighted 2007 - 2014 by G. Andrew
 * Mangogna.  The following terms apply to all files associated
 * with the software unless explicitly disclaimed in individual
 * files.

Changes to mechs/code/msp430/platform.c.

1
2
3
4
5
6
7
8
9
10
/*
 * THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT IT.
 * This file corresponds to Version 1.7 of the STSA literate
 * program.
 */
/*
 * This software is copyrighted 2007 - 2014 by G. Andrew
 * Mangogna.  The following terms apply to all files associated
 * with the software unless explicitly disclaimed in individual
 * files.


|







1
2
3
4
5
6
7
8
9
10
/*
 * THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT IT.
 * This file corresponds to Version 1.8 of the STSA literate
 * program.
 */
/*
 * This software is copyrighted 2007 - 2014 by G. Andrew
 * Mangogna.  The following terms apply to all files associated
 * with the software unless explicitly disclaimed in individual
 * files.

Changes to mechs/code/posix/mechs.c.

1
2
3
4
5
6
7
8
9
10
...
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
/*
 * THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT IT.
 * This file corresponds to Version 1.7 of the STSA literate
 * program.
 */
/*
 * This software is copyrighted 2007 - 2014 by G. Andrew
 * Mangogna.  The following terms apply to all files associated
 * with the software unless explicitly disclaimed in individual
 * files.
................................................................................
    eventQueueInsert(ecb, iter) ;
    /*
     * Since we have stored a reference to the ECB we do
     * the bookkeeping.
     */
    mechEventIncrRef(ecb) ;
}

static void
removeFromDelayedQueue(
    MechEcb ecb)
{
    /*
     * If we are not at the end of the queue, all the delay
     * from the removed entry is accumulated on the next
     * entry in the queue.
     */

    if (ecb->next != eventQueueEnd(&delayedEventQueue)) {
        ecb->next->delay += ecb->delay ;
    }
    /*
     * Remove the ECB from the delayed queue.
     */
    eventQueueRemove(ecb) ;
    /*
     * Return the ECB back to the pool.
     */
    mechEventDelete(ecb) ;
}
#define MECH_DELAY_EXPIRED  UINT32_MAX
static MechEcb
expireDelayedEvents(void)
{
    /*
     * Iterate along the delayed event queue.
     */
    for (MechEcb iter = eventQueueBegin(&delayedEventQueue) ;


|







 







>









>
|











<







1
2
3
4
5
6
7
8
9
10
...
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
/*
 * THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT IT.
 * This file corresponds to Version 1.8 of the STSA literate
 * program.
 */
/*
 * This software is copyrighted 2007 - 2014 by G. Andrew
 * Mangogna.  The following terms apply to all files associated
 * with the software unless explicitly disclaimed in individual
 * files.
................................................................................
    eventQueueInsert(ecb, iter) ;
    /*
     * Since we have stored a reference to the ECB we do
     * the bookkeeping.
     */
    mechEventIncrRef(ecb) ;
}
#define MECH_DELAY_EXPIRED  UINT32_MAX
static void
removeFromDelayedQueue(
    MechEcb ecb)
{
    /*
     * If we are not at the end of the queue, all the delay
     * from the removed entry is accumulated on the next
     * entry in the queue.
     */
    if (!(ecb->delay == MECH_DELAY_EXPIRED ||
            ecb->next == eventQueueEnd(&delayedEventQueue))) {
        ecb->next->delay += ecb->delay ;
    }
    /*
     * Remove the ECB from the delayed queue.
     */
    eventQueueRemove(ecb) ;
    /*
     * Return the ECB back to the pool.
     */
    mechEventDelete(ecb) ;
}

static MechEcb
expireDelayedEvents(void)
{
    /*
     * Iterate along the delayed event queue.
     */
    for (MechEcb iter = eventQueueBegin(&delayedEventQueue) ;

Changes to mechs/code/posix/mechs.h.

1
2
3
4
5
6
7
8
9
10
/*
 * THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT IT.
 * This file corresponds to Version 1.7 of the STSA literate
 * program.
 */
/*
 * This software is copyrighted 2007 - 2014 by G. Andrew
 * Mangogna.  The following terms apply to all files associated
 * with the software unless explicitly disclaimed in individual
 * files.


|







1
2
3
4
5
6
7
8
9
10
/*
 * THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT IT.
 * This file corresponds to Version 1.8 of the STSA literate
 * program.
 */
/*
 * This software is copyrighted 2007 - 2014 by G. Andrew
 * Mangogna.  The following terms apply to all files associated
 * with the software unless explicitly disclaimed in individual
 * files.

Changes to mechs/doc/mechs.pdf.

cannot compute difference between binary files

Changes to mechs/src/mechs.nw.

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
....
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
....
2053
2054
2055
2056
2057
2058
2059





























2060
2061
2062
2063
2064
2065
2066
....
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
\usepackage{noweb}
\usepackage{float}
\usepackage{fixltx2e}
\title{A Single Threaded Software Architecture for Embedded Systems
}
\author{G. Andrew Mangogna}
\date{\today\\
Version 1.7}
\pagestyle{headings}
\begin{document}
\bibliographystyle{plain}
\maketitle
\begin{copyright}
Copyright 2009 - 2014, by G. Andrew Mangogna.
Permission to copy and distribute this article by any means is hereby granted
by the copyright holder provided the work is distributed in its entirety and
this notice appears on all copies.
\end{copyright}
\begin{abstract}
This paper is a literate program for a set of software architecture mechanisms
that constitute the run time support for a single threaded software
................................................................................
    eventQueueInsert(ecb, iter) ;
    /*
     * Since we have stored a reference to the ECB we do
     * the bookkeeping.
     */
    mechEventIncrRef(ecb) ;
}
@
Removing an entry from the queue is much simpler.
The only job here is to account for the time that the entry would
have consumed had it been left in the queue.
That time must be added to its next neighbor (if there is one).
<<delayed event helper>>=
static void
removeFromDelayedQueue(
    MechEcb ecb)
{
    /*
     * If we are not at the end of the queue, all the delay
     * from the removed entry is accumulated on the next
     * entry in the queue.
     */
    if (ecb->next != eventQueueEnd(&delayedEventQueue)) {
        ecb->next->delay += ecb->delay ;
    }
    /*
     * Remove the ECB from the delayed queue.
     */
    eventQueueRemove(ecb) ;
    /*
     * Return the ECB back to the pool.
     */
    mechEventDelete(ecb) ;
}
@
When the timer expires there are several ways to deal with the events
that need to be dispatched.
One temptation would be to simply sync to the background and let code
run there to remove expired events from the delayed event queue
and restart the timer.
Unfortunately, that would introduce considerable jitter into the timing.
................................................................................
This design chooses to mark the events in the delayed event queue as
expired and that provides a means of knowing which events in the delayed
queue are active.
Since a delay value of 0 is meaningful, we use a delay value of
the maximum for a delay time as the expired marker.
<<delayed event helper>>=
#define MECH_DELAY_EXPIRED  UINT32_MAX





























@
The [[expireDelayedEvents]] function
is run to traverse the delayed event queue and
mark any events that have a delay value of 0 as expired.
It is used both in the delayed event timer service as well as
in the background processing of the delayed event queue.
The return value is pointer to an ECB.
................................................................................
the platform and compiler.

It is convenient to keep track of the version of the literate program
document in the source code.
<<version info>>=
/*
 * THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT IT.
 * This file corresponds to Version 1.7 of the STSA literate
 * program.
 */
@

\subsection {POSIX Version}
The posix version of the header file is named \texttt{mechs-posix-gcc.h}.
We are only supporting \texttt{gcc} as a compiler.







|





|







 







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|







2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
....
2010
2011
2012
2013
2014
2015
2016



























2017
2018
2019
2020
2021
2022
2023
....
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
....
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
\usepackage{noweb}
\usepackage{float}
\usepackage{fixltx2e}
\title{A Single Threaded Software Architecture for Embedded Systems
}
\author{G. Andrew Mangogna}
\date{\today\\
Version 1.8}
\pagestyle{headings}
\begin{document}
\bibliographystyle{plain}
\maketitle
\begin{copyright}
Copyright 2009 - 2017, by G. Andrew Mangogna.
Permission to copy and distribute this article by any means is hereby granted
by the copyright holder provided the work is distributed in its entirety and
this notice appears on all copies.
\end{copyright}
\begin{abstract}
This paper is a literate program for a set of software architecture mechanisms
that constitute the run time support for a single threaded software
................................................................................
    eventQueueInsert(ecb, iter) ;
    /*
     * Since we have stored a reference to the ECB we do
     * the bookkeeping.
     */
    mechEventIncrRef(ecb) ;
}



























@
When the timer expires there are several ways to deal with the events
that need to be dispatched.
One temptation would be to simply sync to the background and let code
run there to remove expired events from the delayed event queue
and restart the timer.
Unfortunately, that would introduce considerable jitter into the timing.
................................................................................
This design chooses to mark the events in the delayed event queue as
expired and that provides a means of knowing which events in the delayed
queue are active.
Since a delay value of 0 is meaningful, we use a delay value of
the maximum for a delay time as the expired marker.
<<delayed event helper>>=
#define MECH_DELAY_EXPIRED  UINT32_MAX
@
Removing an entry from the queue is much simpler.
The only job here is to account for the time that the entry would
have consumed had it been left in the queue.
That time must be added to its next neighbor (if there is one).
Note that we must also be careful to ignore any expired events.
<<delayed event helper>>=
static void
removeFromDelayedQueue(
    MechEcb ecb)
{
    /*
     * If we are not at the end of the queue, all the delay
     * from the removed entry is accumulated on the next
     * entry in the queue.
     */
    if (!(ecb->delay == MECH_DELAY_EXPIRED ||
            ecb->next == eventQueueEnd(&delayedEventQueue))) {
        ecb->next->delay += ecb->delay ;
    }
    /*
     * Remove the ECB from the delayed queue.
     */
    eventQueueRemove(ecb) ;
    /*
     * Return the ECB back to the pool.
     */
    mechEventDelete(ecb) ;
}
@
The [[expireDelayedEvents]] function
is run to traverse the delayed event queue and
mark any events that have a delay value of 0 as expired.
It is used both in the delayed event timer service as well as
in the background processing of the delayed event queue.
The return value is pointer to an ECB.
................................................................................
the platform and compiler.

It is convenient to keep track of the version of the literate program
document in the source code.
<<version info>>=
/*
 * THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT IT.
 * This file corresponds to Version 1.8 of the STSA literate
 * program.
 */
@

\subsection {POSIX Version}
The posix version of the header file is named \texttt{mechs-posix-gcc.h}.
We are only supporting \texttt{gcc} as a compiler.

Changes to mechs/tests/mechstest/build/cortex-m3/Makefile.

47
48
49
50
51
52
53
54
55
56
57


58
59
60
61
62
63
64
..
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
	core_cm3.c\
	log.c\
	coreclock.c\
	$(NULL)
OBJS	= $(patsubst %.c,%.o,$(SRCS))
INCLS	= $(patsubst %,-I%,$(VPATH))

CC = arm-stellaris-eabi-gcc
CPPFLAGS =\
	-DMECH_TEST\
	-DCM3_USE_SYSTICK\


	-I$(CURDIR)\
	$(INCLS)\
	$(NULL)
CFLAGS	+=\
	-std=c99\
	-O3\
	-g3\
................................................................................
	-mthumb\
	-T lm3s811-rom-hosted.ld\
	$(NULL)

all : mechstest 

mechstest : $(OBJS)
	$(CC) $(LDFLAGS) -o mechstest $(OBJS)

mechstest.o : mechstest.c mechs.h td.h
td.o : td.c td.h
mechs.o : mechs.c mechs.h core_cm3.h
log.o : log.c log.h
core_cm3.o : core_cm3.c core_cm3.h
mechs.h : core_cm3.h







|



>
>







 







|







47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
..
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
	core_cm3.c\
	log.c\
	coreclock.c\
	$(NULL)
OBJS	= $(patsubst %.c,%.o,$(SRCS))
INCLS	= $(patsubst %,-I%,$(VPATH))

CC = arm-none-eabi-gcc
CPPFLAGS =\
	-DMECH_TEST\
	-DCM3_USE_SYSTICK\
	-mcpu=cortex-m3\
	-march=armv7-m\
	-I$(CURDIR)\
	$(INCLS)\
	$(NULL)
CFLAGS	+=\
	-std=c99\
	-O3\
	-g3\
................................................................................
	-mthumb\
	-T lm3s811-rom-hosted.ld\
	$(NULL)

all : mechstest 

mechstest : $(OBJS)
	-$(CC) $(LDFLAGS) -o mechstest $(OBJS)

mechstest.o : mechstest.c mechs.h td.h
td.o : td.c td.h
mechs.o : mechs.c mechs.h core_cm3.h
log.o : log.c log.h
core_cm3.o : core_cm3.c core_cm3.h
mechs.h : core_cm3.h

Changes to mechs/tests/mechstest/build/posix/mechs.c.gcov.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
...
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582

        -:    0:Source:../../../../code/posix/mechs.c
        -:    0:Graph:mechs.gcno
        -:    0:Data:mechs.gcda
        -:    0:Runs:1
        -:    0:Programs:1
        -:    1:/*
        -:    2: * THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT IT.
        -:    3: * This file corresponds to Version 1.7 of the STSA literate
        -:    4: * program.
        -:    5: */
        -:    6:/*
        -:    7: * This software is copyrighted 2007 - 2014 by G. Andrew
        -:    8: * Mangogna.  The following terms apply to all files associated
        -:    9: * with the software unless explicitly disclaimed in individual
        -:   10: * files.
................................................................................
        9:  534:    eventQueueInsert(ecb, iter) ;
        -:  535:    /*
        -:  536:     * Since we have stored a reference to the ECB we do
        -:  537:     * the bookkeeping.
        -:  538:     */
        9:  539:    mechEventIncrRef(ecb) ;
        9:  540:}
        -:  541:static void
        3:  542:removeFromDelayedQueue(
        -:  543:    MechEcb ecb)
        -:  544:{
        -:  545:    /*
        -:  546:     * If we are not at the end of the queue, all the delay
        -:  547:     * from the removed entry is accumulated on the next
        -:  548:     * entry in the queue.
        -:  549:     */
        3:  550:    if (ecb->next != eventQueueEnd(&delayedEventQueue)) {
    #####:  551:        ecb->next->delay += ecb->delay ;
        -:  552:    }
        -:  553:    /*
        -:  554:     * Remove the ECB from the delayed queue.
        -:  555:     */
        3:  556:    eventQueueRemove(ecb) ;
        -:  557:    /*
        -:  558:     * Return the ECB back to the pool.
        -:  559:     */
        3:  560:    mechEventDelete(ecb) ;
        3:  561:}
        -:  562:#define MECH_DELAY_EXPIRED  UINT32_MAX
        -:  563:static MechEcb
        7:  564:expireDelayedEvents(void)
        -:  565:{
        -:  566:    /*
        -:  567:     * Iterate along the delayed event queue.
        -:  568:     */
       21:  569:    for (MechEcb iter = eventQueueBegin(&delayedEventQueue) ;
       14:  570:            iter != eventQueueEnd(&delayedEventQueue) ;
        7:  571:            iter = iter->next) {
        9:  572:        if (iter->delay == 0) {
        -:  573:            /*
        -:  574:             * Mark all the events that have zero delay time
        -:  575:             * as expired.
        -:  576:             */
        6:  577:            iter->delay = MECH_DELAY_EXPIRED ;
        3:  578:        } else if (iter->delay != MECH_DELAY_EXPIRED) {
        -:  579:            /*
        -:  580:             * Stop at the first non-zero delay time.  This
        -:  581:             * marks the boundary of events that need
        -:  582:             * additional delay time.  The first such event
        -:  583:             * is the next amount of time to delay.
        -:  584:             */
        2:  585:            return iter ;
        -:  586:        }
        -:  587:        /*
        -:  588:         * else ... Skip any events that might already be
        -:  589:         * expired.
        -:  590:         */
        -:  591:    }
        -:  592:    /*
        -:  593:     * We have run the queue without finding an unexpired
        -:  594:     * event.
        -:  595:     */
        5:  596:    return NULL ;
        -:  597:}
        -:  598:static void
       13:  599:transferExpiredEvents(void)
        -:  600:{
        -:  601:    /*
        -:  602:     * Iterate through the delayed event queue looking for
        -:  603:     * those entries that have been marked as expired.
        -:  604:     */
       32:  605:    for (MechEcb iter = eventQueueBegin(&delayedEventQueue) ;
       33:  606:            iter != eventQueueEnd(&delayedEventQueue) &&
       14:  607:            iter->delay == MECH_DELAY_EXPIRED ; ) {
        -:  608:        /*
        -:  609:         * Advance the iterator, because we are about to
        -:  610:         * invalidate it by removing the entry from the
        -:  611:         * queue.
        -:  612:         */
        6:  613:        MechEcb ecb = iter ;
        6:  614:        iter = iter->next ;
        -:  615:
        -:  616:        /*
        -:  617:         * Remove the ECB from the delayed queue and insert
        -:  618:         * it into event queue for dispatch.
        -:  619:         */
        6:  620:        eventQueueRemove(ecb) ;
        6:  621:        eventQueueInsert(ecb, &eventQueue) ;
        6:  622:        assert(ecb->referenceCount != 0) ;
        -:  623:    }
       13:  624:}
        -:  625:static void
       13:  626:startDelayedQueueTiming(void)
        -:  627:{
       13:  628:    if (!eventQueueEmpty(&delayedEventQueue)) {
       10:  629:        MechEcb ecb = eventQueueBegin(&delayedEventQueue) ;
       10:  630:        assert(ecb->delay != 0) ;
       10:  631:        sysTimerStart(ecb->delay) ;
       10:  632:        ecb->delay = 0 ;
        -:  633:    }
       13:  634:}
        -:  635:static void
       13:  636:stopDelayedQueueTiming(void)
        -:  637:{
        -:  638:    /*
        -:  639:     * Avoid the whole thing if there is nothing in the
        -:  640:     * delayed event queue.
        -:  641:     */
       13:  642:    if (!eventQueueEmpty(&delayedEventQueue)) {
        -:  643:        /*
        -:  644:         * Stop the timer, obtaining the residual time.
        -:  645:         */
        7:  646:        MechDelayTime remain = sysTimerStop() ;
        -:  647:        /*
        -:  648:         * There are two cases here. It is possible for the
        -:  649:         * remaining time returned from sysTimerStop() to be
        -:  650:         * zero. This can happen if the physical timing
        -:  651:         * resource (which might be running asynchronously
        -:  652:         * to the processor) happens to expire within a
        -:  653:         * single tick as we are stopping it.
        -:  654:         */
        7:  655:        if (remain == 0) {
        -:  656:            /*
        -:  657:             * Since the timer has expired we must mark any
        -:  658:             * events with a zero delay time value as
        -:  659:             * expired and, since we are running in the
        -:  660:             * background here, transfer the expired events
        -:  661:             * to be dispatched.
        -:  662:             */
        1:  663:            expireDelayedEvents() ;
        1:  664:            transferExpiredEvents() ;
        -:  665:            /*
        -:  666:             * At this point, either the delayed event queue
        -:  667:             * is empty, or the event at the head of the
        -:  668:             * queue has a non-zero delay time.
        -:  669:             */
        -:  670:        } else {
        -:  671:            /*
        -:  672:             * It is possible that the timing resource
        -:  673:             * expired and its interrupt service ran just
        -:  674:             * before we could get the timer stopped. That
        -:  675:             * would mean that there are expired events on
        -:  676:             * the delayed queue at this point and we need
        -:  677:             * to transfer them off the delayed queue to be
        -:  678:             * dispatched.
        -:  679:             */
        6:  680:            transferExpiredEvents() ;
        -:  681:            /*
        -:  682:             * If any events expired, the delayed event
        -:  683:             * queue might now be empty. However, if the
        -:  684:             * queue is not empty, we must make sure the
        -:  685:             * entry at the head preserves the remaining
        -:  686:             * amount of time that needs to elapse.
        -:  687:             */
        6:  688:            if (!eventQueueEmpty(&delayedEventQueue)) {
        6:  689:                MechEcb ecb = eventQueueBegin(&delayedEventQueue) ;
        6:  690:                assert(ecb->delay == 0) ;
        6:  691:                ecb->delay = remain ;
        -:  692:            }
        -:  693:        }
        -:  694:    }
       13:  695:}
        -:  696:static inline
        -:  697:MechDelayTime
        9:  698:mechMsecToTicks(
        -:  699:    MechDelayTime msec)
        -:  700:{
        9:  701:    return msec ;
        -:  702:}
        -:  703:static inline
        -:  704:MechDelayTime
        1:  705:mechTicksToMsec(
        -:  706:    MechDelayTime ticks)
        -:  707:{
        1:  708:    return ticks ;
        -:  709:}
        -:  710:void
        9:  711:mechEventPostDelay(
        -:  712:    MechEcb ecb,
        -:  713:    MechDelayTime time)
        -:  714:{
        9:  715:    assert(ecb != NULL) ;
        -:  716:    /*
        -:  717:     * A delay time of 0 is valid, and the event will be
        -:  718:     * queued immediately.
        -:  719:     */
        9:  720:    if (time == 0) {
    #####:  721:        mechEventPost(ecb) ;
        9:  722:        return ;
        -:  723:    }
        9:  724:    ecb->delay = mechMsecToTicks(time) ;
        -:  725:    /*
        -:  726:     * Stop the timing queue so we may examine it.
        -:  727:     */
        9:  728:    stopDelayedQueueTiming() ;
        -:  729:    /*
        -:  730:     * If the event already exists, remove it.
        -:  731:     */
        9:  732:    MechEcb prevEvent = findEvent(&delayedEventQueue,
        -:  733:            ecb->srcInst, ecb->instOrClass.targetInst,
        9:  734:            ecb->eventNumber) ;
        9:  735:    if (prevEvent) {
        1:  736:        removeFromDelayedQueue(prevEvent) ;
        -:  737:    }
        -:  738:    /*
        -:  739:     * Insert the new event.
        -:  740:     */
        9:  741:    insertIntoDelayedQueue(ecb) ;
        9:  742:    assert(!eventQueueEmpty(&delayedEventQueue)) ;
        -:  743:    /*
        -:  744:     * Start the timer to expire for the first event
        -:  745:     * on the queue.
        -:  746:     */
        9:  747:    startDelayedQueueTiming() ;
        -:  748:}
        -:  749:void
        3:  750:mechEventDelayCancel(
        -:  751:    EventCode event,
        -:  752:    MechInstance targetInst,
        -:  753:    MechInstance srcInst)
        -:  754:{
        3:  755:    assert(targetInst != NULL) ;
        -:  756:    /*
        -:  757:     * Stop delayed queue so that we may examine it.
        -:  758:     */
        3:  759:    stopDelayedQueueTiming() ;
        -:  760:    /*
        -:  761:     * Search for the event in the delayed event queue.
        -:  762:     */
        3:  763:    MechEcb foundEvent = findEvent(&delayedEventQueue,
        -:  764:            srcInst, targetInst, event) ;
        3:  765:    if (foundEvent) {
        -:  766:        /*
        -:  767:         * Removing from the delayed queue requires
        -:  768:         * additional processing of the delay times.
        -:  769:         */
        2:  770:        removeFromDelayedQueue(foundEvent) ;
        -:  771:    } else {
        -:  772:        /*
        -:  773:         * If the event is not in the delayed queue, then
        -:  774:         * search the event queue. The timer could have
        -:  775:         * expired and the event placed in the queue.
        -:  776:         */
        1:  777:        foundEvent = findEvent(&eventQueue, srcInst,
        -:  778:                targetInst, event) ;
        1:  779:        if (foundEvent) {
        1:  780:            eventQueueRemove(foundEvent) ;
        1:  781:            mechEventDelete(foundEvent) ;
        -:  782:        }
        -:  783:        /*
        -:  784:         * We can get here, without finding the event in the
        -:  785:         * delayed queue or the event queue.
        -:  786:         * That's okay, it just amounts to an expensive
        -:  787:         * no-op and implies that the event has expired,
        -:  788:         * was queued and has already been dispatched or
        -:  789:         * had never been generated at all.
        -:  790:         */
        -:  791:    }
        3:  792:    startDelayedQueueTiming() ;
        3:  793:}
        -:  794:MechDelayTime
        1:  795:mechEventDelayRemaining(
        -:  796:    EventCode event,
        -:  797:    MechInstance targetInst,
        -:  798:    MechInstance srcInst)
        -:  799:{
        1:  800:    assert(targetInst != NULL) ;
        -:  801:
        1:  802:    stopDelayedQueueTiming() ;
        -:  803:    /*
        -:  804:     * Iterate through the delayed event time and sum all
        -:  805:     * the delay times to give the total amount of time
        -:  806:     * remaining for the found event.
        -:  807:     */
        1:  808:    MechDelayTime remain = 0 ;
        -:  809:    MechEcb iter ;
        2:  810:    for (iter = eventQueueBegin(&delayedEventQueue) ;
        1:  811:            iter != eventQueueEnd(&delayedEventQueue) ;
    #####:  812:            iter = iter->next) {
        1:  813:        remain += iter->delay ;
        2:  814:        if (iter->srcInst == srcInst &&
        2:  815:                iter->instOrClass.targetInst == targetInst &&
        1:  816:                iter->eventNumber == event) {
        1:  817:            break ;
        -:  818:        }
        -:  819:    }
        1:  820:    startDelayedQueueTiming() ;
        -:  821:    /*
        -:  822:     * Return the amount of time remaining for the event.
        -:  823:     * If we didn't find the event, the just return 0.
        -:  824:     */
        2:  825:    return iter == eventQueueEnd(&delayedEventQueue) ?
        1:  826:            0 : mechTicksToMsec(remain) ;
        -:  827:}
        -:  828:static void
       13:  829:sysTimerMask(void)
        -:  830:{
        -:  831:    /*
        -:  832:     * Make sure SIGALRM does not go off.
        -:  833:     */
        -:  834:    sigset_t mask ;
       13:  835:    sigemptyset(&mask) ;
       13:  836:    sigaddset(&mask, SIGALRM) ;
       13:  837:    if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0) {
    #####:  838:        mechFatalError(mechSignalOpFailed, strerror(errno)) ;
        -:  839:    }
       13:  840:}
        -:  841:
        -:  842:static void
       18:  843:sysTimerUnmask(void)
        -:  844:{
        -:  845:    /*
        -:  846:     * Allow SIGALRM to notify us.
        -:  847:     */
        -:  848:    sigset_t mask ;
       18:  849:    sigemptyset(&mask) ;
       18:  850:    sigaddset(&mask, SIGALRM) ;
       18:  851:    if (sigprocmask(SIG_UNBLOCK, &mask, NULL) != 0) {
    #####:  852:        mechFatalError(mechSignalOpFailed, strerror(errno)) ;
        -:  853:    }
       18:  854:}
        -:  855:static void
       12:  856:sysTimerStart(
        -:  857:    MechDelayTime time)
        -:  858:{
        -:  859:    struct itimerval delayedEventTimer ;
        -:  860:
       12:  861:    delayedEventTimer.it_interval.tv_sec = 0 ;
       12:  862:    delayedEventTimer.it_interval.tv_usec = 0 ;
       12:  863:    delayedEventTimer.it_value.tv_sec = time / 1000 ;
       12:  864:    delayedEventTimer.it_value.tv_usec = (time % 1000) * 1000 ;
        -:  865:
       12:  866:    if (setitimer(ITIMER_REAL, &delayedEventTimer, NULL) != 0) {
    #####:  867:        mechFatalError(mechTimerOpFailed, strerror(errno)) ;
        -:  868:    }
       12:  869:    sysTimerUnmask() ;
       12:  870:}
        -:  871:static MechDelayTime
        7:  872:sysTimerStop(void)
        -:  873:{
        7:  874:    sysTimerMask() ;
        -:  875:    /*
        -:  876:     * Fetch the remaining time.
        -:  877:     */
        -:  878:    struct itimerval delayedEventTimer ;
        7:  879:    if (getitimer(ITIMER_REAL, &delayedEventTimer) != 0) {
    #####:  880:        mechFatalError(mechTimerOpFailed, strerror(errno)) ;
        -:  881:    }
        -:  882:    /*
        -:  883:     * Convert the returned time into milliseconds.
        -:  884:     */
        7:  885:    MechDelayTime remain =
       14:  886:            delayedEventTimer.it_value.tv_sec * 1000 +
        7:  887:            delayedEventTimer.it_value.tv_usec / 1000 ;
        -:  888:    /*
        -:  889:     * Set the current timer value to zero to turn it off.
        -:  890:     */
        7:  891:    memset(&delayedEventTimer, 0, sizeof(delayedEventTimer)) ;
        7:  892:    if (setitimer(ITIMER_REAL, &delayedEventTimer, NULL) != 0) {
    #####:  893:        mechFatalError(mechTimerOpFailed, strerror(errno)) ;
        -:  894:    }
        -:  895:
        7:  896:    return remain ;
        -:  897:}
        -:  898:static void
        6:  899:sysTimerExpire(
        -:  900:    int signum)
        -:  901:{
        6:  902:    MechDelayTime nextTime = mechTimerExpireService() ;
        6:  903:    if (nextTime != 0) {
        2:  904:        sysTimerStart(nextTime) ;
        -:  905:    }
        6:  906:}
        -:  907:static void
        1:  908:sysTimerInit(void)
        -:  909:{
        1:  910:    mechRegisterSignal(SIGALRM, sysTimerExpire) ;
        1:  911:}
        -:  912:static void
        6:  913:mechExpiredEventService(
        -:  914:    SyncParamRef params) /* Not used */
        -:  915:{
        -:  916:    /*
        -:  917:     * Since the expired event queue is accessed here,
        -:  918:     * we must make sure that the timer interrupt does not
        -:  919:     * go off.
        -:  920:     */
        6:  921:    sysTimerMask() ;
        6:  922:    transferExpiredEvents() ;
        6:  923:    sysTimerUnmask() ;
        6:  924:}
        -:  925:MechDelayTime
        6:  926:mechTimerExpireService(void)
        -:  927:{
        -:  928:    MechEcb unexpired ;
        -:  929:    MechDelayTime nextTime ;
        -:  930:    /*
        -:  931:     * Sync to the background to request the expired events
        -:  932:     * be transferred to the event queue.
        -:  933:     */
        6:  934:    mechSyncRequest(mechExpiredEventService) ;
        -:  935:    /*
        -:  936:     * Mark the delayed events as expired, returning a
        -:  937:     * pointer to the first unexpired event.
        -:  938:     */
        6:  939:    unexpired = expireDelayedEvents() ;
        6:  940:    if (unexpired) {
        -:  941:        /*
        -:  942:         * If there is an unexpired event, then its delay
        -:  943:         * time is the next time to expire. We return that
        -:  944:         * time and zero out the delay time.
        -:  945:         */
        2:  946:        assert(unexpired->delay != 0) ;
        2:  947:        nextTime = unexpired->delay ;
        2:  948:        unexpired->delay = 0 ;
        -:  949:    } else {
        -:  950:        /*
        -:  951:         * Otherwise, there is nothing else to time.
        -:  952:         */
        4:  953:        nextTime = 0 ;
        -:  954:    }
        -:  955:
        6:  956:    return nextTime ;
        -:  957:}
        -:  958:#ifdef MECH_SM_TRACE
        -:  959:static MechTraceCallback traceCallback ;
        -:  960:
        -:  961:MechTraceCallback
        -:  962:mechRegisterTrace(
        -:  963:    MechTraceCallback cb)
        -:  964:{
        -:  965:    MechTraceCallback oldcb = traceCallback ;
        -:  966:    traceCallback = cb ;
        -:  967:    return oldcb ;
        -:  968:}
        -:  969:static inline 
        -:  970:void
        -:  971:traceNormalEvent(
        -:  972:    EventCode event,
        -:  973:    MechInstance source,
        -:  974:    MechInstance target,
        -:  975:    StateCode currentState,
        -:  976:    StateCode newState)
        -:  977:{
        -:  978:    if (traceCallback) {
        -:  979:        struct mechtraceinfo trace ;
        -:  980:
        -:  981:        trace.eventType = NormalEvent ;
        -:  982:        trace.eventNumber = event ;
        -:  983:        trace.srcInst = source ;
        -:  984:        trace.dstInst = target ;
        -:  985:        trace.info.normalTrace.currState = currentState ;
        -:  986:        trace.info.normalTrace.newState = newState ;
        -:  987:
        -:  988:        traceCallback(&trace) ;
        -:  989:    }
        -:  990:}
        -:  991:static inline 
        -:  992:void
        -:  993:tracePolyEvent(
        -:  994:    EventCode event,
        -:  995:    MechInstance source,
        -:  996:    MechInstance target,
        -:  997:    SubtypeCode subtype,
        -:  998:    DispatchCount hierarchy,
        -:  999:    EventCode newEvent,
        -: 1000:    MechEventType newEventType)
        -: 1001:{
        -: 1002:    if (traceCallback) {
        -: 1003:        struct mechtraceinfo trace ;
        -: 1004:
        -: 1005:        trace.eventType = PolymorphicEvent ;
        -: 1006:        trace.eventNumber = event ;
        -: 1007:        trace.srcInst = source ;
        -: 1008:        trace.dstInst = target ;
        -: 1009:        trace.info.polyTrace.subcode = subtype ;
        -: 1010:        trace.info.polyTrace.hierarchy = hierarchy ;
        -: 1011:        trace.info.polyTrace.mappedNumber = newEvent ;
        -: 1012:        trace.info.polyTrace.mappedType = newEventType ;
        -: 1013:
        -: 1014:        traceCallback(&trace) ;
        -: 1015:    }
        -: 1016:}
        -: 1017:static inline 
        -: 1018:void
        -: 1019:traceCreationEvent(
        -: 1020:    EventCode event,
        -: 1021:    MechInstance source,
        -: 1022:    MechInstance target,
        -: 1023:    MechClass class)
        -: 1024:{
        -: 1025:    if (traceCallback) {
        -: 1026:        struct mechtraceinfo trace ;
        -: 1027:
        -: 1028:        trace.eventType = CreationEvent ;
        -: 1029:        trace.eventNumber = event ;
        -: 1030:        trace.srcInst = source ;
        -: 1031:        trace.dstInst = target ;
        -: 1032:        trace.info.creationTrace.dstClass = class ;
        -: 1033:
        -: 1034:        traceCallback(&trace) ;
        -: 1035:    }
        -: 1036:}
        -: 1037:#endif /* MECH_SM_TRACE */
        -: 1038:static void dispatchNormalEvent(MechEcb) ;
        -: 1039:static void dispatchPolyEvent(MechEcb) ;
        -: 1040:static void dispatchCreationEvent(MechEcb) ;
        -: 1041:static void (* const ecbDispatchFuncs[])(MechEcb) = {
        -: 1042:    dispatchNormalEvent,
        -: 1043:    dispatchPolyEvent,
        -: 1044:    dispatchCreationEvent,
        -: 1045:} ;
        -: 1046:static void
       23: 1047:mechDispatch(
        -: 1048:    MechEcb ecb)
        -: 1049:{
       23: 1050:    ecbDispatchFuncs[ecb->eventType](ecb) ;
       23: 1051:}
        -: 1052:static void
       21: 1053:dispatchNormalEvent(
        -: 1054:    MechEcb ecb)
        -: 1055:{
       21: 1056:    MechInstance target = ecb->instOrClass.targetInst ;
       21: 1057:    ObjectDispatchBlock db = target->instClass->odb ;
        -: 1058:
        -: 1059:    /*
        -: 1060:     * Test for corruption of the current state
        -: 1061:     * or event number.
        -: 1062:     */
       21: 1063:    assert(db->stateCount > target->currentState) ;
       21: 1064:    assert(db->eventCount > ecb->eventNumber) ;
        -: 1065:    /*
        -: 1066:     * Check for the "event-in-flight" error. This occurs
        -: 1067:     * when an instance is deleted while there is an event
        -: 1068:     * for that instance in the event queue.  For this
        -: 1069:     * architecture, such occurrences are considered as
        -: 1070:     * run-time detected analysis errors.
        -: 1071:     */
       21: 1072:    if (target->alloc != ecb->alloc) {
    #####: 1073:        mechFatalError(mechEventInFlight, ecb->srcInst,
    #####: 1074:                target, ecb->eventNumber) ;
        -: 1075:    }
        -: 1076:    /*
        -: 1077:     * Fetch the new state from the transition table.
        -: 1078:     */
       42: 1079:    StateCode newState = *(db->transitionTable +
       42: 1080:            target->currentState * db->eventCount +
       21: 1081:            ecb->eventNumber) ;
        -: 1082:#       ifdef MECH_SM_TRACE
        -: 1083:    /*
        -: 1084:     * Trace the transition.
        -: 1085:     */
        -: 1086:    traceNormalEvent(ecb->eventNumber, ecb->srcInst,
        -: 1087:            ecb->instOrClass.targetInst, target->currentState, newState) ;
        -: 1088:#       endif
        -: 1089:    /*
        -: 1090:     * Check for a can't happen transition.
        -: 1091:     */
       21: 1092:    if (newState == MECH_STATECODE_CH) {
        2: 1093:        mechFatalError(mechCantHappen, target,
        2: 1094:                target->currentState, ecb->eventNumber) ;
       20: 1095:    } else if (newState != MECH_STATECODE_IG) {
        -: 1096:        /*
        -: 1097:         * Check for corrupt transition table.
        -: 1098:         */
       20: 1099:        assert(newState < db->stateCount) ;
        -: 1100:        /*
        -: 1101:         * We update the current state to reflect the
        -: 1102:         * transition before executing the action for the
        -: 1103:         * state.
        -: 1104:         */
       20: 1105:        target->currentState = newState ;
        -: 1106:        /*
        -: 1107:         * Invoke the state action if there is one.
        -: 1108:         */
       20: 1109:        PtrActionFunction action = db->actionTable[newState] ;
       20: 1110:        if (action) {
       20: 1111:            action(target, &ecb->eventParameters) ;
        -: 1112:        }
        -: 1113:        /*
        -: 1114:         * Check if we have entered a final state. If so,
        -: 1115:         * the instance is deleted.
        -: 1116:         */
       20: 1117:        if (db->finalStates && db->finalStates[newState]) {
        1: 1118:            mechInstDestroy(target) ;
        -: 1119:        }
        -: 1120:    }
        -: 1121:    /*
        -: 1122:     * Return the ECB to the pool.
        -: 1123:     */
       21: 1124:    mechEventDelete(ecb) ;
       21: 1125:}
        -: 1126:static void
        2: 1127:dispatchPolyEvent(
        -: 1128:    MechEcb ecb)
        -: 1129:{
        2: 1130:    PolyDispatchBlock pdb = ecb->instOrClass.targetInst->instClass->pdb ;
        -: 1131:
        2: 1132:    assert(pdb != NULL) ;
        2: 1133:    assert(ecb->eventNumber < pdb->eventCount) ;
        2: 1134:    assert(pdb->hierCount > 0) ;
        -: 1135:    /*
        -: 1136:     * Each generalization hierarchy that originates at the
        -: 1137:     * supertype has an event generated down that
        -: 1138:     * hierarchy to one of the subtypes.
        -: 1139:     */
        2: 1140:    HierarchyDispatch hd = pdb->hierarchy ;
        4: 1141:    for (unsigned hnum = 0 ; hnum < pdb->hierCount ; ++hnum) {
        -: 1142:        /*
        -: 1143:         * The most common case is to dispatch along a
        -: 1144:         * single hierarchy.  In any case, we can modify in
        -: 1145:         * place the input ECB on the last dispatched event.
        -: 1146:         */
        -: 1147:        MechEcb newEcb ;
        2: 1148:        if (hnum == pdb->hierCount - 1) {
        2: 1149:            newEcb = ecb ;
        -: 1150:        } else {
    #####: 1151:            newEcb = mechEventAlloc() ;
        -: 1152:            /*
        -: 1153:             * We set the source as the original sender.
        -: 1154:             */
    #####: 1155:            newEcb->srcInst = ecb->srcInst ;
        -: 1156:            /*
        -: 1157:             * Copy event parameters.
        -: 1158:             */
    #####: 1159:            newEcb->eventParameters = ecb->eventParameters ;
        -: 1160:        }
        2: 1161:        SubtypeCode type =
        4: 1162:                *(SubtypeCode *)((char *)ecb->instOrClass.targetInst +
        2: 1163:                hd->subCodeOffset) ;
        -: 1164:
        2: 1165:        assert(type < hd->subtypeCount) ;
        4: 1166:        PolyEventMap pem = hd->eventMap +
        2: 1167:                (type * pdb->eventCount + ecb->eventNumber) ;
        -: 1168:#           ifdef MECH_SM_TRACE
        -: 1169:        /*
        -: 1170:         * Trace the transition.
        -: 1171:         */
        -: 1172:        tracePolyEvent(ecb->eventNumber, ecb->srcInst,
        -: 1173:                ecb->instOrClass.targetInst, type, hnum,
        -: 1174:                pem->event, pem->eventType) ;
        -: 1175:#           endif /* MECH_SM_TRACE */
        -: 1176:
        2: 1177:        newEcb->eventNumber = pem->event ;
        2: 1178:        newEcb->eventType = pem->eventType ;
        -: 1179:
        2: 1180:        void *subTypeRef =
        2: 1181:                (char *)ecb->instOrClass.targetInst + hd->subInstOffset ;
        4: 1182:        newEcb->instOrClass.targetInst = hd->refStorage == PolyReference ?
        -: 1183:            /*
        -: 1184:             * When the generalization is implemented via a
        -: 1185:             * pointer, we need an extra level of
        -: 1186:             * indirection to fetch the address of the
        -: 1187:             * subtype.
        -: 1188:             */
        2: 1189:            *(MechInstance *)subTypeRef :
        -: 1190:            /*
        -: 1191:             * When the generalization is implemented by a
        -: 1192:             * union, we need only point to the address of
        -: 1193:             * the subtype as it is contained in the
        -: 1194:             * supertype.
        -: 1195:             */
        -: 1196:            (MechInstance)subTypeRef ;
        -: 1197:
        2: 1198:        if (newEcb->eventType == NormalEvent) {
        2: 1199:            newEcb->alloc = newEcb->instOrClass.targetInst->alloc ;
        2: 1200:            assert(newEcb->alloc != 0) ;
        -: 1201:        }
        -: 1202:
        2: 1203:        mechDispatch(newEcb) ;
        2: 1204:        ++hd ;
        -: 1205:    }
        2: 1206:}
        -: 1207:static void
        2: 1208:dispatchCreationEvent(
        -: 1209:    MechEcb ecb)
        -: 1210:{
        -: 1211:    /*
        -: 1212:     * For creation events we must allocate an instance,
        -: 1213:     * set the state to be the creation state (by
        -: 1214:     * convention the creation state is 0).
        -: 1215:     */
        2: 1216:    MechInstance inst = mechInstCreate(ecb->instOrClass.targetClass,
        -: 1217:            MECH_DISPATCH_CREATION_STATE) ;
        -: 1218:#           ifdef MECH_SM_TRACE
        -: 1219:    /*
        -: 1220:     * Trace the transition.
        -: 1221:     */
        -: 1222:    traceCreationEvent(ecb->eventNumber, ecb->srcInst,
        -: 1223:            inst, ecb->instOrClass.targetClass) ;
        -: 1224:#           endif
        -: 1225:    /*
        -: 1226:     * Modify the event structure in place and dispatch it.
        -: 1227:     */
        2: 1228:    ecb->instOrClass.targetInst = inst ;
        2: 1229:    ecb->eventType = NormalEvent ;
        2: 1230:    ecb->alloc = ecb->instOrClass.targetInst->alloc ;
        2: 1231:    assert(ecb->alloc != 0) ;
        -: 1232:
        2: 1233:    dispatchNormalEvent(ecb) ;
        2: 1234:}
        -: 1235:typedef struct fgsyncblock {
        -: 1236:    SyncFunc function ;
        -: 1237:    SyncParamType params ;
        -: 1238:} *FgSyncBlock ;
        -: 1239:struct syncqueue {
        -: 1240:    FgSyncBlock head ;
        -: 1241:    FgSyncBlock tail ;
        -: 1242:} ;
        -: 1243:static struct fgsyncblock
        -: 1244:mechSyncQueueStorage[MECH_SYNCQUEUESIZE] ;
        -: 1245:static struct syncqueue mechSyncQueue = {
        -: 1246:    .head = mechSyncQueueStorage,
        -: 1247:    .tail = mechSyncQueueStorage,
        -: 1248:} ;
        -: 1249:static inline
        -: 1250:bool
       24: 1251:syncQueueEmpty(void)
        -: 1252:{
       24: 1253:    return mechSyncQueue.head == mechSyncQueue.tail ;
        -: 1254:}
        -: 1255:static inline
        -: 1256:SyncParamRef
        6: 1257:syncQueuePut(
        -: 1258:    SyncFunc f,
        -: 1259:    bool fatal)
        -: 1260:{
        6: 1261:    FgSyncBlock tail = mechSyncQueue.tail ;
        6: 1262:    if (++mechSyncQueue.tail >=
        -: 1263:            mechSyncQueueStorage + MECH_SYNCQUEUESIZE) {
    #####: 1264:        mechSyncQueue.tail = mechSyncQueueStorage ;
        -: 1265:    }
        6: 1266:    if (syncQueueEmpty()) {
    #####: 1267:        if (fatal) {
    #####: 1268:            mechFatalError(mechSyncOverflow) ;
        -: 1269:        }
    #####: 1270:        return NULL ;
        -: 1271:    }
        -: 1272:
        6: 1273:    tail->function = f ;
        6: 1274:    return &tail->params ;
        -: 1275:}
        -: 1276:static inline
        -: 1277:FgSyncBlock
       11: 1278:syncQueueGet(void)
        -: 1279:{
        -: 1280:    FgSyncBlock head ;
        -: 1281:
       11: 1282:    beginCriticalSection() ;
       11: 1283:    if (syncQueueEmpty()) {
        5: 1284:        head = NULL ;
        -: 1285:    } else {
        6: 1286:        head = mechSyncQueue.head ;
        6: 1287:        if (++mechSyncQueue.head >=
        -: 1288:                mechSyncQueueStorage + MECH_SYNCQUEUESIZE) {
    #####: 1289:            mechSyncQueue.head = mechSyncQueueStorage ;
        -: 1290:        }
        -: 1291:    }
       11: 1292:    endCriticalSection() ;
        -: 1293:
       11: 1294:    return head ;
        -: 1295:}
        -: 1296:SyncParamRef
        6: 1297:mechSyncRequest(
        -: 1298:    SyncFunc f)
        -: 1299:{
        6: 1300:    return syncQueuePut(f, true) ;
        -: 1301:}
        -: 1302:SyncParamRef
    #####: 1303:mechTrySyncRequest(
        -: 1304:    SyncFunc f)
        -: 1305:{
    #####: 1306:    return syncQueuePut(f, false) ;
        -: 1307:}
        -: 1308:void
        1: 1309:mechRegisterSignal(
        -: 1310:    int sigNum,
        -: 1311:    SignalFunc func)
        -: 1312:{
        1: 1313:    assert(sigNum > 0) ;
        -: 1314:
        -: 1315:    struct sigaction action ;
        1: 1316:    if (func) {
        1: 1317:        action.sa_handler = func ;
        1: 1318:        sigaddset(&mechSigMask, sigNum) ;
        -: 1319:    } else {
    #####: 1320:        action.sa_handler = SIG_DFL ;
    #####: 1321:        sigdelset(&mechSigMask, sigNum) ;
        -: 1322:    }
        1: 1323:    sigfillset(&action.sa_mask) ;
        1: 1324:    action.sa_flags = 0 ;
        -: 1325:
        1: 1326:    int sigresult = sigaction(sigNum, &action, NULL) ;
        1: 1327:    if (sigresult != 0) {
    #####: 1328:        mechFatalError(mechSignalOpFailed, strerror(errno)) ;
        -: 1329:    }
        1: 1330:}
        -: 1331:typedef struct fdservicemap {
        -: 1332:    bool set ;
        -: 1333:    FDServiceFunc read ;
        -: 1334:    FDServiceFunc write ;
        -: 1335:    FDServiceFunc except ;
        -: 1336:} *FDServiceMap ;
        -: 1337:static struct fdservicemap mechFDServicePool[FD_SETSIZE] ;
        -: 1338:static int mechMaxFD = -1 ;
        -: 1339:static fd_set mechReadFDS ;
        -: 1340:static fd_set mechWriteFDS ;
        -: 1341:static fd_set mechExceptFDS ;
        -: 1342:void
        1: 1343:mechRegisterFDService(
        -: 1344:    int fd,
        -: 1345:    FDServiceFunc readService,
        -: 1346:    FDServiceFunc writeService,
        -: 1347:    FDServiceFunc exceptService)
        -: 1348:{
        1: 1349:    assert(fd >= 0 && fd < FD_SETSIZE) ;
        1: 1350:    FDServiceMap fds = mechFDServicePool + fd ;
        -: 1351:
        1: 1352:    fds->read = readService ;
        1: 1353:    if (readService) {
    #####: 1354:        FD_SET(fd, &mechReadFDS) ;
    #####: 1355:        fds->set = true ;
        -: 1356:    } else {
        1: 1357:        FD_CLR(fd, &mechReadFDS) ;
        -: 1358:    }
        -: 1359:
        1: 1360:    fds->write = writeService ;
        1: 1361:    if (writeService) {
        1: 1362:        FD_SET(fd, &mechWriteFDS) ;
        1: 1363:        fds->set = true ;
        -: 1364:    } else {
    #####: 1365:        FD_CLR(fd, &mechWriteFDS) ;
        -: 1366:    }
        -: 1367:
        1: 1368:    fds->except = exceptService ;
        1: 1369:    if (exceptService) {
    #####: 1370:        FD_SET(fd, &mechExceptFDS) ;
    #####: 1371:        fds->set = true ;
        -: 1372:    } else {
        1: 1373:        FD_CLR(fd, &mechExceptFDS) ;
        -: 1374:    }
        -: 1375:
        1: 1376:    if (fds->read == NULL && fds->write == NULL && fds-> except == NULL) {
    #####: 1377:        if (fds->set && fd >= mechMaxFD) {
    #####: 1378:            --mechMaxFD ;
        -: 1379:        }
    #####: 1380:        fds->set = false ;
        1: 1381:    } else if (fds->set && fd > mechMaxFD) {
        1: 1382:        mechMaxFD = fd ;
        -: 1383:    }
        1: 1384:}
        -: 1385:void
        1: 1386:mechRemoveFDService(
        -: 1387:    int fd,
        -: 1388:    bool rmRead,
        -: 1389:    bool rmWrite,
        -: 1390:    bool rmExcept)
        -: 1391:{
        1: 1392:    assert(fd >= 0 && fd < FD_SETSIZE) ;
        1: 1393:    FDServiceMap fds = mechFDServicePool + fd ;
        -: 1394:
        1: 1395:    if (rmRead) {
    #####: 1396:        fds->read = NULL ;
    #####: 1397:        FD_CLR(fd, &mechReadFDS) ;
        -: 1398:    }
        -: 1399:
        1: 1400:    if (rmWrite) {
        1: 1401:        fds->write = NULL ;
        1: 1402:        FD_CLR(fd, &mechWriteFDS) ;
        -: 1403:    }
        -: 1404:
        1: 1405:    if (rmExcept) {
    #####: 1406:        fds->except = NULL ;
    #####: 1407:        FD_CLR(fd, &mechExceptFDS) ;
        -: 1408:    }
        -: 1409:
        2: 1410:    if (fds->read == NULL && fds->write == NULL && fds-> except == NULL &&
        1: 1411:            fd >= mechMaxFD) {
        1: 1412:        mechMaxFD = fd - 1 ;
        -: 1413:    }
        1: 1414:}
        -: 1415:static void
        1: 1416:fdServiceInit(void)
        -: 1417:{
        1: 1418:    FD_ZERO(&mechReadFDS) ;
        1: 1419:    FD_ZERO(&mechWriteFDS) ;
        1: 1420:    FD_ZERO(&mechExceptFDS) ;
        1: 1421:}
        -: 1422:MECH_TEST_STATIC
        -: 1423:void
        7: 1424:mechWait(void)
        -: 1425:{
        7: 1426:    beginCriticalSection() ;
        7: 1427:    if (syncQueueEmpty()) {
        -: 1428:        /*
        -: 1429:         * Copy the file descriptor sets since "pselect"
        -: 1430:         * modifies them in place upon return.
        -: 1431:         */
        -: 1432:        fd_set readfds ;
        7: 1433:        memcpy(&readfds, &mechReadFDS, sizeof(readfds)) ;
        -: 1434:        fd_set writefds ;
        7: 1435:        memcpy(&writefds, &mechWriteFDS, sizeof(writefds)) ;
        -: 1436:        fd_set exceptfds ;
        7: 1437:        memcpy(&exceptfds, &mechExceptFDS, sizeof(exceptfds)) ;
        -: 1438:        /*
        -: 1439:         * Allow all the signals during the select. We
        -: 1440:         * assume that when we enter this function, the
        -: 1441:         * registered signals are masked.
        -: 1442:         */
        -: 1443:        sigset_t mask ;
        7: 1444:        sigemptyset(&mask) ;
        -: 1445:        /*
        -: 1446:         * "mechMaxFD" holds the maximum value of any
        -: 1447:         * registered file descriptor. We must add one to
        -: 1448:         * get the number of file descriptors "pselect" is
        -: 1449:         * to consider.
        -: 1450:         */
        7: 1451:        int r = pselect(mechMaxFD + 1, &readfds, &writefds,
        -: 1452:                &exceptfds, NULL, &mask) ;
        7: 1453:        if (r == -1) {
        6: 1454:            if (errno != EINTR) {
    #####: 1455:                mechFatalError(mechSelectWaitFailed,
    #####: 1456:                        strerror(errno)) ;
        -: 1457:            }
        -: 1458:            /*
        -: 1459:             * Got a signal while waiting. We go back to the
        -: 1460:             * main loop on the assumption that something
        -: 1461:             * has been placed in the sync queue.
        -: 1462:             */
        -: 1463:        } else {
        -: 1464:            /*
        -: 1465:             * Dispatch the service functions for the file
        -: 1466:             * descriptors.
        -: 1467:             */
        1: 1468:            FDServiceMap s = mechFDServicePool ;
        4: 1469:            for (int fd = 0 ; r > 0 && fd <= mechMaxFD ;
        2: 1470:                    ++fd, ++s) {
        -: 1471:                /*
        -: 1472:                 * Do exceptions first. This is only
        -: 1473:                 * important for sockets, but without going
        -: 1474:                 * first the OOB data processing won't
        -: 1475:                 * work.
        -: 1476:                 */
        2: 1477:                if (FD_ISSET(fd, &exceptfds)) {
    #####: 1478:                    assert(s->except != NULL) ;
    #####: 1479:                    s->except(fd) ;
    #####: 1480:                    --r ;
        -: 1481:                }
        2: 1482:                if (FD_ISSET(fd, &readfds)) {
    #####: 1483:                    assert(s->read != NULL) ;
    #####: 1484:                    s->read(fd) ;
    #####: 1485:                    --r ;
        -: 1486:                }
        2: 1487:                if (FD_ISSET(fd, &writefds)) {
        1: 1488:                    assert(s->write != NULL) ;
        1: 1489:                    s->write(fd) ;
        1: 1490:                    --r ;
        -: 1491:                }
        -: 1492:            }
        -: 1493:        }
        -: 1494:    }
        7: 1495:    endCriticalSection() ;
        7: 1496:}
        -: 1497:static void
        1: 1498:sysPlatformInit(void)
        -: 1499:{
        1: 1500:    fdServiceInit() ;
        1: 1501:}
        -: 1502:MECH_TEST_STATIC
        -: 1503:MECH_TEST_INLINE
        -: 1504:void
        1: 1505:mechInit(void)
        -: 1506:{
        1: 1507:    sysPlatformInit() ;
        1: 1508:    mechEventInit() ;
        1: 1509:    initCriticalSection() ;
        1: 1510:    sysTimerInit() ;
        1: 1511:    sysDeviceInit() ;
        1: 1512:    sysDomainInit() ;
        1: 1513:}
        -: 1514:MECH_TEST_STATIC
        -: 1515:MECH_TEST_INLINE
        -: 1516:bool
       11: 1517:mechInvokeOneSyncFunc(void)
        -: 1518:{
        -: 1519:    bool didOne ;
       11: 1520:    FgSyncBlock blk = syncQueueGet() ;
       11: 1521:    if (blk && blk->function) {
        6: 1522:        blk->function(&blk->params) ;
        6: 1523:        didOne = true ;
        -: 1524:    } else {
        5: 1525:        didOne = false ;
        -: 1526:    }
       11: 1527:    return didOne ;
        -: 1528:}
        -: 1529:MECH_TEST_STATIC
        -: 1530:MECH_TEST_INLINE
        -: 1531:bool
       29: 1532:mechDispatchOneEvent(void)
        -: 1533:{
       29: 1534:    bool didOne = !eventQueueEmpty(&eventQueue) ;
       29: 1535:    if (didOne) {
       21: 1536:        MechEcb ecb = eventQueue.next ;
       21: 1537:        eventQueueRemove(ecb) ;
       21: 1538:        mechDispatch(ecb) ;
        -: 1539:    }
       29: 1540:    return didOne ;
        -: 1541:}
        -: 1542:#ifdef MECH_TEST
        -: 1543:void
    #####: 1544:stsa_main(void)
        -: 1545:#else
        -: 1546:int
        -: 1547:main(void)
        -: 1548:#endif /* MECH_TEST */
        -: 1549:{
    #####: 1550:    mechInit() ;
        -: 1551:
        -: 1552:    for (;;) { /* Infinite Big Loop */
        -: 1553:        /*
        -: 1554:         * Empty the foreground/background sync queue.
        -: 1555:         */
        -: 1556:        #ifndef __ARM_ARCH_7M__
        -: 1557:        /*
        -: 1558:         * Empty the foreground / background
        -: 1559:         * synchronization queue.
        -: 1560:         */
    #####: 1561:        while (mechInvokeOneSyncFunc()) {
        -: 1562:            ; /* empty */
        -: 1563:        }
        -: 1564:        #endif /* __ARM_ARCH_7M__ */
        -: 1565:        /*
        -: 1566:         * Dispatch one event from the event queue.
        -: 1567:         */
    #####: 1568:        if (!mechDispatchOneEvent()) {
        -: 1569:            /*
        -: 1570:             * Check if this thread of control is complete
        -: 1571:             * and wait if there is no additional work to
        -: 1572:             * be done.
        -: 1573:             */
    #####: 1574:            mechWait() ;
        -: 1575:        }
    #####: 1576:    }
        -: 1577:}








|







 







|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
...
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
        -:    0:Source:../../../../code/posix/mechs.c
        -:    0:Graph:mechs.gcno
        -:    0:Data:mechs.gcda
        -:    0:Runs:1
        -:    0:Programs:1
        -:    1:/*
        -:    2: * THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT IT.
        -:    3: * This file corresponds to Version 1.8 of the STSA literate
        -:    4: * program.
        -:    5: */
        -:    6:/*
        -:    7: * This software is copyrighted 2007 - 2014 by G. Andrew
        -:    8: * Mangogna.  The following terms apply to all files associated
        -:    9: * with the software unless explicitly disclaimed in individual
        -:   10: * files.
................................................................................
        9:  534:    eventQueueInsert(ecb, iter) ;
        -:  535:    /*
        -:  536:     * Since we have stored a reference to the ECB we do
        -:  537:     * the bookkeeping.
        -:  538:     */
        9:  539:    mechEventIncrRef(ecb) ;
        9:  540:}
        -:  541:#define MECH_DELAY_EXPIRED  UINT32_MAX
        -:  542:static void
        3:  543:removeFromDelayedQueue(
        -:  544:    MechEcb ecb)
        -:  545:{
        -:  546:    /*
        -:  547:     * If we are not at the end of the queue, all the delay
        -:  548:     * from the removed entry is accumulated on the next
        -:  549:     * entry in the queue.
        -:  550:     */
        6:  551:    if (!(ecb->delay == MECH_DELAY_EXPIRED ||
        3:  552:            ecb->next == eventQueueEnd(&delayedEventQueue))) {
    #####:  553:        ecb->next->delay += ecb->delay ;
        -:  554:    }
        -:  555:    /*
        -:  556:     * Remove the ECB from the delayed queue.
        -:  557:     */
        3:  558:    eventQueueRemove(ecb) ;
        -:  559:    /*
        -:  560:     * Return the ECB back to the pool.
        -:  561:     */
        3:  562:    mechEventDelete(ecb) ;
        3:  563:}
        -:  564:static MechEcb
        7:  565:expireDelayedEvents(void)
        -:  566:{
        -:  567:    /*
        -:  568:     * Iterate along the delayed event queue.
        -:  569:     */
       21:  570:    for (MechEcb iter = eventQueueBegin(&delayedEventQueue) ;
       14:  571:            iter != eventQueueEnd(&delayedEventQueue) ;
        7:  572:            iter = iter->next) {
        9:  573:        if (iter->delay == 0) {
        -:  574:            /*
        -:  575:             * Mark all the events that have zero delay time
        -:  576:             * as expired.
        -:  577:             */
        6:  578:            iter->delay = MECH_DELAY_EXPIRED ;
        3:  579:        } else if (iter->delay != MECH_DELAY_EXPIRED) {
        -:  580:            /*
        -:  581:             * Stop at the first non-zero delay time.  This
        -:  582:             * marks the boundary of events that need
        -:  583:             * additional delay time.  The first such event
        -:  584:             * is the next amount of time to delay.
        -:  585:             */
        2:  586:            return iter ;
        -:  587:        }
        -:  588:        /*
        -:  589:         * else ... Skip any events that might already be
        -:  590:         * expired.
        -:  591:         */
        -:  592:    }
        -:  593:    /*
        -:  594:     * We have run the queue without finding an unexpired
        -:  595:     * event.
        -:  596:     */
        5:  597:    return NULL ;
        -:  598:}
        -:  599:static void
       13:  600:transferExpiredEvents(void)
        -:  601:{
        -:  602:    /*
        -:  603:     * Iterate through the delayed event queue looking for
        -:  604:     * those entries that have been marked as expired.
        -:  605:     */
       32:  606:    for (MechEcb iter = eventQueueBegin(&delayedEventQueue) ;
       33:  607:            iter != eventQueueEnd(&delayedEventQueue) &&
       14:  608:            iter->delay == MECH_DELAY_EXPIRED ; ) {
        -:  609:        /*
        -:  610:         * Advance the iterator, because we are about to
        -:  611:         * invalidate it by removing the entry from the
        -:  612:         * queue.
        -:  613:         */
        6:  614:        MechEcb ecb = iter ;
        6:  615:        iter = iter->next ;
        -:  616:
        -:  617:        /*
        -:  618:         * Remove the ECB from the delayed queue and insert
        -:  619:         * it into event queue for dispatch.
        -:  620:         */
        6:  621:        eventQueueRemove(ecb) ;
        6:  622:        eventQueueInsert(ecb, &eventQueue) ;
        6:  623:        assert(ecb->referenceCount != 0) ;
        -:  624:    }
       13:  625:}
        -:  626:static void
       13:  627:startDelayedQueueTiming(void)
        -:  628:{
       13:  629:    if (!eventQueueEmpty(&delayedEventQueue)) {
       10:  630:        MechEcb ecb = eventQueueBegin(&delayedEventQueue) ;
       10:  631:        assert(ecb->delay != 0) ;
       10:  632:        sysTimerStart(ecb->delay) ;
       10:  633:        ecb->delay = 0 ;
        -:  634:    }
       13:  635:}
        -:  636:static void
       13:  637:stopDelayedQueueTiming(void)
        -:  638:{
        -:  639:    /*
        -:  640:     * Avoid the whole thing if there is nothing in the
        -:  641:     * delayed event queue.
        -:  642:     */
       13:  643:    if (!eventQueueEmpty(&delayedEventQueue)) {
        -:  644:        /*
        -:  645:         * Stop the timer, obtaining the residual time.
        -:  646:         */
        7:  647:        MechDelayTime remain = sysTimerStop() ;
        -:  648:        /*
        -:  649:         * There are two cases here. It is possible for the
        -:  650:         * remaining time returned from sysTimerStop() to be
        -:  651:         * zero. This can happen if the physical timing
        -:  652:         * resource (which might be running asynchronously
        -:  653:         * to the processor) happens to expire within a
        -:  654:         * single tick as we are stopping it.
        -:  655:         */
        7:  656:        if (remain == 0) {
        -:  657:            /*
        -:  658:             * Since the timer has expired we must mark any
        -:  659:             * events with a zero delay time value as
        -:  660:             * expired and, since we are running in the
        -:  661:             * background here, transfer the expired events
        -:  662:             * to be dispatched.
        -:  663:             */
        1:  664:            expireDelayedEvents() ;
        1:  665:            transferExpiredEvents() ;
        -:  666:            /*
        -:  667:             * At this point, either the delayed event queue
        -:  668:             * is empty, or the event at the head of the
        -:  669:             * queue has a non-zero delay time.
        -:  670:             */
        -:  671:        } else {
        -:  672:            /*
        -:  673:             * It is possible that the timing resource
        -:  674:             * expired and its interrupt service ran just
        -:  675:             * before we could get the timer stopped. That
        -:  676:             * would mean that there are expired events on
        -:  677:             * the delayed queue at this point and we need
        -:  678:             * to transfer them off the delayed queue to be
        -:  679:             * dispatched.
        -:  680:             */
        6:  681:            transferExpiredEvents() ;
        -:  682:            /*
        -:  683:             * If any events expired, the delayed event
        -:  684:             * queue might now be empty. However, if the
        -:  685:             * queue is not empty, we must make sure the
        -:  686:             * entry at the head preserves the remaining
        -:  687:             * amount of time that needs to elapse.
        -:  688:             */
        6:  689:            if (!eventQueueEmpty(&delayedEventQueue)) {
        6:  690:                MechEcb ecb = eventQueueBegin(&delayedEventQueue) ;
        6:  691:                assert(ecb->delay == 0) ;
        6:  692:                ecb->delay = remain ;
        -:  693:            }
        -:  694:        }
        -:  695:    }
       13:  696:}
        -:  697:static inline
        -:  698:MechDelayTime
        9:  699:mechMsecToTicks(
        -:  700:    MechDelayTime msec)
        -:  701:{
        9:  702:    return msec ;
        -:  703:}
        -:  704:static inline
        -:  705:MechDelayTime
        1:  706:mechTicksToMsec(
        -:  707:    MechDelayTime ticks)
        -:  708:{
        1:  709:    return ticks ;
        -:  710:}
        -:  711:void
        9:  712:mechEventPostDelay(
        -:  713:    MechEcb ecb,
        -:  714:    MechDelayTime time)
        -:  715:{
        9:  716:    assert(ecb != NULL) ;
        -:  717:    /*
        -:  718:     * A delay time of 0 is valid, and the event will be
        -:  719:     * queued immediately.
        -:  720:     */
        9:  721:    if (time == 0) {
    #####:  722:        mechEventPost(ecb) ;
    #####:  723:        return ;
        -:  724:    }
        9:  725:    ecb->delay = mechMsecToTicks(time) ;
        -:  726:    /*
        -:  727:     * Stop the timing queue so we may examine it.
        -:  728:     */
        9:  729:    stopDelayedQueueTiming() ;
        -:  730:    /*
        -:  731:     * If the event already exists, remove it.
        -:  732:     */
        9:  733:    MechEcb prevEvent = findEvent(&delayedEventQueue,
        -:  734:            ecb->srcInst, ecb->instOrClass.targetInst,
        9:  735:            ecb->eventNumber) ;
        9:  736:    if (prevEvent) {
        1:  737:        removeFromDelayedQueue(prevEvent) ;
        -:  738:    }
        -:  739:    /*
        -:  740:     * Insert the new event.
        -:  741:     */
        9:  742:    insertIntoDelayedQueue(ecb) ;
        9:  743:    assert(!eventQueueEmpty(&delayedEventQueue)) ;
        -:  744:    /*
        -:  745:     * Start the timer to expire for the first event
        -:  746:     * on the queue.
        -:  747:     */
        9:  748:    startDelayedQueueTiming() ;
        -:  749:}
        -:  750:void
        3:  751:mechEventDelayCancel(
        -:  752:    EventCode event,
        -:  753:    MechInstance targetInst,
        -:  754:    MechInstance srcInst)
        -:  755:{
        3:  756:    assert(targetInst != NULL) ;
        -:  757:    /*
        -:  758:     * Stop delayed queue so that we may examine it.
        -:  759:     */
        3:  760:    stopDelayedQueueTiming() ;
        -:  761:    /*
        -:  762:     * Search for the event in the delayed event queue.
        -:  763:     */
        3:  764:    MechEcb foundEvent = findEvent(&delayedEventQueue,
        -:  765:            srcInst, targetInst, event) ;
        3:  766:    if (foundEvent) {
        -:  767:        /*
        -:  768:         * Removing from the delayed queue requires
        -:  769:         * additional processing of the delay times.
        -:  770:         */
        2:  771:        removeFromDelayedQueue(foundEvent) ;
        -:  772:    } else {
        -:  773:        /*
        -:  774:         * If the event is not in the delayed queue, then
        -:  775:         * search the event queue. The timer could have
        -:  776:         * expired and the event placed in the queue.
        -:  777:         */
        1:  778:        foundEvent = findEvent(&eventQueue, srcInst,
        -:  779:                targetInst, event) ;
        1:  780:        if (foundEvent) {
        1:  781:            eventQueueRemove(foundEvent) ;
        1:  782:            mechEventDelete(foundEvent) ;
        -:  783:        }
        -:  784:        /*
        -:  785:         * We can get here, without finding the event in the
        -:  786:         * delayed queue or the event queue.
        -:  787:         * That's okay, it just amounts to an expensive
        -:  788:         * no-op and implies that the event has expired,
        -:  789:         * was queued and has already been dispatched or
        -:  790:         * had never been generated at all.
        -:  791:         */
        -:  792:    }
        3:  793:    startDelayedQueueTiming() ;
        3:  794:}
        -:  795:MechDelayTime
        1:  796:mechEventDelayRemaining(
        -:  797:    EventCode event,
        -:  798:    MechInstance targetInst,
        -:  799:    MechInstance srcInst)
        -:  800:{
        1:  801:    assert(targetInst != NULL) ;
        -:  802:
        1:  803:    stopDelayedQueueTiming() ;
        -:  804:    /*
        -:  805:     * Iterate through the delayed event time and sum all
        -:  806:     * the delay times to give the total amount of time
        -:  807:     * remaining for the found event.
        -:  808:     */
        1:  809:    MechDelayTime remain = 0 ;
        -:  810:    MechEcb iter ;
        2:  811:    for (iter = eventQueueBegin(&delayedEventQueue) ;
        1:  812:            iter != eventQueueEnd(&delayedEventQueue) ;
    #####:  813:            iter = iter->next) {
        1:  814:        remain += iter->delay ;
        2:  815:        if (iter->srcInst == srcInst &&
        2:  816:                iter->instOrClass.targetInst == targetInst &&
        1:  817:                iter->eventNumber == event) {
        1:  818:            break ;
        -:  819:        }
        -:  820:    }
        1:  821:    startDelayedQueueTiming() ;
        -:  822:    /*
        -:  823:     * Return the amount of time remaining for the event.
        -:  824:     * If we didn't find the event, the just return 0.
        -:  825:     */
        1:  826:    return iter == eventQueueEnd(&delayedEventQueue) ?
        1:  827:            0 : mechTicksToMsec(remain) ;
        -:  828:}
        -:  829:static void
       13:  830:sysTimerMask(void)
        -:  831:{
        -:  832:    /*
        -:  833:     * Make sure SIGALRM does not go off.
        -:  834:     */
        -:  835:    sigset_t mask ;
       13:  836:    sigemptyset(&mask) ;
       13:  837:    sigaddset(&mask, SIGALRM) ;
       13:  838:    if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0) {
    #####:  839:        mechFatalError(mechSignalOpFailed, strerror(errno)) ;
        -:  840:    }
       13:  841:}
        -:  842:
        -:  843:static void
       18:  844:sysTimerUnmask(void)
        -:  845:{
        -:  846:    /*
        -:  847:     * Allow SIGALRM to notify us.
        -:  848:     */
        -:  849:    sigset_t mask ;
       18:  850:    sigemptyset(&mask) ;
       18:  851:    sigaddset(&mask, SIGALRM) ;
       18:  852:    if (sigprocmask(SIG_UNBLOCK, &mask, NULL) != 0) {
    #####:  853:        mechFatalError(mechSignalOpFailed, strerror(errno)) ;
        -:  854:    }
       18:  855:}
        -:  856:static void
       12:  857:sysTimerStart(
        -:  858:    MechDelayTime time)
        -:  859:{
        -:  860:    struct itimerval delayedEventTimer ;
        -:  861:
       12:  862:    delayedEventTimer.it_interval.tv_sec = 0 ;
       12:  863:    delayedEventTimer.it_interval.tv_usec = 0 ;
       12:  864:    delayedEventTimer.it_value.tv_sec = time / 1000 ;
       12:  865:    delayedEventTimer.it_value.tv_usec = (time % 1000) * 1000 ;
        -:  866:
       12:  867:    if (setitimer(ITIMER_REAL, &delayedEventTimer, NULL) != 0) {
    #####:  868:        mechFatalError(mechTimerOpFailed, strerror(errno)) ;
        -:  869:    }
       12:  870:    sysTimerUnmask() ;
       12:  871:}
        -:  872:static MechDelayTime
        7:  873:sysTimerStop(void)
        -:  874:{
        7:  875:    sysTimerMask() ;
        -:  876:    /*
        -:  877:     * Fetch the remaining time.
        -:  878:     */
        -:  879:    struct itimerval delayedEventTimer ;
        7:  880:    if (getitimer(ITIMER_REAL, &delayedEventTimer) != 0) {
    #####:  881:        mechFatalError(mechTimerOpFailed, strerror(errno)) ;
        -:  882:    }
        -:  883:    /*
        -:  884:     * Convert the returned time into milliseconds.
        -:  885:     */
        7:  886:    MechDelayTime remain =
       14:  887:            delayedEventTimer.it_value.tv_sec * 1000 +
        7:  888:            delayedEventTimer.it_value.tv_usec / 1000 ;
        -:  889:    /*
        -:  890:     * Set the current timer value to zero to turn it off.
        -:  891:     */
        7:  892:    memset(&delayedEventTimer, 0, sizeof(delayedEventTimer)) ;
        7:  893:    if (setitimer(ITIMER_REAL, &delayedEventTimer, NULL) != 0) {
    #####:  894:        mechFatalError(mechTimerOpFailed, strerror(errno)) ;
        -:  895:    }
        -:  896:
        7:  897:    return remain ;
        -:  898:}
        -:  899:static void
        6:  900:sysTimerExpire(
        -:  901:    int signum)
        -:  902:{
        6:  903:    MechDelayTime nextTime = mechTimerExpireService() ;
        6:  904:    if (nextTime != 0) {
        2:  905:        sysTimerStart(nextTime) ;
        -:  906:    }
        6:  907:}
        -:  908:static void
        1:  909:sysTimerInit(void)
        -:  910:{
        1:  911:    mechRegisterSignal(SIGALRM, sysTimerExpire) ;
        1:  912:}
        -:  913:static void
        6:  914:mechExpiredEventService(
        -:  915:    SyncParamRef params) /* Not used */
        -:  916:{
        -:  917:    /*
        -:  918:     * Since the expired event queue is accessed here,
        -:  919:     * we must make sure that the timer interrupt does not
        -:  920:     * go off.
        -:  921:     */
        6:  922:    sysTimerMask() ;
        6:  923:    transferExpiredEvents() ;
        6:  924:    sysTimerUnmask() ;
        6:  925:}
        -:  926:MechDelayTime
        6:  927:mechTimerExpireService(void)
        -:  928:{
        -:  929:    MechEcb unexpired ;
        -:  930:    MechDelayTime nextTime ;
        -:  931:    /*
        -:  932:     * Sync to the background to request the expired events
        -:  933:     * be transferred to the event queue.
        -:  934:     */
        6:  935:    mechSyncRequest(mechExpiredEventService) ;
        -:  936:    /*
        -:  937:     * Mark the delayed events as expired, returning a
        -:  938:     * pointer to the first unexpired event.
        -:  939:     */
        6:  940:    unexpired = expireDelayedEvents() ;
        6:  941:    if (unexpired) {
        -:  942:        /*
        -:  943:         * If there is an unexpired event, then its delay
        -:  944:         * time is the next time to expire. We return that
        -:  945:         * time and zero out the delay time.
        -:  946:         */
        2:  947:        assert(unexpired->delay != 0) ;
        2:  948:        nextTime = unexpired->delay ;
        2:  949:        unexpired->delay = 0 ;
        -:  950:    } else {
        -:  951:        /*
        -:  952:         * Otherwise, there is nothing else to time.
        -:  953:         */
        4:  954:        nextTime = 0 ;
        -:  955:    }
        -:  956:
        6:  957:    return nextTime ;
        -:  958:}
        -:  959:#ifdef MECH_SM_TRACE
        -:  960:static MechTraceCallback traceCallback ;
        -:  961:
        -:  962:MechTraceCallback
        -:  963:mechRegisterTrace(
        -:  964:    MechTraceCallback cb)
        -:  965:{
        -:  966:    MechTraceCallback oldcb = traceCallback ;
        -:  967:    traceCallback = cb ;
        -:  968:    return oldcb ;
        -:  969:}
        -:  970:static inline 
        -:  971:void
        -:  972:traceNormalEvent(
        -:  973:    EventCode event,
        -:  974:    MechInstance source,
        -:  975:    MechInstance target,
        -:  976:    StateCode currentState,
        -:  977:    StateCode newState)
        -:  978:{
        -:  979:    if (traceCallback) {
        -:  980:        struct mechtraceinfo trace ;
        -:  981:
        -:  982:        trace.eventType = NormalEvent ;
        -:  983:        trace.eventNumber = event ;
        -:  984:        trace.srcInst = source ;
        -:  985:        trace.dstInst = target ;
        -:  986:        trace.info.normalTrace.currState = currentState ;
        -:  987:        trace.info.normalTrace.newState = newState ;
        -:  988:
        -:  989:        traceCallback(&trace) ;
        -:  990:    }
        -:  991:}
        -:  992:static inline 
        -:  993:void
        -:  994:tracePolyEvent(
        -:  995:    EventCode event,
        -:  996:    MechInstance source,
        -:  997:    MechInstance target,
        -:  998:    SubtypeCode subtype,
        -:  999:    DispatchCount hierarchy,
        -: 1000:    EventCode newEvent,
        -: 1001:    MechEventType newEventType)
        -: 1002:{
        -: 1003:    if (traceCallback) {
        -: 1004:        struct mechtraceinfo trace ;
        -: 1005:
        -: 1006:        trace.eventType = PolymorphicEvent ;
        -: 1007:        trace.eventNumber = event ;
        -: 1008:        trace.srcInst = source ;
        -: 1009:        trace.dstInst = target ;
        -: 1010:        trace.info.polyTrace.subcode = subtype ;
        -: 1011:        trace.info.polyTrace.hierarchy = hierarchy ;
        -: 1012:        trace.info.polyTrace.mappedNumber = newEvent ;
        -: 1013:        trace.info.polyTrace.mappedType = newEventType ;
        -: 1014:
        -: 1015:        traceCallback(&trace) ;
        -: 1016:    }
        -: 1017:}
        -: 1018:static inline 
        -: 1019:void
        -: 1020:traceCreationEvent(
        -: 1021:    EventCode event,
        -: 1022:    MechInstance source,
        -: 1023:    MechInstance target,
        -: 1024:    MechClass class)
        -: 1025:{
        -: 1026:    if (traceCallback) {
        -: 1027:        struct mechtraceinfo trace ;
        -: 1028:
        -: 1029:        trace.eventType = CreationEvent ;
        -: 1030:        trace.eventNumber = event ;
        -: 1031:        trace.srcInst = source ;
        -: 1032:        trace.dstInst = target ;
        -: 1033:        trace.info.creationTrace.dstClass = class ;
        -: 1034:
        -: 1035:        traceCallback(&trace) ;
        -: 1036:    }
        -: 1037:}
        -: 1038:#endif /* MECH_SM_TRACE */
        -: 1039:static void dispatchNormalEvent(MechEcb) ;
        -: 1040:static void dispatchPolyEvent(MechEcb) ;
        -: 1041:static void dispatchCreationEvent(MechEcb) ;
        -: 1042:static void (* const ecbDispatchFuncs[])(MechEcb) = {
        -: 1043:    dispatchNormalEvent,
        -: 1044:    dispatchPolyEvent,
        -: 1045:    dispatchCreationEvent,
        -: 1046:} ;
        -: 1047:static void
       23: 1048:mechDispatch(
        -: 1049:    MechEcb ecb)
        -: 1050:{
       23: 1051:    ecbDispatchFuncs[ecb->eventType](ecb) ;
       23: 1052:}
        -: 1053:static void
       21: 1054:dispatchNormalEvent(
        -: 1055:    MechEcb ecb)
        -: 1056:{
       21: 1057:    MechInstance target = ecb->instOrClass.targetInst ;
       21: 1058:    ObjectDispatchBlock db = target->instClass->odb ;
        -: 1059:
        -: 1060:    /*
        -: 1061:     * Test for corruption of the current state
        -: 1062:     * or event number.
        -: 1063:     */
       21: 1064:    assert(db->stateCount > target->currentState) ;
       21: 1065:    assert(db->eventCount > ecb->eventNumber) ;
        -: 1066:    /*
        -: 1067:     * Check for the "event-in-flight" error. This occurs
        -: 1068:     * when an instance is deleted while there is an event
        -: 1069:     * for that instance in the event queue.  For this
        -: 1070:     * architecture, such occurrences are considered as
        -: 1071:     * run-time detected analysis errors.
        -: 1072:     */
       21: 1073:    if (target->alloc != ecb->alloc) {
    #####: 1074:        mechFatalError(mechEventInFlight, ecb->srcInst,
    #####: 1075:                target, ecb->eventNumber) ;
        -: 1076:    }
        -: 1077:    /*
        -: 1078:     * Fetch the new state from the transition table.
        -: 1079:     */
       42: 1080:    StateCode newState = *(db->transitionTable +
       42: 1081:            target->currentState * db->eventCount +
       21: 1082:            ecb->eventNumber) ;
        -: 1083:#       ifdef MECH_SM_TRACE
        -: 1084:    /*
        -: 1085:     * Trace the transition.
        -: 1086:     */
        -: 1087:    traceNormalEvent(ecb->eventNumber, ecb->srcInst,
        -: 1088:            ecb->instOrClass.targetInst, target->currentState, newState) ;
        -: 1089:#       endif
        -: 1090:    /*
        -: 1091:     * Check for a can't happen transition.
        -: 1092:     */
       21: 1093:    if (newState == MECH_STATECODE_CH) {
        2: 1094:        mechFatalError(mechCantHappen, target,
        2: 1095:                target->currentState, ecb->eventNumber) ;
       20: 1096:    } else if (newState != MECH_STATECODE_IG) {
        -: 1097:        /*
        -: 1098:         * Check for corrupt transition table.
        -: 1099:         */
       20: 1100:        assert(newState < db->stateCount) ;
        -: 1101:        /*
        -: 1102:         * We update the current state to reflect the
        -: 1103:         * transition before executing the action for the
        -: 1104:         * state.
        -: 1105:         */
       20: 1106:        target->currentState = newState ;
        -: 1107:        /*
        -: 1108:         * Invoke the state action if there is one.
        -: 1109:         */
       20: 1110:        PtrActionFunction action = db->actionTable[newState] ;
       20: 1111:        if (action) {
       20: 1112:            action(target, &ecb->eventParameters) ;
        -: 1113:        }
        -: 1114:        /*
        -: 1115:         * Check if we have entered a final state. If so,
        -: 1116:         * the instance is deleted.
        -: 1117:         */
       20: 1118:        if (db->finalStates && db->finalStates[newState]) {
        1: 1119:            mechInstDestroy(target) ;
        -: 1120:        }
        -: 1121:    }
        -: 1122:    /*
        -: 1123:     * Return the ECB to the pool.
        -: 1124:     */
       21: 1125:    mechEventDelete(ecb) ;
       21: 1126:}
        -: 1127:static void
        2: 1128:dispatchPolyEvent(
        -: 1129:    MechEcb ecb)
        -: 1130:{
        2: 1131:    PolyDispatchBlock pdb = ecb->instOrClass.targetInst->instClass->pdb ;
        -: 1132:
        2: 1133:    assert(pdb != NULL) ;
        2: 1134:    assert(ecb->eventNumber < pdb->eventCount) ;
        2: 1135:    assert(pdb->hierCount > 0) ;
        -: 1136:    /*
        -: 1137:     * Each generalization hierarchy that originates at the
        -: 1138:     * supertype has an event generated down that
        -: 1139:     * hierarchy to one of the subtypes.
        -: 1140:     */
        2: 1141:    HierarchyDispatch hd = pdb->hierarchy ;
        4: 1142:    for (unsigned hnum = 0 ; hnum < pdb->hierCount ; ++hnum) {
        -: 1143:        /*
        -: 1144:         * The most common case is to dispatch along a
        -: 1145:         * single hierarchy.  In any case, we can modify in
        -: 1146:         * place the input ECB on the last dispatched event.
        -: 1147:         */
        -: 1148:        MechEcb newEcb ;
        2: 1149:        if (hnum == pdb->hierCount - 1) {
        2: 1150:            newEcb = ecb ;
        -: 1151:        } else {
    #####: 1152:            newEcb = mechEventAlloc() ;
        -: 1153:            /*
        -: 1154:             * We set the source as the original sender.
        -: 1155:             */
    #####: 1156:            newEcb->srcInst = ecb->srcInst ;
        -: 1157:            /*
        -: 1158:             * Copy event parameters.
        -: 1159:             */
    #####: 1160:            newEcb->eventParameters = ecb->eventParameters ;
        -: 1161:        }
        2: 1162:        SubtypeCode type =
        4: 1163:                *(SubtypeCode *)((char *)ecb->instOrClass.targetInst +
        2: 1164:                hd->subCodeOffset) ;
        -: 1165:
        2: 1166:        assert(type < hd->subtypeCount) ;
        4: 1167:        PolyEventMap pem = hd->eventMap +
        2: 1168:                (type * pdb->eventCount + ecb->eventNumber) ;
        -: 1169:#           ifdef MECH_SM_TRACE
        -: 1170:        /*
        -: 1171:         * Trace the transition.
        -: 1172:         */
        -: 1173:        tracePolyEvent(ecb->eventNumber, ecb->srcInst,
        -: 1174:                ecb->instOrClass.targetInst, type, hnum,
        -: 1175:                pem->event, pem->eventType) ;
        -: 1176:#           endif /* MECH_SM_TRACE */
        -: 1177:
        2: 1178:        newEcb->eventNumber = pem->event ;
        2: 1179:        newEcb->eventType = pem->eventType ;
        -: 1180:
        2: 1181:        void *subTypeRef =
        2: 1182:                (char *)ecb->instOrClass.targetInst + hd->subInstOffset ;
        4: 1183:        newEcb->instOrClass.targetInst = hd->refStorage == PolyReference ?
        -: 1184:            /*
        -: 1185:             * When the generalization is implemented via a
        -: 1186:             * pointer, we need an extra level of
        -: 1187:             * indirection to fetch the address of the
        -: 1188:             * subtype.
        -: 1189:             */
        2: 1190:            *(MechInstance *)subTypeRef :
        -: 1191:            /*
        -: 1192:             * When the generalization is implemented by a
        -: 1193:             * union, we need only point to the address of
        -: 1194:             * the subtype as it is contained in the
        -: 1195:             * supertype.
        -: 1196:             */
        -: 1197:            (MechInstance)subTypeRef ;
        -: 1198:
        2: 1199:        if (newEcb->eventType == NormalEvent) {
        2: 1200:            newEcb->alloc = newEcb->instOrClass.targetInst->alloc ;
        2: 1201:            assert(newEcb->alloc != 0) ;
        -: 1202:        }
        -: 1203:
        2: 1204:        mechDispatch(newEcb) ;
        2: 1205:        ++hd ;
        -: 1206:    }
        2: 1207:}
        -: 1208:static void
        2: 1209:dispatchCreationEvent(
        -: 1210:    MechEcb ecb)
        -: 1211:{
        -: 1212:    /*
        -: 1213:     * For creation events we must allocate an instance,
        -: 1214:     * set the state to be the creation state (by
        -: 1215:     * convention the creation state is 0).
        -: 1216:     */
        2: 1217:    MechInstance inst = mechInstCreate(ecb->instOrClass.targetClass,
        -: 1218:            MECH_DISPATCH_CREATION_STATE) ;
        -: 1219:#           ifdef MECH_SM_TRACE
        -: 1220:    /*
        -: 1221:     * Trace the transition.
        -: 1222:     */
        -: 1223:    traceCreationEvent(ecb->eventNumber, ecb->srcInst,
        -: 1224:            inst, ecb->instOrClass.targetClass) ;
        -: 1225:#           endif
        -: 1226:    /*
        -: 1227:     * Modify the event structure in place and dispatch it.
        -: 1228:     */
        2: 1229:    ecb->instOrClass.targetInst = inst ;
        2: 1230:    ecb->eventType = NormalEvent ;
        2: 1231:    ecb->alloc = ecb->instOrClass.targetInst->alloc ;
        2: 1232:    assert(ecb->alloc != 0) ;
        -: 1233:
        2: 1234:    dispatchNormalEvent(ecb) ;
        2: 1235:}
        -: 1236:typedef struct fgsyncblock {
        -: 1237:    SyncFunc function ;
        -: 1238:    SyncParamType params ;
        -: 1239:} *FgSyncBlock ;
        -: 1240:struct syncqueue {
        -: 1241:    FgSyncBlock head ;
        -: 1242:    FgSyncBlock tail ;
        -: 1243:} ;
        -: 1244:static struct fgsyncblock
        -: 1245:mechSyncQueueStorage[MECH_SYNCQUEUESIZE] ;
        -: 1246:static struct syncqueue mechSyncQueue = {
        -: 1247:    .head = mechSyncQueueStorage,
        -: 1248:    .tail = mechSyncQueueStorage,
        -: 1249:} ;
        -: 1250:static inline
        -: 1251:bool
       24: 1252:syncQueueEmpty(void)
        -: 1253:{
       24: 1254:    return mechSyncQueue.head == mechSyncQueue.tail ;
        -: 1255:}
        -: 1256:static inline
        -: 1257:SyncParamRef
        6: 1258:syncQueuePut(
        -: 1259:    SyncFunc f,
        -: 1260:    bool fatal)
        -: 1261:{
        6: 1262:    FgSyncBlock tail = mechSyncQueue.tail ;
       12: 1263:    if (++mechSyncQueue.tail >=
        6: 1264:            mechSyncQueueStorage + MECH_SYNCQUEUESIZE) {
    #####: 1265:        mechSyncQueue.tail = mechSyncQueueStorage ;
        -: 1266:    }
        6: 1267:    if (syncQueueEmpty()) {
    #####: 1268:        if (fatal) {
    #####: 1269:            mechFatalError(mechSyncOverflow) ;
        -: 1270:        }
    #####: 1271:        return NULL ;
        -: 1272:    }
        -: 1273:
        6: 1274:    tail->function = f ;
        6: 1275:    return &tail->params ;
        -: 1276:}
        -: 1277:static inline
        -: 1278:FgSyncBlock
       11: 1279:syncQueueGet(void)
        -: 1280:{
        -: 1281:    FgSyncBlock head ;
        -: 1282:
       11: 1283:    beginCriticalSection() ;
       11: 1284:    if (syncQueueEmpty()) {
        5: 1285:        head = NULL ;
        -: 1286:    } else {
        6: 1287:        head = mechSyncQueue.head ;
       12: 1288:        if (++mechSyncQueue.head >=
        6: 1289:                mechSyncQueueStorage + MECH_SYNCQUEUESIZE) {
    #####: 1290:            mechSyncQueue.head = mechSyncQueueStorage ;
        -: 1291:        }
        -: 1292:    }
       11: 1293:    endCriticalSection() ;
        -: 1294:
       11: 1295:    return head ;
        -: 1296:}
        -: 1297:SyncParamRef
        6: 1298:mechSyncRequest(
        -: 1299:    SyncFunc f)
        -: 1300:{
        6: 1301:    return syncQueuePut(f, true) ;
        -: 1302:}
        -: 1303:SyncParamRef
    #####: 1304:mechTrySyncRequest(
        -: 1305:    SyncFunc f)
        -: 1306:{
    #####: 1307:    return syncQueuePut(f, false) ;
        -: 1308:}
        -: 1309:void
        1: 1310:mechRegisterSignal(
        -: 1311:    int sigNum,
        -: 1312:    SignalFunc func)
        -: 1313:{
        1: 1314:    assert(sigNum > 0) ;
        -: 1315:
        -: 1316:    struct sigaction action ;
        1: 1317:    if (func) {
        1: 1318:        action.sa_handler = func ;
        1: 1319:        sigaddset(&mechSigMask, sigNum) ;
        -: 1320:    } else {
    #####: 1321:        action.sa_handler = SIG_DFL ;
    #####: 1322:        sigdelset(&mechSigMask, sigNum) ;
        -: 1323:    }
        1: 1324:    sigfillset(&action.sa_mask) ;
        1: 1325:    action.sa_flags = 0 ;
        -: 1326:
        1: 1327:    int sigresult = sigaction(sigNum, &action, NULL) ;
        1: 1328:    if (sigresult != 0) {
    #####: 1329:        mechFatalError(mechSignalOpFailed, strerror(errno)) ;
        -: 1330:    }
        1: 1331:}
        -: 1332:typedef struct fdservicemap {
        -: 1333:    bool set ;
        -: 1334:    FDServiceFunc read ;
        -: 1335:    FDServiceFunc write ;
        -: 1336:    FDServiceFunc except ;
        -: 1337:} *FDServiceMap ;
        -: 1338:static struct fdservicemap mechFDServicePool[FD_SETSIZE] ;
        -: 1339:static int mechMaxFD = -1 ;
        -: 1340:static fd_set mechReadFDS ;
        -: 1341:static fd_set mechWriteFDS ;
        -: 1342:static fd_set mechExceptFDS ;
        -: 1343:void
        1: 1344:mechRegisterFDService(
        -: 1345:    int fd,
        -: 1346:    FDServiceFunc readService,
        -: 1347:    FDServiceFunc writeService,
        -: 1348:    FDServiceFunc exceptService)
        -: 1349:{
        1: 1350:    assert(fd >= 0 && fd < FD_SETSIZE) ;
        1: 1351:    FDServiceMap fds = mechFDServicePool + fd ;
        -: 1352:
        1: 1353:    fds->read = readService ;
        1: 1354:    if (readService) {
    #####: 1355:        FD_SET(fd, &mechReadFDS) ;
    #####: 1356:        fds->set = true ;
        -: 1357:    } else {
        1: 1358:        FD_CLR(fd, &mechReadFDS) ;
        -: 1359:    }
        -: 1360:
        1: 1361:    fds->write = writeService ;
        1: 1362:    if (writeService) {
        1: 1363:        FD_SET(fd, &mechWriteFDS) ;
        1: 1364:        fds->set = true ;
        -: 1365:    } else {
    #####: 1366:        FD_CLR(fd, &mechWriteFDS) ;
        -: 1367:    }
        -: 1368:
        1: 1369:    fds->except = exceptService ;
        1: 1370:    if (exceptService) {
    #####: 1371:        FD_SET(fd, &mechExceptFDS) ;
    #####: 1372:        fds->set = true ;
        -: 1373:    } else {
        1: 1374:        FD_CLR(fd, &mechExceptFDS) ;
        -: 1375:    }
        -: 1376:
        1: 1377:    if (fds->read == NULL && fds->write == NULL && fds-> except == NULL) {
    #####: 1378:        if (fds->set && fd >= mechMaxFD) {
    #####: 1379:            --mechMaxFD ;
        -: 1380:        }
    #####: 1381:        fds->set = false ;
        1: 1382:    } else if (fds->set && fd > mechMaxFD) {
        1: 1383:        mechMaxFD = fd ;
        -: 1384:    }
        1: 1385:}
        -: 1386:void
        1: 1387:mechRemoveFDService(
        -: 1388:    int fd,
        -: 1389:    bool rmRead,
        -: 1390:    bool rmWrite,
        -: 1391:    bool rmExcept)
        -: 1392:{
        1: 1393:    assert(fd >= 0 && fd < FD_SETSIZE) ;
        1: 1394:    FDServiceMap fds = mechFDServicePool + fd ;
        -: 1395:
        1: 1396:    if (rmRead) {
    #####: 1397:        fds->read = NULL ;
    #####: 1398:        FD_CLR(fd, &mechReadFDS) ;
        -: 1399:    }
        -: 1400:
        1: 1401:    if (rmWrite) {
        1: 1402:        fds->write = NULL ;
        1: 1403:        FD_CLR(fd, &mechWriteFDS) ;
        -: 1404:    }
        -: 1405:
        1: 1406:    if (rmExcept) {
    #####: 1407:        fds->except = NULL ;
    #####: 1408:        FD_CLR(fd, &mechExceptFDS) ;
        -: 1409:    }
        -: 1410:
        2: 1411:    if (fds->read == NULL && fds->write == NULL && fds-> except == NULL &&
        1: 1412:            fd >= mechMaxFD) {
        1: 1413:        mechMaxFD = fd - 1 ;
        -: 1414:    }
        1: 1415:}
        -: 1416:static void
        1: 1417:fdServiceInit(void)
        -: 1418:{
        1: 1419:    FD_ZERO(&mechReadFDS) ;
        1: 1420:    FD_ZERO(&mechWriteFDS) ;
        1: 1421:    FD_ZERO(&mechExceptFDS) ;
        1: 1422:}
        -: 1423:MECH_TEST_STATIC
        -: 1424:void
        7: 1425:mechWait(void)
        -: 1426:{
        7: 1427:    beginCriticalSection() ;
        7: 1428:    if (syncQueueEmpty()) {
        -: 1429:        /*
        -: 1430:         * Copy the file descriptor sets since "pselect"
        -: 1431:         * modifies them in place upon return.
        -: 1432:         */
        -: 1433:        fd_set readfds ;
        7: 1434:        memcpy(&readfds, &mechReadFDS, sizeof(readfds)) ;
        -: 1435:        fd_set writefds ;
        7: 1436:        memcpy(&writefds, &mechWriteFDS, sizeof(writefds)) ;
        -: 1437:        fd_set exceptfds ;
        7: 1438:        memcpy(&exceptfds, &mechExceptFDS, sizeof(exceptfds)) ;
        -: 1439:        /*
        -: 1440:         * Allow all the signals during the select. We
        -: 1441:         * assume that when we enter this function, the
        -: 1442:         * registered signals are masked.
        -: 1443:         */
        -: 1444:        sigset_t mask ;
        7: 1445:        sigemptyset(&mask) ;
        -: 1446:        /*
        -: 1447:         * "mechMaxFD" holds the maximum value of any
        -: 1448:         * registered file descriptor. We must add one to
        -: 1449:         * get the number of file descriptors "pselect" is
        -: 1450:         * to consider.
        -: 1451:         */
        7: 1452:        int r = pselect(mechMaxFD + 1, &readfds, &writefds,
        -: 1453:                &exceptfds, NULL, &mask) ;
        7: 1454:        if (r == -1) {
        6: 1455:            if (errno != EINTR) {
    #####: 1456:                mechFatalError(mechSelectWaitFailed,
    #####: 1457:                        strerror(errno)) ;
        -: 1458:            }
        -: 1459:            /*
        -: 1460:             * Got a signal while waiting. We go back to the
        -: 1461:             * main loop on the assumption that something
        -: 1462:             * has been placed in the sync queue.
        -: 1463:             */
        -: 1464:        } else {
        -: 1465:            /*
        -: 1466:             * Dispatch the service functions for the file
        -: 1467:             * descriptors.
        -: 1468:             */
        1: 1469:            FDServiceMap s = mechFDServicePool ;
        4: 1470:            for (int fd = 0 ; r > 0 && fd <= mechMaxFD ;
        2: 1471:                    ++fd, ++s) {
        -: 1472:                /*
        -: 1473:                 * Do exceptions first. This is only
        -: 1474:                 * important for sockets, but without going
        -: 1475:                 * first the OOB data processing won't
        -: 1476:                 * work.
        -: 1477:                 */
        2: 1478:                if (FD_ISSET(fd, &exceptfds)) {
    #####: 1479:                    assert(s->except != NULL) ;
    #####: 1480:                    s->except(fd) ;
    #####: 1481:                    --r ;
        -: 1482:                }
        2: 1483:                if (FD_ISSET(fd, &readfds)) {
    #####: 1484:                    assert(s->read != NULL) ;
    #####: 1485:                    s->read(fd) ;
    #####: 1486:                    --r ;
        -: 1487:                }
        2: 1488:                if (FD_ISSET(fd, &writefds)) {
        1: 1489:                    assert(s->write != NULL) ;
        1: 1490:                    s->write(fd) ;
        1: 1491:                    --r ;
        -: 1492:                }
        -: 1493:            }
        -: 1494:        }
        -: 1495:    }
        7: 1496:    endCriticalSection() ;
        7: 1497:}
        -: 1498:static void
        1: 1499:sysPlatformInit(void)
        -: 1500:{
        1: 1501:    fdServiceInit() ;
        1: 1502:}
        -: 1503:MECH_TEST_STATIC
        -: 1504:MECH_TEST_INLINE
        -: 1505:void
        1: 1506:mechInit(void)
        -: 1507:{
        1: 1508:    sysPlatformInit() ;
        1: 1509:    mechEventInit() ;
        1: 1510:    initCriticalSection() ;
        1: 1511:    sysTimerInit() ;
        1: 1512:    sysDeviceInit() ;
        1: 1513:    sysDomainInit() ;
        1: 1514:}
        -: 1515:MECH_TEST_STATIC
        -: 1516:MECH_TEST_INLINE
        -: 1517:bool
       11: 1518:mechInvokeOneSyncFunc(void)
        -: 1519:{
        -: 1520:    bool didOne ;
       11: 1521:    FgSyncBlock blk = syncQueueGet() ;
       11: 1522:    if (blk && blk->function) {
        6: 1523:        blk->function(&blk->params) ;
        6: 1524:        didOne = true ;
        -: 1525:    } else {
        5: 1526:        didOne = false ;
        -: 1527:    }
       11: 1528:    return didOne ;
        -: 1529:}
        -: 1530:MECH_TEST_STATIC
        -: 1531:MECH_TEST_INLINE
        -: 1532:bool
       29: 1533:mechDispatchOneEvent(void)
        -: 1534:{
       29: 1535:    bool didOne = !eventQueueEmpty(&eventQueue) ;
       29: 1536:    if (didOne) {
       21: 1537:        MechEcb ecb = eventQueue.next ;
       21: 1538:        eventQueueRemove(ecb) ;
       21: 1539:        mechDispatch(ecb) ;
        -: 1540:    }
       29: 1541:    return didOne ;
        -: 1542:}
        -: 1543:#ifdef MECH_TEST
        -: 1544:void
    #####: 1545:stsa_main(void)
        -: 1546:#else
        -: 1547:int
        -: 1548:main(void)
        -: 1549:#endif /* MECH_TEST */
        -: 1550:{
    #####: 1551:    mechInit() ;
        -: 1552:
        -: 1553:    for (;;) { /* Infinite Big Loop */
        -: 1554:        /*
        -: 1555:         * Empty the foreground/background sync queue.
        -: 1556:         */
        -: 1557:        #ifndef __ARM_ARCH_7M__
        -: 1558:        /*
        -: 1559:         * Empty the foreground / background
        -: 1560:         * synchronization queue.
        -: 1561:         */
    #####: 1562:        while (mechInvokeOneSyncFunc()) {
        -: 1563:            ; /* empty */
        -: 1564:        }
        -: 1565:        #endif /* __ARM_ARCH_7M__ */
        -: 1566:        /*
        -: 1567:         * Dispatch one event from the event queue.
        -: 1568:         */
    #####: 1569:        if (!mechDispatchOneEvent()) {
        -: 1570:            /*
        -: 1571:             * Check if this thread of control is complete
        -: 1572:             * and wait if there is no additional work to
        -: 1573:             * be done.
        -: 1574:             */
    #####: 1575:            mechWait() ;
        -: 1576:        }
        -: 1577:    }
        -: 1578:}