]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Preparation of `tor_sleep_msec()`
authorWaldemar Zimpel <w.zimpel@dev.utilizer.de>
Wed, 27 Aug 2025 23:24:50 +0000 (01:24 +0200)
committerWaldemar Zimpel <w.zimpel@dev.utilizer.de>
Thu, 28 Aug 2025 20:04:21 +0000 (22:04 +0200)
- Update `tor_sleep_msec()` with `nanosleep()` function
- Make `tor_sleep_msec()` available outside of unit tests

src/lib/time/compat_time.c
src/lib/time/compat_time.h

index 57a1e45b84b1d9d13477a3ba46e64f05110ae1df..9e534c418089658a9933bafd7056e248b062426c 100644 (file)
@@ -1,6 +1,6 @@
 /* Copyright (c) 2003-2004, Roger Dingledine
  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
- * Copyright (c) 2007-2021, The Tor Project, Inc. */
+ * Copyright (c) 2007-2025, The Tor Project, Inc. */
 /* See LICENSE for licensing information */
 
 /**
 #undef HAVE_CLOCK_GETTIME
 #endif
 
-#ifdef TOR_UNIT_TESTS
-/** Delay for <b>msec</b> milliseconds.  Only used in tests. */
+/** Delay for <b>msec</b> milliseconds. */
 void
 tor_sleep_msec(int msec)
 {
 #ifdef _WIN32
   Sleep(msec);
+#elif defined(HAVE_TIME_H)
+  struct timespec ts = {msec / 1000, (msec % 1000) * 1000 * 1000};
+  while (nanosleep(&ts, &ts) == -1 && errno == EINTR);
 #elif defined(HAVE_USLEEP)
   sleep(msec / 1000);
   /* Some usleep()s hate sleeping more than 1 sec */
@@ -71,7 +73,6 @@ tor_sleep_msec(int msec)
   sleep(CEIL_DIV(msec, 1000));
 #endif /* defined(_WIN32) || ... */
 }
-#endif /* defined(TOR_UNIT_TESTS) */
 
 #define ONE_MILLION ((int64_t) (1000 * 1000))
 #define ONE_BILLION ((int64_t) (1000 * 1000 * 1000))
index eaf676ae84504c516c313216ebe2260050b2093f..eeafe88c93d989c880919ab41291813977f40c3f 100644 (file)
@@ -1,6 +1,6 @@
 /* Copyright (c) 2003-2004, Roger Dingledine
  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
- * Copyright (c) 2007-2021, The Tor Project, Inc. */
+ * Copyright (c) 2007-2025, The Tor Project, Inc. */
 /* See LICENSE for licensing information */
 
 /**
@@ -361,9 +361,9 @@ monotime_coarse_diff_msec32(const monotime_coarse_t *start,
 #endif /* SIZEOF_VOID_P == 8 */
 }
 
-#ifdef TOR_UNIT_TESTS
 void tor_sleep_msec(int msec);
 
+#ifdef TOR_UNIT_TESTS
 void monotime_enable_test_mocking(void);
 void monotime_disable_test_mocking(void);
 void monotime_set_mock_time_nsec(int64_t);