/
vrpn_Thread.C
798 lines (735 loc) · 21.6 KB
/
vrpn_Thread.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
/** @file
@brief Implementation
@date 2015
@author
Sensics, Inc.
<http://sensics.com/osvr>
*/
// Copyright 2015 Sensics, Inc.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// Internal Includes
#include "vrpn_Thread.h"
#include "vrpn_Shared.h"
// Library/third-party includes
// - none
// Standard includes
#include <stdio.h> // for fprintf, stderr, perror, etc
#include <string.h> // for memcpy, strlen, strcpy, etc
#ifndef _WIN32
#include <errno.h> // for EAGAIN, errno
#include <signal.h> // for pthread_kill, SIGKILL
#endif
#ifdef __APPLE__
#include <unistd.h>
#endif
#define ALL_ASSERT(exp, msg) \
if (!(exp)) { \
fprintf(stderr, "\nAssertion failed! \n %s (%s, %d)\n", msg, __FILE__, \
__LINE__); \
}
// init all fields in init()
vrpn_Semaphore::vrpn_Semaphore(int cNumResources)
: cResources(cNumResources)
{
init();
}
#ifdef sgi
bool vrpn_Semaphore::init()
{
if (vrpn_Semaphore::ppaArena == NULL) {
vrpn_Semaphore::allocArena();
}
if (cResources == 1) {
fUsingLock = true;
ps = NULL;
// use lock instead of semaphore
if ((l = usnewlock(vrpn_Semaphore::ppaArena)) == NULL) {
fprintf(stderr, "vrpn_Semaphore::vrpn_Semaphore: error allocating "
"lock from arena.\n");
return false;
}
}
else {
fUsingLock = false;
l = NULL;
if ((ps = usnewsema(vrpn_Semaphore::ppaArena, cResources)) == NULL) {
fprintf(stderr, "vrpn_Semaphore::vrpn_Semaphore: error allocating "
"semaphore from arena.\n");
return false;
}
}
return true;
}
#elif defined(_WIN32)
bool vrpn_Semaphore::init()
{
// args are security, initial count, max count, and name
// TCH 20 Feb 2001 - Make the PC behavior closer to the SGI behavior.
int numMax = cResources;
if (numMax < 1) {
numMax = 1;
}
hSemaphore = CreateSemaphore(NULL, cResources, numMax, NULL);
if (!hSemaphore) {
// get error info from windows (from FormatMessage help page)
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL,
GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
// Default language
(LPTSTR)&lpMsgBuf, 0, NULL);
fprintf(stderr,
"vrpn_Semaphore::vrpn_Semaphore: error creating semaphore, "
"WIN32 CreateSemaphore call caused the following error: %s\n",
(LPTSTR)lpMsgBuf);
// Free the buffer.
LocalFree(lpMsgBuf);
return false;
}
return true;
}
#elif defined(__APPLE__)
bool vrpn_Semaphore::init()
{
// We need to use sem_open on the mac because sem_init is not implemented
int numMax = cResources;
if (numMax < 1) {
numMax = 1;
}
char tempname[100];
snprintf(tempname, 100, "/tmp/vrpn_sem.XXXXXXX");
semaphore = sem_open(mktemp(tempname), O_CREAT, 0600, numMax);
if (semaphore == SEM_FAILED) {
perror("vrpn_Semaphore::vrpn_Semaphore: error opening semaphore");
return false;
}
return true;
}
#else
bool vrpn_Semaphore::init()
{
// Posix threads are the default.
// We use sem_init on linux (instead of sem_open).
int numMax = cResources;
if (numMax < 1) {
numMax = 1;
}
try { semaphore = new sem_t; }
catch (...) { return false; }
if (sem_init(semaphore, 0, numMax) != 0) {
perror("vrpn_Semaphore::vrpn_Semaphore: error initializing semaphore");
return false;
}
return true;
}
#endif
#ifdef sgi
bool vrpn_Semaphore::destroy()
{
if (fUsingLock) {
usfreelock(l, vrpn_Semaphore::ppaArena);
}
else {
usfreesema(ps, vrpn_Semaphore::ppaArena);
}
return true;
}
#elif defined(_WIN32)
bool vrpn_Semaphore::destroy()
{
if (!CloseHandle(hSemaphore)) {
// get error info from windows (from FormatMessage help page)
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL,
GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
// Default language
(LPTSTR)&lpMsgBuf, 0, NULL);
fprintf(stderr,
"vrpn_Semaphore::destroy: error destroying semaphore, "
"WIN32 CloseHandle call caused the following error: %s\n",
(LPTSTR)lpMsgBuf);
// Free the buffer.
LocalFree(lpMsgBuf);
return false;
}
return true;
}
#else
bool vrpn_Semaphore::destroy()
{
// Posix threads are the default.
#ifdef __APPLE__
if (sem_close(semaphore) != 0) {
perror("vrpn_Semaphore::destroy: error destroying semaphore.");
return false;
}
#else
if (sem_destroy(semaphore) != 0) {
fprintf(stderr,
"vrpn_Semaphore::destroy: error destroying semaphore.\n");
return false;
}
try {
delete semaphore;
} catch (...) {
fprintf(stderr, "vrpn_Semaphore::destroy(): delete failed\n");
return false;
}
#endif
semaphore = NULL;
return true;
}
#endif
vrpn_Semaphore::~vrpn_Semaphore()
{
if (!destroy()) {
fprintf(
stderr,
"vrpn_Semaphore::~vrpn_Semaphore: error destroying semaphore.\n");
}
}
// routine to reset it
bool vrpn_Semaphore::reset(int cNumResources)
{
cResources = cNumResources;
// Destroy the old semaphore and then create a new one with the correct
// value.
if (!destroy()) {
fprintf(stderr, "vrpn_Semaphore::reset: error destroying semaphore.\n");
return false;
}
if (!init()) {
fprintf(stderr,
"vrpn_Semaphore::reset: error initializing semaphore.\n");
return false;
}
return true;
}
// routines to use it (p blocks, cond p does not)
// 1 on success, -1 fail
int vrpn_Semaphore::p()
{
#ifdef sgi
if (fUsingLock) {
if (ussetlock(l) != 1) {
perror("vrpn_Semaphore::p: ussetlock:");
return -1;
}
}
else {
if (uspsema(ps) != 1) {
perror("vrpn_Semaphore::p: uspsema:");
return -1;
}
}
#elif defined(_WIN32)
switch (WaitForSingleObject(hSemaphore, INFINITE)) {
case WAIT_OBJECT_0:
// got the resource
break;
case WAIT_TIMEOUT:
ALL_ASSERT(0, "vrpn_Semaphore::p: infinite wait time timed out!");
return -1;
break;
case WAIT_ABANDONED:
ALL_ASSERT(0, "vrpn_Semaphore::p: thread holding resource died");
return -1;
break;
case WAIT_FAILED:
// get error info from windows (from FormatMessage help page)
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL,
GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
// Default language
(LPTSTR)&lpMsgBuf, 0, NULL);
fprintf(stderr,
"vrpn_Semaphore::p: error waiting for resource, "
"WIN32 WaitForSingleObject call caused the following error: %s",
(LPTSTR)lpMsgBuf);
// Free the buffer.
LocalFree(lpMsgBuf);
return -1;
break;
default:
ALL_ASSERT(0, "vrpn_Semaphore::p: unknown return code");
return -1;
}
#else
// Posix by default
if (sem_wait(semaphore) != 0) {
perror("vrpn_Semaphore::p: ");
return -1;
}
#endif
return 1;
}
// 0 on success, -1 fail
int vrpn_Semaphore::v()
{
#ifdef sgi
if (fUsingLock) {
if (usunsetlock(l)) {
perror("vrpn_Semaphore::v: usunsetlock:");
return -1;
}
}
else {
if (usvsema(ps)) {
perror("vrpn_Semaphore::v: uspsema:");
return -1;
}
}
#elif defined(_WIN32)
if (!ReleaseSemaphore(hSemaphore, 1, NULL)) {
// get error info from windows (from FormatMessage help page)
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL,
GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
// Default language
(LPTSTR)&lpMsgBuf, 0, NULL);
fprintf(stderr,
"vrpn_Semaphore::v: error v'ing semaphore, "
"WIN32 ReleaseSemaphore call caused the following error: %s",
(LPTSTR)lpMsgBuf);
// Free the buffer.
LocalFree(lpMsgBuf);
return -1;
}
#else
// Posix by default
if (sem_post(semaphore) != 0) {
perror("vrpn_Semaphore::p: ");
return -1;
}
#endif
return 0;
}
// 0 if it can't get the resource, 1 if it can
// -1 if fail
int vrpn_Semaphore::condP()
{
int iRetVal = 1;
#ifdef sgi
if (fUsingLock) {
// don't spin at all
iRetVal = uscsetlock(l, 0);
if (iRetVal <= 0) {
perror("vrpn_Semaphore::condP: uscsetlock:");
return -1;
}
}
else {
iRetVal = uscpsema(ps);
if (iRetVal <= 0) {
perror("vrpn_Semaphore::condP: uscpsema:");
return -1;
}
}
#elif defined(_WIN32)
switch (WaitForSingleObject(hSemaphore, 0)) {
case WAIT_OBJECT_0:
// got the resource
break;
case WAIT_TIMEOUT:
// resource not free
iRetVal = 0;
break;
case WAIT_ABANDONED:
ALL_ASSERT(0, "vrpn_Semaphore::condP: thread holding resource died");
return -1;
break;
case WAIT_FAILED:
// get error info from windows (from FormatMessage help page)
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL,
GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
// Default language
(LPTSTR)&lpMsgBuf, 0, NULL);
fprintf(stderr,
"Semaphore::condP: error waiting for resource, "
"WIN32 WaitForSingleObject call caused the following error: %s",
(LPTSTR)lpMsgBuf);
// Free the buffer.
LocalFree(lpMsgBuf);
return -1;
break;
default:
ALL_ASSERT(0, "vrpn_Semaphore::p: unknown return code");
return -1;
}
#else
// Posix by default
iRetVal = sem_trywait(semaphore);
if (iRetVal == 0) {
iRetVal = 1;
}
else if (errno == EAGAIN) {
iRetVal = 0;
}
else {
perror("vrpn_Semaphore::condP: ");
iRetVal = -1;
}
#endif
return iRetVal;
}
int vrpn_Semaphore::numResources() { return cResources; }
// static var definition
#ifdef sgi
usptr_t *vrpn_Semaphore::ppaArena = NULL;
#include <sys/stat.h>
// for umask stuff
#include <sys/types.h>
#include <sys/stat.h>
void vrpn_Semaphore::allocArena()
{
// /dev/zero is a special file which can only be shared between
// processes/threads which share file descriptors.
// It never shows up in the file system.
if ((ppaArena = usinit("/dev/zero")) == NULL) {
perror("vrpn_Thread::allocArena: usinit:");
}
}
#endif
namespace vrpn {
SemaphoreGuard::SemaphoreGuard(vrpn_Semaphore &sem)
: locked_(false)
, sem_(sem)
{
lock();
}
// @brief overload that only tries to lock - doesn't block.
SemaphoreGuard::SemaphoreGuard(vrpn_Semaphore &sem, try_to_lock_t)
: locked_(false)
, sem_(sem)
{
try_to_lock();
}
/// @brief Destructor that unlocks if we've locked.
SemaphoreGuard::~SemaphoreGuard() { unlock(); }
/// @brief Locks the semaphore, if we haven't locked it already.
void SemaphoreGuard::lock()
{
if (locked_) {
return;
}
int result = sem_.p();
handleLockResult_(result);
}
/// @brief Tries to lock - returns true if we locked it.
bool SemaphoreGuard::try_to_lock()
{
if (locked_) {
return true;
}
int result = sem_.condP();
handleLockResult_(result);
return locked_;
}
/// @brief Unlocks the resource, if we have locked it.
void SemaphoreGuard::unlock()
{
if (locked_) {
int result = sem_.v();
ALL_ASSERT(result == 0, "failed to unlock semaphore!");
locked_ = false;
}
}
void SemaphoreGuard::handleLockResult_(int result)
{
ALL_ASSERT(result >= 0, "Lock error!");
if (result == 1) {
locked_ = true;
}
}
} // namespace vrpn
vrpn_Thread::vrpn_Thread(void(*pfThreadparm)(vrpn_ThreadData &ThreadData),
vrpn_ThreadData tdparm)
: pfThread(pfThreadparm)
, td(tdparm)
, threadID(0)
{
}
bool vrpn_Thread::go()
{
if (threadID != 0) {
fprintf(stderr, "vrpn_Thread::go: already running\n");
return false;
}
#ifdef sgi
if ((threadID = sproc(&threadFuncShell, PR_SALL, this)) ==
((unsigned long)-1)) {
perror("vrpn_Thread::go: sproc");
return false;
}
// Threads not defined for the CYGWIN environment yet...
#elif defined(_WIN32) && !defined(__CYGWIN__)
// pass in func, let it pick stack size, and arg to pass to thread
if ((threadID = _beginthread(&threadFuncShell, 0, this)) ==
((unsigned long)-1)) {
perror("vrpn_Thread::go: _beginthread");
return false;
}
#else
// Pthreads by default
if (pthread_create(&threadID, NULL, &threadFuncShellPosix, this) != 0) {
perror("vrpn_Thread::go:pthread_create: ");
return false;
}
#endif
return true;
}
bool vrpn_Thread::kill()
{
// kill the os thread
#if defined(sgi) || defined(_WIN32)
if (threadID > 0) {
#ifdef sgi
if (::kill((long)threadID, SIGKILL) < 0) {
perror("vrpn_Thread::kill: kill:");
return false;
}
#elif defined(_WIN32)
// Return value of -1 passed to TerminateThread causes a warning.
if (!TerminateThread((HANDLE)threadID, 1)) {
fprintf(stderr,
"vrpn_Thread::kill: problem with terminateThread call.\n");
return false;
}
#endif
#else
if (threadID) {
// Posix by default. Detach so that the thread's resources will be
// freed automatically when it is killed.
if (pthread_detach(threadID) != 0) {
perror("vrpn_Thread::kill:pthread_detach: ");
return false;
}
if (pthread_kill(threadID, SIGKILL) != 0) {
perror("vrpn_Thread::kill:pthread_kill: ");
return false;
}
#endif
}
else {
fprintf(stderr, "vrpn_Thread::kill: thread is not currently alive.\n");
return false;
}
threadID = 0;
return true;
}
bool vrpn_Thread::running() { return threadID != 0; }
vrpn_Thread::thread_t vrpn_Thread::pid() { return threadID; }
bool vrpn_Thread::available()
{
#ifdef vrpn_THREADS_AVAILABLE
return true;
#else
return false;
#endif
}
void vrpn_Thread::userData(void *pvNewUserData) { td.pvUD = pvNewUserData; }
void *vrpn_Thread::userData() { return td.pvUD; }
void vrpn_Thread::threadFuncShell(void *pvThread)
{
vrpn_Thread *pth = static_cast<vrpn_Thread *>(pvThread);
pth->pfThread(pth->td);
// thread has stopped running
#if !defined(sgi) && !defined(_WIN32)
// Pthreads; need to detach the thread so its resources will be freed.
if (pthread_detach(pth->threadID) != 0) {
perror("vrpn_Thread::threadFuncShell:pthread_detach: ");
}
#endif
pth->threadID = 0;
}
// This is a Posix-compatible function prototype that
// just calls the other function.
void *vrpn_Thread::threadFuncShellPosix(void *pvThread)
{
threadFuncShell(pvThread);
return NULL;
}
vrpn_Thread::~vrpn_Thread()
{
if (running()) {
kill();
}
}
// For the code to get the number of processor cores.
#ifdef __APPLE__
#include <sys/param.h>
#include <sys/sysctl.h>
#endif
unsigned vrpn_Thread::number_of_processors()
{
#ifdef _WIN32
// Copy the hardware information to the SYSTEM_INFO structure.
SYSTEM_INFO siSysInfo;
GetSystemInfo(&siSysInfo);
return siSysInfo.dwNumberOfProcessors;
#elif linux
// For Linux, we look at the /proc/cpuinfo file and count up the number
// of "processor :" entries (tab between processor and colon) in
// the file to find out how many we have.
FILE *f = fopen("/proc/cpuinfo", "r");
int count = 0;
if (f == NULL) {
perror("vrpn_Thread::number_of_processors:fopen: ");
return 1;
}
char line[512];
while (fgets(line, sizeof(line), f) != NULL) {
if (strncmp(line, "processor\t:", strlen("processor\t:")) == 0) {
count++;
}
}
fclose(f);
if (count == 0) {
fprintf(stderr,
"vrpn_Thread::number_of_processors: Found zero, returning 1\n");
count = 1;
}
return count;
#elif __APPLE__
int count;
size_t size = sizeof(count);
if (sysctlbyname("hw.ncpu", &count, &size, NULL, 0)) {
return 1;
}
else {
return static_cast<unsigned>(count);
}
#else
fprintf(stderr, "vrpn_Thread::number_of_processors: Not yet implemented on "
"this architecture.\n");
return 1;
#endif
}
// Thread function to call from within vrpn_test_threads_and_semaphores().
// In this case, the userdata pointer is a pointer to a semaphore that
// the thread should call v() on so that it will free up the main program
// thread.
static void vrpn_test_thread_body(vrpn_ThreadData &threadData)
{
if (threadData.pvUD == NULL) {
fprintf(stderr, "vrpn_test_thread_body(): pvUD is NULL\n");
return;
}
vrpn_Semaphore *s = static_cast<vrpn_Semaphore *>(threadData.pvUD);
s->v();
return;
}
bool vrpn_test_threads_and_semaphores(void)
{
//------------------------------------------------------------
// Make a semaphore to test in single-threaded mode. First run its count
// all the way
// down to zero, then bring it back to the full complement and then bring it
// down
// again. Check that all of the semaphores are available and also that
// there are no
// more than expected available.
const unsigned sem_count = 5;
vrpn_Semaphore s(sem_count);
unsigned i;
for (i = 0; i < sem_count; i++) {
if (s.condP() != 1) {
fprintf(stderr, "vrpn_test_threads_and_semaphores(): Semaphore ran "
"out of counts\n");
return false;
}
}
if (s.condP() != 0) {
fprintf(stderr, "vrpn_test_threads_and_semaphores(): Semaphore had too "
"many counts\n");
return false;
}
for (i = 0; i < sem_count; i++) {
if (s.v() != 0) {
fprintf(stderr, "vrpn_test_threads_and_semaphores(): Could not "
"release Semaphore\n");
return false;
}
}
for (i = 0; i < sem_count; i++) {
if (s.condP() != 1) {
fprintf(stderr, "vrpn_test_threads_and_semaphores(): Semaphore ran "
"out of counts, round 2\n");
return false;
}
}
if (s.condP() != 0) {
fprintf(stderr, "vrpn_test_threads_and_semaphores(): Semaphore had too "
"many counts, round 2\n");
return false;
}
//------------------------------------------------------------
// Get a semaphore and use it to construct a thread data structure and then
// a thread. Use that thread to test whether threading is enabled (if not,
// then
// this completes our testing) and to find out how many processors there
// are.
vrpn_ThreadData td;
td.pvUD = NULL;
vrpn_Thread t(vrpn_test_thread_body, td);
// If threading is not enabled, then we're done.
if (!t.available()) {
return true;
}
// Find out how many processors we have.
unsigned num_procs = t.number_of_processors();
if (num_procs == 0) {
fprintf(stderr, "vrpn_test_threads_and_semaphores(): "
"vrpn_Thread::number_of_processors() returned zero\n");
return false;
}
//------------------------------------------------------------
// Now make sure that we can actually run a thread. Do this by
// creating a semaphore with one entry and calling p() on it.
// Then make sure we can't p() it again and then run a thread
// that will call v() on it when it runs.
vrpn_Semaphore sem;
if (sem.p() != 1) {
fprintf(stderr, "vrpn_test_threads_and_semaphores(): thread-test "
"Semaphore had no count\n");
return false;
}
if (sem.condP() != 0) {
fprintf(stderr, "vrpn_test_threads_and_semaphores(): thread-test "
"Semaphore had too many counts\n");
return false;
}
t.userData(&sem);
if (!t.go()) {
fprintf(stderr,
"vrpn_test_threads_and_semaphores(): Could not start thread\n");
return false;
}
struct timeval start;
struct timeval now;
vrpn_gettimeofday(&start, NULL);
while (true) {
if (sem.condP() == 1) {
// The thread must have run; we got the semaphore!
break;
}
// Time out after three seconds if we haven't had the thread run to
// reset the semaphore.
vrpn_gettimeofday(&now, NULL);
struct timeval diff = vrpn_TimevalDiff(now, start);
if (diff.tv_sec >= 3) {
fprintf(stderr,
"vrpn_test_threads_and_semaphores(): Thread didn't run\n");
return false;
}
vrpn_SleepMsecs(1);
}
return true;
}