diff --git a/.gitignore b/.gitignore index 742de2ac4..b8237101d 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,6 @@ /docs/api **/.vscode/ **/build/ -**/lib/ **/.DS_Store /core/federated/RTI/build/ /cmake-build-debug/ diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index 8eec31eb8..e4a9f1b6c 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -1,17 +1,7 @@ set(CORE_ROOT ${CMAKE_CURRENT_SOURCE_DIR}) set(LF_ROOT ${CMAKE_CURRENT_LIST_DIR}/..) -if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows") - set(CMAKE_SYSTEM_VERSION 10.0) - message("Using Windows SDK version ${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}") -elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Nrf52") - list(APPEND REACTORC_COMPILE_DEFS PLATFORM_NRF52) -elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Zephyr") - list(APPEND REACTORC_COMPILE_DEFS PLATFORM_ZEPHYR) - set(PLATFORM_ZEPHYR true) -elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Rp2040") - list(APPEND REACTORC_COMPILE_DEFS PLATFORM_RP2040) -endif() +include(${LF_ROOT}/core/lf_utils.cmake) # Get the general common sources for reactor-c list(APPEND GENERAL_SOURCES tag.c clock.c port.c mixed_radix.c reactor_common.c lf_token.c environment.c) @@ -50,23 +40,18 @@ endif() # Include sources from subdirectories include(utils/CMakeLists.txt) + +if (DEFINED MODAL_REACTORS) include(modal_models/CMakeLists.txt) +endif() # Print sources used for compilation list(JOIN REACTORC_SOURCES ", " PRINTABLE_SOURCE_LIST) message(STATUS "Including the following sources: " ${PRINTABLE_SOURCE_LIST}) -# Create the reactor-c library. If we are targeting Zephyr we have to use the -# Zephyr Cmake extension to create the library and add the sources. -if(PLATFORM_ZEPHYR) - message("--- Building Zephyr library") - zephyr_library_named(reactor-c) - zephyr_library_sources(${REACTORC_SOURCES}) - zephyr_library_link_libraries(kernel) -else() - add_library(reactor-c) - target_sources(reactor-c PRIVATE ${REACTORC_SOURCES}) -endif() +add_library(reactor-c) +target_sources(reactor-c PRIVATE ${REACTORC_SOURCES}) +lf_enable_compiler_warnings(reactor-c) if (DEFINED LF_TRACE) include(${LF_ROOT}/trace/api/CMakeLists.txt) @@ -98,9 +83,6 @@ include(${LF_ROOT}/platform/impl/CMakeLists.txt) target_link_libraries(reactor-c PUBLIC lf::platform-api) target_link_libraries(reactor-c PRIVATE lf::platform-impl) -# Apply compile definitions to the reactor-c library. -target_compile_definitions(reactor-c PUBLIC ${REACTORC_COMPILE_DEFS}) - target_include_directories(reactor-c PUBLIC ../include) target_include_directories(reactor-c PUBLIC ../include/core) target_include_directories(reactor-c PUBLIC ../include/core/federated) @@ -134,14 +116,18 @@ if(DEFINED _LF_CLOCK_SYNC_ON) endif() endif() -# Link with thread library, unless we are on the Zephyr platform. -if(NOT DEFINED LF_SINGLE_THREADED OR DEFINED LF_TRACE) - if (NOT PLATFORM_ZEPHYR) - find_package(Threads REQUIRED) - target_link_libraries(reactor-c PUBLIC Threads::Threads) - endif() +# Unless specified otherwise initial event queue and reaction queue to size 10 +if (NOT DEFINED INITIAL_EVENT_QUEUE_SIZE) + set(INITIAL_EVENT_QUEUE_SIZE 10) +endif() +if (NOT DEFINED INITIAL_REACT_QUEUE_SIZE) + set(INITIAL_REACT_QUEUE_SIZE 10) endif() +target_compile_definitions(reactor-c PRIVATE INITIAL_EVENT_QUEUE_SIZE=${INITIAL_EVENT_QUEUE_SIZE}) +target_compile_definitions(reactor-c PRIVATE INITIAL_REACT_QUEUE_SIZE=${INITIAL_REACT_QUEUE_SIZE}) +target_compile_definitions(reactor-c PUBLIC PLATFORM_${CMAKE_SYSTEM_NAME}) + # Macro for translating a command-line argument into compile definition for # reactor-c lib macro(define X) @@ -151,12 +137,6 @@ macro(define X) endif(DEFINED ${X}) endmacro() -# FIXME: May want these to be application dependent, hence passed as -# parameters to Cmake. -target_compile_definitions(reactor-c PRIVATE INITIAL_EVENT_QUEUE_SIZE=10) -target_compile_definitions(reactor-c PRIVATE INITIAL_REACT_QUEUE_SIZE=10) -target_compile_definitions(reactor-c PUBLIC PLATFORM_${CMAKE_SYSTEM_NAME}) - # Search and apply all possible compile definitions message(STATUS "Applying preprocessor definitions...") define(_LF_CLOCK_SYNC_ATTENUATION) diff --git a/core/environment.c b/core/environment.c index 7189090e4..40f369c62 100644 --- a/core/environment.c +++ b/core/environment.c @@ -53,7 +53,9 @@ static void environment_init_threaded(environment_t* env, int num_workers) { LF_MUTEX_INIT(&env->mutex); LF_COND_INIT(&env->event_q_changed, &env->mutex); LF_COND_INIT(&env->global_tag_barrier_requestors_reached_zero, &env->mutex); - +#else + (void)env; + (void)num_workers; #endif } /** @@ -67,6 +69,8 @@ static void environment_init_single_threaded(environment_t* env) { env->reaction_q = pqueue_init(INITIAL_REACT_QUEUE_SIZE, in_reverse_order, get_reaction_index, get_reaction_position, set_reaction_position, reaction_matches, print_reaction); +#else + (void)env; #endif } @@ -97,6 +101,10 @@ static void environment_init_modes(environment_t* env, int num_modes, int num_st } else { env->modes = NULL; } +#else + (void)env; + (void)num_modes; + (void)num_state_resets; #endif } @@ -113,6 +121,9 @@ static void environment_init_federated(environment_t* env, int num_is_present_fi env->_lf_intended_tag_fields = NULL; env->_lf_intended_tag_fields_size = 0; } +#else + (void)env; + (void)num_is_present_fields; #endif } @@ -132,12 +143,16 @@ static void environment_free_threaded(environment_t* env) { #if !defined(LF_SINGLE_THREADED) free(env->thread_ids); lf_sched_free(env->scheduler); +#else + (void)env; #endif } static void environment_free_single_threaded(environment_t* env) { #ifdef LF_SINGLE_THREADED pqueue_free(env->reaction_q); +#else + (void)env; #endif } @@ -148,12 +163,16 @@ static void environment_free_modes(environment_t* env) { free(env->modes->state_resets); free(env->modes); } +#else + (void)env; #endif } static void environment_free_federated(environment_t* env) { #ifdef FEDERATED_DECENTRALIZED free(env->_lf_intended_tag_fields); +#else + (void)env; #endif } @@ -179,6 +198,7 @@ int environment_init(environment_t* env, const char* name, int id, int num_worke int num_startup_reactions, int num_shutdown_reactions, int num_reset_reactions, int num_is_present_fields, int num_modes, int num_state_resets, int num_watchdogs, const char* trace_file_name) { + (void)trace_file_name; // Will be used with future enclave support. env->name = malloc(strlen(name) + 1); // +1 for the null terminator LF_ASSERT_NON_NULL(env->name); diff --git a/core/federated/RTI/rti_common.h b/core/federated/RTI/rti_common.h index 1c3396749..d4e0c1236 100644 --- a/core/federated/RTI/rti_common.h +++ b/core/federated/RTI/rti_common.h @@ -54,11 +54,11 @@ typedef struct scheduling_node_t { tag_t last_provisionally_granted; // The maximum PTAG that has been provisionally granted (or NEVER if none granted) tag_t next_event; // Most recent NET received from the scheduling node (or NEVER if none received). scheduling_node_state_t state; // State of the scheduling node. - int* upstream; // Array of upstream scheduling node ids. + uint16_t* upstream; // Array of upstream scheduling node ids. interval_t* upstream_delay; // Minimum delay on connections from upstream scheduling nodes. // Here, NEVER encodes no delay. 0LL is a microstep delay. int num_upstream; // Size of the array of upstream scheduling nodes and delays. - int* downstream; // Array of downstream scheduling node ids. + uint16_t* downstream; // Array of downstream scheduling node ids. int num_downstream; // Size of the array of downstream scheduling nodes. execution_mode_t mode; // FAST or REALTIME. minimum_delay_t* min_delays; // Array of minimum delays from upstream nodes, not including this node. diff --git a/core/federated/RTI/rti_remote.c b/core/federated/RTI/rti_remote.c index c2b7f8c35..e8fd3dba9 100644 --- a/core/federated/RTI/rti_remote.c +++ b/core/federated/RTI/rti_remote.c @@ -891,11 +891,6 @@ void* clock_synchronization_thread(void* noargs) { } // Initiate a clock synchronization every rti->clock_sync_period_ns - // Initiate a clock synchronization every rti->clock_sync_period_ns - struct timespec sleep_time = {(time_t)rti_remote->clock_sync_period_ns / BILLION, - rti_remote->clock_sync_period_ns % BILLION}; - struct timespec remaining_time; - bool any_federates_connected = true; while (any_federates_connected) { // Sleep @@ -1323,17 +1318,20 @@ static int receive_connection_information(int* socket_id, uint16_t fed_id) { // Allocate memory for the upstream and downstream pointers if (fed->enclave.num_upstream > 0) { - fed->enclave.upstream = (int*)malloc(sizeof(uint16_t) * fed->enclave.num_upstream); + fed->enclave.upstream = (uint16_t*)malloc(sizeof(uint16_t) * fed->enclave.num_upstream); + LF_ASSERT_NON_NULL(fed->enclave.upstream); // Allocate memory for the upstream delay pointers fed->enclave.upstream_delay = (interval_t*)malloc(sizeof(interval_t) * fed->enclave.num_upstream); + LF_ASSERT_NON_NULL(fed->enclave.upstream_delay); } else { - fed->enclave.upstream = (int*)NULL; + fed->enclave.upstream = (uint16_t*)NULL; fed->enclave.upstream_delay = (interval_t*)NULL; } if (fed->enclave.num_downstream > 0) { - fed->enclave.downstream = (int*)malloc(sizeof(uint16_t) * fed->enclave.num_downstream); + fed->enclave.downstream = (uint16_t*)malloc(sizeof(uint16_t) * fed->enclave.num_downstream); + LF_ASSERT_NON_NULL(fed->enclave.downstream); } else { - fed->enclave.downstream = (int*)NULL; + fed->enclave.downstream = (uint16_t*)NULL; } size_t connections_info_body_size = ((sizeof(uint16_t) + sizeof(int64_t)) * fed->enclave.num_upstream) + @@ -1341,6 +1339,7 @@ static int receive_connection_information(int* socket_id, uint16_t fed_id) { unsigned char* connections_info_body = NULL; if (connections_info_body_size > 0) { connections_info_body = (unsigned char*)malloc(connections_info_body_size); + LF_ASSERT_NON_NULL(connections_info_body); read_from_socket_fail_on_error(socket_id, connections_info_body_size, connections_info_body, NULL, "RTI failed to read MSG_TYPE_NEIGHBOR_STRUCTURE message body from federate %d.", fed_id); diff --git a/core/federated/clock-sync.c b/core/federated/clock-sync.c index ddd845d6f..577d1104a 100644 --- a/core/federated/clock-sync.c +++ b/core/federated/clock-sync.c @@ -438,6 +438,7 @@ void handle_T4_clock_sync_message(unsigned char* buffer, int socket, instant_t r * Thread that listens for UDP inputs from the RTI. */ void* listen_to_rti_UDP_thread(void* args) { + (void)args; initialize_lf_thread_id(); // Listen for UDP messages from the RTI. // The only expected messages are T1 and T4, which have @@ -468,12 +469,12 @@ void* listen_to_rti_UDP_thread(void* args) { if (bytes > 0) { bytes_read += bytes; } - } while ((errno == EAGAIN || errno == EWOULDBLOCK) && bytes_read < message_size); + } while ((errno == EAGAIN || errno == EWOULDBLOCK) && bytes_read < (ssize_t)message_size); // Get local physical time before doing anything else. instant_t receive_time = lf_time_physical(); - if (bytes_read < message_size) { + if (bytes_read < (ssize_t)message_size) { // Either the socket has closed or the RTI has sent EOF. // Exit the thread to halt clock synchronization. lf_print_error("Clock sync: UDP socket to RTI is broken: %s. Clock sync is now disabled.", strerror(errno)); @@ -533,9 +534,9 @@ void clock_sync_remove_offset(instant_t* t) { *t -= (_lf_clock_sync_offset + _lf void clock_sync_set_constant_bias(interval_t offset) { _lf_clock_sync_constant_bias = offset; } #else -void clock_sync_apply_offset(instant_t* t) {} -void clock_sync_remove_offset(instant_t* t) {} -void clock_sync_set_constant_bias(interval_t offset) {} +void clock_sync_apply_offset(instant_t* t) { (void)t; } +void clock_sync_remove_offset(instant_t* t) { (void)t; } +void clock_sync_set_constant_bias(interval_t offset) { (void)offset; } #endif /** @@ -550,6 +551,8 @@ int create_clock_sync_thread(lf_thread_t* thread_id) { #ifdef _LF_CLOCK_SYNC_ON // One for UDP messages if clock synchronization is enabled for this federate return lf_thread_create(thread_id, listen_to_rti_UDP_thread, NULL); +#else + (void)thread_id; #endif // _LF_CLOCK_SYNC_ON return 0; } diff --git a/core/federated/federate.c b/core/federated/federate.c index 5d55cd3bd..584e9fc3b 100644 --- a/core/federated/federate.c +++ b/core/federated/federate.c @@ -88,8 +88,8 @@ federate_instance_t _fed = {.socket_TCP_RTI = -1, .has_upstream = false, .has_downstream = false, .received_stop_request_from_rti = false, - .last_sent_LTC = (tag_t){.time = NEVER, .microstep = 0u}, - .last_sent_NET = (tag_t){.time = NEVER, .microstep = 0u}, + .last_sent_LTC = {.time = NEVER, .microstep = 0u}, + .last_sent_NET = {.time = NEVER, .microstep = 0u}, .min_delay_from_physical_action_to_federate_output = NEVER}; federation_metadata_t federation_metadata = { @@ -184,7 +184,7 @@ extern size_t staa_lst_size; * @return A pointer to an action struct or null if the ID is out of range. */ static lf_action_base_t* action_for_port(int port_id) { - if (port_id >= 0 && port_id < _lf_action_table_size) { + if (port_id >= 0 && ((size_t)port_id) < _lf_action_table_size) { return _lf_action_table[port_id]; } lf_print_error_and_exit("Invalid port ID: %d", port_id); @@ -206,7 +206,7 @@ static lf_action_base_t* action_for_port(int port_id) { static void update_last_known_status_on_input_ports(tag_t tag) { LF_PRINT_DEBUG("In update_last_known_status_on_input ports."); bool notify = false; - for (int i = 0; i < _lf_action_table_size; i++) { + for (size_t i = 0; i < _lf_action_table_size; i++) { lf_action_base_t* input_port_action = _lf_action_table[i]; // This is called when a TAG is received. // But it is possible for an input port to have received already @@ -215,7 +215,7 @@ static void update_last_known_status_on_input_ports(tag_t tag) { // is in the future and should not be rolled back. So in that case, // we do not update the last known status tag. if (lf_tag_compare(tag, input_port_action->trigger->last_known_status_tag) >= 0) { - LF_PRINT_DEBUG("Updating the last known status tag of port %d from " PRINTF_TAG " to " PRINTF_TAG ".", i, + LF_PRINT_DEBUG("Updating the last known status tag of port %zu from " PRINTF_TAG " to " PRINTF_TAG ".", i, input_port_action->trigger->last_known_status_tag.time - lf_time_start(), input_port_action->trigger->last_known_status_tag.microstep, tag.time - lf_time_start(), tag.microstep); @@ -436,7 +436,7 @@ static void close_inbound_socket(int fed_id, int flag) { * @param intended_tag The intended tag. */ static bool handle_message_now(environment_t* env, trigger_t* trigger, tag_t intended_tag) { - return trigger->reactions[0]->index >= max_level_allowed_to_advance && + return trigger->reactions[0]->index >= ((index_t)max_level_allowed_to_advance) && lf_tag_compare(intended_tag, lf_tag(env)) == 0 && lf_tag_compare(intended_tag, trigger->last_tag) > 0 && lf_tag_compare(intended_tag, trigger->last_known_status_tag) > 0 && env->execution_started && !trigger->is_physical; @@ -451,6 +451,7 @@ static bool handle_message_now(environment_t* env, trigger_t* trigger, tag_t int * @return 0 for success, -1 for failure. */ static int handle_message(int* socket, int fed_id) { + (void)fed_id; // Read the header. size_t bytes_to_read = sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint32_t); unsigned char buffer[bytes_to_read]; @@ -1046,7 +1047,7 @@ static void handle_tag_advance_grant(void) { */ static bool a_port_is_unknown(staa_t* staa_elem) { bool do_wait = false; - for (int j = 0; j < staa_elem->num_actions; ++j) { + for (size_t j = 0; j < staa_elem->num_actions; ++j) { if (staa_elem->actions[j]->trigger->status == unknown) { do_wait = true; break; @@ -1054,20 +1055,21 @@ static bool a_port_is_unknown(staa_t* staa_elem) { } return do_wait; } -#endif /** * @brief Return the port ID of the port associated with the given action. * @return The port ID or -1 if there is no match. */ static int id_of_action(lf_action_base_t* input_port_action) { - for (int i = 0; i < _lf_action_table_size; i++) { + for (size_t i = 0; i < _lf_action_table_size; i++) { if (_lf_action_table[i] == input_port_action) return i; } return -1; } +#endif + /** * @brief Thread handling setting the known absent status of input ports. * For the code-generated array of staa offsets `staa_lst`, which is sorted by STAA offset, @@ -1077,18 +1079,19 @@ static int id_of_action(lf_action_base_t* input_port_action) { */ #ifdef FEDERATED_DECENTRALIZED static void* update_ports_from_staa_offsets(void* args) { + (void)args; initialize_lf_thread_id(); if (staa_lst_size == 0) return NULL; // Nothing to do. // NOTE: Using only the top-level environment, which is the one that deals with network // input ports. environment_t* env; - int num_envs = _lf_get_environments(&env); + _lf_get_environments(&env); LF_MUTEX_LOCK(&env->mutex); while (1) { LF_PRINT_DEBUG("**** (update thread) starting"); tag_t tag_when_started_waiting = lf_tag(env); - for (int i = 0; i < staa_lst_size; ++i) { + for (size_t i = 0; i < staa_lst_size; ++i) { staa_t* staa_elem = staa_lst[i]; // The staa_elem is adjusted in the code generator to have subtracted the delay on the connection. // The list is sorted in increasing order of adjusted STAA offsets. @@ -1111,7 +1114,7 @@ static void* update_ports_from_staa_offsets(void* args) { } while (a_port_is_unknown(staa_elem)) { LF_PRINT_DEBUG("**** (update thread) waiting until: " PRINTF_TIME, wait_until_time - lf_time_start()); - if (wait_until(env, wait_until_time, &lf_port_status_changed)) { + if (wait_until(wait_until_time, &lf_port_status_changed)) { if (lf_tag_compare(lf_tag(env), tag_when_started_waiting) != 0) { break; } @@ -1123,7 +1126,7 @@ static void* update_ports_from_staa_offsets(void* args) { lf_time_start()); */ - for (int j = 0; j < staa_elem->num_actions; ++j) { + for (size_t j = 0; j < staa_elem->num_actions; ++j) { lf_action_base_t* input_port_action = staa_elem->actions[j]; if (input_port_action->trigger->status == unknown) { input_port_action->trigger->status = absent; @@ -1155,7 +1158,7 @@ static void* update_ports_from_staa_offsets(void* args) { // it would be huge mistake to enter the wait for a new tag below because the // program will freeze. First, check whether any ports are unknown: bool port_unkonwn = false; - for (int i = 0; i < staa_lst_size; ++i) { + for (size_t i = 0; i < staa_lst_size; ++i) { staa_t* staa_elem = staa_lst[i]; if (a_port_is_unknown(staa_elem)) { port_unkonwn = true; @@ -1439,7 +1442,7 @@ static void handle_stop_request_message() { /** * Send a resign signal to the RTI. */ -static void send_resign_signal(environment_t* env) { +static void send_resign_signal() { size_t bytes_to_write = 1; unsigned char buffer[bytes_to_write]; buffer[0] = MSG_TYPE_RESIGN; @@ -1453,7 +1456,7 @@ static void send_resign_signal(environment_t* env) { /** * Send a failed signal to the RTI. */ -static void send_failed_signal(environment_t* env) { +static void send_failed_signal() { size_t bytes_to_write = 1; unsigned char buffer[bytes_to_write]; buffer[0] = MSG_TYPE_FAILED; @@ -1476,6 +1479,7 @@ static void handle_rti_failed_message(void) { exit(1); } * @param args Ignored */ static void* listen_to_rti_TCP(void* args) { + (void)args; initialize_lf_thread_id(); // Buffer for incoming messages. // This does not constrain the message size @@ -1623,10 +1627,10 @@ void lf_terminate_execution(environment_t* env) { if (_fed.socket_TCP_RTI >= 0) { if (_lf_normal_termination) { tracepoint_federate_to_rti(send_RESIGN, _lf_my_fed_id, &env->current_tag); - send_resign_signal(env); + send_resign_signal(); } else { tracepoint_federate_to_rti(send_FAILED, _lf_my_fed_id, &env->current_tag); - send_failed_signal(env); + send_failed_signal(); } } @@ -1656,7 +1660,7 @@ void lf_terminate_execution(environment_t* env) { if (_fed.number_of_inbound_p2p_connections > 0 && _fed.inbound_socket_listeners != NULL) { LF_PRINT_LOG("Waiting for %zu threads listening for incoming messages to exit.", _fed.number_of_inbound_p2p_connections); - for (int i = 0; i < _fed.number_of_inbound_p2p_connections; i++) { + for (size_t i = 0; i < _fed.number_of_inbound_p2p_connections; i++) { // Ignoring errors here. lf_thread_join(_fed.inbound_socket_listeners[i], NULL); } @@ -2082,7 +2086,7 @@ void lf_enqueue_port_absent_reactions(environment_t* env) { LF_PRINT_DEBUG("No port absent reactions."); return; } - for (int i = 0; i < num_port_absent_reactions; i++) { + for (size_t i = 0; i < num_port_absent_reactions; i++) { reaction_t* reaction = port_absent_reaction[i]; if (reaction && reaction->status == inactive) { LF_PRINT_DEBUG("Inserting port absent reaction on reaction queue."); @@ -2092,9 +2096,8 @@ void lf_enqueue_port_absent_reactions(environment_t* env) { } void* lf_handle_p2p_connections_from_federates(void* env_arg) { - assert(env_arg); - environment_t* env = (environment_t*)env_arg; - int received_federates = 0; + LF_ASSERT_NON_NULL(env_arg); + size_t received_federates = 0; // Allocate memory to store thread IDs. _fed.inbound_socket_listeners = (lf_thread_t*)calloc(_fed.number_of_inbound_p2p_connections, sizeof(lf_thread_t)); while (received_federates < _fed.number_of_inbound_p2p_connections && !_lf_termination_executed) { @@ -2217,7 +2220,6 @@ void lf_latest_tag_complete(tag_t tag_to_send) { } parse_rti_code_t lf_parse_rti_addr(const char* rti_addr) { - bool has_host = false, has_port = false, has_user = false; rti_addr_info_t rti_addr_info = {0}; extract_rti_addr_info(rti_addr, &rti_addr_info); if (!rti_addr_info.has_host && !rti_addr_info.has_port && !rti_addr_info.has_user) { @@ -2225,8 +2227,8 @@ parse_rti_code_t lf_parse_rti_addr(const char* rti_addr) { } if (rti_addr_info.has_host) { if (validate_host(rti_addr_info.rti_host_str)) { - char* rti_host = (char*)calloc(256, sizeof(char)); - strncpy(rti_host, rti_addr_info.rti_host_str, 255); + char* rti_host = (char*)calloc(257, sizeof(char)); + strncpy(rti_host, rti_addr_info.rti_host_str, 256); federation_metadata.rti_host = rti_host; } else { return INVALID_HOST; @@ -2241,8 +2243,8 @@ parse_rti_code_t lf_parse_rti_addr(const char* rti_addr) { } if (rti_addr_info.has_user) { if (validate_user(rti_addr_info.rti_user_str)) { - char* rti_user = (char*)calloc(256, sizeof(char)); - strncpy(rti_user, rti_addr_info.rti_user_str, 255); + char* rti_user = (char*)calloc(257, sizeof(char)); + strncpy(rti_user, rti_addr_info.rti_user_str, 256); federation_metadata.rti_user = rti_user; } else { return INVALID_USER; @@ -2255,7 +2257,7 @@ void lf_reset_status_fields_on_input_port_triggers() { environment_t* env; _lf_get_environments(&env); tag_t now = lf_tag(env); - for (int i = 0; i < _lf_action_table_size; i++) { + for (size_t i = 0; i < _lf_action_table_size; i++) { if (lf_tag_compare(_lf_action_table[i]->trigger->last_known_status_tag, now) >= 0) { set_network_port_status(i, absent); // Default may be overriden to become present. } else { @@ -2655,6 +2657,8 @@ bool lf_update_max_level(tag_t tag, bool is_provisional) { int prev_max_level_allowed_to_advance = max_level_allowed_to_advance; max_level_allowed_to_advance = INT_MAX; #ifdef FEDERATED_DECENTRALIZED + (void)tag; + (void)is_provisional; size_t action_table_size = _lf_action_table_size; lf_action_base_t** action_table = _lf_action_table; #else @@ -2670,7 +2674,7 @@ bool lf_update_max_level(tag_t tag, bool is_provisional) { size_t action_table_size = _lf_zero_delay_cycle_action_table_size; lf_action_base_t** action_table = _lf_zero_delay_cycle_action_table; #endif // FEDERATED_DECENTRALIZED - for (int i = 0; i < action_table_size; i++) { + for (size_t i = 0; i < action_table_size; i++) { lf_action_base_t* input_port_action = action_table[i]; #ifdef FEDERATED_DECENTRALIZED // In decentralized execution, if the current_tag is close enough to the diff --git a/core/federated/network/net_util.c b/core/federated/network/net_util.c index c6b3b57a6..3ab04c5a2 100644 --- a/core/federated/network/net_util.c +++ b/core/federated/network/net_util.c @@ -165,7 +165,6 @@ int write_to_socket(int socket, size_t num_bytes, unsigned char* buffer) { return -1; } ssize_t bytes_written = 0; - va_list args; while (bytes_written < (ssize_t)num_bytes) { ssize_t more = write(socket, buffer + bytes_written, num_bytes - (size_t)bytes_written); if (more <= 0 && (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)) { @@ -414,10 +413,6 @@ void extract_header(unsigned char* buffer, uint16_t* port_id, uint16_t* federate // The next four bytes are the message length. uint32_t local_length_signed = extract_uint32(&(buffer[sizeof(uint16_t) + sizeof(uint16_t)])); - if (local_length_signed < 0) { - lf_print_error_and_exit("Received an invalid message length (%d) from federate %d.", local_length_signed, - *federate_id); - } *length = (size_t)local_length_signed; // printf("DEBUG: Federate receiving message to port %d to federate %d of length %d.\n", port_id, federate_id, @@ -492,7 +487,7 @@ bool validate_user(const char* user) { return match_regex(user, username_regex); } -bool extract_match_group(const char* rti_addr, char* dest, regmatch_t group, int max_len, int min_len, +bool extract_match_group(const char* rti_addr, char* dest, regmatch_t group, size_t max_len, size_t min_len, const char* err_msg) { size_t size = group.rm_eo - group.rm_so; if (size > max_len || size < min_len) { @@ -505,7 +500,7 @@ bool extract_match_group(const char* rti_addr, char* dest, regmatch_t group, int } bool extract_match_groups(const char* rti_addr, char** rti_addr_strs, bool** rti_addr_flags, regmatch_t* group_array, - int* gids, int* max_lens, int* min_lens, const char** err_msgs) { + int* gids, size_t* max_lens, size_t* min_lens, const char** err_msgs) { for (int i = 0; i < 3; i++) { if (group_array[gids[i]].rm_so != -1) { if (!extract_match_group(rti_addr, rti_addr_strs[i], group_array[gids[i]], max_lens[i], min_lens[i], @@ -527,8 +522,8 @@ void extract_rti_addr_info(const char* rti_addr, rti_addr_info_t* rti_addr_info) int gids[3] = {user_gid, host_gid, port_gid}; char* rti_addr_strs[3] = {rti_addr_info->rti_user_str, rti_addr_info->rti_host_str, rti_addr_info->rti_port_str}; bool* rti_addr_flags[3] = {&rti_addr_info->has_user, &rti_addr_info->has_host, &rti_addr_info->has_port}; - int max_lens[3] = {255, 255, 5}; - int min_lens[3] = {1, 1, 1}; + size_t max_lens[3] = {255, 255, 5}; + size_t min_lens[3] = {1, 1, 1}; const char* err_msgs[3] = {"User name must be between 1 to 255 characters long.", "Host must be between 1 to 255 characters long.", "Port must be between 1 to 5 characters long."}; @@ -543,7 +538,7 @@ void extract_rti_addr_info(const char* rti_addr, rti_addr_info_t* rti_addr_info) if (regexec(®ex_compiled, rti_addr, max_groups, group_array, 0) == 0) { // Check for matched username. group_array[0] is the entire matched string. - for (int i = 1; i < max_groups; i++) { + for (size_t i = 1; i < max_groups; i++) { // Annoyingly, the rm_so and rm_eo fields are long long on some platforms and int on others. // To suppress warnings, cast to long long LF_PRINT_DEBUG("runtime rti_addr regex: so: %lld eo: %lld\n", (long long)group_array[i].rm_so, diff --git a/core/lf_token.c b/core/lf_token.c index 4e13b9e6a..48913f85b 100644 --- a/core/lf_token.c +++ b/core/lf_token.c @@ -76,7 +76,7 @@ static lf_token_t* _lf_writable_copy_locked(lf_port_base_t* port) { lf_token_t* token = port->tmplt.token; if (token == NULL) return NULL; - LF_PRINT_DEBUG("lf_writable_copy: Requesting writable copy of token %p with reference count %zu.", token, + LF_PRINT_DEBUG("lf_writable_copy: Requesting writable copy of token %p with reference count %zu.", (void*)token, token->ref_count); if (port->num_destinations == 1 && token->ref_count == 1) { LF_PRINT_DEBUG("lf_writable_copy: Avoided copy because there " @@ -170,13 +170,13 @@ token_freed _lf_free_token(lf_token_t* token) { } if (hashset_num_items(_lf_token_recycling_bin) < _LF_TOKEN_RECYCLING_BIN_SIZE_LIMIT) { // Recycle instead of freeing. - LF_PRINT_DEBUG("_lf_free_token: Putting token on the recycling bin: %p", token); + LF_PRINT_DEBUG("_lf_free_token: Putting token on the recycling bin: %p", (void*)token); if (!hashset_add(_lf_token_recycling_bin, token)) { - lf_print_warning("Putting token %p on the recycling bin, but it is already there!", token); + lf_print_warning("Putting token %p on the recycling bin, but it is already there!", (void*)token); } } else { // Recycling bin is full. - LF_PRINT_DEBUG("_lf_free_token: Freeing allocated memory for token: %p", token); + LF_PRINT_DEBUG("_lf_free_token: Freeing allocated memory for token: %p", (void*)token); free(token); } #if !defined NDEBUG @@ -197,7 +197,7 @@ lf_token_t* _lf_new_token(token_type_t* type, void* value, size_t length) { if (hashset_iterator_next(iterator) >= 0) { result = hashset_iterator_value(iterator); hashset_remove(_lf_token_recycling_bin, result); - LF_PRINT_DEBUG("_lf_new_token: Retrieved token from the recycling bin: %p", result); + LF_PRINT_DEBUG("_lf_new_token: Retrieved token from the recycling bin: %p", (void*)result); } free(iterator); } @@ -212,7 +212,7 @@ lf_token_t* _lf_new_token(token_type_t* type, void* value, size_t length) { if (result == NULL) { // Nothing found on the recycle bin. result = (lf_token_t*)calloc(1, sizeof(lf_token_t)); - LF_PRINT_DEBUG("_lf_new_token: Allocated memory for token: %p", result); + LF_PRINT_DEBUG("_lf_new_token: Allocated memory for token: %p", (void*)result); } result->type = type; result->length = length; @@ -224,7 +224,7 @@ lf_token_t* _lf_new_token(token_type_t* type, void* value, size_t length) { lf_token_t* _lf_get_token(token_template_t* tmplt) { if (tmplt->token != NULL) { if (tmplt->token->ref_count == 1) { - LF_PRINT_DEBUG("_lf_get_token: Reusing template token: %p with ref_count %zu", tmplt->token, + LF_PRINT_DEBUG("_lf_get_token: Reusing template token: %p with ref_count %zu", (void*)tmplt->token, tmplt->token->ref_count); // Free any previous value in the token. _lf_free_token_value(tmplt->token); @@ -268,7 +268,8 @@ void _lf_initialize_template(token_template_t* tmplt, size_t element_size) { lf_token_t* _lf_initialize_token_with_value(token_template_t* tmplt, void* value, size_t length) { assert(tmplt != NULL); - LF_PRINT_DEBUG("_lf_initialize_token_with_value: template %p, value %p", tmplt, value); + LF_PRINT_DEBUG("_lf_initialize_token_with_value: template %p, value %p", (void*)tmplt, value); + lf_token_t* result = _lf_get_token(tmplt); result->value = value; // Count allocations to issue a warning if this is never freed. @@ -322,14 +323,15 @@ void _lf_free_all_tokens() { void _lf_replace_template_token(token_template_t* tmplt, lf_token_t* newtoken) { assert(tmplt != NULL); - LF_PRINT_DEBUG("_lf_replace_template_token: template: %p newtoken: %p.", tmplt, newtoken); + LF_PRINT_DEBUG("_lf_replace_template_token: template: %p newtoken: %p.", (void*)tmplt, (void*)newtoken); if (tmplt->token != newtoken) { if (tmplt->token != NULL) { _lf_done_using(tmplt->token); } if (newtoken != NULL) { newtoken->ref_count++; - LF_PRINT_DEBUG("_lf_replace_template_token: Incremented ref_count of %p to %zu.", newtoken, newtoken->ref_count); + LF_PRINT_DEBUG("_lf_replace_template_token: Incremented ref_count of %p to %zu.", (void*)newtoken, + newtoken->ref_count); } tmplt->token = newtoken; } @@ -339,16 +341,16 @@ token_freed _lf_done_using(lf_token_t* token) { if (token == NULL) { return NOT_FREED; } - LF_PRINT_DEBUG("_lf_done_using: token = %p, ref_count = %zu.", token, token->ref_count); + LF_PRINT_DEBUG("_lf_done_using: token = %p, ref_count = %zu.", (void*)token, token->ref_count); if (token->ref_count == 0) { - lf_print_warning("Token being freed that has already been freed: %p", token); + lf_print_warning("Token being freed that has already been freed: %p", (void*)token); return NOT_FREED; } token->ref_count--; return _lf_free_token(token); } -void _lf_free_token_copies(struct environment_t* env) { +void _lf_free_token_copies() { while (_lf_tokens_allocated_in_reactions != NULL) { lf_token_t* next = _lf_tokens_allocated_in_reactions->next; _lf_done_using(_lf_tokens_allocated_in_reactions); diff --git a/core/lf_utils.cmake b/core/lf_utils.cmake new file mode 100644 index 000000000..5b6b35921 --- /dev/null +++ b/core/lf_utils.cmake @@ -0,0 +1,11 @@ +function(lf_enable_compiler_warnings target) + if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux") + target_compile_options(${target} PRIVATE -Wall -Wextra -Wpedantic -Werror) + elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Windows") + target_compile_options(${target} PRIVATE /W4) + elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Darwin") + target_compile_options(${target} PRIVATE -Wall) + else() + target_compile_options(${target} PRIVATE -Wall) + endif() +endfunction() \ No newline at end of file diff --git a/core/reactor.c b/core/reactor.c index 8bf3b9460..235e1d34d 100644 --- a/core/reactor.c +++ b/core/reactor.c @@ -27,9 +27,18 @@ extern instant_t start_time; int lf_thread_id() { return 0; } -int lf_mutex_unlock(lf_mutex_t* mutex) { return 0; } -int lf_mutex_init(lf_mutex_t* mutex) { return 0; } -int lf_mutex_lock(lf_mutex_t* mutex) { return 0; } +int lf_mutex_unlock(lf_mutex_t* mutex) { + (void)mutex; + return 0; +} +int lf_mutex_init(lf_mutex_t* mutex) { + (void)mutex; + return 0; +} +int lf_mutex_lock(lf_mutex_t* mutex) { + (void)mutex; + return 0; +} // Defined in reactor_common.c: extern bool fast; @@ -83,11 +92,13 @@ void lf_print_snapshot(environment_t* env) { } #else // NDEBUG void lf_print_snapshot(environment_t* env) { + (void)env; // Do nothing. } #endif // NDEBUG void _lf_trigger_reaction(environment_t* env, reaction_t* reaction, int worker_number) { + (void)worker_number; assert(env != GLOBAL_ENVIRONMENT); #ifdef MODAL_REACTORS @@ -273,8 +284,7 @@ int next(environment_t* env) { void lf_request_stop(void) { // There is only one enclave, so get its environment. environment_t* env; - int num_environments = _lf_get_environments(&env); - assert(num_environments == 1); + _lf_get_environments(&env); tag_t new_stop_tag; new_stop_tag.time = env->current_tag.time; @@ -368,9 +378,18 @@ int lf_reactor_c_main(int argc, const char* argv[]) { * @brief Notify of new event by calling the single-threaded platform API * @param env Environment in which we are executing. */ -int lf_notify_of_event(environment_t* env) { return _lf_single_threaded_notify_of_event(); } +int lf_notify_of_event(environment_t* env) { + (void)env; + return _lf_single_threaded_notify_of_event(); +} -int lf_critical_section_enter(environment_t* env) { return lf_disable_interrupts_nested(); } +int lf_critical_section_enter(environment_t* env) { + (void)env; + return lf_disable_interrupts_nested(); +} -int lf_critical_section_exit(environment_t* env) { return lf_enable_interrupts_nested(); } +int lf_critical_section_exit(environment_t* env) { + (void)env; + return lf_enable_interrupts_nested(); +} #endif diff --git a/core/reactor_common.c b/core/reactor_common.c index 6fc3d3824..ba6f9574c 100644 --- a/core/reactor_common.c +++ b/core/reactor_common.c @@ -119,7 +119,7 @@ void lf_free(struct allocation_record_t** head) { LF_PRINT_DEBUG("Freeing memory at %p", record->allocated); free(record->allocated); struct allocation_record_t* tmp = record->next; - LF_PRINT_DEBUG("Freeing allocation record at %p", record); + LF_PRINT_DEBUG("Freeing allocation record at %p", (void*)record); free(record); record = tmp; } @@ -171,7 +171,7 @@ void _lf_start_time_step(environment_t* env) { LF_PRINT_LOG("--------- Start time step at tag " PRINTF_TAG ".", env->current_tag.time - start_time, env->current_tag.microstep); // Handle dynamically created tokens for mutable inputs. - _lf_free_token_copies(env); + _lf_free_token_copies(); bool** is_present_fields = env->is_present_fields_abbreviated; int size = env->is_present_fields_abbreviated_size; @@ -199,7 +199,7 @@ void _lf_start_time_step(environment_t* env) { #ifdef FEDERATED // If the environment is the top-level one, we have some work to do. environment_t* envs; - int num_envs = _lf_get_environments(&envs); + _lf_get_environments(&envs); if (env == envs) { // This is the top-level environment. @@ -278,7 +278,7 @@ void _lf_pop_events(environment_t* env) { reaction->is_STP_violated = true; LF_PRINT_LOG("Trigger %p has violated the reaction's STP offset. Intended tag: " PRINTF_TAG ". Current tag: " PRINTF_TAG, - event->trigger, event->intended_tag.time - start_time, event->intended_tag.microstep, + (void*)event->trigger, event->intended_tag.time - start_time, event->intended_tag.microstep, env->current_tag.time - start_time, env->current_tag.microstep); // Need to update the last_known_status_tag of the port because otherwise, // the MLAA could get stuck, causing the program to lock up. @@ -519,7 +519,7 @@ trigger_handle_t _lf_schedule_at_tag(environment_t* env, trigger_t* trigger, tag // Increment the reference count of the token. if (token != NULL) { token->ref_count++; - LF_PRINT_DEBUG("_lf_schedule_at_tag: Incremented ref_count of %p to %zu.", token, token->ref_count); + LF_PRINT_DEBUG("_lf_schedule_at_tag: Incremented ref_count of %p to %zu.", (void*)token, token->ref_count); } // Do not schedule events if the tag is after the stop tag @@ -854,7 +854,7 @@ void schedule_output_reactions(environment_t* env, reaction_t* reaction, int wor for (int j = 0; j < reaction->triggered_sizes[i]; j++) { trigger_t* trigger = triggerArray[j]; if (trigger != NULL) { - LF_PRINT_DEBUG("Trigger %p lists %d reactions.", trigger, trigger->number_of_reactions); + LF_PRINT_DEBUG("Trigger %p lists %d reactions.", (void*)trigger, trigger->number_of_reactions); for (int k = 0; k < trigger->number_of_reactions; k++) { reaction_t* downstream_reaction = trigger->reactions[k]; #ifdef FEDERATED_DECENTRALIZED // Only pass down tardiness for federated LF programs @@ -1202,11 +1202,11 @@ void initialize_global(void) { _lf_count_token_allocations = 0; #endif - environment_t* envs; - int num_envs = _lf_get_environments(&envs); #if defined(LF_SINGLE_THREADED) int max_threads_tracing = 1; #else + environment_t* envs; + int num_envs = _lf_get_environments(&envs); int max_threads_tracing = envs[0].num_workers * num_envs + 1; // add 1 for the main thread #endif #if defined(FEDERATED) @@ -1329,7 +1329,7 @@ void termination(void) { } index_t lf_combine_deadline_and_level(interval_t deadline, int level) { - if (deadline > ULLONG_MAX >> 16) + if (deadline > (interval_t)(ULLONG_MAX >> 16)) return ((ULLONG_MAX >> 16) << 16) | level; else return (deadline << 16) | level; diff --git a/core/tag.c b/core/tag.c index b45b67acc..9bb35933f 100644 --- a/core/tag.c +++ b/core/tag.c @@ -106,7 +106,7 @@ instant_t lf_time_logical(void* env) { interval_t lf_time_logical_elapsed(void* env) { return lf_time_logical(env) - start_time; } instant_t lf_time_physical(void) { - instant_t now, last_read_local; + instant_t now; // Get the current clock value LF_ASSERTN(lf_clock_gettime(&now), "Failed to read physical clock."); return now; @@ -194,7 +194,7 @@ size_t lf_readable_time(char* buffer, instant_t time) { } size_t printed = lf_comma_separated_time(buffer, time); buffer += printed; - snprintf(buffer, 3, " %s", units); + snprintf(buffer, 4, " %s", units); buffer += strlen(units) + 1; } return (buffer - original_buffer); diff --git a/core/threaded/reactor_threaded.c b/core/threaded/reactor_threaded.c index 3419c3483..e364675d6 100644 --- a/core/threaded/reactor_threaded.c +++ b/core/threaded/reactor_threaded.c @@ -184,7 +184,7 @@ void lf_set_present(lf_port_base_t* port) { // Support for sparse destination multiports. if (port->sparse_record && port->destination_channel >= 0 && port->sparse_record->size >= 0) { - int next = lf_atomic_fetch_add32(&port->sparse_record->size, 1); + size_t next = (size_t)lf_atomic_fetch_add32(&port->sparse_record->size, 1); if (next >= port->sparse_record->capacity) { // Buffer is full. Have to revert to the classic iteration. port->sparse_record->size = -1; @@ -220,7 +220,7 @@ void lf_set_present(lf_port_base_t* port) { * the stop time, if one was specified. Return true if the full wait time * was reached. */ -bool wait_until(environment_t* env, instant_t logical_time, lf_cond_t* condition) { +bool wait_until(instant_t logical_time, lf_cond_t* condition) { LF_PRINT_DEBUG("-------- Waiting until physical time matches logical time " PRINTF_TIME, logical_time); interval_t wait_until_time = logical_time; #ifdef FEDERATED_DECENTRALIZED // Only apply the STA if coordination is decentralized @@ -326,6 +326,8 @@ tag_t send_next_event_tag(environment_t* env, tag_t tag, bool wait_for_reply) { #elif defined(LF_ENCLAVES) return rti_next_event_tag_locked(env->enclave_info, tag); #else + (void)env; + (void)wait_for_reply; return tag; #endif } @@ -425,7 +427,7 @@ void _lf_next_locked(environment_t* env) { // This can be interrupted if a physical action triggers (e.g., a message // arrives from an upstream federate or a local physical action triggers). LF_PRINT_LOG("Waiting until elapsed time " PRINTF_TIME ".", (next_tag.time - start_time)); - while (!wait_until(env, next_tag.time, &env->event_q_changed)) { + while (!wait_until(next_tag.time, &env->event_q_changed)) { LF_PRINT_DEBUG("_lf_next_locked(): Wait until time interrupted."); // Sleep was interrupted. Check for a new next_event. // The interruption could also have been due to a call to lf_request_stop(). @@ -647,7 +649,7 @@ void _lf_initialize_start_tag(environment_t* env) { // Here we wait until the start time and also release the environment mutex. // this means that the other worker threads will be allowed to start. We need // this to avoid potential deadlock in federated startup. - while (!wait_until(env, start_time + lf_fed_STA_offset, &env->event_q_changed)) { + while (!wait_until(start_time + lf_fed_STA_offset, &env->event_q_changed)) { }; LF_PRINT_DEBUG("Done waiting for start time + STA offset " PRINTF_TIME ".", start_time + lf_fed_STA_offset); LF_PRINT_DEBUG("Physical time is ahead of current time by " PRINTF_TIME ". This should be close to the STA offset.", @@ -857,6 +859,8 @@ void _lf_worker_invoke_reaction(environment_t* env, int worker_number, reaction_ void try_advance_level(environment_t* env, volatile size_t* next_reaction_level) { #ifdef FEDERATED lf_stall_advance_level_federation(env, *next_reaction_level); +#else + (void)env; #endif if (*next_reaction_level < SIZE_MAX) *next_reaction_level += 1; @@ -978,6 +982,7 @@ void lf_print_snapshot(environment_t* env) { } #else // NDEBUG void lf_print_snapshot(environment_t* env) { + (void)env; // Do nothing. } #endif // NDEBUG @@ -987,7 +992,7 @@ void start_threads(environment_t* env) { assert(env != GLOBAL_ENVIRONMENT); LF_PRINT_LOG("Starting %u worker threads in environment", env->num_workers); - for (unsigned int i = 0; i < env->num_workers; i++) { + for (int i = 0; i < env->num_workers; i++) { if (lf_thread_create(&env->thread_ids[i], worker, env) != 0) { lf_print_error_and_exit("Could not start thread-%u", i); } diff --git a/core/threaded/scheduler_GEDF_NP.c b/core/threaded/scheduler_GEDF_NP.c index 0f3d971d5..d590adecb 100644 --- a/core/threaded/scheduler_GEDF_NP.c +++ b/core/threaded/scheduler_GEDF_NP.c @@ -155,7 +155,8 @@ void _lf_scheduler_try_advance_tag_and_distribute(lf_scheduler_t* scheduler) { void _lf_sched_wait_for_work(lf_scheduler_t* scheduler, size_t worker_number) { // Increment the number of idle workers by 1 and check if this is the last // worker thread to become idle. - if (lf_atomic_add_fetch32((int32_t*)&scheduler->number_of_idle_workers, 1) == scheduler->number_of_workers) { + if (((size_t)lf_atomic_add_fetch32((int32_t*)&scheduler->number_of_idle_workers, 1)) == + scheduler->number_of_workers) { // Last thread to go idle LF_PRINT_DEBUG("Scheduler: Worker %zu is the last idle thread.", worker_number); // Call on the scheduler to distribute work or advance tag. @@ -279,6 +280,7 @@ reaction_t* lf_sched_get_ready_reaction(lf_scheduler_t* scheduler, int worker_nu * @param done_reaction The reaction that is done. */ void lf_sched_done_with_reaction(size_t worker_number, reaction_t* done_reaction) { + (void)worker_number; if (!lf_atomic_bool_compare_and_swap32((int32_t*)&done_reaction->status, queued, inactive)) { lf_print_error_and_exit("Unexpected reaction status: %d. Expected %d.", done_reaction->status, queued); } @@ -301,6 +303,7 @@ void lf_sched_done_with_reaction(size_t worker_number, reaction_t* done_reaction * worker number does not make sense (e.g., the caller is not a worker thread). */ void lf_scheduler_trigger_reaction(lf_scheduler_t* scheduler, reaction_t* reaction, int worker_number) { + (void)worker_number; if (reaction == NULL || !lf_atomic_bool_compare_and_swap32((int32_t*)&reaction->status, inactive, queued)) { return; } diff --git a/core/threaded/scheduler_NP.c b/core/threaded/scheduler_NP.c index 630464dd6..01b510477 100644 --- a/core/threaded/scheduler_NP.c +++ b/core/threaded/scheduler_NP.c @@ -92,7 +92,7 @@ int _lf_sched_distribute_ready_reactions(lf_scheduler_t* scheduler) { (void*)((reaction_t***)scheduler->triggered_reactions)[scheduler->next_reaction_level - 1]; LF_PRINT_DEBUG("Start of rxn queue at %zu is %p", scheduler->next_reaction_level - 1, - ((reaction_t**)scheduler->executing_reactions)[0]); + (void*)((reaction_t**)scheduler->executing_reactions)[0]); if (((reaction_t**)scheduler->executing_reactions)[0] != NULL) { // There is at least one reaction to execute return 1; @@ -109,14 +109,11 @@ int _lf_sched_distribute_ready_reactions(lf_scheduler_t* scheduler) { */ void _lf_sched_notify_workers(lf_scheduler_t* scheduler) { // Calculate the number of workers that we need to wake up, which is the - // Note: All threads are idle. Therefore, there is no need to lock the mutex - // while accessing the index for the current level. - size_t workers_to_awaken = LF_MIN(scheduler->number_of_idle_workers, - scheduler->indexes[scheduler->next_reaction_level - 1 // Current - // reaction - // level - // to execute. - ]); + // number of reactions enabled at this level. + // Note: All threads are idle. Therefore, there is no need to lock the mutex while accessing the index for the + // current level. + size_t workers_to_awaken = + LF_MIN(scheduler->number_of_idle_workers, (size_t)(scheduler->indexes[scheduler->next_reaction_level - 1])); LF_PRINT_DEBUG("Scheduler: Notifying %zu workers.", workers_to_awaken); scheduler->number_of_idle_workers -= workers_to_awaken; @@ -188,7 +185,7 @@ void _lf_scheduler_try_advance_tag_and_distribute(lf_scheduler_t* scheduler) { void _lf_sched_wait_for_work(lf_scheduler_t* scheduler, size_t worker_number) { // Increment the number of idle workers by 1 and check if this is the last // worker thread to become idle. - if (lf_atomic_add_fetch32((int32_t*)&scheduler->number_of_idle_workers, 1) == scheduler->number_of_workers) { + if (lf_atomic_add_fetch32((int32_t*)&scheduler->number_of_idle_workers, 1) == (int)scheduler->number_of_workers) { // Last thread to go idle LF_PRINT_DEBUG("Scheduler: Worker %zu is the last idle thread.", worker_number); // Call on the scheduler to distribute work or advance tag. @@ -338,6 +335,7 @@ reaction_t* lf_sched_get_ready_reaction(lf_scheduler_t* scheduler, int worker_nu * @param done_reaction The reaction that is done. */ void lf_sched_done_with_reaction(size_t worker_number, reaction_t* done_reaction) { + (void)worker_number; if (!lf_atomic_bool_compare_and_swap32((int32_t*)&done_reaction->status, queued, inactive)) { lf_print_error_and_exit("Unexpected reaction status: %d. Expected %d.", done_reaction->status, queued); } @@ -363,6 +361,8 @@ void lf_sched_done_with_reaction(size_t worker_number, reaction_t* done_reaction * */ void lf_scheduler_trigger_reaction(lf_scheduler_t* scheduler, reaction_t* reaction, int worker_number) { + (void)worker_number; + if (reaction == NULL || !lf_atomic_bool_compare_and_swap32((int32_t*)&reaction->status, inactive, queued)) { return; } diff --git a/core/threaded/scheduler_adaptive.c b/core/threaded/scheduler_adaptive.c index 99f50c043..4b0843028 100644 --- a/core/threaded/scheduler_adaptive.c +++ b/core/threaded/scheduler_adaptive.c @@ -293,13 +293,13 @@ static void worker_states_init(lf_scheduler_t* scheduler, size_t number_of_worke worker_states->worker_conds = (lf_cond_t*)malloc(sizeof(lf_cond_t) * num_conds); worker_states->cumsum_of_worker_group_sizes = (size_t*)calloc(num_conds, sizeof(size_t)); worker_states->mutex_held = (bool*)calloc(number_of_workers, sizeof(bool)); - for (int i = 0; i < number_of_workers; i++) { + for (size_t i = 0; i < number_of_workers; i++) { worker_states->cumsum_of_worker_group_sizes[cond_of(i)]++; } - for (int i = 1; i < num_conds; i++) { + for (size_t i = 1; i < num_conds; i++) { worker_states->cumsum_of_worker_group_sizes[i] += worker_states->cumsum_of_worker_group_sizes[i - 1]; } - for (int i = 0; i < num_conds; i++) { + for (size_t i = 0; i < num_conds; i++) { LF_COND_INIT(worker_states->worker_conds + i, &scheduler->env->mutex); } worker_states->num_loose_threads = scheduler->number_of_workers; @@ -322,7 +322,7 @@ static void worker_states_free(lf_scheduler_t* scheduler) { static void worker_states_awaken_locked(lf_scheduler_t* scheduler, size_t worker, size_t num_to_awaken) { worker_states_t* worker_states = scheduler->custom_data->worker_states; worker_assignments_t* worker_assignments = scheduler->custom_data->worker_assignments; - assert(num_to_awaken <= worker_assignments->max_num_workers); + LF_ASSERT(num_to_awaken <= worker_assignments->max_num_workers, "Sched requested to wake too many workers"); if ((worker == 0) && (num_to_awaken <= 1)) { worker_states->num_loose_threads = 1; return; @@ -339,7 +339,7 @@ static void worker_states_awaken_locked(lf_scheduler_t* scheduler, size_t worker worker_states->num_loose_threads += worker >= worker_states->num_loose_threads; worker_states->num_awakened = worker_states->num_loose_threads; scheduler->custom_data->level_counter++; - for (int cond = 0; cond <= max_cond; cond++) { + for (size_t cond = 0; cond <= max_cond; cond++) { lf_cond_broadcast(worker_states->worker_conds + cond); } } @@ -348,8 +348,8 @@ static void worker_states_awaken_locked(lf_scheduler_t* scheduler, size_t worker static void worker_states_lock(lf_scheduler_t* scheduler, size_t worker) { worker_states_t* worker_states = scheduler->custom_data->worker_states; worker_assignments_t* worker_assignments = scheduler->custom_data->worker_assignments; - assert(worker_states->num_loose_threads > 0); - assert(worker_states->num_loose_threads <= worker_assignments->max_num_workers); + LF_ASSERT(worker_states->num_loose_threads > 0, "Sched: No loose threads"); + LF_ASSERT(worker_states->num_loose_threads <= worker_assignments->max_num_workers, "Sched: Too many loose threads"); size_t lt = worker_states->num_loose_threads; if (lt > 1 || !fast) { // FIXME: Lock should be partially optimized out even when !fast LF_MUTEX_LOCK(&scheduler->env->mutex); @@ -377,9 +377,7 @@ static void worker_states_unlock(lf_scheduler_t* scheduler, size_t worker) { static bool worker_states_finished_with_level_locked(lf_scheduler_t* scheduler, size_t worker) { worker_states_t* worker_states = scheduler->custom_data->worker_states; worker_assignments_t* worker_assignments = scheduler->custom_data->worker_assignments; - assert(worker >= 0); - assert(worker_states->num_loose_threads > 0); - assert(worker_assignments->num_reactions_by_worker[worker] != 1); + LF_ASSERT(worker_assignments->num_reactions_by_worker[worker] != 1, "Sched: Current worker not assigned"); assert(((int64_t)worker_assignments->num_reactions_by_worker[worker]) <= 0); // Why use an atomic operation when we are supposed to be "as good as locked"? Because I took a // shortcut, and the shortcut was imperfect. @@ -401,8 +399,8 @@ static bool worker_states_finished_with_level_locked(lf_scheduler_t* scheduler, static void worker_states_sleep_and_unlock(lf_scheduler_t* scheduler, size_t worker, size_t level_counter_snapshot) { worker_states_t* worker_states = scheduler->custom_data->worker_states; worker_assignments_t* worker_assignments = scheduler->custom_data->worker_assignments; - assert(worker < worker_assignments->max_num_workers); - assert(worker_states->num_loose_threads <= worker_assignments->max_num_workers); + LF_ASSERT(worker < worker_assignments->max_num_workers, "Sched: Invalid worker"); + LF_ASSERT(worker_states->num_loose_threads <= worker_assignments->max_num_workers, "Sched: Too many loose threads"); if (!worker_states->mutex_held[worker]) { LF_MUTEX_LOCK(&scheduler->env->mutex); } @@ -413,7 +411,8 @@ static void worker_states_sleep_and_unlock(lf_scheduler_t* scheduler, size_t wor lf_cond_wait(worker_states->worker_conds + cond); } while (level_counter_snapshot == scheduler->custom_data->level_counter || worker >= worker_states->num_awakened); } - assert(!worker_states->mutex_held[worker]); // This thread holds the mutex, but it did not report that. + LF_ASSERT(!worker_states->mutex_held[worker], + "Sched: Worker doesnt hold the mutex"); // This thread holds the mutex, but it did not report that. LF_MUTEX_UNLOCK(&scheduler->env->mutex); } @@ -423,7 +422,6 @@ static void worker_states_sleep_and_unlock(lf_scheduler_t* scheduler, size_t wor */ static void advance_level_and_unlock(lf_scheduler_t* scheduler, size_t worker) { worker_assignments_t* worker_assignments = scheduler->custom_data->worker_assignments; - worker_states_t* worker_states = scheduler->custom_data->worker_states; size_t max_level = worker_assignments->num_levels - 1; while (true) { if (worker_assignments->current_level == max_level) { @@ -443,7 +441,7 @@ static void advance_level_and_unlock(lf_scheduler_t* scheduler, size_t worker) { size_t total_num_reactions = get_num_reactions(scheduler); if (total_num_reactions) { size_t num_workers_to_awaken = LF_MIN(total_num_reactions, worker_assignments->num_workers); - assert(num_workers_to_awaken > 0); + LF_ASSERT(num_workers_to_awaken > 0, ""); worker_states_awaken_locked(scheduler, worker, num_workers_to_awaken); worker_states_unlock(scheduler, worker); return; @@ -476,7 +474,7 @@ static void possible_nums_workers_init(lf_scheduler_t* scheduler) { data_collection->possible_nums_workers = (size_t*)malloc(pnw_length * sizeof(size_t)); temp = 1; data_collection->possible_nums_workers[0] = 0; - for (int i = 1; i < pnw_length; i++) { + for (size_t i = 1; i < pnw_length; i++) { data_collection->possible_nums_workers[i] = temp; temp *= 2; } @@ -492,7 +490,7 @@ static void possible_nums_workers_init(lf_scheduler_t* scheduler) { * would like to optimize. */ static int get_jitter(size_t current_state, interval_t execution_time) { - static const size_t parallelism_cost_max = 114688; + static const interval_t parallelism_cost_max = 114688; // The following handles the case where the current level really is just fluff: // No parallelism needed, no work to be done. if (execution_time < 16384 && current_state == 1) @@ -595,7 +593,6 @@ static size_t restrict_to_range(size_t start_inclusive, size_t end_inclusive, si */ static void compute_number_of_workers(lf_scheduler_t* scheduler, size_t* num_workers_by_level, size_t* max_num_workers_by_level, bool jitter) { - worker_assignments_t* worker_assignments = scheduler->custom_data->worker_assignments; data_collection_t* data_collection = scheduler->custom_data->data_collection; for (size_t level = 0; level < data_collection->num_levels; level++) { interval_t this_execution_time = @@ -603,7 +600,6 @@ static void compute_number_of_workers(lf_scheduler_t* scheduler, size_t* num_wor size_t ideal_number_of_workers; size_t max_reasonable_num_workers = max_num_workers_by_level[level]; ideal_number_of_workers = data_collection->execution_times_argmins[level]; - int range = 1; if (jitter) { ideal_number_of_workers = get_nums_workers_neighboring_state(scheduler, ideal_number_of_workers, this_execution_time); @@ -621,7 +617,6 @@ static void compute_number_of_workers(lf_scheduler_t* scheduler, size_t* num_wor */ static void compute_costs(lf_scheduler_t* scheduler, size_t* num_workers_by_level) { data_collection_t* data_collection = scheduler->custom_data->data_collection; - worker_assignments_t* worker_assignments = scheduler->custom_data->worker_assignments; for (size_t level = 0; level < data_collection->num_levels; level++) { interval_t score = data_collection->execution_times_by_num_workers_by_level[level][num_workers_by_level[level]]; if (!data_collection->execution_times_mins[level] | (score < data_collection->execution_times_mins[level]) | @@ -640,7 +635,6 @@ static void compute_costs(lf_scheduler_t* scheduler, size_t* num_workers_by_leve */ static void data_collection_end_tag(lf_scheduler_t* scheduler, size_t* num_workers_by_level, size_t* max_num_workers_by_level) { - worker_assignments_t* worker_assignments = scheduler->custom_data->worker_assignments; data_collection_t* data_collection = scheduler->custom_data->data_collection; if (data_collection->collecting_data && data_collection->start_times_by_level[0]) { compute_costs(scheduler, num_workers_by_level); @@ -716,13 +710,13 @@ reaction_t* lf_sched_get_ready_reaction(lf_scheduler_t* scheduler, int worker_nu } void lf_sched_done_with_reaction(size_t worker_number, reaction_t* done_reaction) { - assert(worker_number >= 0); - assert(done_reaction->status != inactive); + (void)worker_number; + LF_ASSERT(done_reaction->status != inactive, ""); done_reaction->status = inactive; } void lf_scheduler_trigger_reaction(lf_scheduler_t* scheduler, reaction_t* reaction, int worker_number) { - assert(worker_number >= -1); + LF_ASSERT(worker_number >= -1, "Sched: Invalid worker number"); if (!lf_atomic_bool_compare_and_swap32((int32_t*)&reaction->status, inactive, queued)) return; worker_assignments_put(scheduler, reaction); diff --git a/core/threaded/watchdog.c b/core/threaded/watchdog.c index 4f26f26e7..3b9a6d62c 100644 --- a/core/threaded/watchdog.c +++ b/core/threaded/watchdog.c @@ -43,13 +43,12 @@ void _lf_initialize_watchdogs(environment_t* env) { * @brief Terminate all watchdog threads. */ void _lf_watchdog_terminate_all(environment_t* env) { - void* thread_return; + void* thread_ret; for (int i = 0; i < env->watchdogs_size; i++) { watchdog_t* watchdog = env->watchdogs[i]; LF_MUTEX_LOCK(watchdog->base->reactor_mutex); _lf_watchdog_terminate(watchdog); LF_MUTEX_UNLOCK(watchdog->base->reactor_mutex); - void* thread_ret; lf_thread_join(watchdog->thread_id, &thread_ret); } } diff --git a/core/tracepoint.c b/core/tracepoint.c index 24b2f2434..b121be8da 100644 --- a/core/tracepoint.c +++ b/core/tracepoint.c @@ -61,7 +61,7 @@ int register_user_trace_event(void* self, char* description) { } void call_tracepoint(int event_type, void* reactor, tag_t tag, int worker, int src_id, int dst_id, - instant_t* physical_time, trigger_t* trigger, interval_t extra_delay, bool is_interval_start) { + instant_t* physical_time, trigger_t* trigger, interval_t extra_delay) { instant_t local_time; if (physical_time == NULL) { local_time = lf_time_physical(); @@ -97,7 +97,7 @@ void tracepoint_schedule(environment_t* env, trigger_t* trigger, interval_t extr // This is OK because it is called only while holding the mutex lock. // True argument specifies to record physical time as late as possible, when // the event is already on the event queue. - call_tracepoint(schedule_called, reactor, env->current_tag, -1, 0, 0, NULL, trigger, extra_delay, true); + call_tracepoint(schedule_called, reactor, env->current_tag, -1, 0, 0, NULL, trigger, extra_delay); } /** @@ -119,7 +119,7 @@ void tracepoint_user_event(void* self, char* description) { // There will be a performance hit for this. LF_ASSERT(self, "A pointer to the self struct is needed to trace an event"); environment_t* env = ((self_base_t*)self)->environment; - call_tracepoint(user_event, description, env->current_tag, -1, -1, -1, NULL, NULL, 0, false); + call_tracepoint(user_event, description, env->current_tag, -1, -1, -1, NULL, NULL, 0); } /** @@ -144,7 +144,7 @@ void tracepoint_user_value(void* self, char* description, long long value) { // because multiple reactions might be calling the same tracepoint function. // There will be a performance hit for this. environment_t* env = ((self_base_t*)self)->environment; - call_tracepoint(user_value, description, env->current_tag, -1, -1, -1, NULL, NULL, value, false); + call_tracepoint(user_value, description, env->current_tag, -1, -1, -1, NULL, NULL, value); } //////////////////////////////////////////////////////////// @@ -168,8 +168,7 @@ void tracepoint_federate_to_rti(trace_event_t event_type, int fed_id, tag_t* tag -1, // int dst_id, NULL, // instant_t* physical_time (will be generated) NULL, // trigger_t* trigger, - 0, // interval_t extra_delay - true // is_interval_start + 0 // interval_t extra_delay ); } @@ -190,8 +189,7 @@ void tracepoint_federate_from_rti(trace_event_t event_type, int fed_id, tag_t* t -1, // int dst_id, NULL, // instant_t* physical_time (will be generated) NULL, // trigger_t* trigger, - 0, // interval_t extra_delay - false // is_interval_start + 0 // interval_t extra_delay ); } @@ -212,8 +210,7 @@ void tracepoint_federate_to_federate(trace_event_t event_type, int fed_id, int p partner_id, // int dst_id, NULL, // instant_t* physical_time (will be generated) NULL, // trigger_t* trigger, - 0, // interval_t extra_delay - true // is_interval_start + 0 // interval_t extra_delay ); } @@ -234,8 +231,7 @@ void tracepoint_federate_from_federate(trace_event_t event_type, int fed_id, int partner_id, // int dst_id, NULL, // instant_t* physical_time (will be generated) NULL, // trigger_t* trigger, - 0, // interval_t extra_delay - false // is_interval_start + 0 // interval_t extra_delay ); } #endif // FEDERATED @@ -261,8 +257,7 @@ void tracepoint_rti_to_federate(trace_event_t event_type, int fed_id, tag_t* tag fed_id, // int dst_id NULL, // instant_t* physical_time (will be generated) NULL, // trigger_t* trigger, - 0, // interval_t extra_delay - true // is_interval_start + 0 // interval_t extra_delay ); } @@ -282,8 +277,7 @@ void tracepoint_rti_from_federate(trace_event_t event_type, int fed_id, tag_t* t fed_id, // int dst_id NULL, // instant_t* physical_time (will be generated) NULL, // trigger_t* trigger, - 0, // interval_t extra_delay - false // is_interval_start + 0 // interval_t extra_delay ); } diff --git a/core/utils/CMakeLists.txt b/core/utils/CMakeLists.txt index 7ab0db8d4..723b942c8 100644 --- a/core/utils/CMakeLists.txt +++ b/core/utils/CMakeLists.txt @@ -1,5 +1,8 @@ -set(UTIL_SOURCES vector.c pqueue_base.c pqueue_tag.c pqueue.c util.c lf_semaphore.c) +set(UTIL_SOURCES vector.c pqueue_base.c pqueue_tag.c pqueue.c util.c) +if(NOT DEFINED LF_SINGLE_THREADED) + list(APPEND UTIL_SOURCES lf_semaphore.c) +endif() list(TRANSFORM UTIL_SOURCES PREPEND utils/) list(APPEND REACTORC_SOURCES ${UTIL_SOURCES}) diff --git a/core/utils/pqueue.c b/core/utils/pqueue.c index e73e3ed48..d26d8b2aa 100644 --- a/core/utils/pqueue.c +++ b/core/utils/pqueue.c @@ -15,7 +15,11 @@ int in_reverse_order(pqueue_pri_t thiz, pqueue_pri_t that) { return (thiz > that); } -int in_no_particular_order(pqueue_pri_t thiz, pqueue_pri_t that) { return 0; } +int in_no_particular_order(pqueue_pri_t thiz, pqueue_pri_t that) { + (void)thiz; + (void)that; + return 0; +} int event_matches(void* event1, void* event2) { return (((event_t*)event1)->trigger == ((event_t*)event2)->trigger); } @@ -35,10 +39,10 @@ void set_reaction_position(void* reaction, size_t pos) { ((reaction_t*)reaction) void print_reaction(void* reaction) { reaction_t* r = (reaction_t*)reaction; - LF_PRINT_DEBUG("%s: chain_id: %llu, index: %llx, reaction: %p", r->name, r->chain_id, r->index, r); + LF_PRINT_DEBUG("%s: chain_id: %llu, index: %llx, reaction: %p", r->name, r->chain_id, r->index, reaction); } void print_event(void* event) { event_t* e = (event_t*)event; - LF_PRINT_DEBUG("time: " PRINTF_TIME ", trigger: %p, token: %p", e->time, e->trigger, e->token); + LF_PRINT_DEBUG("time: " PRINTF_TIME ", trigger: %p, token: %p", e->time, (void*)e->trigger, (void*)e->token); } diff --git a/core/utils/pqueue_support.h b/core/utils/pqueue_support.h index b7c0a08c1..c751b67f1 100644 --- a/core/utils/pqueue_support.h +++ b/core/utils/pqueue_support.h @@ -97,7 +97,7 @@ static void set_reaction_position(void* a, size_t pos) { ((reaction_t*)a)->pos = */ static void print_reaction(void* reaction) { reaction_t* r = (reaction_t*)reaction; - LF_PRINT_DEBUG("%s: chain_id:%llu, index: %llx, reaction: %p", r->name, r->chain_id, r->index, r); + LF_PRINT_DEBUG("%s: chain_id:%llu, index: %llx, reaction: %p", r->name, r->chain_id, r->index, (void*)r); } /** @@ -107,7 +107,7 @@ static void print_reaction(void* reaction) { */ static void print_event(void* event) { event_t* e = (event_t*)event; - LF_PRINT_DEBUG("time: " PRINTF_TIME ", trigger: %p, token: %p", e->time, e->trigger, e->token); + LF_PRINT_DEBUG("time: " PRINTF_TIME ", trigger: %p, token: %p", e->time, (void*)e->trigger, (void*)e->token); } // ********** Priority Queue Support End diff --git a/core/utils/pqueue_tag.c b/core/utils/pqueue_tag.c index 9406ca1ca..9fe00653d 100644 --- a/core/utils/pqueue_tag.c +++ b/core/utils/pqueue_tag.c @@ -81,7 +81,7 @@ pqueue_tag_t* pqueue_tag_init(size_t initial_size) { } void pqueue_tag_free(pqueue_tag_t* q) { - for (int i = 1; i < q->size; i++) { + for (size_t i = 1; i < q->size; i++) { if (q->d[i] != NULL && ((pqueue_tag_element_t*)q->d[i])->is_dynamic) { free(q->d[i]); } diff --git a/core/utils/util.c b/core/utils/util.c index 554707edb..881b6dc05 100644 --- a/core/utils/util.c +++ b/core/utils/util.c @@ -73,20 +73,20 @@ int lf_fed_id() { return _lf_my_fed_id; } // Declaration needed to attach attributes to suppress warnings of the form: // "warning: function '_lf_message_print' might be a candidate for 'gnu_printf' // format attribute [-Wsuggest-attribute=format]" -void _lf_message_print(int is_error, const char* prefix, const char* format, va_list args, int log_level) - ATTRIBUTE_FORMAT_PRINTF(3, 0); +void _lf_message_print(const char* prefix, const char* format, va_list args, int log_level) + ATTRIBUTE_FORMAT_PRINTF(2, 0); /** * Print a fatal error message. Internal function. */ static void lf_vprint_fatal_error(const char* format, va_list args) { - _lf_message_print(1, "FATAL ERROR: ", format, args, LOG_LEVEL_ERROR); + _lf_message_print("FATAL ERROR: ", format, args, LOG_LEVEL_ERROR); } /** * Internal implementation of the next few reporting functions. */ -void _lf_message_print(int is_error, const char* prefix, const char* format, va_list args, +void _lf_message_print(const char* prefix, const char* format, va_list args, int log_level) { // Disable warnings about format check. // The logging level may be set either by a LOG_LEVEL #define // (which is code generated based on the logging target property) @@ -151,7 +151,7 @@ void lf_print(const char* format, ...) { va_end(args); } -void lf_vprint(const char* format, va_list args) { _lf_message_print(0, "", format, args, LOG_LEVEL_INFO); } +void lf_vprint(const char* format, va_list args) { _lf_message_print("", format, args, LOG_LEVEL_INFO); } void lf_print_log(const char* format, ...) { va_list args; @@ -160,7 +160,7 @@ void lf_print_log(const char* format, ...) { va_end(args); } -void lf_vprint_log(const char* format, va_list args) { _lf_message_print(0, "LOG: ", format, args, LOG_LEVEL_LOG); } +void lf_vprint_log(const char* format, va_list args) { _lf_message_print("LOG: ", format, args, LOG_LEVEL_LOG); } void lf_print_debug(const char* format, ...) { va_list args; @@ -169,9 +169,7 @@ void lf_print_debug(const char* format, ...) { va_end(args); } -void lf_vprint_debug(const char* format, va_list args) { - _lf_message_print(0, "DEBUG: ", format, args, LOG_LEVEL_DEBUG); -} +void lf_vprint_debug(const char* format, va_list args) { _lf_message_print("DEBUG: ", format, args, LOG_LEVEL_DEBUG); } void lf_print_error(const char* format, ...) { va_list args; @@ -180,9 +178,7 @@ void lf_print_error(const char* format, ...) { va_end(args); } -void lf_vprint_error(const char* format, va_list args) { - _lf_message_print(1, "ERROR: ", format, args, LOG_LEVEL_ERROR); -} +void lf_vprint_error(const char* format, va_list args) { _lf_message_print("ERROR: ", format, args, LOG_LEVEL_ERROR); } void lf_print_warning(const char* format, ...) { va_list args; @@ -192,7 +188,7 @@ void lf_print_warning(const char* format, ...) { } void lf_vprint_warning(const char* format, va_list args) { - _lf_message_print(1, "WARNING: ", format, args, LOG_LEVEL_WARNING); + _lf_message_print("WARNING: ", format, args, LOG_LEVEL_WARNING); } void lf_print_error_and_exit(const char* format, ...) { diff --git a/include/core/federated/network/net_util.h b/include/core/federated/network/net_util.h index 555c8df89..24b4782f9 100644 --- a/include/core/federated/network/net_util.h +++ b/include/core/federated/network/net_util.h @@ -365,7 +365,7 @@ bool validate_user(const char* user); * Extract one match group from the rti_addr regex . * @return true if SUCCESS, else false. */ -bool extract_match_group(const char* rti_addr, char* dest, regmatch_t group, int max_len, int min_len, +bool extract_match_group(const char* rti_addr, char* dest, regmatch_t group, size_t max_len, size_t min_len, const char* err_msg); /** @@ -373,7 +373,7 @@ bool extract_match_group(const char* rti_addr, char* dest, regmatch_t group, int * @return true if success, else false. */ bool extract_match_groups(const char* rti_addr, char** rti_addr_strs, bool** rti_addr_flags, regmatch_t* group_array, - int* gids, int* max_lens, int* min_lens, const char** err_msgs); + int* gids, size_t* max_lens, size_t* min_lens, const char** err_msgs); /** * Extract the host, port and user from rti_addr. diff --git a/include/core/lf_token.h b/include/core/lf_token.h index f60fd18b5..219538dd3 100644 --- a/include/core/lf_token.h +++ b/include/core/lf_token.h @@ -341,7 +341,7 @@ token_freed _lf_done_using(lf_token_t* token); * to avoid memory leaks. * @param env Environment in which we are executing. */ -void _lf_free_token_copies(struct environment_t* env); +void _lf_free_token_copies(void); #endif /* LF_TOKEN_H */ /** @} */ diff --git a/include/core/threaded/reactor_threaded.h b/include/core/threaded/reactor_threaded.h index 6971cec17..96de7ac49 100644 --- a/include/core/threaded/reactor_threaded.h +++ b/include/core/threaded/reactor_threaded.h @@ -90,7 +90,7 @@ void _lf_decrement_tag_barrier_locked(environment_t* env); int _lf_wait_on_tag_barrier(environment_t* env, tag_t proposed_tag); void lf_synchronize_with_other_federates(void); -bool wait_until(environment_t* env, instant_t logical_time_ns, lf_cond_t* condition); +bool wait_until(instant_t logical_time_ns, lf_cond_t* condition); tag_t get_next_event_tag(environment_t* env); tag_t send_next_event_tag(environment_t* env, tag_t tag, bool wait_for_reply); void _lf_next_locked(environment_t* env); diff --git a/include/core/tracepoint.h b/include/core/tracepoint.h index aea972b14..c43763a07 100644 --- a/include/core/tracepoint.h +++ b/include/core/tracepoint.h @@ -106,68 +106,6 @@ typedef enum { #include "trace.h" -/** - * String description of event types. - */ -static const char* trace_event_names[] = { - "Reaction starts", - "Reaction ends", - "Reaction deadline missed", - "Schedule called", - "User-defined event", - "User-defined valued event", - "Worker wait starts", - "Worker wait ends", - "Scheduler advancing time starts", - "Scheduler advancing time ends", - "Federated marker", - // Sending messages - "Sending ACK", - "Sending FAILED", - "Sending TIMESTAMP", - "Sending NET", - "Sending LTC", - "Sending STOP_REQ", - "Sending STOP_REQ_REP", - "Sending STOP_GRN", - "Sending FED_ID", - "Sending PTAG", - "Sending TAG", - "Sending REJECT", - "Sending RESIGN", - "Sending PORT_ABS", - "Sending CLOSE_RQ", - "Sending TAGGED_MSG", - "Sending P2P_TAGGED_MSG", - "Sending MSG", - "Sending P2P_MSG", - "Sending ADR_AD", - "Sending ADR_QR", - // Receiving messages - "Receiving ACK", - "Receiving FAILED", - "Receiving TIMESTAMP", - "Receiving NET", - "Receiving LTC", - "Receiving STOP_REQ", - "Receiving STOP_REQ_REP", - "Receiving STOP_GRN", - "Receiving FED_ID", - "Receiving PTAG", - "Receiving TAG", - "Receiving REJECT", - "Receiving RESIGN", - "Receiving PORT_ABS", - "Receiving CLOSE_RQ", - "Receiving TAGGED_MSG", - "Receiving P2P_TAGGED_MSG", - "Receiving MSG", - "Receiving P2P_MSG", - "Receiving ADR_AD", - "Receiving ADR_QR", - "Receiving UNIDENTIFIED", -}; - /** * @brief A trace record that gets written in binary to the trace file in the default implementation. */ @@ -199,7 +137,7 @@ typedef struct trace_record_t { * argument is currently unused) */ void call_tracepoint(int event_type, void* reactor, tag_t tag, int worker, int src_id, int dst_id, - instant_t* physical_time, trigger_t* trigger, interval_t extra_delay, bool is_interval_start); + instant_t* physical_time, trigger_t* trigger, interval_t extra_delay); /** * Register a trace object. @@ -228,8 +166,7 @@ int register_user_trace_event(void* self, char* description); * @param worker The thread number of the worker thread or 0 for single-threaded execution. */ #define tracepoint_reaction_starts(env, reaction, worker) \ - call_tracepoint(reaction_starts, reaction->self, env->current_tag, worker, worker, reaction->number, NULL, NULL, 0, \ - true) + call_tracepoint(reaction_starts, reaction->self, env->current_tag, worker, worker, reaction->number, NULL, NULL, 0) /** * Trace the end of a reaction execution. @@ -238,8 +175,7 @@ int register_user_trace_event(void* self, char* description); * @param worker The thread number of the worker thread or 0 for single-threaded execution. */ #define tracepoint_reaction_ends(env, reaction, worker) \ - call_tracepoint(reaction_ends, reaction->self, env->current_tag, worker, worker, reaction->number, NULL, NULL, 0, \ - false) + call_tracepoint(reaction_ends, reaction->self, env->current_tag, worker, worker, reaction->number, NULL, NULL, 0) /** * Trace a call to schedule. @@ -281,7 +217,7 @@ void tracepoint_user_value(void* self, char* description, long long value); * @param worker The thread number of the worker thread or 0 for single-threaded execution. */ #define tracepoint_worker_wait_starts(env, worker) \ - call_tracepoint(worker_wait_starts, NULL, env->current_tag, worker, worker, -1, NULL, NULL, 0, true) + call_tracepoint(worker_wait_starts, NULL, env->current_tag, worker, worker, -1, NULL, NULL, 0) /** * Trace the end of a worker waiting for something to change on the event or reaction queue. @@ -289,7 +225,7 @@ void tracepoint_user_value(void* self, char* description, long long value); * @param worker The thread number of the worker thread or 0 for single-threaded execution. */ #define tracepoint_worker_wait_ends(env, worker) \ - call_tracepoint(worker_wait_ends, NULL, env->current_tag, worker, worker, -1, NULL, NULL, 0, false) + call_tracepoint(worker_wait_ends, NULL, env->current_tag, worker, worker, -1, NULL, NULL, 0) /** * Trace the start of the scheduler waiting for logical time to advance or an event to @@ -297,7 +233,7 @@ void tracepoint_user_value(void* self, char* description, long long value); * @param trace The trace object. */ #define tracepoint_scheduler_advancing_time_starts(env) \ - call_tracepoint(scheduler_advancing_time_starts, NULL, env->current_tag, -1, -1, -1, NULL, NULL, 0, true); + call_tracepoint(scheduler_advancing_time_starts, NULL, env->current_tag, -1, -1, -1, NULL, NULL, 0); /** * Trace the end of the scheduler waiting for logical time to advance or an event to @@ -305,7 +241,7 @@ void tracepoint_user_value(void* self, char* description, long long value); * @param trace The trace object. */ #define tracepoint_scheduler_advancing_time_ends(env) \ - call_tracepoint(scheduler_advancing_time_ends, NULL, env->current_tag, -1, -1, -1, NULL, NULL, 0, false) + call_tracepoint(scheduler_advancing_time_ends, NULL, env->current_tag, -1, -1, -1, NULL, NULL, 0) /** * Trace the occurrence of a deadline miss. @@ -315,7 +251,7 @@ void tracepoint_user_value(void* self, char* description, long long value); */ #define tracepoint_reaction_deadline_missed(env, reaction, worker) \ call_tracepoint(reaction_deadline_missed, reaction->self, env->current_tag, worker, worker, reaction->number, NULL, \ - NULL, 0, false) + NULL, 0) /** * @brief Check if the tracing library is compatible with the current version @@ -367,10 +303,28 @@ void tracepoint_federate_to_federate(trace_event_t event_type, int fed_id, int p void tracepoint_federate_from_federate(trace_event_t event_type, int fed_id, int partner_id, tag_t* tag); #else -#define tracepoint_federate_to_rti(...) ; -#define tracepoint_federate_from_rti(...) ; -#define tracepoint_federate_to_federate(...) ; -#define tracepoint_federate_from_federate(...) ; +static inline void tracepoint_federate_to_rti(trace_event_t event_type, int fed_id, tag_t* tag) { + (void)event_type; + (void)fed_id; + (void)tag; +} +static inline void tracepoint_federate_from_rti(trace_event_t event_type, int fed_id, tag_t* tag) { + (void)event_type; + (void)fed_id; + (void)tag; +} +static inline void tracepoint_federate_to_federate(trace_event_t event_type, int fed_id, int partner_id, tag_t* tag) { + (void)event_type; + (void)fed_id; + (void)partner_id; + (void)tag; +} +static inline void tracepoint_federate_from_federate(trace_event_t event_type, int fed_id, int partner_id, tag_t* tag) { + (void)event_type; + (void)fed_id; + (void)partner_id; + (void)tag; +} #endif // FEDERATED //////////////////////////////////////////////////////////// @@ -397,38 +351,115 @@ void tracepoint_rti_to_federate(trace_event_t event_type, int fed_id, tag_t* tag void tracepoint_rti_from_federate(trace_event_t event_type, int fed_id, tag_t* tag); #else -#define tracepoint_rti_to_federate(...) ; -#define tracepoint_rti_from_federate(...) ; +static inline void tracepoint_rti_to_federate(trace_event_t event_type, int fed_id, tag_t* tag) { + (void)event_type; + (void)fed_id; + (void)tag; +} +static inline void tracepoint_rti_from_federate(trace_event_t event_type, int fed_id, tag_t* tag) { + (void)event_type; + (void)fed_id; + (void)tag; +} #endif // RTI_TRACE #else typedef struct trace_t trace_t; +static inline int register_user_trace_event(void* self, char* description) { + (void)self; + (void)description; + return 0; +} +static inline void tracepoint_schedule(environment_t* env, trigger_t* trigger, interval_t extra_delay) { + (void)env; + (void)trigger; + (void)extra_delay; +} +static inline void tracepoint_user_event(void* self, char* description) { + (void)self; + (void)description; +} +static inline void tracepoint_user_value(void* self, char* description, long long value) { + (void)self; + (void)description; + (void)value; +} +static inline void tracepoint_rti_to_federate(trace_event_t event_type, int fed_id, tag_t* tag) { + (void)event_type; + (void)fed_id; + (void)tag; +} +static inline void tracepoint_rti_from_federate(trace_event_t event_type, int fed_id, tag_t* tag) { + (void)event_type; + (void)fed_id; + (void)tag; +} +static inline void tracepoint_federate_to_rti(trace_event_t event_type, int fed_id, tag_t* tag) { + (void)event_type; + (void)fed_id; + (void)tag; +} +static inline void tracepoint_federate_from_rti(trace_event_t event_type, int fed_id, tag_t* tag) { + (void)event_type; + (void)fed_id; + (void)tag; +} +static inline void tracepoint_federate_to_federate(trace_event_t event_type, int fed_id, int partner_id, tag_t* tag) { + (void)event_type; + (void)fed_id; + (void)partner_id; + (void)tag; +} +static inline void tracepoint_federate_from_federate(trace_event_t event_type, int fed_id, int partner_id, tag_t* tag) { + (void)event_type; + (void)fed_id; + (void)partner_id; + (void)tag; +} +static inline void lf_tracing_global_init(char* file_name_prefix, int process_id, int max_num_local_threads) { + (void)file_name_prefix; + (void)process_id; + (void)max_num_local_threads; +} +static inline void lf_tracing_global_shutdown() {} +static inline void lf_tracing_set_start_time(int64_t start_time) { (void)start_time; } -// empty definition in case we compile without tracing -#define _lf_register_trace_event(...) 1 -#define register_user_trace_event(...) 1 -#define tracepoint_reaction_starts(...) -#define tracepoint_reaction_ends(...) -#define tracepoint_schedule(...) -#define tracepoint_user_event(...) -#define tracepoint_user_value(...) -#define tracepoint_worker_wait_starts(...) -#define tracepoint_worker_wait_ends(...) -#define tracepoint_scheduler_advancing_time_starts(...) ; -#define tracepoint_scheduler_advancing_time_ends(...) ; -#define tracepoint_reaction_deadline_missed(...) ; -#define tracepoint_federate_to_rti(...) ; -#define tracepoint_federate_from_rti(...) ; -#define tracepoint_federate_to_federate(...) ; -#define tracepoint_federate_from_federate(...) ; -#define tracepoint_rti_to_federate(...) ; -#define tracepoint_rti_from_federate(...) ; - -#define lf_tracing_register_trace_event(...) ; -#define lf_tracing_set_start_time(...) ; -#define tracepoint(...) ; -#define lf_tracing_global_init(...) ; -#define lf_tracing_global_shutdown(...) ; +#define tracepoint_reaction_starts(env, reaction, worker) \ + while (0) { \ + (void)env; \ + (void)reaction; \ + (void)worker; \ + } +#define tracepoint_reaction_ends(env, reaction, worker) \ + while (0) { \ + (void)env; \ + (void)reaction; \ + (void)worker; \ + } +#define tracepoint_worker_wait_starts(env, worker) \ + while (0) { \ + (void)env; \ + (void)worker; \ + } +#define tracepoint_worker_wait_ends(env, worker) \ + while (0) { \ + (void)env; \ + (void)worker; \ + } +#define tracepoint_scheduler_advancing_time_starts(env) \ + while (0) { \ + (void)env; \ + } +#define tracepoint_scheduler_advancing_time_ends(env) \ + while (0) { \ + (void)env; \ + } +#define tracepoint_reaction_deadline_missed(env, reaction, worker) \ + while (0) { \ + (void)env; \ + (void)reaction; \ + (void)worker; \ + } #endif // LF_TRACE #endif // TRACEPOINT_H diff --git a/include/core/utils/impl/hashmap.h b/include/core/utils/impl/hashmap.h index 8490e3f62..e64774887 100644 --- a/include/core/utils/impl/hashmap.h +++ b/include/core/utils/impl/hashmap.h @@ -125,7 +125,6 @@ void HASHMAP(free)(HASHMAP(t) * hashmap) { void HASHMAP(put)(HASHMAP(t) * hashmap, K key, V value) { assert(key != hashmap->nothing); - assert(key >= 0); HASHMAP(entry_t)* write_to = HASHMAP(get_actual_address)(hashmap, key); write_to->key = key; write_to->value = value; diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index f0b2c18bc..0b805b7e4 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -1,3 +1,8 @@ +set(LF_ROOT ${CMAKE_CURRENT_LIST_DIR}/..) +include(${LF_ROOT}/core/lf_utils.cmake) + add_library(lib schedule.c) target_link_libraries(lib PRIVATE lf::low-level-platform-api) target_link_libraries(lib PRIVATE lf::logging-api) + +lf_enable_compiler_warnings(lib) \ No newline at end of file diff --git a/lib/schedule.c b/lib/schedule.c index 645bb41ec..8d237ce55 100644 --- a/lib/schedule.c +++ b/lib/schedule.c @@ -45,12 +45,6 @@ trigger_handle_t lf_schedule_token(void* action, interval_t extra_delay, lf_toke } trigger_handle_t lf_schedule_copy(void* action, interval_t offset, void* value, size_t length) { - if (length < 0) { - lf_print_error("schedule_copy():" - " Ignoring request to copy a value with a negative length (%zu).", - length); - return -1; - } if (value == NULL) { return lf_schedule_token(action, offset, NULL); } @@ -129,13 +123,13 @@ trigger_handle_t lf_schedule_trigger(environment_t* env, trigger_t* trigger, int extra_delay = 0LL; } - LF_PRINT_DEBUG("lf_schedule_trigger: scheduling trigger %p with delay " PRINTF_TIME " and token %p.", trigger, - extra_delay, token); + LF_PRINT_DEBUG("lf_schedule_trigger: scheduling trigger %p with delay " PRINTF_TIME " and token %p.", (void*)trigger, + extra_delay, (void*)token); // Increment the reference count of the token. if (token != NULL) { token->ref_count++; - LF_PRINT_DEBUG("lf_schedule_trigger: Incremented ref_count of %p to %zu.", token, token->ref_count); + LF_PRINT_DEBUG("lf_schedule_trigger: Incremented ref_count of %p to %zu.", (void*)token, token->ref_count); } // The trigger argument could be null, meaning that nothing is triggered. diff --git a/lingua-franca-ref.txt b/lingua-franca-ref.txt index 1f7391f92..8b25206ff 100644 --- a/lingua-franca-ref.txt +++ b/lingua-franca-ref.txt @@ -1 +1 @@ -master +master \ No newline at end of file diff --git a/logging/api/logging_macros.h b/logging/api/logging_macros.h index 73939f576..6f7ea1eba 100644 --- a/logging/api/logging_macros.h +++ b/logging/api/logging_macros.h @@ -76,7 +76,7 @@ #if defined(NDEBUG) #define LF_ASSERT(condition, format, ...) (void)(condition) #define LF_ASSERTN(condition, format, ...) (void)(condition) -#define LF_ASSERT_NON_NULL(pointer) +#define LF_ASSERT_NON_NULL(pointer) (void)(pointer) #else #define LF_ASSERT(condition, format, ...) \ do { \ diff --git a/low_level_platform/api/low_level_platform.h b/low_level_platform/api/low_level_platform.h index e37a166a2..103801c35 100644 --- a/low_level_platform/api/low_level_platform.h +++ b/low_level_platform/api/low_level_platform.h @@ -77,7 +77,7 @@ int lf_critical_section_exit(environment_t* env); // To support the single-threaded runtime, we need the following functions. They // are not required by the threaded runtime and is thus hidden behind a #ifdef. #if defined(LF_SINGLE_THREADED) -typedef void lf_mutex_t; +typedef void* lf_mutex_t; /** * @brief Disable interrupts with support for nested calls * @return 0 on success diff --git a/low_level_platform/api/platform/lf_POSIX_threads_support.h b/low_level_platform/api/platform/lf_POSIX_threads_support.h index d27e7a16f..340fc3e88 100644 --- a/low_level_platform/api/platform/lf_POSIX_threads_support.h +++ b/low_level_platform/api/platform/lf_POSIX_threads_support.h @@ -44,4 +44,4 @@ typedef struct { } lf_cond_t; typedef pthread_t lf_thread_t; -#endif \ No newline at end of file +#endif diff --git a/low_level_platform/api/platform/lf_nrf52_support.h b/low_level_platform/api/platform/lf_nrf52_support.h index b93edaf8e..18613b2e0 100644 --- a/low_level_platform/api/platform/lf_nrf52_support.h +++ b/low_level_platform/api/platform/lf_nrf52_support.h @@ -48,7 +48,7 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. /** * No mutex or condition variable needed for single threaded NRF platforms */ -typedef void lf_mutex_t; +typedef void* lf_mutex_t; typedef void _lf_cond_var_t; #endif // LF_nRF52832_SUPPORT_H diff --git a/low_level_platform/impl/CMakeLists.txt b/low_level_platform/impl/CMakeLists.txt index 7641dd663..44c9e9366 100644 --- a/low_level_platform/impl/CMakeLists.txt +++ b/low_level_platform/impl/CMakeLists.txt @@ -1,32 +1,69 @@ # Check which system we are running on to select the correct platform support # file and assign the file's path to LF_PLATFORM_FILE set(LF_ROOT ${CMAKE_CURRENT_LIST_DIR}/../..) -set(LF_LOW_LEVEL_PLATFORM_FILES - ${CMAKE_CURRENT_LIST_DIR}/src/lf_unix_clock_support.c - ${CMAKE_CURRENT_LIST_DIR}/src/lf_unix_syscall_support.c - ${CMAKE_CURRENT_LIST_DIR}/src/lf_linux_support.c - ${CMAKE_CURRENT_LIST_DIR}/src/lf_macos_support.c - ${CMAKE_CURRENT_LIST_DIR}/src/lf_windows_support.c - ${CMAKE_CURRENT_LIST_DIR}/src/lf_nrf52_support.c - ${CMAKE_CURRENT_LIST_DIR}/src/lf_zephyr_support.c - ${CMAKE_CURRENT_LIST_DIR}/src/lf_zephyr_clock_counter.c - ${CMAKE_CURRENT_LIST_DIR}/src/lf_zephyr_clock_kernel.c - ${CMAKE_CURRENT_LIST_DIR}/src/lf_rp2040_support.c - ${CMAKE_CURRENT_LIST_DIR}/src/lf_atomic_windows.c - ${CMAKE_CURRENT_LIST_DIR}/src/lf_atomic_gcc_clang.c - ${CMAKE_CURRENT_LIST_DIR}/src/lf_atomic_irq.c - ${CMAKE_CURRENT_LIST_DIR}/src/platform_internal.c -) -if(PLATFORM_ZEPHYR) +include(${LF_ROOT}/core/lf_utils.cmake) + +if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows") + set(CMAKE_SYSTEM_VERSION 10.0) + message("Using Windows SDK version ${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}") + set(LF_LOW_LEVEL_PLATFORM_FILES + ${CMAKE_CURRENT_LIST_DIR}/src/lf_windows_support.c + ${CMAKE_CURRENT_LIST_DIR}/src/lf_atomic_windows.c + ) +elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Linux") + set(LF_LOW_LEVEL_PLATFORM_FILES + ${CMAKE_CURRENT_LIST_DIR}/src/lf_unix_clock_support.c + ${CMAKE_CURRENT_LIST_DIR}/src/lf_unix_syscall_support.c + ${CMAKE_CURRENT_LIST_DIR}/src/lf_linux_support.c + ${CMAKE_CURRENT_LIST_DIR}/src/lf_atomic_gcc_clang.c + ) +elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Darwin") + set(LF_LOW_LEVEL_PLATFORM_FILES + ${CMAKE_CURRENT_LIST_DIR}/src/lf_unix_clock_support.c + ${CMAKE_CURRENT_LIST_DIR}/src/lf_unix_syscall_support.c + ${CMAKE_CURRENT_LIST_DIR}/src/lf_macos_support.c + ${CMAKE_CURRENT_LIST_DIR}/src/lf_atomic_gcc_clang.c + ) +elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Nrf52") + set(LF_LOW_LEVEL_PLATFORM_FILES + ${CMAKE_CURRENT_LIST_DIR}/src/lf_nrf52_support.c + ${CMAKE_CURRENT_LIST_DIR}/src/lf_atomic_irq.c + ) +elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Zephyr") + set(LF_LOW_LEVEL_PLATFORM_FILES + ${CMAKE_CURRENT_LIST_DIR}/src/lf_zephyr_support.c + ${CMAKE_CURRENT_LIST_DIR}/src/lf_zephyr_clock_counter.c + ${CMAKE_CURRENT_LIST_DIR}/src/lf_zephyr_clock_kernel.c + ${CMAKE_CURRENT_LIST_DIR}/src/lf_atomic_irq.c + ) +elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Rp2040") + set(LF_LOW_LEVEL_PLATFORM_FILES + ${CMAKE_CURRENT_LIST_DIR}/src/lf_rp2040_support.c + ${CMAKE_CURRENT_LIST_DIR}/src/lf_atomic_irq.c + ) +else() + message(FATAL_ERROR "Your platform is not supported! The C target supports Linux, MacOS, Windows, Zephyr, Nrf52 and RP2040.") +endif() + +list(APPEND LF_LOW_LEVEL_PLATFORM_FILES ${CMAKE_CURRENT_LIST_DIR}/src/platform_internal.c) + +if(${CMAKE_SYSTEM_NAME} STREQUAL "Zephyr") message("--- Building Zephyr library") zephyr_library_named(lf-low-level-platform-impl) zephyr_library_sources(${LF_LOW_LEVEL_PLATFORM_FILES}) zephyr_library_link_libraries(kernel) else() -message("--- Building non-Zephyr library") + message("--- Building non-Zephyr library") add_library(lf-low-level-platform-impl STATIC ${LF_LOW_LEVEL_PLATFORM_FILES}) + # Link the platform to a threading library + if(NOT DEFINED LF_SINGLE_THREADED OR DEFINED LF_TRACE) + find_package(Threads REQUIRED) + target_link_libraries(lf-low-level-platform-impl PRIVATE Threads::Threads) + endif() endif() + add_library(lf::low-level-platform-impl ALIAS lf-low-level-platform-impl) +lf_enable_compiler_warnings(lf-low-level-platform-impl) target_link_libraries(lf-low-level-platform-impl PRIVATE lf::low-level-platform-api) target_link_libraries(lf-low-level-platform-impl PUBLIC lf-logging-api) diff --git a/low_level_platform/impl/Platform.cmake b/low_level_platform/impl/Platform.cmake deleted file mode 100644 index cc6042c7c..000000000 --- a/low_level_platform/impl/Platform.cmake +++ /dev/null @@ -1,18 +0,0 @@ -# Check which system we are running on to select the correct platform support -# file and assign the file's path to LF_PLATFORM_FILE -if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux") - set(LF_PLATFORM_FILE lf_linux_support.c) -elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Darwin") - set(LF_PLATFORM_FILE lf_macos_support.c) -elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Windows") - set(LF_PLATFORM_FILE lf_windows_support.c) - set(CMAKE_SYSTEM_VERSION 10.0) - message("Using Windows SDK version ${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}") -elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Zephyr") - set(LF_PLATFORM_FILE lf_zephyr_support.c) -elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Rp2040") - message("Using pico-sdk for RP2040 target") - set(LF_PLATFORM_FILE lf_rp2040_support.c) -else() - message(FATAL_ERROR "Your platform is not supported! The C target supports Linux, MacOS and Windows.") -endif() diff --git a/low_level_platform/impl/src/lf_C11_threads_support.c b/low_level_platform/impl/src/lf_C11_threads_support.c index 527ce28d3..34ccd3969 100644 --- a/low_level_platform/impl/src/lf_C11_threads_support.c +++ b/low_level_platform/impl/src/lf_C11_threads_support.c @@ -5,8 +5,24 @@ #include #include // For fixed-width integral types +struct lf_thread_data { + void* (*thread)(void*); + void* arguments; +}; + +static int lf_thread_c11_wrapper(void* args) { + struct lf_thread_data* thread_data = (struct lf_thread_data*)args; + thread_data->thread(thread_data->arguments); + free(thread_data); + return 0; +} + int lf_thread_create(lf_thread_t* thread, void* (*lf_thread)(void*), void* arguments) { - return thrd_create((thrd_t*)thread, (thrd_start_t)lf_thread, arguments); + struct lf_thread_data* thread_data = (struct lf_thread_data*)malloc(sizeof(struct lf_thread_data)); + thread_data->thread = lf_thread; + thread_data->arguments = arguments; + + return thrd_create((thrd_t*)thread, (thrd_start_t)lf_thread_c11_wrapper, thread_data); } int lf_thread_join(lf_thread_t thread, void** thread_return) { diff --git a/low_level_platform/impl/src/lf_linux_support.c b/low_level_platform/impl/src/lf_linux_support.c index 3edf8d7ea..fb2d19cba 100644 --- a/low_level_platform/impl/src/lf_linux_support.c +++ b/low_level_platform/impl/src/lf_linux_support.c @@ -57,6 +57,7 @@ int lf_sleep(interval_t sleep_duration) { } int _lf_interruptable_sleep_until_locked(environment_t* env, instant_t wakeup_time) { + (void)env; interval_t sleep_duration = wakeup_time - lf_time_physical(); if (sleep_duration <= 0) { diff --git a/low_level_platform/impl/src/lf_macos_support.c b/low_level_platform/impl/src/lf_macos_support.c index 54fcfd296..4cb3002cd 100644 --- a/low_level_platform/impl/src/lf_macos_support.c +++ b/low_level_platform/impl/src/lf_macos_support.c @@ -57,6 +57,7 @@ int lf_sleep(interval_t sleep_duration) { } int _lf_interruptable_sleep_until_locked(environment_t* env, instant_t wakeup_time) { + (void)env; interval_t sleep_duration = wakeup_time - lf_time_physical(); if (sleep_duration <= 0) { diff --git a/platform/impl/CMakeLists.txt b/platform/impl/CMakeLists.txt index df24aac27..eadea7764 100644 --- a/platform/impl/CMakeLists.txt +++ b/platform/impl/CMakeLists.txt @@ -1,7 +1,7 @@ set(LF_PLATFORM_FILES ${CMAKE_CURRENT_LIST_DIR}/platform.c) -if(PLATFORM_ZEPHYR) +if(${CMAKE_SYSTEM_NAME} STREQUAL "Zephyr") message("--- Building Zephyr library") zephyr_library_named(lf-platform-impl) zephyr_library_sources(${LF_PLATFORM_FILES}) diff --git a/platform/impl/platform.c b/platform/impl/platform.c index ddd182404..361f36992 100644 --- a/platform/impl/platform.c +++ b/platform/impl/platform.c @@ -20,7 +20,8 @@ lf_platform_mutex_ptr_t lf_platform_mutex_new() { if (mutex) lf_mutex_init(mutex); return mutex; -}; +} + void lf_platform_mutex_free(lf_platform_mutex_ptr_t mutex) { free((void*)mutex); } int lf_platform_mutex_lock(lf_platform_mutex_ptr_t mutex) { return lf_mutex_lock((lf_mutex_t*)mutex); } int lf_platform_mutex_unlock(lf_platform_mutex_ptr_t mutex) { return lf_mutex_unlock((lf_mutex_t*)mutex); } diff --git a/test/RTI/rti_common_test.c b/test/RTI/rti_common_test.c index b0fc2d21c..3d2c73af9 100644 --- a/test/RTI/rti_common_test.c +++ b/test/RTI/rti_common_test.c @@ -49,7 +49,8 @@ void set_scheduling_node(int id, int num_upstream, int num_downstream, int* upst // If there is any upstream nodes, store IDs and delays from the upstream nodes into the structure. if (test_rti.scheduling_nodes[id]->num_upstream > 0) { - test_rti.scheduling_nodes[id]->upstream = (int*)calloc(test_rti.scheduling_nodes[id]->num_upstream, sizeof(int)); + test_rti.scheduling_nodes[id]->upstream = + (uint16_t*)calloc(test_rti.scheduling_nodes[id]->num_upstream, sizeof(uint16_t)); test_rti.scheduling_nodes[id]->upstream_delay = (interval_t*)calloc(test_rti.scheduling_nodes[id]->num_upstream, sizeof(interval_t)); for (int i = 0; i < test_rti.scheduling_nodes[id]->num_upstream; i++) { @@ -60,7 +61,7 @@ void set_scheduling_node(int id, int num_upstream, int num_downstream, int* upst // If there is any downstream nodes, store IDs of the downstream nodes into the structure. if (test_rti.scheduling_nodes[id]->num_downstream > 0) { test_rti.scheduling_nodes[id]->downstream = - (int*)calloc(test_rti.scheduling_nodes[id]->num_downstream, sizeof(int)); + (uint16_t*)calloc(test_rti.scheduling_nodes[id]->num_downstream, sizeof(uint16_t)); for (int i = 0; i < test_rti.scheduling_nodes[id]->num_downstream; i++) { test_rti.scheduling_nodes[id]->downstream[i] = downstream[i]; } diff --git a/test/general/utils/hashmap_test.c b/test/general/utils/hashmap_test.c index 2134071e6..28e8deb27 100644 --- a/test/general/utils/hashmap_test.c +++ b/test/general/utils/hashmap_test.c @@ -15,9 +15,9 @@ static hashmap_object2int_entry_t mock[CAPACITY]; static size_t mock_size = 0; void test_put(hashmap_object2int_t* h) { - void* key = NULL; + int* key = NULL; while (!key) - key = NULL + (rand() % CAPACITY); // Generate a dummy pointer. + key += (rand() % CAPACITY); // Generate a dummy pointer. int value = rand(); hashmap_object2int_entry_t entry = (hashmap_object2int_entry_t){.key = key, .value = value}; hashmap_object2int_put(h, entry.key, entry.value); @@ -54,7 +54,6 @@ void test_get(hashmap_object2int_t* h) { * which each of two actions are performed, expressed as percents. */ void run_test(hashmap_object2int_t* h, int* distribution) { - int result = 1; int r = rand(); int choice = (r < 0 ? -r : r) % 100; if ((choice = choice - distribution[0]) < 0) { diff --git a/test/src_gen_stub.c b/test/src_gen_stub.c index d67d238ea..1e4630a6e 100644 --- a/test/src_gen_stub.c +++ b/test/src_gen_stub.c @@ -19,4 +19,4 @@ void logical_tag_complete(tag_t tag_to_send) {} int _lf_get_environments(environment_t** envs) { *envs = &_env; return 1; -} \ No newline at end of file +} diff --git a/trace/impl/CMakeLists.txt b/trace/impl/CMakeLists.txt index 6aeeb6870..f4a6b8b55 100644 --- a/trace/impl/CMakeLists.txt +++ b/trace/impl/CMakeLists.txt @@ -1,9 +1,13 @@ +set(LF_ROOT ${CMAKE_CURRENT_LIST_DIR}/../..) +include(${LF_ROOT}/core/lf_utils.cmake) + add_library(lf-trace-impl STATIC) add_library(lf::trace-impl ALIAS lf-trace-impl) target_link_libraries(lf-trace-impl PRIVATE lf::trace-api) target_link_libraries(lf-trace-impl PRIVATE lf::platform-api) target_link_libraries(lf-trace-impl PRIVATE lf::logging-api) target_link_libraries(lf-trace-impl PRIVATE lf::version-api) +lf_enable_compiler_warnings(lf-trace-impl) target_sources(lf-trace-impl PUBLIC ${CMAKE_CURRENT_LIST_DIR}/src/trace_impl.c) diff --git a/trace/impl/include/trace_impl.h b/trace/impl/include/trace_impl.h index 3e1bd6fe6..779a7be4a 100644 --- a/trace/impl/include/trace_impl.h +++ b/trace/impl/include/trace_impl.h @@ -6,6 +6,9 @@ /** Size of the table of trace objects. */ #define TRACE_OBJECT_TABLE_SIZE 1024 +/** Max length of trace file name*/ +#define TRACE_MAX_FILENAME_LENGTH 128 + // TYPE DEFINITIONS ********************************************************** /** @@ -20,10 +23,10 @@ typedef struct trace_t { * which will create a significant pause in the calling thread. */ trace_record_nodeps_t** _lf_trace_buffer; - int* _lf_trace_buffer_size; + size_t* _lf_trace_buffer_size; /** The number of trace buffers allocated when tracing starts. */ - int _lf_number_of_trace_buffers; + size_t _lf_number_of_trace_buffers; /** Marker that tracing is stopping or has stopped. */ int _lf_trace_stop; @@ -32,11 +35,11 @@ typedef struct trace_t { FILE* _lf_trace_file; /** The file name where the traces are written*/ - char* filename; + char filename[TRACE_MAX_FILENAME_LENGTH]; /** Table of pointers to a description of the object. */ object_description_t _lf_trace_object_descriptions[TRACE_OBJECT_TABLE_SIZE]; - int _lf_trace_object_descriptions_size; + size_t _lf_trace_object_descriptions_size; /** Indicator that the trace header information has been written to the file. */ bool _lf_trace_header_written; diff --git a/trace/impl/src/trace_impl.c b/trace/impl/src/trace_impl.c index d43a36e87..7f79c49a5 100644 --- a/trace/impl/src/trace_impl.c +++ b/trace/impl/src/trace_impl.c @@ -45,7 +45,7 @@ static int write_trace_header(trace_t* trace) { _LF_TRACE_FAILURE(trace); // Next we write the table. - for (int i = 0; i < trace->_lf_trace_object_descriptions_size; i++) { + for (size_t i = 0; i < trace->_lf_trace_object_descriptions_size; i++) { // Write the pointer to the self struct. items_written = fwrite(&trace->_lf_trace_object_descriptions[i].pointer, sizeof(void*), 1, trace->_lf_trace_file); if (items_written != 1) @@ -63,7 +63,7 @@ static int write_trace_header(trace_t* trace) { _LF_TRACE_FAILURE(trace); // Write the description. - int description_size = strlen(trace->_lf_trace_object_descriptions[i].description); + size_t description_size = strlen(trace->_lf_trace_object_descriptions[i].description); items_written = fwrite(trace->_lf_trace_object_descriptions[i].description, sizeof(char), description_size + 1, // Include null terminator. trace->_lf_trace_file); @@ -137,11 +137,11 @@ static void start_trace(trace_t* trace, int max_num_local_threads) { trace->_lf_trace_buffer = (trace_record_nodeps_t**)malloc(sizeof(trace_record_nodeps_t*) * (trace->_lf_number_of_trace_buffers + 1)); trace->_lf_trace_buffer++; // the buffer at index -1 is a fallback for user threads. - for (int i = -1; i < trace->_lf_number_of_trace_buffers; i++) { + for (int i = -1; i < (int)trace->_lf_number_of_trace_buffers; i++) { trace->_lf_trace_buffer[i] = (trace_record_nodeps_t*)malloc(sizeof(trace_record_nodeps_t) * TRACE_BUFFER_CAPACITY); } // Array of counters that track the size of each trace record (per thread). - trace->_lf_trace_buffer_size = (int*)calloc(sizeof(int), trace->_lf_number_of_trace_buffers + 1); + trace->_lf_trace_buffer_size = (size_t*)calloc(sizeof(size_t), trace->_lf_number_of_trace_buffers + 1); trace->_lf_trace_buffer_size++; trace->_lf_trace_stop = 0; @@ -150,15 +150,8 @@ static void start_trace(trace_t* trace, int max_num_local_threads) { static void trace_new(char* filename) { - // Determine length of the filename - size_t len = strlen(filename) + 1; - - // Allocate memory for the filename on the trace struct - trace.filename = (char*)malloc(len * sizeof(char)); - LF_ASSERT(trace.filename, "Out of memory"); - // Copy it to the struct - strncpy(trace.filename, filename, len); + strncpy(trace.filename, filename, TRACE_MAX_FILENAME_LENGTH); // FIXME: location of trace file should be customizable. trace._lf_trace_file = fopen(trace.filename, "w"); if (trace._lf_trace_file == NULL) { @@ -171,16 +164,14 @@ static void trace_new(char* filename) { } } -static void trace_free(trace_t* trace) { free(trace->filename); } - static void stop_trace_locked(trace_t* trace) { if (trace->_lf_trace_stop) { // Trace was already stopped. Nothing to do. return; } - for (int i = -1; i < trace->_lf_number_of_trace_buffers; i++) { + for (int i = -1; i < (int)trace->_lf_number_of_trace_buffers; i++) { // Flush the buffer if it has data. - LF_PRINT_DEBUG("Trace buffer %d has %d records.", i, trace->_lf_trace_buffer_size[i]); + LF_PRINT_DEBUG("Trace buffer %d has %zu records.", i, trace->_lf_trace_buffer_size[i]); if (trace->_lf_trace_buffer_size && trace->_lf_trace_buffer_size[i] > 0) { flush_trace_locked(trace, i); } @@ -231,6 +222,7 @@ void lf_tracing_register_trace_event(object_description_t description) { } void lf_tracing_tracepoint(int worker, trace_record_nodeps_t* tr) { + (void)worker; // Worker argument determines which buffer to write to. int tid = lf_thread_id(); if (tid < 0) { @@ -239,8 +231,8 @@ void lf_tracing_tracepoint(int worker, trace_record_nodeps_t* tr) { // Therefore we should fall back to using a mutex. lf_platform_mutex_lock(trace_mutex); } - if (tid > trace._lf_number_of_trace_buffers) { - lf_print_error_and_exit("the thread id (%d) exceeds the number of trace buffers (%d)", tid, + if (tid > (int)trace._lf_number_of_trace_buffers) { + lf_print_error_and_exit("the thread id (%d) exceeds the number of trace buffers (%zu)", tid, trace._lf_number_of_trace_buffers); } @@ -280,6 +272,5 @@ void lf_tracing_global_init(char* file_name_prefix, int fedid, int max_num_local void lf_tracing_set_start_time(int64_t time) { start_time = time; } void lf_tracing_global_shutdown() { stop_trace(&trace); - trace_free(&trace); lf_platform_mutex_free(trace_mutex); } diff --git a/util/tracing/trace_util.c b/util/tracing/trace_util.c index ed32c5baa..ffe0c6b8f 100644 --- a/util/tracing/trace_util.c +++ b/util/tracing/trace_util.c @@ -62,6 +62,65 @@ typedef struct open_file_t { } open_file_t; open_file_t* _open_files = NULL; +const char* trace_event_names[] = { + "Reaction starts", + "Reaction ends", + "Reaction deadline missed", + "Schedule called", + "User-defined event", + "User-defined valued event", + "Worker wait starts", + "Worker wait ends", + "Scheduler advancing time starts", + "Scheduler advancing time ends", + "Federated marker", + // Sending messages + "Sending ACK", + "Sending FAILED", + "Sending TIMESTAMP", + "Sending NET", + "Sending LTC", + "Sending STOP_REQ", + "Sending STOP_REQ_REP", + "Sending STOP_GRN", + "Sending FED_ID", + "Sending PTAG", + "Sending TAG", + "Sending REJECT", + "Sending RESIGN", + "Sending PORT_ABS", + "Sending CLOSE_RQ", + "Sending TAGGED_MSG", + "Sending P2P_TAGGED_MSG", + "Sending MSG", + "Sending P2P_MSG", + "Sending ADR_AD", + "Sending ADR_QR", + // Receiving messages + "Receiving ACK", + "Receiving FAILED", + "Receiving TIMESTAMP", + "Receiving NET", + "Receiving LTC", + "Receiving STOP_REQ", + "Receiving STOP_REQ_REP", + "Receiving STOP_GRN", + "Receiving FED_ID", + "Receiving PTAG", + "Receiving TAG", + "Receiving REJECT", + "Receiving RESIGN", + "Receiving PORT_ABS", + "Receiving CLOSE_RQ", + "Receiving TAGGED_MSG", + "Receiving P2P_TAGGED_MSG", + "Receiving MSG", + "Receiving P2P_MSG", + "Receiving ADR_AD", + "Receiving ADR_QR", + "Receiving UNIDENTIFIED", +}; + /** * Function to be invoked upon exiting. */ diff --git a/util/tracing/trace_util.h b/util/tracing/trace_util.h index 67d3c705b..089e57ee1 100644 --- a/util/tracing/trace_util.h +++ b/util/tracing/trace_util.h @@ -33,6 +33,11 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "reactor.h" #include "trace.h" +/** + * String description of event types. + */ +extern const char* trace_event_names[]; + /** Macro to use when access to trace file fails. */ #define _LF_TRACE_FAILURE(trace_file) \ do { \