Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mishandling of extern "C" { #122

Open
petervdonovan opened this issue Jun 8, 2023 · 0 comments
Open

Mishandling of extern "C" { #122

petervdonovan opened this issue Jun 8, 2023 · 0 comments
Labels
highlighting Syntax or semantic highlighting

Comments

@petervdonovan
Copy link
Contributor

I'm not sure if this is in our power to fix -- it seems subtly related to the way the C/C++ highlighter that we delegate to works -- but it does not look good.

The following is highlighted correctly:

target C {
    threading: true,
    cmake-include: "include/federate__receiver_extension.cmake",
    keepalive: true,
    tracing: true,
    compile-definitions: {
        EXECUTABLE_PREAMBLE: "",
        WORKERS_NEEDED_FOR_FEDERATE: "2",
        NUMBER_OF_FEDERATES: "2",
        FEDERATED: "",
        FEDERATED_CENTRALIZED: ""
    },
    _fed_setup: "include/_federate__receiver_preamble.h"
}

preamble {=
    // #ifdef __cplusplus
    {
    // #endif
    #include "core/federated/federate.h"
    #include "core/federated/net_common.h"
    #include "core/federated/net_util.h"
    #include "core/federated/clock-sync.h"
    #include "core/threaded/reactor_threaded.h"
    #include "core/utils/util.h"
    extern federate_instance_t _fed;
    // #ifdef __cplusplus
    }
    // #endif
=}

reactor Receiver(
    stp_offset: time = 10 msec  // Used in the decentralized variant of the test
) {
    input in: int
    state reaction_invoked_correctly: bool = false

    reaction(in) {=
        lf_print("Received %d at (%lld, %u).",
                     in->value,
                     lf_time_logical_elapsed(),
                     lf_tag().microstep);
        if (lf_time_logical_elapsed() == USEC(1)) {
            lf_print("Requesting stop at (%lld, %u).",
                     lf_time_logical_elapsed(),
                     lf_tag().microstep);
            lf_request_stop();
            // The receiver should receive a message at tag
            // (1 usec, 1) and trigger this reaction
            self->reaction_invoked_correctly = true;
        }

        tag_t _1usec1 = (tag_t) { .time = USEC(1) + lf_time_start(), .microstep = 1u };
        if (lf_tag_compare(lf_tag(), _1usec1) > 0) {
            self->reaction_invoked_correctly = false;
        }
    =}

    reaction(shutdown) {=
        // Sender should have requested stop earlier than the receiver.
        // Therefore, the shutdown events must occur at (1000, 0) on the
        // receiver.
        if (lf_time_logical_elapsed() != USEC(1) ||
            lf_tag().microstep != 1) {
            lf_print_error_and_exit("Receiver failed to stop the federation at the right time. "
                    "Stopping at (%lld, %u).",
                     lf_time_logical_elapsed(),
                     lf_tag().microstep);
        } else if (self->reaction_invoked_correctly == false) {
            lf_print_error_and_exit("Receiver reaction(in) was not invoked the correct number of times. "
                    "Stopping at (%lld, %u).",
                     lf_time_logical_elapsed(),
                     lf_tag().microstep);
        }
        lf_print("SUCCESS: Successfully stopped the federation at (%lld, %u).",
                     lf_time_logical_elapsed(),
                     lf_tag().microstep);
    =}
}

reactor NetworkReceiver_0 {
    output msg: int

    logical action networkMessage_0: int

    reaction(networkMessage_0) -> msg {=
        msg->physical_time_of_arrival = self->_lf__networkMessage_0.physical_time_of_arrival;
        lf_set(msg, networkMessage_0->value);
    =}
}

@_fed_config(network_message_actions="networkMessage_0", dependencyPairs="")
main reactor {
    receiver = new Receiver()

    nr_federate__receiver = new NetworkReceiver_0()
    nr_federate__receiver.msg -> receiver.in
}

But the following is not handled correctly:

target C {
    threading: true,
    cmake-include: "include/federate__receiver_extension.cmake",
    keepalive: true,
    tracing: true,
    compile-definitions: {
        EXECUTABLE_PREAMBLE: "",
        WORKERS_NEEDED_FOR_FEDERATE: "2",
        NUMBER_OF_FEDERATES: "2",
        FEDERATED: "",
        FEDERATED_CENTRALIZED: ""
    },
    _fed_setup: "include/_federate__receiver_preamble.h"
}

preamble {=
    // #ifdef __cplusplus
    extern "C" {
    // #endif
    #include "core/federated/federate.h"
    #include "core/federated/net_common.h"
    #include "core/federated/net_util.h"
    #include "core/federated/clock-sync.h"
    #include "core/threaded/reactor_threaded.h"
    #include "core/utils/util.h"
    extern federate_instance_t _fed;
    // #ifdef __cplusplus
    }
    // #endif
=}

reactor Receiver(
    stp_offset: time = 10 msec  // Used in the decentralized variant of the test
) {
    input in: int
    state reaction_invoked_correctly: bool = false

    reaction(in) {=
        lf_print("Received %d at (%lld, %u).",
                     in->value,
                     lf_time_logical_elapsed(),
                     lf_tag().microstep);
        if (lf_time_logical_elapsed() == USEC(1)) {
            lf_print("Requesting stop at (%lld, %u).",
                     lf_time_logical_elapsed(),
                     lf_tag().microstep);
            lf_request_stop();
            // The receiver should receive a message at tag
            // (1 usec, 1) and trigger this reaction
            self->reaction_invoked_correctly = true;
        }

        tag_t _1usec1 = (tag_t) { .time = USEC(1) + lf_time_start(), .microstep = 1u };
        if (lf_tag_compare(lf_tag(), _1usec1) > 0) {
            self->reaction_invoked_correctly = false;
        }
    =}

    reaction(shutdown) {=
        // Sender should have requested stop earlier than the receiver.
        // Therefore, the shutdown events must occur at (1000, 0) on the
        // receiver.
        if (lf_time_logical_elapsed() != USEC(1) ||
            lf_tag().microstep != 1) {
            lf_print_error_and_exit("Receiver failed to stop the federation at the right time. "
                    "Stopping at (%lld, %u).",
                     lf_time_logical_elapsed(),
                     lf_tag().microstep);
        } else if (self->reaction_invoked_correctly == false) {
            lf_print_error_and_exit("Receiver reaction(in) was not invoked the correct number of times. "
                    "Stopping at (%lld, %u).",
                     lf_time_logical_elapsed(),
                     lf_tag().microstep);
        }
        lf_print("SUCCESS: Successfully stopped the federation at (%lld, %u).",
                     lf_time_logical_elapsed(),
                     lf_tag().microstep);
    =}
}

reactor NetworkReceiver_0 {
    output msg: int

    logical action networkMessage_0: int

    reaction(networkMessage_0) -> msg {=
        msg->physical_time_of_arrival = self->_lf__networkMessage_0.physical_time_of_arrival;
        lf_set(msg, networkMessage_0->value);
    =}
}

@_fed_config(network_message_actions="networkMessage_0", dependencyPairs="")
main reactor {
    receiver = new Receiver()

    nr_federate__receiver = new NetworkReceiver_0()
    nr_federate__receiver.msg -> receiver.in
}

The entire file after the preamble is highlighted as if it were plain C.

Note the extern "C" appearing in the preamble. That is the only difference.

@petervdonovan petervdonovan added the highlighting Syntax or semantic highlighting label Jun 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
highlighting Syntax or semantic highlighting
Projects
None yet
Development

No branches or pull requests

1 participant