2 * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include <openssl/crypto.h>
11 #include "internal/e_os.h"
13 /* system-specific variants defining OSSL_sleep() */
14 #if defined(OPENSSL_SYS_UNIX) || defined(__DJGPP__)
16 void OSSL_sleep(uint64_t millis)
18 # ifdef OPENSSL_SYS_VXWORKS
21 ts.tv_sec = (long int) (millis / 1000);
22 ts.tv_nsec = (long int) (millis % 1000) * 1000000ul;
24 # elif defined(__TANDEM)
25 # if !defined(_REENTRANT)
26 # include <cextdecs.h(PROCESS_DELAY_)>
28 /* HPNS does not support usleep for non threaded apps */
29 PROCESS_DELAY_(millis * 1000);
30 # elif defined(_SPT_MODEL_)
31 # include <spthread.h>
32 # include <spt_extensions.h>
34 usleep(millis * 1000);
36 usleep(millis * 1000);
39 usleep(millis * 1000);
45 void OSSL_sleep(uint64_t millis)
48 * Windows' Sleep() takes a DWORD argument, which is smaller than
49 * a uint64_t, so we need to split the two to shut the compiler up.
54 dword_times = (DWORD)(millis >> (8 * sizeof(DWORD)));
56 if (dword_times > 0) {
57 for (i = dword_times; i-- > 0;)
60 * The loop above slept 1 millisec less on each iteration than it
61 * should, this compensates by sleeping as many milliseconds as there
62 * were iterations. Yes, this is nit picky!
67 /* Now, sleep the remaining milliseconds */
68 Sleep((DWORD)(millis));
71 /* Fallback to a busy wait */
72 # include "internal/time.h"
74 static void ossl_sleep_secs(uint64_t secs)
77 * sleep() takes an unsigned int argument, which is smaller than
78 * a uint64_t, so it needs to be called in smaller increments.
80 unsigned int uint_times;
83 uint_times = (unsigned int)(secs >> (8 * sizeof(unsigned int)));
85 for (i = uint_times; i-- > 0;)
86 sleep((unsigned int)-1);
88 * The loop above slept 1 second less on each iteration than it
89 * should, this compensates by sleeping as many seconds as there were
90 * iterations. Yes, this is nit picky!
96 static void ossl_sleep_millis(uint64_t millis)
98 const OSSL_TIME finish
99 = ossl_time_add(ossl_time_now(), ossl_ms2time(millis));
101 while (ossl_time_compare(ossl_time_now(), finish) < 0)
105 void OSSL_sleep(uint64_t millis)
107 ossl_sleep_secs(millis / 1000);
108 ossl_sleep_millis(millis % 1000);
110 #endif /* defined(OPENSSL_SYS_UNIX) || defined(__DJGPP__) */