Electroneum
Loading...
Searching...
No Matches
misc_os_dependent.h
Go to the documentation of this file.
1// Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are met:
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above copyright
9// notice, this list of conditions and the following disclaimer in the
10// documentation and/or other materials provided with the distribution.
11// * Neither the name of the Andrey N. Sabelnikov nor the
12// names of its contributors may be used to endorse or promote products
13// derived from this software without specific prior written permission.
14//
15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY
19// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25//
26#ifdef _WIN32
27#include <winsock2.h>
28#endif
29
30#ifdef WIN32
31 #ifndef WIN32_LEAN_AND_MEAN
32 #define WIN32_LEAN_AND_MEAN
33 #endif
34
35 //#ifdef _WIN32_WINNT
36 // #undef _WIN32_WINNT
37 // #define _WIN32_WINNT 0x0600
38 //#endif
39
40
41#include <windows.h>
42#endif
43
44#ifdef __MACH__
45#include <mach/clock.h>
46#include <mach/mach.h>
47#endif
48
49#include <iostream>
50#include <boost/lexical_cast.hpp>
51
52#pragma once
53namespace epee
54{
55namespace misc_utils
56{
57
59 {
60#if defined(_MSC_VER)
61 return ::GetTickCount64() * 1000000;
62#elif defined(WIN32)
63 static LARGE_INTEGER pcfreq = {0};
64 LARGE_INTEGER ticks;
65 if (!pcfreq.QuadPart)
66 QueryPerformanceFrequency(&pcfreq);
67 QueryPerformanceCounter(&ticks);
68 ticks.QuadPart *= 1000000000; /* we want nsec */
69 return ticks.QuadPart / pcfreq.QuadPart;
70#elif defined(__MACH__)
71 clock_serv_t cclock;
72 mach_timespec_t mts;
73
74 host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
75 clock_get_time(cclock, &mts);
76 mach_port_deallocate(mach_task_self(), cclock);
77
78 return ((uint64_t)mts.tv_sec * 1000000000) + (mts.tv_nsec);
79#else
80 struct timespec ts;
81 if(clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
82 return 0;
83 }
84 return ((uint64_t)ts.tv_sec * 1000000000) + (ts.tv_nsec);
85#endif
86 }
87
89 {
90 return get_ns_count() / 1000000;
91 }
92
93
94 inline int call_sys_cmd(const std::string& cmd)
95 {
96 std::cout << "# " << cmd << std::endl;
97
98 FILE * fp ;
99 //char tstCommand[] ="ls *";
100 char path[1000] = {0};
101#if !defined(__GNUC__)
102 fp = _popen(cmd.c_str(), "r");
103#else
104 fp = popen(cmd.c_str(), "r");
105#endif
106 while ( fgets( path, 1000, fp ) != NULL )
107 std::cout << path;
108
109#if !defined(__GNUC__)
110 _pclose(fp);
111#else
112 pclose(fp);
113#endif
114 return 0;
115
116 }
117
118
119 inline std::string get_thread_string_id()
120 {
121#if defined(_WIN32)
122 return boost::lexical_cast<std::string>(GetCurrentThreadId());
123#elif defined(__GNUC__)
124 return boost::lexical_cast<std::string>(pthread_self());
125#endif
126 }
127
128 inline bool get_gmt_time(time_t t, struct tm &tm)
129 {
130#ifdef _WIN32
131 return gmtime_s(&tm, &t);
132#else
133 return gmtime_r(&t, &tm);
134#endif
135 }
136}
137}
std::string get_thread_string_id()
uint64_t get_tick_count()
int call_sys_cmd(const std::string &cmd)
bool get_gmt_time(time_t t, struct tm &tm)
unsigned __int64 uint64_t
Definition stdint.h:136