alpar@491: /* -*- mode: C++; indent-tabs-mode: nil; -*- alpar@491: * alpar@491: * This file is a part of LEMON, a generic C++ optimization library. alpar@491: * alpar@1092: * Copyright (C) 2003-2013 alpar@491: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@491: * (Egervary Research Group on Combinatorial Optimization, EGRES). alpar@491: * alpar@491: * Permission to use, modify and distribute this software is granted alpar@491: * provided that this copyright notice appears in all copies. For alpar@491: * precise terms see the accompanying LICENSE file. alpar@491: * alpar@491: * This software is provided "AS IS" with no warranty of any kind, alpar@491: * express or implied, and with no claim as to its suitability for any alpar@491: * purpose. alpar@491: * alpar@491: */ alpar@491: alpar@491: ///\file alpar@491: ///\brief Some basic non-inline functions and static global data. alpar@491: alpar@491: #include alpar@491: alpar@1134: #ifdef LEMON_WIN32 alpar@491: #ifndef WIN32_LEAN_AND_MEAN alpar@491: #define WIN32_LEAN_AND_MEAN alpar@491: #endif alpar@491: #ifndef NOMINMAX alpar@491: #define NOMINMAX alpar@491: #endif tapolcai@493: #ifdef UNICODE tapolcai@493: #undef UNICODE tapolcai@493: #endif alpar@491: #include tapolcai@493: #ifdef LOCALE_INVARIANT tapolcai@493: #define MY_LOCALE LOCALE_INVARIANT tapolcai@493: #else tapolcai@493: #define MY_LOCALE LOCALE_NEUTRAL tapolcai@493: #endif alpar@491: #else alpar@491: #include alpar@491: #include alpar@1134: #ifndef LEMON_WIN32 alpar@491: #include alpar@940: #endif alpar@491: #include alpar@491: #endif alpar@491: alpar@491: #include alpar@491: #include alpar@491: alpar@491: namespace lemon { alpar@491: namespace bits { alpar@491: void getWinProcTimes(double &rtime, alpar@491: double &utime, double &stime, alpar@491: double &cutime, double &cstime) alpar@491: { alpar@1134: #ifdef LEMON_WIN32 alpar@491: static const double ch = 4294967296.0e-7; alpar@491: static const double cl = 1.0e-7; alpar@491: alpar@491: FILETIME system; alpar@491: GetSystemTimeAsFileTime(&system); alpar@491: rtime = ch * system.dwHighDateTime + cl * system.dwLowDateTime; alpar@491: alpar@491: FILETIME create, exit, kernel, user; alpar@491: if (GetProcessTimes(GetCurrentProcess(),&create, &exit, &kernel, &user)) { alpar@491: utime = ch * user.dwHighDateTime + cl * user.dwLowDateTime; alpar@491: stime = ch * kernel.dwHighDateTime + cl * kernel.dwLowDateTime; alpar@491: cutime = 0; alpar@491: cstime = 0; alpar@491: } else { alpar@491: rtime = 0; alpar@491: utime = 0; alpar@491: stime = 0; alpar@491: cutime = 0; alpar@491: cstime = 0; alpar@491: } alpar@491: #else alpar@491: timeval tv; alpar@491: gettimeofday(&tv, 0); alpar@491: rtime=tv.tv_sec+double(tv.tv_usec)/1e6; alpar@491: alpar@491: tms ts; alpar@491: double tck=sysconf(_SC_CLK_TCK); alpar@491: times(&ts); alpar@491: utime=ts.tms_utime/tck; alpar@491: stime=ts.tms_stime/tck; alpar@491: cutime=ts.tms_cutime/tck; alpar@491: cstime=ts.tms_cstime/tck; alpar@491: #endif alpar@491: } alpar@491: alpar@491: std::string getWinFormattedDate() alpar@491: { alpar@491: std::ostringstream os; alpar@1134: #ifdef LEMON_WIN32 alpar@491: SYSTEMTIME time; alpar@491: GetSystemTime(&time); tapolcai@493: char buf1[11], buf2[9], buf3[5]; alpar@1134: if (GetDateFormat(MY_LOCALE, 0, &time, tapolcai@493: ("ddd MMM dd"), buf1, 11) && tapolcai@493: GetTimeFormat(MY_LOCALE, 0, &time, tapolcai@493: ("HH':'mm':'ss"), buf2, 9) && tapolcai@493: GetDateFormat(MY_LOCALE, 0, &time, tapolcai@493: ("yyyy"), buf3, 5)) { alpar@491: os << buf1 << ' ' << buf2 << ' ' << buf3; alpar@491: } alpar@491: else os << "unknown"; alpar@491: #else alpar@491: timeval tv; alpar@491: gettimeofday(&tv, 0); alpar@491: alpar@491: char cbuf[26]; alpar@491: ctime_r(&tv.tv_sec,cbuf); alpar@491: os << cbuf; alpar@491: #endif alpar@491: return os.str(); alpar@491: } alpar@491: alpar@491: int getWinRndSeed() alpar@491: { alpar@1134: #ifdef LEMON_WIN32 alpar@491: FILETIME time; alpar@491: GetSystemTimeAsFileTime(&time); alpar@491: return GetCurrentProcessId() + time.dwHighDateTime + time.dwLowDateTime; alpar@491: #else alpar@491: timeval tv; alpar@491: gettimeofday(&tv, 0); alpar@491: return getpid() + tv.tv_sec + tv.tv_usec; alpar@491: #endif alpar@491: } deba@979: deba@979: WinLock::WinLock() { alpar@1134: #ifdef LEMON_WIN32 deba@979: CRITICAL_SECTION *lock = new CRITICAL_SECTION; deba@979: InitializeCriticalSection(lock); deba@979: _repr = lock; alpar@1001: #else alpar@1001: _repr = 0; //Just to avoid 'unused variable' warning with clang deba@979: #endif deba@979: } alpar@1092: deba@979: WinLock::~WinLock() { alpar@1134: #ifdef LEMON_WIN32 deba@979: CRITICAL_SECTION *lock = static_cast(_repr); deba@979: DeleteCriticalSection(lock); deba@979: delete lock; deba@979: #endif deba@979: } deba@979: deba@979: void WinLock::lock() { alpar@1134: #ifdef LEMON_WIN32 deba@979: CRITICAL_SECTION *lock = static_cast(_repr); deba@979: EnterCriticalSection(lock); deba@979: #endif deba@979: } deba@979: deba@979: void WinLock::unlock() { alpar@1134: #ifdef LEMON_WIN32 deba@979: CRITICAL_SECTION *lock = static_cast(_repr); deba@979: LeaveCriticalSection(lock); deba@979: #endif deba@979: } alpar@491: } alpar@491: }