lemon/bits/windows.cc
author Peter Kovacs <kpeter@inf.elte.hu>
Mon, 30 Jan 2012 20:21:45 +0100
changeset 984 fcb6ad1e67d0
parent 941 6660ac776acf
child 1001 e344f0887c59
permissions -rw-r--r--
Improve the Altering List pivot rule for NetworkSimplex (#435)
Much less candidate arcs are preserved from an iteration to the
next one and partial_sort() is used instead of heap operations.
     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-2010
     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 #ifdef WIN32
    25 #ifndef WIN32_LEAN_AND_MEAN
    26 #define WIN32_LEAN_AND_MEAN
    27 #endif
    28 #ifndef NOMINMAX
    29 #define NOMINMAX
    30 #endif
    31 #ifdef UNICODE
    32 #undef UNICODE
    33 #endif
    34 #include <windows.h>
    35 #ifdef LOCALE_INVARIANT
    36 #define MY_LOCALE LOCALE_INVARIANT
    37 #else
    38 #define MY_LOCALE LOCALE_NEUTRAL
    39 #endif
    40 #else
    41 #include <unistd.h>
    42 #include <ctime>
    43 #ifndef WIN32
    44 #include <sys/times.h>
    45 #endif
    46 #include <sys/time.h>
    47 #endif
    48 
    49 #include <cmath>
    50 #include <sstream>
    51 
    52 namespace lemon {
    53   namespace bits {
    54     void getWinProcTimes(double &rtime,
    55                          double &utime, double &stime,
    56                          double &cutime, double &cstime)
    57     {
    58 #ifdef WIN32
    59       static const double ch = 4294967296.0e-7;
    60       static const double cl = 1.0e-7;
    61 
    62       FILETIME system;
    63       GetSystemTimeAsFileTime(&system);
    64       rtime = ch * system.dwHighDateTime + cl * system.dwLowDateTime;
    65 
    66       FILETIME create, exit, kernel, user;
    67       if (GetProcessTimes(GetCurrentProcess(),&create, &exit, &kernel, &user)) {
    68         utime = ch * user.dwHighDateTime + cl * user.dwLowDateTime;
    69         stime = ch * kernel.dwHighDateTime + cl * kernel.dwLowDateTime;
    70         cutime = 0;
    71         cstime = 0;
    72       } else {
    73         rtime = 0;
    74         utime = 0;
    75         stime = 0;
    76         cutime = 0;
    77         cstime = 0;
    78       }
    79 #else
    80       timeval tv;
    81       gettimeofday(&tv, 0);
    82       rtime=tv.tv_sec+double(tv.tv_usec)/1e6;
    83 
    84       tms ts;
    85       double tck=sysconf(_SC_CLK_TCK);
    86       times(&ts);
    87       utime=ts.tms_utime/tck;
    88       stime=ts.tms_stime/tck;
    89       cutime=ts.tms_cutime/tck;
    90       cstime=ts.tms_cstime/tck;
    91 #endif
    92     }
    93 
    94     std::string getWinFormattedDate()
    95     {
    96       std::ostringstream os;
    97 #ifdef WIN32
    98       SYSTEMTIME time;
    99       GetSystemTime(&time);
   100       char buf1[11], buf2[9], buf3[5];
   101           if (GetDateFormat(MY_LOCALE, 0, &time,
   102                         ("ddd MMM dd"), buf1, 11) &&
   103           GetTimeFormat(MY_LOCALE, 0, &time,
   104                         ("HH':'mm':'ss"), buf2, 9) &&
   105           GetDateFormat(MY_LOCALE, 0, &time,
   106                         ("yyyy"), buf3, 5)) {
   107         os << buf1 << ' ' << buf2 << ' ' << buf3;
   108       }
   109       else os << "unknown";
   110 #else
   111       timeval tv;
   112       gettimeofday(&tv, 0);
   113 
   114       char cbuf[26];
   115       ctime_r(&tv.tv_sec,cbuf);
   116       os << cbuf;
   117 #endif
   118       return os.str();
   119     }
   120 
   121     int getWinRndSeed()
   122     {
   123 #ifdef WIN32
   124       FILETIME time;
   125       GetSystemTimeAsFileTime(&time);
   126       return GetCurrentProcessId() + time.dwHighDateTime + time.dwLowDateTime;
   127 #else
   128       timeval tv;
   129       gettimeofday(&tv, 0);
   130       return getpid() + tv.tv_sec + tv.tv_usec;
   131 #endif
   132     }
   133 
   134     WinLock::WinLock() {
   135 #ifdef WIN32
   136       CRITICAL_SECTION *lock = new CRITICAL_SECTION;
   137       InitializeCriticalSection(lock);
   138       _repr = lock;
   139 #endif
   140     }
   141     
   142     WinLock::~WinLock() {
   143 #ifdef WIN32
   144       CRITICAL_SECTION *lock = static_cast<CRITICAL_SECTION*>(_repr);
   145       DeleteCriticalSection(lock);
   146       delete lock;
   147 #endif
   148     }
   149 
   150     void WinLock::lock() {
   151 #ifdef WIN32
   152       CRITICAL_SECTION *lock = static_cast<CRITICAL_SECTION*>(_repr);
   153       EnterCriticalSection(lock);
   154 #endif
   155     }
   156 
   157     void WinLock::unlock() {
   158 #ifdef WIN32
   159       CRITICAL_SECTION *lock = static_cast<CRITICAL_SECTION*>(_repr);
   160       LeaveCriticalSection(lock);
   161 #endif
   162     }
   163   }
   164 }