Files @ 95e019faaf52
Branch filter:

Location: CSY/reowolf/src/runtime2/component/component_pdl.rs

95e019faaf52 46.4 KiB application/rls-services+xml Show Annotation Show as Raw Download as Raw
mh
Getting builtin component instantiation to compile
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
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
use crate::random::Random;
use crate::protocol::*;
use crate::protocol::ast::ProcedureDefinitionId;
use crate::protocol::eval::{
    PortId as EvalPortId, Prompt,
    ValueGroup, Value,
    EvalContinuation, EvalResult, EvalError
};

use crate::runtime2::scheduler::SchedulerCtx;
use crate::runtime2::communication::*;

use super::component::*;
use super::component_context::*;
use super::control_layer::*;
use super::consensus::Consensus;

pub enum ExecStmt {
    CreatedChannel((Value, Value)),
    PerformedPut,
    PerformedGet(ValueGroup),
    PerformedSelectWait(u32),
    None,
}

impl ExecStmt {
    fn take(&mut self) -> ExecStmt {
        let mut value = ExecStmt::None;
        std::mem::swap(self, &mut value);
        return value;
    }

    fn is_none(&self) -> bool {
        match self {
            ExecStmt::None => return true,
            _ => return false,
        }
    }
}

pub struct ExecCtx {
    stmt: ExecStmt,
}

impl RunContext for ExecCtx {
    fn performed_put(&mut self, _port: EvalPortId) -> bool {
        match self.stmt.take() {
            ExecStmt::None => return false,
            ExecStmt::PerformedPut => return true,
            _ => unreachable!(),
        }
    }

    fn performed_get(&mut self, _port: EvalPortId) -> Option<ValueGroup> {
        match self.stmt.take() {
            ExecStmt::None => return None,
            ExecStmt::PerformedGet(value) => return Some(value),
            _ => unreachable!(),
        }
    }

    fn fires(&mut self, _port: EvalPortId) -> Option<Value> {
        todo!("remove fires")
    }

    fn performed_fork(&mut self) -> Option<bool> {
        todo!("remove fork")
    }

    fn created_channel(&mut self) -> Option<(Value, Value)> {
        match self.stmt.take() {
            ExecStmt::None => return None,
            ExecStmt::CreatedChannel(ports) => return Some(ports),
            _ => unreachable!(),
        }
    }

    fn performed_select_wait(&mut self) -> Option<u32> {
        match self.stmt.take() {
            ExecStmt::None => return None,
            ExecStmt::PerformedSelectWait(selected_case) => Some(selected_case),
            _v => unreachable!(),
        }
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub(crate) enum Mode {
    NonSync, // not in sync mode
    Sync, // in sync mode, can interact with other components
    SyncEnd, // awaiting a solution, i.e. encountered the end of the sync block
    BlockedGet, // blocked because we need to receive a message on a particular port
    BlockedPut, // component is blocked because the port is blocked
    BlockedSelect, // waiting on message to complete the select statement
    StartExit, // temporary state: if encountered then we start the shutdown process
    BusyExit, // temporary state: waiting for Acks for all the closed ports
    Exit, // exiting: shutdown process started, now waiting until the reference count drops to 0
}

impl Mode {
    fn is_in_sync_block(&self) -> bool {
        use Mode::*;

        match self {
            Sync | SyncEnd | BlockedGet | BlockedPut | BlockedSelect => true,
            NonSync | StartExit | BusyExit | Exit => false,
        }
    }
}

struct SelectCase {
    involved_ports: Vec<LocalPortHandle>,
}

// TODO: @Optimize, flatten cases into single array, have index-pointers to next case
struct SelectState {
    cases: Vec<SelectCase>,
    next_case: u32,
    num_cases: u32,
    random: Random,
    candidates_workspace: Vec<usize>,
}

enum SelectDecision {
    None,
    Case(u32), // contains case index, should be passed along to PDL code
}

type InboxMain = Vec<Option<DataMessage>>;

impl SelectState {
    fn new() -> Self {
        return Self{
            cases: Vec::new(),
            next_case: 0,
            num_cases: 0,
            random: Random::new(),
            candidates_workspace: Vec::new(),
        }
    }

    fn handle_select_start(&mut self, num_cases: u32) {
        self.cases.clear();
        self.next_case = 0;
        self.num_cases = num_cases;
    }

    /// Register a port as belonging to a particular case. As for correctness of
    /// PDL code one cannot register the same port twice, this function might
    /// return an error
    fn register_select_case_port(&mut self, comp_ctx: &CompCtx, case_index: u32, _port_index: u32, port_id: PortId) -> Result<(), PortId> {
        // Retrieve case and port handle
        self.ensure_at_case(case_index);
        let cur_case = &mut self.cases[case_index as usize];
        let port_handle = comp_ctx.get_port_handle(port_id);
        debug_assert_eq!(cur_case.involved_ports.len(), _port_index as usize);

        // Make sure port wasn't added before, we disallow having the same port
        // in the same select guard twice.
        if cur_case.involved_ports.contains(&port_handle) {
            return Err(port_id);
        }

        cur_case.involved_ports.push(port_handle);
        return Ok(());
    }

    /// Notification that all ports have been registered and we should now wait
    /// until the appropriate messages have come in.
    fn handle_select_waiting_point(&mut self, inbox: &InboxMain, comp_ctx: &CompCtx) -> SelectDecision {
        if self.num_cases != self.next_case {
            // This happens when there are >=1 select cases written at the end
            // of the select block.
            self.ensure_at_case(self.num_cases - 1);
        }

        return self.has_decision(inbox, comp_ctx);
    }

    fn handle_updated_inbox(&mut self, inbox: &InboxMain, comp_ctx: &CompCtx) -> SelectDecision {
        return self.has_decision(inbox, comp_ctx);
    }

    /// Internal helper, pushes empty cases inbetween last case and provided new
    /// case index.
    fn ensure_at_case(&mut self, new_case_index: u32) {
        // Push an empty case for all intermediate cases that were not
        // registered with a port.
        debug_assert!(new_case_index >= self.next_case && new_case_index < self.num_cases);
        for _ in self.next_case..new_case_index + 1 {
            self.cases.push(SelectCase{ involved_ports: Vec::new() });
        }
        self.next_case = new_case_index + 1;
    }

    /// Checks if a decision can be reached
    fn has_decision(&mut self, inbox: &InboxMain, comp_ctx: &CompCtx) -> SelectDecision {
        self.candidates_workspace.clear();
        if self.cases.is_empty() {
            // If there are no cases then we can immediately reach a "bogus
            // decision".
            return SelectDecision::Case(0);
        }

        // Need to check for valid case
        'case_loop: for (case_index, case) in self.cases.iter().enumerate() {
            for port_handle in case.involved_ports.iter().copied() {
                let port_index = comp_ctx.get_port_index(port_handle);
                if inbox[port_index].is_none() {
                    // Condition not satisfied
                    continue 'case_loop;
                }
            }

            // If here then the case guard is satisfied
            self.candidates_workspace.push(case_index);
        }

        if self.candidates_workspace.is_empty() {
            return SelectDecision::None;
        } else {
            let candidate_index = self.random.get_u64() as usize % self.candidates_workspace.len();
            return SelectDecision::Case(self.candidates_workspace[candidate_index] as u32);
        }
    }
}

pub(crate) struct CompPDL {
    pub mode: Mode,
    pub mode_port: PortId, // when blocked on a port
    pub mode_value: ValueGroup, // when blocked on a put
    select: SelectState,
    pub prompt: Prompt,
    pub control: ControlLayer,
    pub consensus: Consensus,
    pub sync_counter: u32,
    pub exec_ctx: ExecCtx,
    // TODO: Temporary field, simulates future plans of having one storage place
    //  reserved per port.
    // Should be same length as the number of ports. Corresponding indices imply
    // message is intended for that port.
    pub inbox_main: InboxMain,
    pub inbox_backup: Vec<DataMessage>,
}

impl Component for CompPDL {
    fn adopt_message(&mut self, comp_ctx: &mut CompCtx, message: DataMessage) {
        let port_handle = comp_ctx.get_port_handle(message.data_header.target_port);
        let port_index = comp_ctx.get_port_index(port_handle);
        if self.inbox_main[port_index].is_none() {
            self.inbox_main[port_index] = Some(message);
        } else {
            self.inbox_backup.push(message);
        }
    }

    fn handle_message(&mut self, sched_ctx: &mut SchedulerCtx, comp_ctx: &mut CompCtx, mut message: Message) {
        sched_ctx.log(&format!("handling message: {:#?}", message));
        if let Some(new_target) = self.control.should_reroute(&mut message) {
            let mut target = sched_ctx.runtime.get_component_public(new_target);
            target.send_message(sched_ctx, message, false); // not waking up: we schedule once we've received all PortPeerChanged Acks
            let _should_remove = target.decrement_users();
            debug_assert!(_should_remove.is_none());
            return;
        }

        match message {
            Message::Data(message) => {
                self.handle_incoming_data_message(sched_ctx, comp_ctx, message);
            },
            Message::Control(message) => {
                self.handle_incoming_control_message(sched_ctx, comp_ctx, message);
            },
            Message::Sync(message) => {
                self.handle_incoming_sync_message(sched_ctx, comp_ctx, message);
            }
        }
    }

    fn run(&mut self, sched_ctx: &mut SchedulerCtx, comp_ctx: &mut CompCtx) -> Result<CompScheduling, EvalError> {
        use EvalContinuation as EC;

        sched_ctx.log(&format!("Running component (mode: {:?})", self.mode));

        // Depending on the mode don't do anything at all, take some special
        // actions, or fall through and run the PDL code.
        match self.mode {
            Mode::NonSync | Mode::Sync => {
                // continue and run PDL code
            },
            Mode::SyncEnd | Mode::BlockedGet | Mode::BlockedPut | Mode::BlockedSelect => {
                return Ok(CompScheduling::Sleep);
            }
            Mode::StartExit => {
                self.handle_component_exit(sched_ctx, comp_ctx);
                return Ok(CompScheduling::Immediate);
            },
            Mode::BusyExit => {
                if self.control.has_acks_remaining() {
                    return Ok(CompScheduling::Sleep);
                } else {
                    self.mode = Mode::Exit;
                    return Ok(CompScheduling::Exit);
                }
            },
            Mode::Exit => {
                return Ok(CompScheduling::Exit);
            }
        }

        let run_result = self.execute_prompt(&sched_ctx)?;

        match run_result {
            EC::Stepping => unreachable!(), // execute_prompt runs until this is no longer returned
            EC::BranchInconsistent | EC::NewFork | EC::BlockFires(_) => todo!("remove these"),
            // Results that can be returned in sync mode
            EC::SyncBlockEnd => {
                debug_assert_eq!(self.mode, Mode::Sync);
                self.handle_sync_end(sched_ctx, comp_ctx);
                return Ok(CompScheduling::Immediate);
            },
            EC::BlockGet(port_id) => {
                debug_assert_eq!(self.mode, Mode::Sync);
                debug_assert!(self.exec_ctx.stmt.is_none());

                let port_id = port_id_from_eval(port_id);
                let port_handle = comp_ctx.get_port_handle(port_id);
                let port_index = comp_ctx.get_port_index(port_handle);
                if let Some(message) = &self.inbox_main[port_index] {
                    // Check if we can actually receive the message
                    if self.consensus.try_receive_data_message(sched_ctx, comp_ctx, message) {
                        // Message was received. Make sure any blocked peers and
                        // pending messages are handled.
                        let message = self.inbox_main[port_index].take().unwrap();
                        self.handle_received_data_message(sched_ctx, comp_ctx, port_handle);

                        self.exec_ctx.stmt = ExecStmt::PerformedGet(message.content);
                        return Ok(CompScheduling::Immediate);
                    } else {
                        todo!("handle sync failure due to message deadlock");
                        return Ok(CompScheduling::Sleep);
                    }
                } else {
                    // We need to wait
                    self.mode = Mode::BlockedGet;
                    self.mode_port = port_id;
                    return Ok(CompScheduling::Sleep);
                }
            },
            EC::Put(port_id, value) => {
                debug_assert_eq!(self.mode, Mode::Sync);
                sched_ctx.log(&format!("Putting value {:?}", value));
                let port_id = port_id_from_eval(port_id);
                let port_handle = comp_ctx.get_port_handle(port_id);
                let port_info = comp_ctx.get_port(port_handle);
                if port_info.state.is_blocked() {
                    self.mode = Mode::BlockedPut;
                    self.mode_port = port_id;
                    self.mode_value = value;
                    self.exec_ctx.stmt = ExecStmt::PerformedPut; // prepare for when we become unblocked
                    return Ok(CompScheduling::Sleep);
                } else {
                    self.send_data_message_and_wake_up(sched_ctx, comp_ctx, port_handle, value);
                    self.exec_ctx.stmt = ExecStmt::PerformedPut;
                    return Ok(CompScheduling::Immediate);
                }
            },
            EC::SelectStart(num_cases, _num_ports) => {
                debug_assert_eq!(self.mode, Mode::Sync);
                self.select.handle_select_start(num_cases);
                return Ok(CompScheduling::Requeue);
            },
            EC::SelectRegisterPort(case_index, port_index, port_id) => {
                debug_assert_eq!(self.mode, Mode::Sync);
                let port_id = port_id_from_eval(port_id);
                if let Err(_err) = self.select.register_select_case_port(comp_ctx, case_index, port_index, port_id) {
                    todo!("handle registering a port multiple times");
                }
                return Ok(CompScheduling::Immediate);
            },
            EC::SelectWait => {
                debug_assert_eq!(self.mode, Mode::Sync);
                let select_decision = self.select.handle_select_waiting_point(&self.inbox_main, comp_ctx);
                if let SelectDecision::Case(case_index) = select_decision {
                    // Reached a conclusion, so we can continue immediately
                    self.exec_ctx.stmt = ExecStmt::PerformedSelectWait(case_index);
                    self.mode = Mode::Sync;
                    return Ok(CompScheduling::Immediate);
                } else {
                    // No decision yet
                    self.mode = Mode::BlockedSelect;
                    return Ok(CompScheduling::Sleep);
                }
            },
            // Results that can be returned outside of sync mode
            EC::ComponentTerminated => {
                self.mode = Mode::StartExit; // next call we'll take care of the exit
                return Ok(CompScheduling::Immediate);
            },
            EC::SyncBlockStart => {
                debug_assert_eq!(self.mode, Mode::NonSync);
                self.handle_sync_start(sched_ctx, comp_ctx);
                return Ok(CompScheduling::Immediate);
            },
            EC::NewComponent(definition_id, type_id, arguments) => {
                debug_assert_eq!(self.mode, Mode::NonSync);
                self.create_component_and_transfer_ports(
                    sched_ctx, comp_ctx,
                    definition_id, type_id, arguments
                );
                return Ok(CompScheduling::Requeue);
            },
            EC::NewChannel => {
                debug_assert_eq!(self.mode, Mode::NonSync);
                debug_assert!(self.exec_ctx.stmt.is_none());
                let channel = comp_ctx.create_channel();
                self.exec_ctx.stmt = ExecStmt::CreatedChannel((
                    Value::Output(port_id_to_eval(channel.putter_id)),
                    Value::Input(port_id_to_eval(channel.getter_id))
                ));
                self.inbox_main.push(None);
                self.inbox_main.push(None);
                return Ok(CompScheduling::Immediate);
            }
        }
    }
}

impl CompPDL {
    pub(crate) fn new(initial_state: Prompt, num_ports: usize) -> Self {
        let mut inbox_main = Vec::new();
        inbox_main.reserve(num_ports);
        for _ in 0..num_ports {
            inbox_main.push(None);
        }

        return Self{
            mode: Mode::NonSync,
            mode_port: PortId::new_invalid(),
            mode_value: ValueGroup::default(),
            select: SelectState::new(),
            prompt: initial_state,
            control: ControlLayer::default(),
            consensus: Consensus::new(),
            sync_counter: 0,
            exec_ctx: ExecCtx{
                stmt: ExecStmt::None,
            },
            inbox_main,
            inbox_backup: Vec::new(),
        }
    }

    // -------------------------------------------------------------------------
    // Running component and handling changes in global component state
    // -------------------------------------------------------------------------

    fn execute_prompt(&mut self, sched_ctx: &SchedulerCtx) -> EvalResult {
        let mut step_result = EvalContinuation::Stepping;
        while let EvalContinuation::Stepping = step_result {
            step_result = self.prompt.step(
                &sched_ctx.runtime.protocol.types, &sched_ctx.runtime.protocol.heap,
                &sched_ctx.runtime.protocol.modules, &mut self.exec_ctx,
            )?;
        }

        return Ok(step_result)
    }

    fn handle_sync_start(&mut self, sched_ctx: &SchedulerCtx, comp_ctx: &mut CompCtx) {
        sched_ctx.log("Component starting sync mode");
        self.consensus.notify_sync_start(comp_ctx);
        for message in self.inbox_main.iter() {
            if let Some(message) = message {
                self.consensus.handle_new_data_message(comp_ctx, message);
            }
        }
        debug_assert_eq!(self.mode, Mode::NonSync);
        self.mode = Mode::Sync;
    }

    /// Handles end of sync. The conclusion to the sync round might arise
    /// immediately (and be handled immediately), or might come later through
    /// messaging. In any case the component should be scheduled again
    /// immediately
    fn handle_sync_end(&mut self, sched_ctx: &SchedulerCtx, comp_ctx: &mut CompCtx) {
        sched_ctx.log("Component ending sync mode (now waiting for solution)");
        let decision = self.consensus.notify_sync_end(sched_ctx, comp_ctx);
        self.mode = Mode::SyncEnd;
        self.handle_sync_decision(sched_ctx, comp_ctx, decision);
    }

    /// Handles decision from the consensus round. This will cause a change in
    /// the internal `Mode`, such that the next call to `run` can take the
    /// appropriate next steps.
    fn handle_sync_decision(&mut self, sched_ctx: &SchedulerCtx, _comp_ctx: &mut CompCtx, decision: SyncRoundDecision) {
        sched_ctx.log(&format!("Handling sync decision: {:?} (in mode {:?})", decision, self.mode));
        let is_success = match decision {
            SyncRoundDecision::None => {
                // No decision yet
                return;
            },
            SyncRoundDecision::Solution => true,
            SyncRoundDecision::Failure => false,
        };

        // If here then we've reached a decision
        debug_assert_eq!(self.mode, Mode::SyncEnd);
        if is_success {
            self.mode = Mode::NonSync;
            self.consensus.notify_sync_decision(decision);
        } else {
            self.mode = Mode::StartExit;
        }
    }

    fn handle_component_exit(&mut self, sched_ctx: &SchedulerCtx, comp_ctx: &mut CompCtx) {
        sched_ctx.log("Component exiting");
        debug_assert_eq!(self.mode, Mode::StartExit);
        self.mode = Mode::BusyExit;

        // Doing this by index, then retrieving the handle is a bit rediculous,
        // but Rust is being Rust with its borrowing rules.
        for port_index in 0..comp_ctx.num_ports() {
            let port = comp_ctx.get_port_by_index_mut(port_index);
            if port.state == PortState::Closed {
                // Already closed, or in the process of being closed
                continue;
            }

            // Mark as closed
            let port_id = port.self_id;
            port.state = PortState::Closed;

            // Notify peer of closing
            let port_handle = comp_ctx.get_port_handle(port_id);
            let (peer, message) = self.control.initiate_port_closing(port_handle, comp_ctx);
            let peer_info = comp_ctx.get_peer(peer);
            peer_info.handle.send_message(sched_ctx, Message::Control(message), true);
        }
    }

    // -------------------------------------------------------------------------
    // Handling messages
    // -------------------------------------------------------------------------

    fn send_data_message_and_wake_up(&mut self, sched_ctx: &SchedulerCtx, comp_ctx: &CompCtx, source_port_handle: LocalPortHandle, value: ValueGroup) {
        let port_info = comp_ctx.get_port(source_port_handle);
        let peer_handle = comp_ctx.get_peer_handle(port_info.peer_comp_id);
        let peer_info = comp_ctx.get_peer(peer_handle);
        let annotated_message = self.consensus.annotate_data_message(comp_ctx, port_info, value);
        peer_info.handle.send_message(sched_ctx, Message::Data(annotated_message), true);
    }

    /// Handles a message that came in through the public inbox. This function
    /// will handle putting it in the correct place, and potentially blocking
    /// the port in case too many messages are being received.
    fn handle_incoming_data_message(&mut self, sched_ctx: &SchedulerCtx, comp_ctx: &mut CompCtx, message: DataMessage) {
        // Whatever we do, glean information from headers in message
        if self.mode.is_in_sync_block() {
            self.consensus.handle_new_data_message(comp_ctx, &message);
        }

        // Check if we can insert it directly into the storage associated with
        // the port
        let target_port_id = message.data_header.target_port;
        let port_handle = comp_ctx.get_port_handle(target_port_id);
        let port_index = comp_ctx.get_port_index(port_handle);
        if self.inbox_main[port_index].is_none() {
            self.inbox_main[port_index] = Some(message);

            // After direct insertion, check if this component's execution is 
            // blocked on receiving a message on that port
            debug_assert!(!comp_ctx.get_port(port_handle).state.is_blocked()); // because we could insert directly
            if self.mode == Mode::BlockedGet && self.mode_port == target_port_id {
                // We were indeed blocked
                self.mode = Mode::Sync;
                self.mode_port = PortId::new_invalid();
            } else if self.mode == Mode::BlockedSelect {
                let select_decision = self.select.handle_updated_inbox(&self.inbox_main, comp_ctx);
                if let SelectDecision::Case(case_index) = select_decision {
                    self.exec_ctx.stmt = ExecStmt::PerformedSelectWait(case_index);
                    self.mode = Mode::Sync;
                }
            }
            
            return;
        }

        // The direct inbox is full, so the port will become (or was already) blocked
        let port_info = comp_ctx.get_port_mut(port_handle);
        debug_assert!(port_info.state == PortState::Open || port_info.state.is_blocked());

        if port_info.state == PortState::Open {
            comp_ctx.set_port_state(port_handle, PortState::BlockedDueToFullBuffers);
            let (peer_handle, message) =
                self.control.initiate_port_blocking(comp_ctx, port_handle);

            let peer = comp_ctx.get_peer(peer_handle);
            peer.handle.send_message(sched_ctx, Message::Control(message), true);
        }

        // But we still need to remember the message, so:
        self.inbox_backup.push(message);
    }

    /// Handles when a message has been handed off from the inbox to the PDL
    /// code. We check to see if there are more messages waiting and, if not,
    /// then we handle the case where the port might have been blocked
    /// previously.
    fn handle_received_data_message(&mut self, sched_ctx: &SchedulerCtx, comp_ctx: &mut CompCtx, port_handle: LocalPortHandle) {
        let port_index = comp_ctx.get_port_index(port_handle);
        debug_assert!(self.inbox_main[port_index].is_none()); // this function should be called after the message is taken out

        // Check for any more messages
        let port_info = comp_ctx.get_port(port_handle);
        for message_index in 0..self.inbox_backup.len() {
            let message = &self.inbox_backup[message_index];
            if message.data_header.target_port == port_info.self_id {
                // One more message for this port
                let message = self.inbox_backup.remove(message_index);
                debug_assert!(comp_ctx.get_port(port_handle).state.is_blocked()); // since we had >1 message on the port
                self.inbox_main[port_index] = Some(message);

                return;
            }
        }

        // Did not have any more messages. So if we were blocked, then we need
        // to send the "unblock" message.
        if port_info.state == PortState::BlockedDueToFullBuffers {
            comp_ctx.set_port_state(port_handle, PortState::Open);
            let (peer_handle, message) = self.control.cancel_port_blocking(comp_ctx, port_handle);
            let peer_info = comp_ctx.get_peer(peer_handle);
            peer_info.handle.send_message(sched_ctx, Message::Control(message), true);
        }
    }

    fn handle_incoming_control_message(&mut self, sched_ctx: &SchedulerCtx, comp_ctx: &mut CompCtx, message: ControlMessage) {
        // Little local utility to send an Ack
        fn send_control_ack_message(sched_ctx: &SchedulerCtx, comp_ctx: &CompCtx, causer_id: ControlId, peer_handle: LocalPeerHandle) {
            let peer_info = comp_ctx.get_peer(peer_handle);
            peer_info.handle.send_message(sched_ctx, Message::Control(ControlMessage{
                id: causer_id,
                sender_comp_id: comp_ctx.id,
                target_port_id: None,
                content: ControlMessageContent::Ack,
            }), true);
        }

        // Handle the content of the control message, and optionally Ack it
        match message.content {
            ControlMessageContent::Ack => {
                self.handle_ack(sched_ctx, comp_ctx, message.id);
            },
            ControlMessageContent::BlockPort(port_id) => {
                // On of our messages was accepted, but the port should be
                // blocked.
                let port_handle = comp_ctx.get_port_handle(port_id);
                let port_info = comp_ctx.get_port(port_handle);
                debug_assert_eq!(port_info.kind, PortKind::Putter);
                if port_info.state == PortState::Open {
                    // only when open: we don't do this when closed, and we we don't do this if we're blocked due to peer changes
                    comp_ctx.set_port_state(port_handle, PortState::BlockedDueToFullBuffers);
                }
            },
            ControlMessageContent::ClosePort(port_id) => {
                // Request to close the port. We immediately comply and remove
                // the component handle as well
                let port_handle = comp_ctx.get_port_handle(port_id);
                let peer_comp_id = comp_ctx.get_port(port_handle).peer_comp_id;
                let peer_handle = comp_ctx.get_peer_handle(peer_comp_id);

                // One exception to sending an `Ack` is if we just closed the
                // port ourselves, meaning that the `ClosePort` messages got
                // sent to one another.
                if let Some(control_id) = self.control.has_close_port_entry(port_handle, comp_ctx) {
                    self.handle_ack(sched_ctx, comp_ctx, control_id);
                } else {
                    send_control_ack_message(sched_ctx, comp_ctx, message.id, peer_handle);
                    comp_ctx.remove_peer(sched_ctx, port_handle, peer_comp_id, false); // do not remove if closed
                    comp_ctx.set_port_state(port_handle, PortState::Closed); // now set to closed
                }
            },
            ControlMessageContent::UnblockPort(port_id) => {
                // We were previously blocked (or already closed)
                let port_handle = comp_ctx.get_port_handle(port_id);
                let port_info = comp_ctx.get_port(port_handle);
                debug_assert_eq!(port_info.kind, PortKind::Putter);
                if port_info.state == PortState::BlockedDueToFullBuffers {
                    self.handle_unblock_port_instruction(sched_ctx, comp_ctx, port_handle);
                }
            },
            ControlMessageContent::PortPeerChangedBlock(port_id) => {
                // The peer of our port has just changed. So we are asked to
                // temporarily block the port (while our original recipient is
                // potentially rerouting some of the in-flight messages) and
                // Ack. Then we wait for the `unblock` call.
                debug_assert_eq!(message.target_port_id, Some(port_id));
                let port_handle = comp_ctx.get_port_handle(port_id);
                comp_ctx.set_port_state(port_handle, PortState::BlockedDueToPeerChange);

                let port_info = comp_ctx.get_port(port_handle);
                let peer_handle = comp_ctx.get_peer_handle(port_info.peer_comp_id);

                send_control_ack_message(sched_ctx, comp_ctx, message.id, peer_handle);
            },
            ControlMessageContent::PortPeerChangedUnblock(new_port_id, new_comp_id) => {
                let port_handle = comp_ctx.get_port_handle(message.target_port_id.unwrap());
                let port_info = comp_ctx.get_port(port_handle);
                debug_assert!(port_info.state == PortState::BlockedDueToPeerChange);
                let old_peer_id = port_info.peer_comp_id;

                comp_ctx.remove_peer(sched_ctx, port_handle, old_peer_id, false);

                let port_info = comp_ctx.get_port_mut(port_handle);
                port_info.peer_comp_id = new_comp_id;
                port_info.peer_port_id = new_port_id;
                comp_ctx.add_peer(port_handle, sched_ctx, new_comp_id, None);
                self.handle_unblock_port_instruction(sched_ctx, comp_ctx, port_handle);
            }
        }
    }

    fn handle_incoming_sync_message(&mut self, sched_ctx: &SchedulerCtx, comp_ctx: &mut CompCtx, message: SyncMessage) {
        let decision = self.consensus.receive_sync_message(sched_ctx, comp_ctx, message);
        self.handle_sync_decision(sched_ctx, comp_ctx, decision);
    }

    /// Little helper that notifies the control layer of an `Ack`, and takes the
    /// appropriate subsequent action
    fn handle_ack(&mut self, sched_ctx: &SchedulerCtx, comp_ctx: &mut CompCtx, control_id: ControlId) {
        let mut to_ack = control_id;
        loop {
            let (action, new_to_ack) = self.control.handle_ack(to_ack, sched_ctx, comp_ctx);
            match action {
                AckAction::SendMessage(target_comp, message) => {
                    // FIX @NoDirectHandle
                    let mut handle = sched_ctx.runtime.get_component_public(target_comp);
                    handle.send_message(sched_ctx, Message::Control(message), true);
                    let _should_remove = handle.decrement_users();
                    debug_assert!(_should_remove.is_none());
                },
                AckAction::ScheduleComponent(to_schedule) => {
                    // FIX @NoDirectHandle
                    let mut handle = sched_ctx.runtime.get_component_public(to_schedule);

                    // Note that the component is intentionally not
                    // sleeping, so we just wake it up
                    debug_assert!(!handle.sleeping.load(std::sync::atomic::Ordering::Acquire));
                    let key = unsafe{ to_schedule.upgrade() };
                    sched_ctx.runtime.enqueue_work(key);
                    let _should_remove = handle.decrement_users();
                    debug_assert!(_should_remove.is_none());
                },
                AckAction::None => {}
            }

            match new_to_ack {
                Some(new_to_ack) => to_ack = new_to_ack,
                None => break,
            }
        }
    }

    // -------------------------------------------------------------------------
    // Handling ports
    // -------------------------------------------------------------------------

    /// Unblocks a port, potentially continuing execution of the component, in
    /// response to a message that told us to unblock a previously blocked
    fn handle_unblock_port_instruction(&mut self, sched_ctx: &SchedulerCtx, comp_ctx: &mut CompCtx, port_handle: LocalPortHandle) {
        let port_info = comp_ctx.get_port_mut(port_handle);
        let port_id = port_info.self_id;
        debug_assert!(port_info.state.is_blocked());
        port_info.state = PortState::Open;

        if self.mode == Mode::BlockedPut && port_id == self.mode_port {
            // We were blocked on the port that just became unblocked, so
            // send the message.
            debug_assert_eq!(port_info.kind, PortKind::Putter);
            let mut replacement = ValueGroup::default();
            std::mem::swap(&mut replacement, &mut self.mode_value);
            self.send_data_message_and_wake_up(sched_ctx, comp_ctx, port_handle, replacement);

            self.mode = Mode::Sync;
            self.mode_port = PortId::new_invalid();
        }
    }

    fn create_component_and_transfer_ports(
        &mut self,
        sched_ctx: &SchedulerCtx, creator_ctx: &mut CompCtx,
        definition_id: ProcedureDefinitionId, type_id: TypeId, mut arguments: ValueGroup
    ) {
        struct PortPair{
            creator_handle: LocalPortHandle,
            creator_id: PortId,
            created_handle: LocalPortHandle,
            created_id: PortId,
        }
        let mut port_id_pairs = Vec::new();

        let reservation = sched_ctx.runtime.start_create_pdl_component();
        let mut created_ctx = CompCtx::new(&reservation);

        // Take all the ports ID that are in the `args` (and currently belong to
        // the creator component) and translate them into new IDs that are
        // associated with the component we're about to create
        let mut arg_iter = ValueGroupPortIter::new(&mut arguments);
        while let Some(port_reference) = arg_iter.next() {
            // Create port entry for new component
            let creator_port_id = port_reference.id;
            let creator_port_handle = creator_ctx.get_port_handle(creator_port_id);
            let creator_port = creator_ctx.get_port(creator_port_handle);
            let created_port_handle = created_ctx.add_port(
                creator_port.peer_comp_id, creator_port.peer_port_id,
                creator_port.kind, creator_port.state
            );
            let created_port = created_ctx.get_port(created_port_handle);
            let created_port_id = created_port.self_id;

            port_id_pairs.push(PortPair{
                creator_handle: creator_port_handle,
                creator_id: creator_port_id,
                created_handle: created_port_handle,
                created_id: created_port_id,
            });

            // Modify value in arguments (bit dirty, but double vec in ValueGroup causes lifetime issues)
            let arg_value = if let Some(heap_pos) = port_reference.heap_pos {
                &mut arg_iter.group.regions[heap_pos][port_reference.index]
            } else {
                &mut arg_iter.group.values[port_reference.index]
            };
            match arg_value {
                Value::Input(id) => *id = port_id_to_eval(created_port_id),
                Value::Output(id) => *id = port_id_to_eval(created_port_id),
                _ => unreachable!(),
            }
        }

        // For each transferred port pair set their peer components to the
        // correct values. This will only change the values for the ports of
        // the new component.
        let mut created_component_has_remote_peers = false;

        for pair in port_id_pairs.iter() {
            let creator_port_info = creator_ctx.get_port(pair.creator_handle);
            let created_port_info = created_ctx.get_port_mut(pair.created_handle);

            if created_port_info.peer_comp_id == creator_ctx.id {
                // Port peer is owned by the creator as well
                let created_peer_port_index = port_id_pairs
                    .iter()
                    .position(|v| v.creator_id == creator_port_info.peer_port_id);
                match created_peer_port_index {
                    Some(created_peer_port_index) => {
                        // Peer port moved to the new component as well. So
                        // adjust IDs appropriately.
                        let peer_pair = &port_id_pairs[created_peer_port_index];
                        created_port_info.peer_port_id = peer_pair.created_id;
                        created_port_info.peer_comp_id = reservation.id();
                        todo!("either add 'self peer', or remove that idea from Ctx altogether")
                    },
                    None => {
                        // Peer port remains with creator component.
                        created_port_info.peer_comp_id = creator_ctx.id;
                        created_ctx.add_peer(pair.created_handle, sched_ctx, creator_ctx.id, None);
                    }
                }
            } else {
                // Peer is a different component. We'll deal with sending the
                // appropriate messages later
                let peer_handle = creator_ctx.get_peer_handle(created_port_info.peer_comp_id);
                let peer_info = creator_ctx.get_peer(peer_handle);
                created_ctx.add_peer(pair.created_handle, sched_ctx, peer_info.id, Some(&peer_info.handle));
                created_component_has_remote_peers = true;
            }
        }

        // We'll now actually turn our reservation for a new component into an
        // actual component. Note that we initialize it as "not sleeping" as
        // its initial scheduling might be performed based on `Ack`s in response
        // to message exchanges between remote peers.
        let prompt = Prompt::new(
            &sched_ctx.runtime.protocol.types, &sched_ctx.runtime.protocol.heap,
            definition_id, type_id, arguments,
        );
        let component = CompPDL::new(prompt, port_id_pairs.len());
        let (created_key, component) = sched_ctx.runtime.finish_create_pdl_component(
            reservation, component, created_ctx, false,
        );

        // Now modify the creator's ports: remove every transferred port and
        // potentially remove the peer component.
        for pair in port_id_pairs.iter() {
            // Remove peer if appropriate
            let creator_port_info = creator_ctx.get_port(pair.creator_handle);
            let creator_port_index = creator_ctx.get_port_index(pair.creator_handle);
            let creator_peer_comp_id = creator_port_info.peer_comp_id;
            creator_ctx.remove_peer(sched_ctx, pair.creator_handle, creator_peer_comp_id, false);
            creator_ctx.remove_port(pair.creator_handle);

            // Transfer any messages
            if let Some(mut message) = self.inbox_main.remove(creator_port_index) {
                message.data_header.target_port = pair.created_id;
                component.component.adopt_message(&mut component.ctx, message)
            }

            let mut message_index = 0;
            while message_index < self.inbox_backup.len() {
                let message = &self.inbox_backup[message_index];
                if message.data_header.target_port == pair.creator_id {
                    // transfer message
                    let mut message = self.inbox_backup.remove(message_index);
                    message.data_header.target_port = pair.created_id;
                    component.component.adopt_message(&mut component.ctx, message);
                } else {
                    message_index += 1;
                }
            }

            // Handle potential channel between creator and created component
            let created_port_info = component.ctx.get_port(pair.created_handle);

            if created_port_info.peer_comp_id == creator_ctx.id {
                let peer_port_handle = creator_ctx.get_port_handle(created_port_info.peer_port_id);
                let peer_port_info = creator_ctx.get_port_mut(peer_port_handle);
                peer_port_info.peer_comp_id = component.ctx.id;
                peer_port_info.peer_port_id = created_port_info.self_id;
                creator_ctx.add_peer(peer_port_handle, sched_ctx, component.ctx.id, None);
            }
        }

        // By now all ports and messages have been transferred. If there are any
        // peers that need to be notified about this new component, then we
        // initiate the protocol that will notify everyone here.
        if created_component_has_remote_peers {
            let created_ctx = &component.ctx;
            let schedule_entry_id = self.control.add_schedule_entry(created_ctx.id);
            for pair in port_id_pairs.iter() {
                let port_info = created_ctx.get_port(pair.created_handle);
                if port_info.peer_comp_id != creator_ctx.id && port_info.peer_comp_id != created_ctx.id {
                    let message = self.control.add_reroute_entry(
                        creator_ctx.id, port_info.peer_port_id, port_info.peer_comp_id,
                        pair.creator_id, pair.created_id, created_ctx.id,
                        schedule_entry_id
                    );
                    let peer_handle = created_ctx.get_peer_handle(port_info.peer_comp_id);
                    let peer_info = created_ctx.get_peer(peer_handle);
                    peer_info.handle.send_message(sched_ctx, message, true);
                }
            }
        } else {
            // Peer can be scheduled immediately
            sched_ctx.runtime.enqueue_work(created_key);
        }
    }
}

#[inline]
fn port_id_from_eval(port_id: EvalPortId) -> PortId {
    return PortId(port_id.id);
}

#[inline]
fn port_id_to_eval(port_id: PortId) -> EvalPortId {
    return EvalPortId{ id: port_id.0 };
}

/// Recursively goes through the value group, attempting to find ports.
/// Duplicates will only be added once.
pub(crate) fn find_ports_in_value_group(value_group: &ValueGroup, ports: &mut Vec<PortId>) {
    // Helper to check a value for a port and recurse if needed.
    fn find_port_in_value(group: &ValueGroup, value: &Value, ports: &mut Vec<PortId>) {
        match value {
            Value::Input(port_id) | Value::Output(port_id) => {
                // This is an actual port
                let cur_port = PortId(port_id.id);
                for prev_port in ports.iter() {
                    if *prev_port == cur_port {
                        // Already added
                        return;
                    }
                }

                ports.push(cur_port);
            },
            Value::Array(heap_pos) |
            Value::Message(heap_pos) |
            Value::String(heap_pos) |
            Value::Struct(heap_pos) |
            Value::Union(_, heap_pos) => {
                // Reference to some dynamic thing which might contain ports,
                // so recurse
                let heap_region = &group.regions[*heap_pos as usize];
                for embedded_value in heap_region {
                    find_port_in_value(group, embedded_value, ports);
                }
            },
            _ => {}, // values we don't care about
        }
    }

    // Clear the ports, then scan all the available values
    ports.clear();
    for value in &value_group.values {
        find_port_in_value(value_group, value, ports);
    }
}

struct ValueGroupPortIter<'a> {
    group: &'a mut ValueGroup,
    heap_stack: Vec<(usize, usize)>,
    index: usize,
}

impl<'a> ValueGroupPortIter<'a> {
    fn new(group: &'a mut ValueGroup) -> Self {
        return Self{ group, heap_stack: Vec::new(), index: 0 }
    }
}

struct ValueGroupPortRef {
    id: PortId,
    heap_pos: Option<usize>, // otherwise: on stack
    index: usize,
}

impl<'a> Iterator for ValueGroupPortIter<'a> {
    type Item = ValueGroupPortRef;

    fn next(&mut self) -> Option<Self::Item> {
        // Enter loop that keeps iterating until a port is found
        loop {
            if let Some(pos) = self.heap_stack.last() {
                let (heap_pos, region_index) = *pos;
                if region_index >= self.group.regions[heap_pos].len() {
                    self.heap_stack.pop();
                    continue;
                }

                let value = &self.group.regions[heap_pos][region_index];
                self.heap_stack.last_mut().unwrap().1 += 1;

                match value {
                    Value::Input(id) | Value::Output(id) => {
                        let id = PortId(id.id);
                        return Some(ValueGroupPortRef{
                            id,
                            heap_pos: Some(heap_pos),
                            index: region_index,
                        });
                    },
                    _ => {},
                }

                if let Some(heap_pos) = value.get_heap_pos() {
                    self.heap_stack.push((heap_pos as usize, 0));
                }
            } else {
                if self.index >= self.group.values.len() {
                    return None;
                }

                let value = &mut self.group.values[self.index];
                self.index += 1;

                match value {
                    Value::Input(id) | Value::Output(id) => {
                        let id = PortId(id.id);
                        return Some(ValueGroupPortRef{
                            id,
                            heap_pos: None,
                            index: self.index - 1
                        });
                    },
                    _ => {},
                }

                // Not a port, check if we need to enter a heap region
                if let Some(heap_pos) = value.get_heap_pos() {
                    self.heap_stack.push((heap_pos as usize, 0));
                } // else: just consider the next value
            }
        }
    }
}