]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/sleep.c
2 * Copyright 2022-2024 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__)
17 void OSSL_sleep(uint64_t millis
)
19 # ifdef OPENSSL_SYS_VXWORKS
22 ts
.tv_sec
= (long int) (millis
/ 1000);
23 ts
.tv_nsec
= (long int) (millis
% 1000) * 1000000ul;
25 # elif defined(__TANDEM) && !defined(_REENTRANT)
26 # include <cextdecs.h(PROCESS_DELAY_)>
28 /* HPNS does not support usleep for non threaded apps */
29 PROCESS_DELAY_(millis
* 1000);
31 unsigned int s
= (unsigned int)(millis
/ 1000);
32 unsigned int us
= (unsigned int)((millis
% 1000) * 1000);
39 #elif defined(_WIN32) && !defined(OPENSSL_SYS_UEFI)
42 void OSSL_sleep(uint64_t millis
)
45 * Windows' Sleep() takes a DWORD argument, which is smaller than
46 * a uint64_t, so we need to limit it to 49 days, which should be enough.
48 DWORD limited_millis
= (DWORD
)-1;
50 if (millis
< limited_millis
)
51 limited_millis
= (DWORD
)millis
;
52 Sleep(limited_millis
);
56 /* Fallback to a busy wait */
57 # include "internal/time.h"
59 static void ossl_sleep_secs(uint64_t secs
)
62 * sleep() takes an unsigned int argument, which is smaller than
63 * a uint64_t, so it needs to be limited to 136 years which
64 * should be enough even for Sleeping Beauty.
66 unsigned int limited_secs
= UINT_MAX
;
68 if (secs
< limited_secs
)
69 limited_secs
= (unsigned int)secs
;
73 static void ossl_sleep_millis(uint64_t millis
)
75 const OSSL_TIME finish
76 = ossl_time_add(ossl_time_now(), ossl_ms2time(millis
));
78 while (ossl_time_compare(ossl_time_now(), finish
) < 0)
82 void OSSL_sleep(uint64_t millis
)
84 ossl_sleep_secs(millis
/ 1000);
85 ossl_sleep_millis(millis
% 1000);
87 #endif /* defined(OPENSSL_SYS_UNIX) || defined(__DJGPP__) */