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