sipxportlib  Version 3.3
OsTaskWnt.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2006-2013 SIPez LLC. All rights reserved.
3 //
4 // Copyright (C) 2004-2006 SIPfoundry Inc.
5 // Licensed by SIPfoundry under the LGPL license.
6 //
7 // Copyright (C) 2004-2006 Pingtel Corp. All rights reserved.
8 // Licensed to SIPfoundry under a Contributor Agreement.
9 //
10 // $$
12 
13 
14 #ifndef _OsTaskWnt_h_
15 #define _OsTaskWnt_h_
16 
17 // SYSTEM INCLUDES
18 #define WIN32_LEAN_AND_MEAN
19 #include <windows.h>
20 
21 // APPLICATION INCLUDES
22 #include "utl/UtlDefs.h"
23 #include "os/OsDefs.h"
24 #include "os/OsMutex.h"
25 #include "os/OsRWMutex.h"
26 #include "os/OsStatus.h"
27 #include "os/OsTask.h"
28 
29 // DEFINES
30 // MACROS
31 // EXTERNAL FUNCTIONS
32 // EXTERNAL VARIABLES
33 // CONSTANTS
34 // STRUCTS
35 // TYPEDEFS
36 // FORWARD DECLARATIONS
37 class OsTime;
38 class UtlString;
39 
40 //:Task abstraction for Windows NT
41 // A task represents a thread of execution. All tasks run within the same
42 // address space but have their own stack and program counter. Tasks may be
43 // created and deleted dynamically.
44 //
45 // Users create tasks by:
46 // 1) Deriving a new class based on OsTask or one of its descendants,
47 // and overriding the run() method in the derived class.
48 // 2) Calling the constructor for the derived class.
49 // 3) Invoking the start() method for the derived class. This creates the
50 // corresponding low-level OS task and associates it with the class.
51 //
52 // Note: Many of the methods in this class are only applicable once the
53 // start() method for the object has been called and the corresponding
54 // low-level task has been created. Accordingly, before a successful call
55 // to start(), most of the methods in this class return the
56 // OS_TASK_NOT_STARTED status.
57 class OsTaskWnt : public OsTaskBase
58 {
59 /* //////////////////////////// PUBLIC //////////////////////////////////// */
60 public:
61 
62 /* ============================ CREATORS ================================== */
63 
64  OsTaskWnt(const UtlString& name="",
65  void* pArg=NULL,
66  const int priority=DEF_PRIO,
67  const int options=DEF_OPTIONS,
68  const int stackSize=DEF_STACKSIZE);
69  //:Constructor
70  // For Windows NT, the "options" parameter is ignored.
71 
72  virtual
73  ~OsTaskWnt();
74  //:Destructor -- delete the task
75 
76  virtual OsStatus deleteForce(void);
77  //:Delete the task even if the task is protected from deletion
78  // After calling this method, the user will still need to delete the
79  // corresponding OsTask object to reclaim its storage.
80 
81 /* ============================ MANIPULATORS ============================== */
82 
83  virtual UtlBoolean restart(void);
84  //:Restart the task
85  // The task is first terminated, and then reinitialized with the same
86  // name, priority, options, stack size, original entry point, and
87  // parameters it had when it was terminated.
88  // Return TRUE if the restart of the task is successful.
89 
90  virtual OsStatus resume(void);
91  //:Resume the task
92  // This routine resumes the task. The task suspension is cleared, and
93  // the task operates in the remaining state.
94 
95  virtual UtlBoolean start(void);
96  //:Spawn a new task and invoke its run() method.
97  // Return TRUE if the spawning of the new task is successful.
98  // Return FALSE if the task spawn fails or if the task has already
99  // been started.
100 
101  virtual OsStatus suspend(void);
102  //:Suspend the task
103  // This routine suspends the task. Suspension is additive: thus, tasks
104  // can be delayed and suspended, or pended and suspended. Suspended,
105  // delayed tasks whose delays expire remain suspended. Likewise,
106  // suspended, pended tasks that unblock remain suspended only.
107 
108  virtual OsStatus setErrno(int errno);
109  //:Set the errno status for the task
110  // This call has no effect under Windows NT and, if the task has been
111  // started, will always returns OS_SUCCESS
112 
113  virtual OsStatus setOptions(int options);
114  //:Set the execution options for the task
115  // The only option that can be changed after a task has been created
116  // is whether to allow breakpoint debugging.
117  // This call has no effect under Windows NT
118 
119  virtual OsStatus setPriority(int priority);
120  //:Set the priority of the task
121  // Priorities range from 0, the highest priority, to 255, the lowest
122  // priority.
123 
124  virtual OsStatus varAdd(int* pVar);
125  //:Add a task variable to the task
126  // This routine adds a specified variable pVar (4-byte memory
127  // location) to its task's context. After calling this routine, the
128  // variable is private to the task. The task can access and modify
129  // the variable, but the modifications are not visible to other tasks,
130  // and other tasks' modifications to that variable do not affect the
131  // value seen by the task. This is accomplished by saving and restoring
132  // the variable's value each time a task switch occurs to or from the
133  // calling task.
134 
135  virtual OsStatus varDelete(int* pVar);
136  //:Remove a task variable from the task
137  // This routine removes a specified task variable, pVar, from its
138  // task's context. The private value of that variable is lost.
139 
140  virtual OsStatus varSet(int* pVar, int value);
141  //:Set the value of a private task variable
142  // This routine sets the private value of the task variable for a
143  // specified task. The specified task is usually not the calling task,
144  // which can set its private value by directly modifying the variable.
145  // This routine is provided primarily for debugging purposes.
146 
147  static OsStatus delay(const int milliSecs);
148  //:Delay a task from executing for the specified number of milliseconds
149  // This routine causes the calling task to relinquish the CPU for the
150  // duration specified. This is commonly referred to as manual
151  // rescheduling, but it is also useful when waiting for some external
152  // condition that does not have an interrupt associated with it.
153 
154  static OsStatus safe(void);
155  //:Make the calling task safe from deletion
156  // This routine protects the calling task from deletion. Tasks that
157  // attempt to delete a protected task will block until the task is
158  // made unsafe, using unsafe(). When a task becomes unsafe, the
159  // deleter will be unblocked and allowed to delete the task.
160  // The safe() primitive utilizes a count to keep track of
161  // nested calls for task protection. When nesting occurs,
162  // the task becomes unsafe only after the outermost unsafe()
163  // is executed.
164 
165  static OsStatus unsafe(void);
166  //:Make the calling task unsafe from deletion
167  // This routine removes the calling task's protection from deletion.
168  // Tasks that attempt to delete a protected task will block until the
169  // task is unsafe. When a task becomes unsafe, the deleter will be
170  // unblocked and allowed to delete the task.
171  // The unsafe() primitive utilizes a count to keep track of nested
172  // calls for task protection. When nesting occurs, the task becomes
173  // unsafe only after the outermost unsafe() is executed.
174 
175  static void yield(void);
176  //:Yield the CPU if a task of equal or higher priority is ready to run.
177 
178 /* ============================ ACCESSORS ================================= */
179 
180  static OsTaskWnt* getCurrentTask(void);
181  //:Return a pointer to the OsTask object for the currently executing task
182  // Return NULL if none exists.
183 
184  static OsStatus getCurrentTaskId(int &rid);
185  //:Return an Id of the currently executing task
186 
187  static OsTaskWnt* getTaskByName(const UtlString& taskName);
188  //:Return a pointer to the OsTask object corresponding to the named task
189  // Return NULL if there is no task object with that name.
190 
191  static OsTaskWnt* getTaskById(const int taskId);
192  //:Return a pointer to the OsTask object corresponding to taskId
193  // Return NULL is there is no task object with that id.
194 
195  virtual OsStatus getErrno(int& rErrno);
196  //:Get the errno status for the task
197  // Under Windows NT, the rErrno value will always be 0.
198 
199  virtual int getOptions(void);
200  //:Return the execution options for the task
201 
202  virtual OsStatus getPriority(int& rPriority);
203  //:Return the priority of the task
204 
205  virtual OsStatus varGet(void);
206  //:Get the value of a task variable
207  // This routine returns the private value of a task variable for its
208  // task. The task is usually not the calling task, which can get its
209  // private value by directly accessing the variable. This routine is
210  // provided primarily for debugging purposes.
211 
212  static void getIdString(UtlString&, DWORD);
213  //:See the linux version...
214 
215 /* ============================ INQUIRY =================================== */
216 
217  virtual OsStatus id(OsTaskId_t& rId);
218  //:Get the task ID for this task
219 
220  virtual UtlBoolean isSuspended(void);
221  //:Check if the task is suspended
222  // Return TRUE is the task is suspended, otherwise FALSE.
223 
224 /* //////////////////////////// PROTECTED ///////////////////////////////// */
225 protected:
226 
227  virtual int run(void* pArg) = 0;
228  //:The entry point for the task.
229  // Derive new tasks as subclasses of OsTask, overriding this method.
230 
231 /* //////////////////////////// PRIVATE /////////////////////////////////// */
232 private:
233  OsRWMutex mDeleteGuard; // RWMutex guard to prevent unwanted task deletion
234  int mSuspendCnt; // Counts the nesting level of taskSuspend() calls
235  HANDLE mThreadH; // Windows NT thread handle
236  int mThreadId; // Windows NT unique ID for thread
237 
238  // saved initialization information (used for task restarts)
239  int mOptions;
240  int mPriority;
241  int mStackSize;
242 
243  UtlBoolean doWntCreateTask(void);
244  //:Do the real work associated with creating a new WinNT task
245  // The mDataGuard lock should be held upon entry into this method.
246 
247  void doWntTerminateTask(UtlBoolean doForce);
248  //:Do the real work associated with terminating a WinNT task
249  // The mDataGuard lock should be held upon entry into this method.
250 
251  static unsigned int __stdcall threadEntry(LPVOID arg);
252  //:Function that serves as the starting address for a Windows thread
253 
254  OsTaskWnt(const OsTaskWnt& rOsTaskWnt);
255  //:Copy constructor (not implemented for this class)
256 
257  OsTaskWnt& operator=(const OsTaskWnt& rhs);
258  //:Assignment operator (not implemented for this class)
259 
260 };
261 
262 /* ============================ INLINE METHODS ============================ */
263 
264 #endif // _OsTaskWnt_h_
265 
OsTaskWnt(const UtlString &name="", void *pArg=NULL, const int priority=DEF_PRIO, const int options=DEF_OPTIONS, const int stackSize=DEF_STACKSIZE)
Definition: OsTaskWnt.cpp:40
static OsStatus safe(void)
Definition: OsTaskWnt.cpp:260
static void getIdString(UtlString &, DWORD)
virtual OsStatus getErrno(int &rErrno)
Definition: OsTaskWnt.cpp:373
virtual ~OsTaskWnt()
Definition: OsTaskWnt.cpp:58
static OsStatus getCurrentTaskId(int &rid)
Definition: OsTaskWnt.cpp:321
static OsTaskWnt * getCurrentTask(void)
Definition: OsTaskWnt.cpp:302
virtual OsStatus varGet(void)
Definition: OsTaskWnt.cpp:408
virtual OsStatus varDelete(int *pVar)
Definition: OsTaskWnt.cpp:217
virtual UtlBoolean isSuspended(void)
Definition: OsTaskWnt.cpp:438
static OsStatus delay(const int milliSecs)
Definition: OsTaskWnt.cpp:243
static void yield(void)
Definition: OsTaskWnt.cpp:293
virtual OsStatus id(OsTaskId_t &rId)
Definition: OsTaskWnt.cpp:419
OsStatus
Definition: OsStatus.h:27
#define NULL
Definition: UtlDefs.h:29
static const int DEF_PRIO
Definition: OsTask.h:65
static OsStatus unsafe(void)
Definition: OsTaskWnt.cpp:280
Definition: OsTask.h:59
virtual OsStatus resume(void)
Definition: OsTaskWnt.cpp:99
virtual UtlBoolean start(void)
Definition: OsTaskWnt.cpp:122
virtual int getOptions(void)
Definition: OsTaskWnt.cpp:384
Definition: UtlString.h:48
virtual int run(void *pArg)=0
int errno
virtual OsStatus setErrno(int errno)
Definition: OsTaskWnt.cpp:159
static const int DEF_OPTIONS
Definition: OsTask.h:64
virtual OsStatus varAdd(int *pVar)
Definition: OsTaskWnt.cpp:206
static OsTaskWnt * getTaskByName(const UtlString &taskName)
Definition: OsTaskWnt.cpp:329
Definition: OsTime.h:37
virtual OsStatus deleteForce(void)
enumcode: UNINITIALIZED - no low-level task, no name DB entries enumcode: STARTED - low-level task an...
Definition: OsTaskWnt.cpp:70
int UtlBoolean
Definition: UtlDefs.h:41
static OsTaskWnt * getTaskById(const int taskId)
Definition: OsTaskWnt.cpp:348
static const int DEF_STACKSIZE
Definition: OsTask.h:66
virtual OsStatus varSet(int *pVar, int value)
Definition: OsTaskWnt.cpp:230
Definition: OsTaskWnt.h:57
virtual OsStatus setOptions(int options)
Definition: OsTaskWnt.cpp:171
virtual OsStatus getPriority(int &rPriority)
Definition: OsTaskWnt.cpp:390
virtual OsStatus setPriority(int priority)
Definition: OsTaskWnt.cpp:178
virtual UtlBoolean restart(void)
Definition: OsTaskWnt.cpp:88
virtual OsStatus suspend(void)
Definition: OsTaskWnt.cpp:137