cisst-saw
Loading...
Searching...
No Matches
osaThread.h
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
3
4/*
5
6 Author(s): Ankur Kapoor
7 Created on: 2004-04-30
8
9 (C) Copyright 2004-2019 Johns Hopkins University (JHU), All Rights Reserved.
10
11--- begin cisst license - do not edit ---
12
13This software is provided "as is" under an open source license, with
14no warranty. The complete license can be found in license.txt and
15http://www.cisst.org/cisst/license.txt.
16
17--- end cisst license ---
18*/
19
20
26
27#ifndef _osaThread_h
28#define _osaThread_h
29
33
34// Always include last
36
37
38#if (CISST_OS == CISST_WINDOWS)
39#define SCHED_FIFO 0
40#endif
41
42// SCHED_FIFO is not defined otherwise
43#if (CISST_OS == CISST_LINUX) || (CISST_OS == CISST_DARWIN)
44#include <pthread.h>
45#endif
46
55typedef int PriorityType;
56
57
63
64
65
78
80 enum {INTERNALS_SIZE = 8};
81 char Internals[INTERNALS_SIZE];
82 bool Valid;
83
85 friend class osaThread;
86
89 static unsigned int SizeOfInternals(void);
90 friend class osaThreadTest;
91
92public:
93
98
101
103 bool IsValid(void) const { return Valid; }
104
106 bool Equal(const osaThreadId & other) const;
107
109 inline bool operator == (const osaThreadId & other) const {
110 return this->Equal(other);
111 }
112
114 inline bool operator != (const osaThreadId & other) const {
115 return !this->Equal(other);
116 }
117
119#if (CISST_OS == CISST_LINUX_RTAI) || (CISST_OS == CISST_LINUX) || (CISST_OS == CISST_SOLARIS) || (CISST_OS == CISST_QNX) || (CISST_OS == CISST_WINDOWS)
120 bool operator () (const osaThreadId & lhs, const osaThreadId & rhs) const;
121#endif
122
124 // bool Lesser(const osaThreadId & other) const;
125
127 // inline bool operator < (const osaThreadId & other) const {
128 // return this->Lesser(other);
129 // }
130
132 void ToStream(std::ostream & outputStream) const;
133};
134
135
137inline
138std::ostream & operator << (std::ostream & output,
139 const osaThreadId & threadId) {
140 threadId.ToStream(output);
141 return output;
142}
143
144
151
152
153
154
169
171 osaThreadSignal Signal;
172
174 enum { INTERNALS_SIZE = 96 }; // PKAZ: this can be reduced
175 char Internals[INTERNALS_SIZE];
176
179 static unsigned int SizeOfInternals(void);
180 friend class osaThreadTest;
181
183 char Name[6];
184
188
191 PriorityType Priority;
192
194 osaThreadId ThreadId;
195
197 bool Valid;
198
200 bool Running;
201
202protected:
203
205 void CreateInternal(const char* name, void* func, void* userdata);
206
207 void SetThreadName(const char* name);
208
209public:
210
213
216
218 void Create(void * (*threadStart)(void),
219 const char * name = 0, int priority = 0, int policy = SCHED_FIFO)
220 {
221 Priority = priority;
222 Policy = policy;
223 CreateInternal(name, (void *)threadStart, 0);
224 }
225
227 template <class _userDataType>
228 void Create(void * (*threadStart)(_userDataType),
229 _userDataType userData = _userDataType(),
230 const char * name = 0, int priority = 0, int policy = SCHED_FIFO)
231 {
232 Priority = priority;
233 Policy = policy;
234 CreateInternal(name, (void *)threadStart, (void *)userData);
235 }
236
240 template <class _entryType, class _userDataType>
241 void Create(_entryType * obj, void * (_entryType::*threadStart)(_userDataType),
242 _userDataType userData = _userDataType(),
243 const char * name = 0, int priority = 0, int policy = SCHED_FIFO)
244 {
245 Priority = priority;
246 Policy = policy;
248 // In the following, the third parameter is not the UserData, but rather an osaHeapCallBack
249 // object that includes the UserData. When the CallbackAndDestroy method is called, it takes
250 // its void* parameter, casts it to osaHeapCallBack*, and then finds the actual callback method
251 // and UserData within the osaHeapCallBack object.
252 CreateInternal(name, (void *)adapter_t::CallbackAndDestroy, (void *)adapter_t::Create(obj, threadStart, userData));
253 }
254
257 void CreateFromCurrentThread(const char * name = 0, int priority = 0, int policy = SCHED_FIFO)
258 {
259 Priority = priority;
260 Policy = policy;
261 ThreadId = osaGetCurrentThreadId();
262 SetThreadName(name);
263 Valid = true;
264 }
265
266 void CreateFromThreadId(const osaThreadId &threadId,
267 const char * name = 0, int priority = 0, int policy = SCHED_FIFO)
268 {
269 Priority = priority;
270 Policy = policy;
271 ThreadId = threadId;
272 SetThreadName(name);
273 Valid = true;
274 }
275
280 void Delete(void);
281
283 void Wait(void);
284
286 inline void WaitForWakeup(void) { Signal.Wait(); }
287
289 inline void WaitForWakeup(double timeoutInSec) { Signal.Wait(timeoutInSec); }
290
292 inline void Wakeup(void) { Signal.Raise(); }
293
295 inline const char * GetName(void) const { return Name; }
296
298 inline osaThreadId GetId(void) const { return ThreadId; }
299
302
304 void SetPriority(PriorityType priority);
305
309
313
324 void Sleep(double timeInSeconds);
325
327 inline bool IsRunning(void) const { return Running; }
328
330 inline bool IsValid(void) const { return Valid; }
331
332};
333
340
341
342
343#endif // _osaThread_h
344
void CreateFromThreadId(const osaThreadId &threadId, const char *name=0, int priority=0, int policy=SCHED_FIFO)
Definition osaThread.h:266
const char * GetName(void) const
Definition osaThread.h:295
bool IsRunning(void) const
Definition osaThread.h:327
PriorityType GetPriority(void) const
void Create(_entryType *obj, void *(_entryType::*threadStart)(_userDataType), _userDataType userData=_userDataType(), const char *name=0, int priority=0, int policy=SCHED_FIFO)
Definition osaThread.h:241
friend class osaThreadTest
Definition osaThread.h:180
void SetSchedulingPolicy(SchedulingPolicyType policy)
void Wakeup(void)
Definition osaThread.h:292
void SetThreadName(const char *name)
void Create(void *(*threadStart)(_userDataType), _userDataType userData=_userDataType(), const char *name=0, int priority=0, int policy=SCHED_FIFO)
Definition osaThread.h:228
void Wait(void)
void WaitForWakeup(double timeoutInSec)
Definition osaThread.h:289
SchedulingPolicyType GetSchedulingPolicy(void)
void CreateFromCurrentThread(const char *name=0, int priority=0, int policy=SCHED_FIFO)
Definition osaThread.h:257
osaThreadId GetId(void) const
Definition osaThread.h:298
void Delete(void)
void Create(void *(*threadStart)(void), const char *name=0, int priority=0, int policy=SCHED_FIFO)
Definition osaThread.h:218
void CreateInternal(const char *name, void *func, void *userdata)
void WaitForWakeup(void)
Definition osaThread.h:286
bool IsValid(void) const
Definition osaThread.h:330
void Sleep(double timeInSeconds)
void SetPriority(PriorityType priority)
ThreadId type.
Definition osaThread.h:77
friend class osaThread
Definition osaThread.h:85
void ToStream(std::ostream &outputStream) const
friend class osaThreadTest
Definition osaThread.h:90
bool Equal(const osaThreadId &other) const
friend CISST_EXPORT osaThreadId osaGetCurrentThreadId(void)
bool IsValid(void) const
Definition osaThread.h:103
osaThreadId(void)
Definition osaThreadSignal.h:41
#define CISST_EXPORT
Definition cmnExportMacros.h:50
Portability across compilers and operating systems tools.
Macros to export the symbols of cisstOSAbstraction (in a Dll).
int PriorityType
PriorityType and SchedulingPolicyType.
Definition osaThread.h:55
std::ostream & operator<<(std::ostream &output, const osaThreadId &threadId)
Definition osaThread.h:138
int SchedulingPolicyType
SchedulingPolicyType. For now SchedulingPolicy is typedef'ed to int.
Definition osaThread.h:62
CISST_EXPORT osaThreadId osaGetCurrentThreadId(void)
#define SCHED_FIFO
Definition osaThread.h:39
CISST_EXPORT void osaCurrentThreadYield(void)
Defines a thread adapter.
Declaration of osaThreadSignal.
Definition osaThreadAdapter.h:85
bool operator==(const vctDynamicConstMatrixBase< __matrixOwnerType, _elementType > &otherMatrix) const
Definition vctDynamicConstMatrixBase.h:715
bool operator!=(const vctDynamicConstMatrixBase< __matrixOwnerType, _elementType > &otherMatrix) const
Definition vctDynamicConstMatrixBase.h:743
const_reference operator()(size_type rowIndex, size_type colIndex) const CISST_THROW(std
Definition vctDynamicConstMatrixBase.h:352