RflySimSDK v3.05
RflySimSDK说明文档
载入中...
搜索中...
未找到
vrpn_Thread.h
浏览该文件的文档.
1
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
63class VRPN_API vrpn_Semaphore {
64public:
66 vrpn_Semaphore(int cNumResources = 1);
67
70
73 bool reset(int cNumResources = 1);
74
77 int p();
78
81 int v();
82
86 int condP();
87
90
91private:
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
121namespace vrpn {
123 };
124
129 class VRPN_API SemaphoreGuard {
130 public:
133
136
139
141 bool locked() const { return locked_; }
142
144 void lock();
145
148
150 void unlock();
151
152 private:
153 void handleLockResult_(int result);
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
175struct VRPN_API vrpn_ThreadData {
176 void *pvUD;
177};
178
179typedef 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.
185class VRPN_API vrpn_Thread {
186public:
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
223protected:
224 // user func and data ptrs
225 void(*pfThread)(vrpn_ThreadData &ThreadData);
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.
240extern bool vrpn_test_threads_and_semaphores(void);
241
242
243
244#endif // INCLUDED_vrpn_Thread_h_GUID_A455652F_72CE_4F8A_859E_543489012D01
245
An RAII lock/guard class for vrpn_Semaphore
定义 vrpn_Thread.h:129
bool try_to_lock()
Tries to lock - returns true if we locked it.
SemaphoreGuard(vrpn_Semaphore &sem, try_to_lock_t)
overload that only tries to lock (condP) - doesn't block.
bool locked() const
Checks to see if we locked.
定义 vrpn_Thread.h:141
void unlock()
Unlocks the resource, if we have locked it.
~SemaphoreGuard()
Destructor that unlocks if we've locked.
SemaphoreGuard(vrpn_Semaphore &sem)
Constructor that locks (p) the semaphore
void lock()
Locks the semaphore, if we haven't locked it already.
定义 vrpn_Thread.h:63
int condP()
Non-blocking attempt to acquire resource ("down")
bool reset(int cNumResources=1)
routine to reset it (true on success, false on failure) (may create new semaphore)
int p()
Blocking acquire of resource. ("down")
int numResources()
read values
int v()
Release of resource. ("up")
vrpn_Semaphore(int cNumResources=1)
constructor - mutex by default (0 is a sync primitive)
~vrpn_Semaphore()
destructor
定义 vrpn_Thread.h:185
定义 vrpn_Thread.h:122
定义 vrpn_Thread.h:175
const try_to_lock_t try_to_lock
Dummy variable to pass to SemaphoreGuard to indicate we only want a conditional lock.
定义 vrpn_Thread.h:127
Header to minimally include windows.h