Perform QPC timer check on windows in PBD::init when PBD_TEST_TIMERS env is defined
[ardour.git] / libs / pbd / pbd.cc
1 /*
2     Copyright (C) 2011 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <iostream>
21 #include <cstdlib>
22 #include <string>
23
24 #ifdef PLATFORM_WINDOWS
25 #include <fcntl.h>
26 #endif
27
28 #include <giomm.h>
29
30 #include <glibmm/thread.h>
31
32 #include "pbd/pbd.h"
33 #include "pbd/debug.h"
34 #include "pbd/error.h"
35 #include "pbd/id.h"
36 #include "pbd/enumwriter.h"
37 #include "pbd/fpu.h"
38 #ifdef PLATFORM_WINDOWS
39 #include "pbd/windows_timer_utils.h"
40 #endif
41
42 #ifdef PLATFORM_WINDOWS
43 #include <winsock2.h>
44 #endif
45
46 #include "i18n.h"
47
48 extern void setup_libpbd_enums ();
49
50 namespace {
51
52 static bool libpbd_initialized = false;
53
54 }
55
56 void
57 set_debug_options_from_env ()
58 {
59         bool set;
60         std::string options;
61
62         options = Glib::getenv ("PBD_DEBUG", set);
63         if (set) {
64                 std::cerr << "PBD_DEBUG=" << options << std::endl;
65                 PBD::parse_debug_options (options.c_str());
66         }
67 }
68
69 #ifdef PLATFORM_WINDOWS
70 void
71 test_timers_from_env ()
72 {
73         bool set;
74         std::string options;
75
76         options = Glib::getenv ("PBD_TEST_TIMERS", set);
77         if (set) {
78                 if (!PBD::QPC::check_timer_valid ()) {
79                         PBD::error << "Windows QPC Timer source not usable." << endmsg;
80                 } else {
81                         PBD::info << "Windows QPC Timer source usable." << endmsg;
82                 }
83         }
84 }
85 #endif
86
87 bool
88 PBD::init ()
89 {
90         if (libpbd_initialized) {
91                 return true;
92         }
93
94 #ifdef PLATFORM_WINDOWS
95         // Essential!!  Make sure that any files used by Ardour
96         //              will be created or opened in BINARY mode!
97         _fmode = O_BINARY;
98
99         WSADATA wsaData;
100
101         /* Initialize windows socket DLL for PBD::CrossThreadChannel
102          */
103         
104         if (WSAStartup(MAKEWORD(1,1),&wsaData) != 0) {
105                 fatal << "Windows socket initialization failed with error: " << WSAGetLastError() << endmsg;
106                 abort();
107                 /*NOTREACHED*/
108                 return false;
109         }
110
111         test_timers_from_env ();
112 #endif
113
114         if (!Glib::thread_supported()) {
115                 Glib::thread_init();
116         }
117
118         Gio::init ();
119
120         PBD::ID::init ();
121
122         setup_libpbd_enums ();
123
124         set_debug_options_from_env ();
125
126         libpbd_initialized = true;
127         return true;
128 }
129
130 void
131 PBD::cleanup ()
132 {
133 #ifdef PLATFORM_WINDOWS
134         WSACleanup();
135 #endif  
136
137         EnumWriter::destroy ();
138         FPU::destroy ();
139 }