cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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-2007 Johns Hopkins University (JHU), All Rights
10  Reserved.
11 
12 --- begin cisst license - do not edit ---
13 
14 This software is provided "as is" under an open source license, with
15 no warranty. The complete license can be found in license.txt and
16 http://www.cisst.org/cisst/license.txt.
17 
18 --- end cisst license ---
19 */
20 
21 
28 #ifndef _osaThread_h
29 #define _osaThread_h
30 
34 
35 // Always include last
37 
38 
39 #if (CISST_OS == CISST_WINDOWS)
40 #define SCHED_FIFO 0
41 #endif
42 
43 #if (CISST_OS == CISST_DARWIN) // SCHED_FIFO is not defined otherwise
44 #include <pthread.h>
45 #endif
46 
55 typedef int PriorityType;
56 
57 
63 
64 
65 
78 
80  enum {INTERNALS_SIZE = 8};
81  char Internals[INTERNALS_SIZE];
82 
84  friend class osaThread;
85 
88  static unsigned int SizeOfInternals(void);
89  friend class osaThreadTest;
90 
91 public:
92 
96  osaThreadId(void);
97 
99  ~osaThreadId();
100 
102  bool Equal(const osaThreadId & other) const;
103 
105  inline bool operator == (const osaThreadId & other) const {
106  return this->Equal(other);
107  }
108 
110  inline bool operator != (const osaThreadId & other) const {
111  return !this->Equal(other);
112  }
113 
115 #if (CISST_OS == CISST_LINUX_RTAI) || (CISST_OS == CISST_LINUX) || (CISST_OS == CISST_SOLARIS) || (CISST_OS == CISST_QNX) || (CISST_OS == CISST_WINDOWS)
116  bool operator () (const osaThreadId & lhs, const osaThreadId & rhs) const;
117 #endif
118 
120  // bool Lesser(const osaThreadId & other) const;
121 
123  // inline bool operator < (const osaThreadId & other) const {
124  // return this->Lesser(other);
125  // }
126 
128  void ToStream(std::ostream & outputStream) const;
129 };
130 
131 
133 inline
134 std::ostream & operator << (std::ostream & output,
135  const osaThreadId & threadId) {
136  threadId.ToStream(output);
137  return output;
138 }
139 
140 
147 
148 
149 
150 
165 
167  osaThreadSignal Signal;
168 
170  enum { INTERNALS_SIZE = 96 }; // PKAZ: this can be reduced
171  char Internals[INTERNALS_SIZE];
172 
175  static unsigned int SizeOfInternals(void);
176  friend class osaThreadTest;
177 
179  char Name[6];
180 
183  SchedulingPolicyType Policy;
184 
187  PriorityType Priority;
188 
190  osaThreadId ThreadId;
191 
193  bool Valid;
194 
196  bool Running;
197 
198 protected:
199 
201  void CreateInternal(const char* name, void* func, void* userdata);
202 
203  void SetThreadName(const char* name);
204 
205 public:
206 
208  osaThread();
209 
211  ~osaThread();
212 
214  void Create(void * (*threadStart)(void),
215  const char * name = 0, int priority = 0, int policy = SCHED_FIFO)
216  {
217  Priority = priority;
218  Policy = policy;
219  CreateInternal(name, (void *)threadStart, 0);
220  }
221 
223  template <class _userDataType>
224  void Create(void * (*threadStart)(_userDataType),
225  _userDataType userData = _userDataType(),
226  const char * name = 0, int priority = 0, int policy = SCHED_FIFO)
227  {
228  Priority = priority;
229  Policy = policy;
230  CreateInternal(name, (void *)threadStart, (void *)userData);
231  }
232 
236  template <class _entryType, class _userDataType>
237  void Create(_entryType * obj, void * (_entryType::*threadStart)(_userDataType),
238  _userDataType userData = _userDataType(),
239  const char * name = 0, int priority = 0, int policy = SCHED_FIFO)
240  {
241  Priority = priority;
242  Policy = policy;
244  // In the following, the third parameter is not the UserData, but rather an osaHeapCallBack
245  // object that includes the UserData. When the CallbackAndDestroy method is called, it takes
246  // its void* parameter, casts it to osaHeapCallBack*, and then finds the actual callback method
247  // and UserData within the osaHeapCallBack object.
248  CreateInternal(name, (void *)adapter_t::CallbackAndDestroy, (void *)adapter_t::Create(obj, threadStart, userData));
249  }
250 
253  void CreateFromCurrentThread(const char * name = 0, int priority = 0, int policy = SCHED_FIFO)
254  {
255  Priority = priority;
256  Policy = policy;
257  ThreadId = osaGetCurrentThreadId();
258  SetThreadName(name);
259  Valid = true;
260  }
261 
262  void CreateFromThreadId(const osaThreadId &threadId,
263  const char * name = 0, int priority = 0, int policy = SCHED_FIFO)
264  {
265  Priority = priority;
266  Policy = policy;
267  ThreadId = threadId;
268  SetThreadName(name);
269  Valid = true;
270  }
271 
276  void Delete(void);
277 
279  void Wait(void);
280 
282  inline void WaitForWakeup(void) { Signal.Wait(); }
283 
285  inline void WaitForWakeup(double timeoutInSec) { Signal.Wait(timeoutInSec); }
286 
288  inline void Wakeup(void) { Signal.Raise(); }
289 
291  inline const char * GetName(void) const { return Name; }
292 
294  inline osaThreadId GetId(void) const { return ThreadId; }
295 
297  PriorityType GetPriority(void) const;
298 
300  void SetPriority(PriorityType priority);
301 
304  SchedulingPolicyType GetSchedulingPolicy(void);
305 
308  void SetSchedulingPolicy(SchedulingPolicyType policy);
309 
320  void Sleep(double timeInSeconds);
321 
323  inline bool IsRunning(void) const { return Running; }
324 
326  inline bool IsValid(void) const { return Valid; }
327 
328 };
329 
336 
337 
338 
339 #endif // _osaThread_h
340 
#define CISST_EXPORT
Definition: cmnExportMacros.h:50
const char * GetName(void) const
Definition: osaThread.h:291
int PriorityType
PriorityType and SchedulingPolicyType.
Definition: osaThread.h:55
void Create(void *(*threadStart)(void), const char *name=0, int priority=0, int policy=SCHED_FIFO)
Definition: osaThread.h:214
void ToStream(std::ostream &outputStream) const
Portability across compilers and operating systems tools.
#define SCHED_FIFO
Definition: osaThread.h:40
osaThreadId GetId(void) const
Definition: osaThread.h:294
std::ostream & operator<<(std::ostream &output, const osaThreadId &threadId)
Definition: osaThread.h:134
Define a thread object.
Definition: osaThread.h:164
void Wakeup(void)
Definition: osaThread.h:288
bool IsValid(void) const
Definition: osaThread.h:326
CISST_EXPORT osaThreadId osaGetCurrentThreadId(void)
CISST_EXPORT void osaCurrentThreadYield(void)
void WaitForWakeup(void)
Definition: osaThread.h:282
Declaration of osaThreadSignal.
void Create(void *(*threadStart)(_userDataType), _userDataType userData=_userDataType(), const char *name=0, int priority=0, int policy=SCHED_FIFO)
Definition: osaThread.h:224
ThreadId type.
Definition: osaThread.h:77
Macros to export the symbols of cisstOSAbstraction (in a Dll).
int SchedulingPolicyType
SchedulingPolicyType. For now SchedulingPolicy is typedef'ed to int.
Definition: osaThread.h:62
void CreateFromThreadId(const osaThreadId &threadId, const char *name=0, int priority=0, int policy=SCHED_FIFO)
Definition: osaThread.h:262
bool IsRunning(void) const
Definition: osaThread.h:323
void WaitForWakeup(double timeoutInSec)
Definition: osaThread.h:285
Definition: osaThreadSignal.h:40
Defines a thread adapter.
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:237
Definition: osaThreadAdapter.h:85
void CreateFromCurrentThread(const char *name=0, int priority=0, int policy=SCHED_FIFO)
Definition: osaThread.h:253