NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
threads.cpp
Go to the documentation of this file.
1 /*
2  * nvbio
3  * Copyright (c) 2011-2014, NVIDIA CORPORATION. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the NVIDIA CORPORATION nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <nvbio/basic/threads.h>
29 
30 #ifdef WIN32
31 #include "windows.h"
32 #else
33 #include <pthread.h>
34 #include <unistd.h>
35 #include <string>
36 using namespace std;
37 #endif
38 
39 #include <string.h>
40 
41 namespace nvbio {
42 
43 #if !defined(WIN32) && defined(PLATFORM_X86)
44 void cpuID(unsigned i, unsigned regs[4])
45 {
46  asm volatile
47  ("cpuid" : "=a" (regs[0]), "=b" (regs[1]), "=c" (regs[2]), "=d" (regs[3])
48  : "a" (i), "c" (0));
49  // ECX is set to zero for CPUID function 4
50 }
51 #endif
52 
54 {
55  #ifdef WIN32
56  SYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer[32];
57  DWORD returnLength = 32 * sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
58 
59  GetLogicalProcessorInformation(
60  buffer,
61  &returnLength );
62 
63  SYSTEM_LOGICAL_PROCESSOR_INFORMATION* ptr = buffer;
64 
65  uint32 processorCoreCount = 0;
66 
67  for (uint32 byteOffset = 0;
68  byteOffset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= returnLength;
69  byteOffset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION))
70  {
71  switch (ptr->Relationship)
72  {
73  case RelationProcessorCore:
74  processorCoreCount++;
75  break;
76  }
77 
78  ptr++;
79  }
80  return processorCoreCount;
81  #elif defined(PLATFORM_X86)
82  unsigned regs[4];
83 
84  // Get vendor
85  char vendor[12];
86  cpuID(0, regs);
87  memcpy(&vendor[0], &regs[1], sizeof(unsigned)); // EBX
88  memcpy(&vendor[1], &regs[3], sizeof(unsigned)); // EDX
89  memcpy(&vendor[2], &regs[2], sizeof(unsigned)); // ECX
90  string cpuVendor = string(vendor, 12);
91 
92  // Get CPU features
93  cpuID(1, regs);
94 // unsigned cpuFeatures = regs[3]; // EDX
95 
96  // Logical core count per CPU
97  cpuID(1, regs);
98  unsigned logical = (regs[1] >> 16) & 0xff; // EBX[23:16]
99  unsigned cores = logical;
100 
101  if (cpuVendor == "GenuineIntel")
102  {
103  // Get DCP cache info
104  cpuID(4, regs);
105  cores = ((regs[0] >> 26) & 0x3f) + 1; // EAX[31:26] + 1
106  }
107  else if (cpuVendor == "AuthenticAMD")
108  {
109  // Get NC: Number of CPU cores - 1
110  cpuID(0x80000008, regs);
111  cores = ((unsigned)(regs[2] & 0xff)) + 1; // ECX[7:0] + 1
112  }
113  return cores;
114  #else
115  return 0;
116  #endif
117 }
119 {
120  #ifdef WIN32
121  SYSTEM_INFO sysinfo;
122  GetSystemInfo( &sysinfo );
123  return uint32( sysinfo.dwNumberOfProcessors );
124  #else
125  return uint32( sysconf( _SC_NPROCESSORS_ONLN ) );
126  #endif
127 }
128 
129 
130 #if NOTHREADS
131 
132 struct ThreadBase::Impl
133 {
134 };
135 
136 // constructor
137 ThreadBase::ThreadBase() : m_id( 0u ), m_impl( new Impl )
138 {
139 }
140 
141 // destructor
142 ThreadBase::~ThreadBase()
143 {
144 }
145 
146 // create the thread
147 void ThreadBase::create(void* (*func)(void*), void* arg)
148 {
149  func( arg );
150 }
151 // join the thread
152 void ThreadBase::join()
153 {
154 }
155 
157 struct Mutex::Impl
158 {
159 };
160 
161 Mutex::Mutex() : m_impl( new Impl )
162 {
163 }
164 Mutex::~Mutex()
165 {
166 }
167 
168 void Mutex::lock() {}
169 void Mutex::unlock() {}
170 
171 void yield() {}
172 
173 #elif defined(WIN32)
174 
175 namespace {
176 
177 struct Func
178 {
179  typedef void* (*FuncType)(void*);
180 
181  FuncType m_func;
182  void* m_arg;
183 };
184 DWORD __stdcall CallFunc(void* arg)
185 {
186  Func* func = reinterpret_cast<Func*>(arg);
187  func->m_func( func->m_arg );
188  return DWORD(0);
189 }
190 
191 };
192 
193 struct ThreadBase::Impl
194 {
195  Impl() : m_handle(0), m_tid(0) {}
196  ~Impl()
197  {
198  if (m_handle)
199  CloseHandle( m_handle );
200  }
201 
202  HANDLE m_handle;
203  DWORD m_tid; // thread id
204  Func m_func_arg;
205 };
206 
207 // constructor
208 ThreadBase::ThreadBase() : m_id( 0u ), m_impl( new Impl )
209 {
210 }
211 
212 // destructor
213 ThreadBase::~ThreadBase()
214 {
215 }
216 
217 // create the thread
218 void ThreadBase::create(void* (*func)(void*), void* arg)
219 {
220  // fill in the function wrapper
221  m_impl->m_func_arg.m_func = func;
222  m_impl->m_func_arg.m_arg = arg;
223 
224  // create the thread in a suspended state
225  m_impl->m_handle = CreateThread(
226  0, // Security attributes
227  0, // Stack size
228  CallFunc,
229  &m_impl->m_func_arg,
230  CREATE_SUSPENDED,
231  &m_impl->m_tid);
232 
233  // and run it
234  ResumeThread( m_impl->m_handle );
235 }
236 // join the thread
237 void ThreadBase::join()
238 {
239  WaitForSingleObject( m_impl->m_handle, INFINITE );
240 }
241 
243 struct Mutex::Impl
244 {
245  Impl() { InitializeCriticalSection( &m_mutex ); }
246  ~Impl() { DeleteCriticalSection( &m_mutex ); }
247 
248  CRITICAL_SECTION m_mutex;
249 };
250 
251 Mutex::Mutex() : m_impl( new Impl )
252 {
253 }
254 Mutex::~Mutex()
255 {
256 }
257 
258 void Mutex::lock() { EnterCriticalSection( &m_impl->m_mutex ); }
259 void Mutex::unlock() { LeaveCriticalSection( &m_impl->m_mutex ); }
260 
261 void yield() {}
262 
263 #else
264 
266 {
267  pthread_t m_thread;
268 };
269 
270 // constructor
271 ThreadBase::ThreadBase() : m_id( 0u ), m_impl( new Impl )
272 {
273 }
274 
275 // destructor
277 {
278 }
279 
280 // create the thread
281 void ThreadBase::create(void* (*func)(void*), void* arg)
282 {
283  pthread_create( &m_impl->m_thread, NULL, func, arg );
284 }
285 // join the thread
287 {
288  pthread_join( m_impl->m_thread, NULL );
289 }
290 
293 {
294  Impl() { pthread_mutex_init( &m_mutex, NULL ); }
295  ~Impl() { pthread_mutex_destroy( &m_mutex ); }
296 
297  pthread_mutex_t m_mutex;
298 };
299 
300 Mutex::Mutex() : m_impl( new Impl )
301 {
302 }
304 {
305 }
306 
307 void Mutex::lock() { pthread_mutex_lock( &m_impl->m_mutex ); }
308 void Mutex::unlock() { pthread_mutex_unlock( &m_impl->m_mutex ); }
309 
310 void yield() { pthread_yield(); }
311 
312 #endif
313 
314 } // namespace nvbio