Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Change nanosleep to lf_sleep in federate and RTI code #219

Merged
merged 1 commit into from
May 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions core/federated/RTI/rti_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -1109,20 +1109,14 @@ void* clock_synchronization_thread(void* noargs) {
interval_t ns_to_wait = start_time - lf_time_physical();

if (ns_to_wait > 0LL) {
struct timespec wait_time = {ns_to_wait / BILLION, ns_to_wait % BILLION};
struct timespec rem_time;
nanosleep(&wait_time, &rem_time);
lf_sleep(ns_to_wait);
}

// Initiate a clock synchronization every _RTI.clock_sync_period_ns
struct timespec sleep_time = {(time_t) _RTI.clock_sync_period_ns / BILLION,
_RTI.clock_sync_period_ns % BILLION};
struct timespec remaining_time;

bool any_federates_connected = true;
while (any_federates_connected) {
// Sleep
nanosleep(&sleep_time, &remaining_time); // Can be interrupted
lf_sleep(_RTI.clock_sync_period_ns); // Can be interrupted
any_federates_connected = false;
for (int fed = 0; fed < _RTI.number_of_federates; fed++) {
if (_RTI.federates[fed].state == NOT_CONNECTED) {
Expand Down
23 changes: 8 additions & 15 deletions core/federated/federate.c
Original file line number Diff line number Diff line change
Expand Up @@ -772,9 +772,8 @@ void connect_to_federate(uint16_t remote_federate_id) {
lf_print_error_and_exit("TIMEOUT obtaining IP/port for federate %d from the RTI.",
remote_federate_id);
}
struct timespec wait_time = {0L, ADDRESS_QUERY_RETRY_INTERVAL};
struct timespec remaining_time;
if (nanosleep(&wait_time, &remaining_time) != 0) {
// Wait ADDRESS_QUERY_RETRY_INTERVAL nanoseconds.
if (lf_sleep(ADDRESS_QUERY_RETRY_INTERVAL) != 0) {
// Sleep was interrupted.
continue;
}
Expand Down Expand Up @@ -838,10 +837,8 @@ void connect_to_federate(uint16_t remote_federate_id) {
}
lf_print_warning("Could not connect to federate %d. Will try again every " PRINTF_TIME " nanoseconds.\n",
remote_federate_id, ADDRESS_QUERY_RETRY_INTERVAL);
// Wait CONNECT_RETRY_INTERVAL seconds.
struct timespec wait_time = {0L, ADDRESS_QUERY_RETRY_INTERVAL};
struct timespec remaining_time;
if (nanosleep(&wait_time, &remaining_time) != 0) {
// Wait ADDRESS_QUERY_RETRY_INTERVAL nanoseconds.
if (lf_sleep(ADDRESS_QUERY_RETRY_INTERVAL) != 0) {
// Sleep was interrupted.
continue;
}
Expand Down Expand Up @@ -1066,9 +1063,7 @@ void connect_to_rti(const char* hostname, int port) {
lf_print("Failed to connect to RTI on port %d. Trying %d.", uport, uport + 1);
uport++;
// Wait PORT_KNOCKING_RETRY_INTERVAL seconds.
struct timespec wait_time = {0L, PORT_KNOCKING_RETRY_INTERVAL};
struct timespec remaining_time;
if (nanosleep(&wait_time, &remaining_time) != 0) {
if (lf_sleep(PORT_KNOCKING_RETRY_INTERVAL) != 0) {
// Sleep was interrupted.
continue;
}
Expand All @@ -1084,11 +1079,9 @@ void connect_to_rti(const char* hostname, int port) {
CONNECT_NUM_RETRIES);
}
lf_print("Could not connect to RTI at %s. Will try again every %d seconds.",
hostname, CONNECT_RETRY_INTERVAL);
// Wait CONNECT_RETRY_INTERVAL seconds.
struct timespec wait_time = {(time_t)CONNECT_RETRY_INTERVAL, 0L};
struct timespec remaining_time;
if (nanosleep(&wait_time, &remaining_time) != 0) {
hostname, CONNECT_RETRY_INTERVAL / BILLION);
// Wait CONNECT_RETRY_INTERVAL nanoseconds.
if (lf_sleep(CONNECT_RETRY_INTERVAL) != 0) {
// Sleep was interrupted.
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions include/core/federated/net_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,10 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define FED_COM_BUFFER_SIZE 256u

/**
* Number of seconds that elapse between a federate's attempts
* Number of nanoseconds that elapse between a federate's attempts
* to connect to the RTI.
*/
#define CONNECT_RETRY_INTERVAL 2
#define CONNECT_RETRY_INTERVAL 2000000000LL

/**
* Bound on the number of retries to connect to the RTI.
Expand Down