lemon/bits/windows.cc
author Alpar Juttner <alpar@cs.elte.hu>
Fri, 22 May 2015 17:48:24 +0200
branch1.3
changeset 1153 48a0d237db3c
parent 1143 f70f688d9ef9
permissions -rw-r--r--
Merge bugfix #598 to branch 1.3
     1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library.
     4  *
     5  * Copyright (C) 2003-2013
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     9  * Permission to use, modify and distribute this software is granted
    10  * provided that this copyright notice appears in all copies. For
    11  * precise terms see the accompanying LICENSE file.
    12  *
    13  * This software is provided "AS IS" with no warranty of any kind,
    14  * express or implied, and with no claim as to its suitability for any
    15  * purpose.
    16  *
    17  */
    18 
    19 ///\file
    20 ///\brief Some basic non-inline functions and static global data.
    21 
    22 #include<lemon/bits/windows.h>
    23 
    24 #if defined(LEMON_WIN32) && defined(__GNUC__)
    25 #pragma GCC diagnostic ignored "-Wold-style-cast"
    26 #endif
    27 
    28 #ifdef LEMON_WIN32
    29 #ifndef WIN32_LEAN_AND_MEAN
    30 #define WIN32_LEAN_AND_MEAN
    31 #endif
    32 #ifndef NOMINMAX
    33 #define NOMINMAX
    34 #endif
    35 #ifdef UNICODE
    36 #undef UNICODE
    37 #endif
    38 #include <windows.h>
    39 #ifdef LOCALE_INVARIANT
    40 #define MY_LOCALE LOCALE_INVARIANT
    41 #else
    42 #define MY_LOCALE LOCALE_NEUTRAL
    43 #endif
    44 #else
    45 #include <unistd.h>
    46 #include <ctime>
    47 #ifndef LEMON_WIN32
    48 #include <sys/times.h>
    49 #endif
    50 #include <sys/time.h>
    51 #endif
    52 
    53 #include <cmath>
    54 #include <sstream>
    55 
    56 namespace lemon {
    57   namespace bits {
    58     void getWinProcTimes(double &rtime,
    59                          double &utime, double &stime,
    60                          double &cutime, double &cstime)
    61     {
    62 #ifdef LEMON_WIN32
    63       static const double ch = 4294967296.0e-7;
    64       static const double cl = 1.0e-7;
    65 
    66       FILETIME system;
    67       GetSystemTimeAsFileTime(&system);
    68       rtime = ch * system.dwHighDateTime + cl * system.dwLowDateTime;
    69 
    70       FILETIME create, exit, kernel, user;
    71       if (GetProcessTimes(GetCurrentProcess(),&create, &exit, &kernel, &user)) {
    72         utime = ch * user.dwHighDateTime + cl * user.dwLowDateTime;
    73         stime = ch * kernel.dwHighDateTime + cl * kernel.dwLowDateTime;
    74         cutime = 0;
    75         cstime = 0;
    76       } else {
    77         rtime = 0;
    78         utime = 0;
    79         stime = 0;
    80         cutime = 0;
    81         cstime = 0;
    82       }
    83 #else
    84       timeval tv;
    85       gettimeofday(&tv, 0);
    86       rtime=tv.tv_sec+double(tv.tv_usec)/1e6;
    87 
    88       tms ts;
    89       double tck=sysconf(_SC_CLK_TCK);
    90       times(&ts);
    91       utime=ts.tms_utime/tck;
    92       stime=ts.tms_stime/tck;
    93       cutime=ts.tms_cutime/tck;
    94       cstime=ts.tms_cstime/tck;
    95 #endif
    96     }
    97 
    98     std::string getWinFormattedDate()
    99     {
   100       std::ostringstream os;
   101 #ifdef LEMON_WIN32
   102       SYSTEMTIME time;
   103       GetSystemTime(&time);
   104       char buf1[11], buf2[9], buf3[5];
   105       if (GetDateFormat(MY_LOCALE, 0, &time,
   106                         ("ddd MMM dd"), buf1, 11) &&
   107           GetTimeFormat(MY_LOCALE, 0, &time,
   108                         ("HH':'mm':'ss"), buf2, 9) &&
   109           GetDateFormat(MY_LOCALE, 0, &time,
   110                         ("yyyy"), buf3, 5)) {
   111         os << buf1 << ' ' << buf2 << ' ' << buf3;
   112       }
   113       else os << "unknown";
   114 #else
   115       timeval tv;
   116       gettimeofday(&tv, 0);
   117 
   118       char cbuf[26];
   119       ctime_r(&tv.tv_sec,cbuf);
   120       os << cbuf;
   121 #endif
   122       return os.str();
   123     }
   124 
   125     int getWinRndSeed()
   126     {
   127 #ifdef LEMON_WIN32
   128       FILETIME time;
   129       GetSystemTimeAsFileTime(&time);
   130       return GetCurrentProcessId() + time.dwHighDateTime + time.dwLowDateTime;
   131 #else
   132       timeval tv;
   133       gettimeofday(&tv, 0);
   134       return getpid() + tv.tv_sec + tv.tv_usec;
   135 #endif
   136     }
   137 
   138     WinLock::WinLock() {
   139 #ifdef LEMON_WIN32
   140       CRITICAL_SECTION *lock = new CRITICAL_SECTION;
   141       InitializeCriticalSection(lock);
   142       _repr = lock;
   143 #else
   144       _repr = 0; //Just to avoid 'unused variable' warning with clang
   145 #endif
   146     }
   147 
   148     WinLock::~WinLock() {
   149 #ifdef LEMON_WIN32
   150       CRITICAL_SECTION *lock = static_cast<CRITICAL_SECTION*>(_repr);
   151       DeleteCriticalSection(lock);
   152       delete lock;
   153 #endif
   154     }
   155 
   156     void WinLock::lock() {
   157 #ifdef LEMON_WIN32
   158       CRITICAL_SECTION *lock = static_cast<CRITICAL_SECTION*>(_repr);
   159       EnterCriticalSection(lock);
   160 #endif
   161     }
   162 
   163     void WinLock::unlock() {
   164 #ifdef LEMON_WIN32
   165       CRITICAL_SECTION *lock = static_cast<CRITICAL_SECTION*>(_repr);
   166       LeaveCriticalSection(lock);
   167 #endif
   168     }
   169   }
   170 }