lemon/bits/windows.cc
author Peter Madarasi <madarasip@caesar.elte.hu>
Mon, 30 Mar 2015 17:42:30 +0200
changeset 1141 a037254714b3
parent 1134 f70f688d9ef9
permissions -rw-r--r--
VF2 algorithm added (#597)

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