|
| 1 | + |
| 2 | +/* |
| 3 | + * Copyright (c) 2015-2019, ARM Limited, All Rights Reserved |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +#ifndef __MBED_CHRONO_H__ |
| 20 | +#define __MBED_CHRONO_H__ |
| 21 | + |
| 22 | +#include <cstdint> |
| 23 | +#include <cassert> |
| 24 | +#include <ratio> |
| 25 | +#include <chrono> |
| 26 | + |
| 27 | +/** \addtogroup platform-public-api */ |
| 28 | +/** @{*/ |
| 29 | + |
| 30 | +/** |
| 31 | + * \defgroup platform_chrono chrono utilities |
| 32 | + * |
| 33 | + * Additions and variations of std::chrono |
| 34 | + * |
| 35 | + * - unsigned 32-bit variants of standard signed 64-bit duration types |
| 36 | + * - centiseconds and deciseconds |
| 37 | + * @{ |
| 38 | + */ |
| 39 | +namespace mbed { |
| 40 | + |
| 41 | +/* Extensions declared in mbed::chrono, following pattern of std::chrono */ |
| 42 | +namespace chrono { |
| 43 | + |
| 44 | +/* Add deciseconds and centiseconds - may be |
| 45 | + * useful to use lower precision when not messing with templating. |
| 46 | + */ |
| 47 | +using deciseconds = std::chrono::duration<long long, std::deci>; |
| 48 | +using centiseconds = std::chrono::duration<long long, std::centi>; |
| 49 | + |
| 50 | +/** 32-bit microsecond duration type |
| 51 | + * |
| 52 | + * Standard std::chrono::microseconds is signed 64-bit. For some purposes |
| 53 | + * it's more efficient to process small times as 32-bit. And when doing so, |
| 54 | + * as we likely need to worry about wrapping, use of an unsigned |
| 55 | + * value to process modulo 2**32 is appropriate. |
| 56 | + */ |
| 57 | +using microseconds_u32 = std::chrono::duration<std::uint32_t, std::micro>; |
| 58 | + |
| 59 | +/** 32-bit millisecond duration type |
| 60 | + * |
| 61 | + * Standard std::chrono::milliseconds is signed 64-bit. For some purposes |
| 62 | + * it's more efficient to process times as 32-bit. And when doing so, |
| 63 | + * as we likely need to worry about wrapping, use of an unsigned |
| 64 | + * value to process modulo 2**32 is appropriate. |
| 65 | + */ |
| 66 | +using milliseconds_u32 = std::chrono::duration<std::uint32_t, std::milli>; |
| 67 | + |
| 68 | +} // namespace chrono |
| 69 | + |
| 70 | +inline namespace literals { |
| 71 | + |
| 72 | +inline namespace chrono_literals { |
| 73 | + |
| 74 | +/** User-defined literal for deciseconds (1/10 of a second) |
| 75 | + * |
| 76 | + * Useful in case we might change kernel tick frequency to be slower - with tick frequency 1000Hz, it is |
| 77 | + * possible to assign 500ms to a KernelClock::duration, but that would fail at slower rates. |
| 78 | + * |
| 79 | + * Example use: |
| 80 | + * |
| 81 | + * using namespace mbed::chrono_literals; |
| 82 | + * |
| 83 | + * ThisThread::sleep_for(5_ds); |
| 84 | + */ |
| 85 | +constexpr chrono::deciseconds operator "" _ds(unsigned long long x) |
| 86 | +{ |
| 87 | + chrono::deciseconds::rep val = static_cast<chrono::deciseconds::rep>(x); |
| 88 | + assert(val >= 0 && static_cast<unsigned long long>(val) == x); |
| 89 | + return chrono::deciseconds(val); |
| 90 | +} |
| 91 | + |
| 92 | +/** User-defined literal for centiseconds (1/100 of a second) |
| 93 | + * |
| 94 | + * Useful in case we might change kernel tick frequency to be slower - with tick frequency 1000Hz, it is |
| 95 | + * possible to assign 500ms to a KernelClock::duration, but that would fail at slower rates. |
| 96 | + * |
| 97 | + * Example use: |
| 98 | + * |
| 99 | + * using namespace mbed::chrono_literals; |
| 100 | + * |
| 101 | + * ThisThread::sleep_for(1_cs); |
| 102 | + */ |
| 103 | +constexpr chrono::centiseconds operator "" _cs(unsigned long long x) |
| 104 | +{ |
| 105 | + chrono::centiseconds::rep val = static_cast<chrono::centiseconds::rep>(x); |
| 106 | + assert(val >= 0 && static_cast<unsigned long long>(val) == x); |
| 107 | + return chrono::centiseconds(val); |
| 108 | +} |
| 109 | + |
| 110 | +} // inline namespace chrono_literals |
| 111 | + |
| 112 | +} // inline namespace literals |
| 113 | + |
| 114 | +namespace chrono { |
| 115 | + |
| 116 | +using namespace chrono_literals; |
| 117 | + |
| 118 | +} // namespace chrono |
| 119 | + |
| 120 | +} // namespace mbed |
| 121 | + |
| 122 | +/**@}*/ |
| 123 | + |
| 124 | +/**@}*/ |
| 125 | + |
| 126 | +#endif // __MBED_CHRONO_H__ |
| 127 | + |
0 commit comments