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