diff --git a/CMakeLists.txt b/CMakeLists.txt index 6a17868..b75f9c9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -229,5 +229,6 @@ target_compile_options(node PRIVATE target_compile_definitions(node PRIVATE CHAIN_DATA_DIR="${CMAKE_BINARY_DIR}/chain_data" $<$:SKALACOIN_AUTOLYKOS2_REF_AVAILABLE> + $<$:_POSIX_C_SOURCE=200809L> ) set_target_properties(node PROPERTIES OUTPUT_NAME "skalacoin_node") diff --git a/include/utils.h b/include/utils.h index 1a37466..086c200 100644 --- a/include/utils.h +++ b/include/utils.h @@ -36,6 +36,19 @@ static inline uint64_t get_current_time_ms(void) { return (spec.tv_sec * 1000) + (spec.tv_nsec / 1000000); } +static inline void sleep_for_microseconds(uint64_t microseconds) { + struct timespec req; + req.tv_sec = (time_t)(microseconds / 1000000ULL); + req.tv_nsec = (long)((microseconds % 1000000ULL) * 1000ULL); + while (nanosleep(&req, &req) == -1) { + continue; + } +} + +static inline void sleep_for_milliseconds(uint64_t milliseconds) { + sleep_for_microseconds(milliseconds * 1000ULL); +} + static inline void AddressToHexString(const uint8_t address[32], char out[65]) { if (!address || !out) { return; diff --git a/src/autolykos2/autolykos2.c b/src/autolykos2/autolykos2.c index 830dc3e..179def3 100644 --- a/src/autolykos2/autolykos2.c +++ b/src/autolykos2/autolykos2.c @@ -254,7 +254,7 @@ static bool Autolykos2_HashCore( DWORD ms = (DWORD)((autolykos2_sleepBetweenHashOperationsMicroseconds + 999) / 1000); Sleep(ms); #else - usleep((useconds_t)autolykos2_sleepBetweenHashOperationsMicroseconds); + sleep_for_microseconds(autolykos2_sleepBetweenHashOperationsMicroseconds); #endif } diff --git a/src/main.c b/src/main.c index fdc6c9f..1e1b83a 100644 --- a/src/main.c +++ b/src/main.c @@ -830,7 +830,7 @@ int main(int argc, char* argv[]) { // Poll for completions or timeouts if (inFlight == 0) { // nothing in flight; small sleep to avoid busy-loop - usleep(100 * 1000); + sleep_for_milliseconds(100); continue; } @@ -945,7 +945,7 @@ int main(int argc, char* argv[]) { // retry with exponential backoff retryCount[i]++; uint64_t backoff = SYNC_BACKOFF_BASE_MS * (1ULL << (retryCount[i] - 1)); - usleep((useconds_t)(backoff * 1000ULL)); + sleep_for_milliseconds(backoff); uint64_t req = requestedHeights[i]; if (Node_SendPacket(node, peerConn, PACKET_TYPE_FETCH_BLOCK, &req, sizeof(req)) != 0) { @@ -964,7 +964,7 @@ int main(int argc, char* argv[]) { if (!progressed) { // small sleep to avoid spinning - usleep(50 * 1000); + sleep_for_milliseconds(50); } } diff --git a/src/nets/net_node.c b/src/nets/net_node.c index 5f3dd39..2702d00 100644 --- a/src/nets/net_node.c +++ b/src/nets/net_node.c @@ -40,7 +40,7 @@ static void* Node_MaintenanceThread(void* arg) { BalanceSheet_SaveToFile(chainDataDir); } } - usleep((useconds_t)(n->maintenanceIntervalMs * 1000)); + sleep_for_milliseconds((uint64_t)n->maintenanceIntervalMs); } return NULL; }