COIN-OR::LEMON - Graph Library

source: lemon/lemon/bits/windows.cc @ 1340:f70f688d9ef9

Last change on this file since 1340:f70f688d9ef9 was 1340:f70f688d9ef9, checked in by Alpar Juttner <alpar@…>, 9 years ago

Replace #define WIN32 (#595)

File size: 4.0 KB
RevLine 
[511]1/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 *
3 * This file is a part of LEMON, a generic C++ optimization library.
4 *
[1270]5 * Copyright (C) 2003-2013
[511]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
[1340]24#ifdef LEMON_WIN32
[511]25#ifndef WIN32_LEAN_AND_MEAN
26#define WIN32_LEAN_AND_MEAN
27#endif
28#ifndef NOMINMAX
29#define NOMINMAX
30#endif
[513]31#ifdef UNICODE
32#undef UNICODE
33#endif
[511]34#include <windows.h>
[513]35#ifdef LOCALE_INVARIANT
36#define MY_LOCALE LOCALE_INVARIANT
37#else
38#define MY_LOCALE LOCALE_NEUTRAL
39#endif
[511]40#else
41#include <unistd.h>
42#include <ctime>
[1340]43#ifndef LEMON_WIN32
[511]44#include <sys/times.h>
[1053]45#endif
[511]46#include <sys/time.h>
47#endif
48
49#include <cmath>
50#include <sstream>
51
52namespace lemon {
53  namespace bits {
54    void getWinProcTimes(double &rtime,
55                         double &utime, double &stime,
56                         double &cutime, double &cstime)
57    {
[1340]58#ifdef LEMON_WIN32
[511]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;
[1340]97#ifdef LEMON_WIN32
[511]98      SYSTEMTIME time;
99      GetSystemTime(&time);
[513]100      char buf1[11], buf2[9], buf3[5];
[1340]101      if (GetDateFormat(MY_LOCALE, 0, &time,
[513]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)) {
[511]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    {
[1340]123#ifdef LEMON_WIN32
[511]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    }
[1131]133
134    WinLock::WinLock() {
[1340]135#ifdef LEMON_WIN32
[1131]136      CRITICAL_SECTION *lock = new CRITICAL_SECTION;
137      InitializeCriticalSection(lock);
138      _repr = lock;
[1163]139#else
140      _repr = 0; //Just to avoid 'unused variable' warning with clang
[1131]141#endif
142    }
[1270]143
[1131]144    WinLock::~WinLock() {
[1340]145#ifdef LEMON_WIN32
[1131]146      CRITICAL_SECTION *lock = static_cast<CRITICAL_SECTION*>(_repr);
147      DeleteCriticalSection(lock);
148      delete lock;
149#endif
150    }
151
152    void WinLock::lock() {
[1340]153#ifdef LEMON_WIN32
[1131]154      CRITICAL_SECTION *lock = static_cast<CRITICAL_SECTION*>(_repr);
155      EnterCriticalSection(lock);
156#endif
157    }
158
159    void WinLock::unlock() {
[1340]160#ifdef LEMON_WIN32
[1131]161      CRITICAL_SECTION *lock = static_cast<CRITICAL_SECTION*>(_repr);
162      LeaveCriticalSection(lock);
163#endif
164    }
[511]165  }
166}
Note: See TracBrowser for help on using the repository browser.