vrpn
07.35
Virtual Reality Peripheral Network
Toggle main menu visibility
Loading...
Searching...
No Matches
vrpn_Thread.h
Go to the documentation of this file.
1
17
18
// Copyright 2015 Sensics, Inc.
19
// Distributed under the Boost Software License, Version 1.0.
20
// (See accompanying file LICENSE_1_0.txt or copy at
21
// http://www.boost.org/LICENSE_1_0.txt)
22
23
#ifndef INCLUDED_vrpn_Thread_h_GUID_A455652F_72CE_4F8A_859E_543489012D01
24
#define INCLUDED_vrpn_Thread_h_GUID_A455652F_72CE_4F8A_859E_543489012D01
25
26
// Internal Includes
27
#include "
vrpn_Configure.h
"
// for VRPN_API
28
29
// Library/third-party includes
30
// - none
31
32
// Standard includes
33
34
#if defined(sgi) || (defined(_WIN32) && !defined(__CYGWIN__)) || \
35
defined(linux) || defined(__APPLE__)
36
#define vrpn_THREADS_AVAILABLE
37
#else
38
#undef vrpn_THREADS_AVAILABLE
39
#endif
40
41
// multi process stuff
42
#if defined(sgi)
43
#include <task.h>
44
#include <ulocks.h>
45
#elif defined(_WIN32)
46
#include "
vrpn_WindowsH.h
"
47
#include <process.h>
48
#else
49
#include <pthread.h>
// for pthread_t
50
#include <semaphore.h>
// for sem_t
51
#endif
52
53
// make the SGI compile without tons of warnings
54
#ifdef sgi
55
#pragma set woff 1110, 1424, 3201
56
#endif
57
58
// and reset the warnings
59
#ifdef sgi
60
#pragma reset woff 1110, 1424, 3201
61
#endif
62
63
class
VRPN_API
vrpn_Semaphore
{
64
public
:
66
vrpn_Semaphore
(
int
cNumResources = 1);
67
69
~vrpn_Semaphore
();
70
73
bool
reset
(
int
cNumResources = 1);
74
77
int
p
();
78
81
int
v
();
82
86
int
condP
();
87
89
int
numResources
();
90
91
private
:
93
vrpn_Semaphore
(
const
vrpn_Semaphore
&);
95
vrpn_Semaphore
& operator=(
const
vrpn_Semaphore
&);
98
bool
init();
99
bool
destroy();
101
102
int
cResources;
103
104
// arch specific details
105
#ifdef sgi
106
// single mem area for dynamically alloced shared mem
107
static
usptr_t *ppaArena;
108
static
void
allocArena();
109
110
// the semaphore struct in the arena
111
usema_t *ps;
112
ulock_t l;
113
bool
fUsingLock;
114
#elif defined(_WIN32)
115
HANDLE hSemaphore;
116
#else
117
sem_t *semaphore;
// Posix
118
#endif
119
};
120
121
namespace
vrpn
{
122
struct
try_to_lock_t
{
123
};
124
127
const
try_to_lock_t
try_to_lock
= {};
129
class
VRPN_API
SemaphoreGuard
{
130
public
:
132
explicit
SemaphoreGuard
(
vrpn_Semaphore
&sem);
133
135
SemaphoreGuard
(
vrpn_Semaphore
&sem,
try_to_lock_t
);
136
138
~SemaphoreGuard
();
139
141
bool
locked
()
const
{
return
locked_; }
142
144
void
lock();
145
147
bool
try_to_lock
();
148
150
void
unlock();
151
152
private
:
153
void
handleLockResult_(
int
result);
155
SemaphoreGuard
(
SemaphoreGuard
const
&);
157
SemaphoreGuard
&operator=(
SemaphoreGuard
const
&);
158
bool
locked_;
159
vrpn_Semaphore
&sem_;
160
};
161
162
}
// namespace vrpn
163
164
// A ptr to this struct will be passed to the
165
// thread function. The user data ptr will be in pvUD.
166
// (There used to be a non-functional semaphore object
167
// also in this structure, but it was removed. This leaves
168
// a struct with only one element, which is a pain but
169
// at least it doesn't break existing code. If we need
170
// to add something else later, there is a place for it.
171
172
// The user should create and manage any semaphore needed
173
// to handle access control to the userdata.
174
175
struct
VRPN_API
vrpn_ThreadData
{
176
void
*
pvUD
;
177
};
178
179
typedef
void(*
vrpn_THREAD_FUNC
)(
vrpn_ThreadData
&threadData);
180
181
// Don't let the existence of a Thread class fool you into thinking
182
// that VRPN is thread-safe. This and the Semaphore are included as
183
// building blocks towards making your own code thread-safe. They are
184
// here to enable the vrpn_Imager_Stream_Buffer class to do its thing.
185
class
VRPN_API
vrpn_Thread
{
186
public
:
187
// args are the routine to run in the thread
188
// a ThreadData struct which will be passed into
189
// the thread (it will be passed as a void *).
190
vrpn_Thread
(
vrpn_THREAD_FUNC
pfThread
,
vrpn_ThreadData
td
);
191
~vrpn_Thread
();
192
193
#if defined(sgi)
194
typedef
unsigned
long
thread_t
;
195
#elif defined(_WIN32)
196
typedef
uintptr_t
thread_t
;
197
#else
198
typedef
pthread_t
thread_t
;
199
#endif
200
201
// start/kill the thread (true on success, false on failure)
202
bool
go
();
203
bool
kill
();
204
205
// thread info: check if running, get proc id
206
bool
running
();
207
thread_t
pid
();
208
209
// run-time user function to test if threads are available
210
// (same value as #ifdef THREADS_AVAILABLE)
211
static
bool
available
();
212
213
// Number of processors available on this machine.
214
static
unsigned
number_of_processors
();
215
216
// This can be used to change the ThreadData user data ptr
217
// between calls to go (ie, when a thread object is used
218
// many times with different args). This will take
219
// effect the next time go() is called.
220
void
userData
(
void
*pvNewUserData);
221
void
*
userData
();
222
223
protected
:
224
// user func and data ptrs
225
void(*
pfThread
)(
vrpn_ThreadData
&ThreadData);
226
vrpn_ThreadData
td
;
227
228
// utility func for calling the specified function.
229
static
void
threadFuncShell
(
void
*pvThread);
230
231
// Posix version of the utility function, makes the
232
// function prototype match.
233
static
void
*
threadFuncShellPosix
(
void
*pvThread);
234
235
// the process id
236
thread_t
threadID
;
237
};
238
239
// Returns true if they work and false if they do not.
240
extern
bool
vrpn_test_threads_and_semaphores
(
void
);
241
242
243
244
#endif
// INCLUDED_vrpn_Thread_h_GUID_A455652F_72CE_4F8A_859E_543489012D01
245
vrpn::SemaphoreGuard
An RAII lock/guard class for vrpn_Semaphore.
Definition
vrpn_Thread.h:129
vrpn::SemaphoreGuard::SemaphoreGuard
SemaphoreGuard(vrpn_Semaphore &sem)
Constructor that locks (p) the semaphore.
Definition
vrpn_Thread.C:430
vrpn::SemaphoreGuard::locked
bool locked() const
Checks to see if we locked.
Definition
vrpn_Thread.h:141
vrpn_Semaphore
Definition
vrpn_Thread.h:63
vrpn_Semaphore::v
int v()
Release of resource. ("up").
Definition
vrpn_Thread.C:292
vrpn_Semaphore::vrpn_Semaphore
vrpn_Semaphore(int cNumResources=1)
constructor - mutex by default (0 is a sync primitive)
Definition
vrpn_Thread.C:43
vrpn_Semaphore::reset
bool reset(int cNumResources=1)
routine to reset it (true on success, false on failure) (may create new semaphore)
Definition
vrpn_Thread.C:212
vrpn_Semaphore::numResources
int numResources()
read values
Definition
vrpn_Thread.C:407
vrpn_Semaphore::condP
int condP()
Non-blocking attempt to acquire resource ("down").
Definition
vrpn_Thread.C:337
vrpn_Semaphore::p
int p()
Blocking acquire of resource. ("down").
Definition
vrpn_Thread.C:232
vrpn_Thread::threadFuncShellPosix
static void * threadFuncShellPosix(void *pvThread)
Definition
vrpn_Thread.C:600
vrpn_Thread::threadFuncShell
static void threadFuncShell(void *pvThread)
Definition
vrpn_Thread.C:584
vrpn_Thread::number_of_processors
static unsigned number_of_processors()
Definition
vrpn_Thread.C:619
vrpn_Thread::running
bool running()
Definition
vrpn_Thread.C:567
vrpn_Thread::kill
bool kill()
Definition
vrpn_Thread.C:527
vrpn_Thread::pfThread
void(* pfThread)(vrpn_ThreadData &ThreadData)
Definition
vrpn_Thread.h:225
vrpn_Thread::vrpn_Thread
vrpn_Thread(vrpn_THREAD_FUNC pfThread, vrpn_ThreadData td)
Definition
vrpn_Thread.C:488
vrpn_Thread::threadID
thread_t threadID
Definition
vrpn_Thread.h:236
vrpn_Thread::available
static bool available()
Definition
vrpn_Thread.C:571
vrpn_Thread::td
vrpn_ThreadData td
Definition
vrpn_Thread.h:226
vrpn_Thread::go
bool go()
Definition
vrpn_Thread.C:496
vrpn_Thread::userData
void userData(void *pvNewUserData)
Definition
vrpn_Thread.C:580
vrpn_Thread::pid
thread_t pid()
Definition
vrpn_Thread.C:569
vrpn_Thread::thread_t
pthread_t thread_t
Definition
vrpn_Thread.h:198
vrpn
Definition
vrpn_Assert.C:25
vrpn::try_to_lock
const try_to_lock_t try_to_lock
Dummy variable to pass to SemaphoreGuard to indicate we only want a conditional lock.
Definition
vrpn_Thread.h:127
vrpn::try_to_lock_t
Definition
vrpn_Thread.h:122
vrpn_ThreadData
Definition
vrpn_Thread.h:175
vrpn_ThreadData::pvUD
void * pvUD
Definition
vrpn_Thread.h:176
vrpn_Configure.h
VRPN_API
#define VRPN_API
Definition
vrpn_Configure.h:648
vrpn_test_threads_and_semaphores
bool vrpn_test_threads_and_semaphores(void)
Definition
vrpn_Thread.C:685
vrpn_THREAD_FUNC
void(* vrpn_THREAD_FUNC)(vrpn_ThreadData &threadData)
Definition
vrpn_Thread.h:179
vrpn_WindowsH.h
Header to minimally include windows.h.
vrpn_Thread.h
Generated by
1.17.0