]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/sleep.c
2 * Copyright 2022-2023 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)
26 # if !defined(_REENTRANT)
27 # include <cextdecs.h(PROCESS_DELAY_)>
29 /* HPNS does not support usleep for non threaded apps */
30 PROCESS_DELAY_(millis
* 1000);
31 # elif defined(_SPT_MODEL_)
32 # include <spthread.h>
33 # include <spt_extensions.h>
35 usleep(millis
* 1000);
37 usleep(millis
* 1000);
40 unsigned int s
= (unsigned int)(millis
/ 1000);
41 unsigned int us
= (unsigned int)((millis
% 1000) * 1000);
47 #elif defined(_WIN32) && !defined(OPENSSL_SYS_UEFI)
50 void OSSL_sleep(uint64_t millis
)
53 * Windows' Sleep() takes a DWORD argument, which is smaller than
54 * a uint64_t, so we need to limit it to 49 days, which should be enough.
56 DWORD limited_millis
= (DWORD
)-1;
58 if (millis
< limited_millis
)
59 limited_millis
= (DWORD
)millis
;
60 Sleep(limited_millis
);
64 /* Fallback to a busy wait */
65 # include "internal/time.h"
67 static void ossl_sleep_secs(uint64_t secs
)
70 * sleep() takes an unsigned int argument, which is smaller than
71 * a uint64_t, so it needs to be limited to 136 years which
72 * should be enough even for Sleeping Beauty.
74 unsigned int limited_secs
= UINT_MAX
;
76 if (secs
< limited_secs
)
77 limited_secs
= (unsigned int)secs
;
81 static void ossl_sleep_millis(uint64_t millis
)
83 const OSSL_TIME finish
84 = ossl_time_add(ossl_time_now(), ossl_ms2time(millis
));
86 while (ossl_time_compare(ossl_time_now(), finish
) < 0)
90 void OSSL_sleep(uint64_t millis
)
92 ossl_sleep_secs(millis
/ 1000);
93 ossl_sleep_millis(millis
% 1000);
95 #endif /* defined(OPENSSL_SYS_UNIX) || defined(__DJGPP__) */