001:
002:
003:
004:
005:
006:
007:
008:
009:
010:
011:
012:
013:
014:
015:
016:
017:
018:
019:
020:
021:
022:
023:
024:
025:
026:
027:
028:
029:
030:
031:
032:
033:
034:
035:
036:
037:
038:
039:
040:
041:
042:
043:
044:
045:
046:
047:
048:
049:
050:
051:
052:
053:
054:
055:
056:
057:
058:
059:
060:
061:
062:
063:
064:
065:
066:
067:
068:
069:
070:
071:
072:
073:
074:
075:
076:
077:
078:
079:
080:
081:
082:
083:
084:
085:
086:
087:
088:
089:
090:
091:
092:
093:
094:
095:
096:
097:
098:
099:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:





































































































































/*
* Copyright (c), Zeriph Enterprises
* All rights reserved.
*
* Contributor(s):
* Zechariah Perez, omni (at) zeriph (dot) com
*
* THIS SOFTWARE IS PROVIDED BY ZERIPH AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ZERIPH AND CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES);
* LOSS OF USE, DATA, OR PROFITS); OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#if !defined(OMNI_TICK_T_HPP)
#define OMNI_TICK_T_HPP 1
#include <omni/defs/global.hpp>

#if !defined(OMNI_OS_WIN)
    #include <cstring> // memset
#endif

#if defined(OMNI_OS_WIN) && defined(OMNI_WIN_API)
    #if !defined(OMNI_WIN_MONO_TICK_QPC) && !defined(OMNI_WIN_MONO_TICK_COUNT) && !defined(OMNI_WIN_MONO_TICK_COUNT64)
        #define OMNI_WIN_MONO_TICK_QPC
    #endif
    #if defined(OMNI_WIN_MONO_TICK_QPC)
        #define OMNI_CLOCK_TICK_T LARGE_INTEGER
    #elif defined(OMNI_WIN_MONO_TICK_COUNT)
        #define OMNI_CLOCK_TICK_T DWORD
    #elif defined(OMNI_WIN_MONO_TICK_COUNT64)
        #define OMNI_CLOCK_TICK_T ULONGLONG
    #endif
#elif defined(OMNI_OS_APPLE)
    #include <unistd.h>
    #include <sys/time.h>
    #include <mach/mach_time.h> // mach_time func's
    #define OMNI_CLOCK_TICK_T uint64_t
    #define OMNI_CLOCK_FREQ_T mach_timebase_info_data_t
#else
    // DEV_NOTE: some systems might need -lrt
    #include <ctime>
    // DEV_NOTE: csignal contains a reference to CLOCK_MONOTONIC
    #include <csignal>
    #if defined(OMNI_OS_WIN)
        #define OMNI_CLOCK_TICK_T clock_t
    #else
        // non Win/OSX use clock_gettime for tick call
        #define OMNI_CLOCK_TICK_T struct timespec
        #if !defined(CLOCK_MONOTONIC)
            #error CLOCK_MONOTONIC is not defined
        #endif
    #endif
    /* DEV_NOTE: some systems (like QNX) only take CLOCK_REALTIME for clock_getres, but will return correct
    values for clock_gettime with CLOCK_MONOTONIC, use OMNI_CLOCK_GETRES_REALTIME to change this behaviour */
    #if defined(OMNI_CLOCK_GETRES_REALTIME)
        #define OMNI_CLOCK_GETRES_CID_FW CLOCK_REALTIME
    #else
        #define OMNI_CLOCK_GETRES_CID_FW CLOCK_MONOTONIC
    #endif
#endif
#if !defined(OMNI_CLOCK_FREQ_T)
    #define OMNI_CLOCK_FREQ_T OMNI_CLOCK_TICK_T
#endif

namespace omni {
    namespace chrono {    
        typedef OMNI_CLOCK_TICK_T tick_t;
        typedef OMNI_CLOCK_FREQ_T freq_t;

        inline int64_t MAX_TICKS() { return std::numeric_limits<int64_t>::max(); }
        inline int64_t MIN_TICKS() { return std::numeric_limits<int64_t>::min(); }

        OMNI_CONSTEXT_FW const double TICKS_PER_NANOSECOND OMNI_EXT_ASSN_FW(0.01);
        OMNI_CONSTEXT_FW const int64_t TICKS_PER_MICROSECOND OMNI_EXT_ASSN_FW(10);
        OMNI_CONSTEXT_FW const int64_t TICKS_PER_MILLISECOND OMNI_EXT_ASSN_FW(10000);
        OMNI_CONSTEXT_FW const int64_t TICKS_PER_TENTH_SECOND OMNI_EXT_ASSN_FW(1000000); // TICKS_PER_MILLISECOND * 100);
        OMNI_CONSTEXT_FW const int64_t TICKS_PER_SECOND OMNI_EXT_ASSN_FW(10000000);
        OMNI_CONSTEXT_FW const int64_t TICKS_PER_MINUTE OMNI_EXT_ASSN_FW(600000000);
        OMNI_CONSTEXT_FW const int64_t TICKS_PER_HOUR OMNI_EXT_ASSN_FW(36000000000);
        OMNI_CONSTEXT_FW const int64_t TICKS_PER_DAY OMNI_EXT_ASSN_FW(864000000000);

        OMNI_CONSTEXT_FW const int64_t NANOSECONDS_PER_MICROSECOND OMNI_EXT_ASSN_FW(1000);
        OMNI_CONSTEXT_FW const int64_t NANOSECONDS_PER_MILLISECOND OMNI_EXT_ASSN_FW(1000000);
        OMNI_CONSTEXT_FW const int64_t NANOSECONDS_PER_SECOND OMNI_EXT_ASSN_FW(1000000000);
        OMNI_CONSTEXT_FW const int64_t NANOSECONDS_PER_MINUTE OMNI_EXT_ASSN_FW(60000000000);
        OMNI_CONSTEXT_FW const int64_t NANOSECONDS_PER_HOUR OMNI_EXT_ASSN_FW(3600000000000);
        OMNI_CONSTEXT_FW const int64_t NANOSECONDS_PER_DAY OMNI_EXT_ASSN_FW(86400000000000);
        
        OMNI_CONSTEXT_FW const double MICROSECONDS_PER_NANOSECOND OMNI_EXT_ASSN_FW(0.001);
        OMNI_CONSTEXT_FW const int64_t MICROSECONDS_PER_MILLISECOND OMNI_EXT_ASSN_FW(1000);
        OMNI_CONSTEXT_FW const int64_t MICROSECONDS_PER_SECOND OMNI_EXT_ASSN_FW(1000000);
        OMNI_CONSTEXT_FW const int64_t MICROSECONDS_PER_MINUTE OMNI_EXT_ASSN_FW(60000000);
        OMNI_CONSTEXT_FW const int64_t MICROSECONDS_PER_HOUR OMNI_EXT_ASSN_FW(3600000000);
        OMNI_CONSTEXT_FW const int64_t MICROSECONDS_PER_DAY OMNI_EXT_ASSN_FW(86400000000);

        OMNI_CONSTEXT_FW const double MILLISECONDS_PER_NANOSECOND OMNI_EXT_ASSN_FW(0.0001);
        OMNI_CONSTEXT_FW const double MILLISECONDS_PER_MICROSECOND OMNI_EXT_ASSN_FW(0.001);
        OMNI_CONSTEXT_FW const int64_t MILLISECONDS_PER_SECOND OMNI_EXT_ASSN_FW(1000);
        OMNI_CONSTEXT_FW const int64_t MILLISECONDS_PER_MINUTE OMNI_EXT_ASSN_FW(60000);
        OMNI_CONSTEXT_FW const int64_t MILLISECONDS_PER_HOUR OMNI_EXT_ASSN_FW(3600000);
        OMNI_CONSTEXT_FW const int64_t MILLISECONDS_PER_DAY OMNI_EXT_ASSN_FW(86400000);

        OMNI_CONSTEXT_FW const double SECONDS_PER_NANOSECOND OMNI_EXT_ASSN_FW(0.000000001);
        OMNI_CONSTEXT_FW const double SECONDS_PER_MICROSECOND OMNI_EXT_ASSN_FW(0.000001);
        OMNI_CONSTEXT_FW const double SECONDS_PER_MILLISECOND OMNI_EXT_ASSN_FW(0.001);
        OMNI_CONSTEXT_FW const int64_t SECONDS_PER_MINUTE OMNI_EXT_ASSN_FW(60);
        OMNI_CONSTEXT_FW const int64_t SECONDS_PER_HOUR OMNI_EXT_ASSN_FW(3600);
        OMNI_CONSTEXT_FW const int64_t SECONDS_PER_DAY OMNI_EXT_ASSN_FW(86400);

        OMNI_CONSTEXT_FW const double MINUTES_PER_NANOSECOND OMNI_EXT_ASSN_FW(0.000000000016667);
        OMNI_CONSTEXT_FW const double MINUTES_PER_MICROSECOND OMNI_EXT_ASSN_FW(0.000000016667);
        OMNI_CONSTEXT_FW const double MINUTES_PER_MILLISECOND OMNI_EXT_ASSN_FW(0.000016667);
        OMNI_CONSTEXT_FW const double MINUTES_PER_SECOND OMNI_EXT_ASSN_FW(0.016667);
        OMNI_CONSTEXT_FW const int64_t MINUTES_PER_HOUR OMNI_EXT_ASSN_FW(60);
        OMNI_CONSTEXT_FW const int64_t MINUTES_PER_DAY OMNI_EXT_ASSN_FW(1440);

        OMNI_CONSTEXT_FW const int64_t NANOSECONDS_PER_TICK OMNI_EXT_ASSN_FW(100);
        OMNI_CONSTEXT_FW const double MICROSECONDS_PER_TICK OMNI_EXT_ASSN_FW(0.1);
        OMNI_CONSTEXT_FW const double MILLISECONDS_PER_TICK OMNI_EXT_ASSN_FW(0.0001);
        OMNI_CONSTEXT_FW const double SECONDS_PER_TICK OMNI_EXT_ASSN_FW(0.0000001);
        OMNI_CONSTEXT_FW const double MINUTES_PER_TICK OMNI_EXT_ASSN_FW(0.000000001666666666667);
        OMNI_CONSTEXT_FW const double HOURS_PER_TICK OMNI_EXT_ASSN_FW(0.000000000027777777778);
        OMNI_CONSTEXT_FW const double DAYS_PER_TICK OMNI_EXT_ASSN_FW(0.000000000001157407407);
    }
}

#endif // OMNI_TICK_T_HPP