1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                   TASK.H                                  */
4 /*                                                                           */
5 /*                      (C) 1996 MU Softwareentwicklung                      */
6 /*                                                                           */
7 /*      Ullrich von Bassewitz                          Michael Peschel       */
8 /*      Wacholderweg 14                                   Ledergasse 3       */
9 /*      D-70597 Stuttgart                            D-72555 Metzingen       */
10 /*      uz@ibb.schwaben.com                      mipe@ibb.schwaben.com       */
11 /*                                                                           */
12 /*****************************************************************************/
13 
14 
15 
16 // Task (thread) object. Currently implemented for OS/2 and NT. There are some
17 // quirks when working together with the rest of the spunk library, but the
18 // object has shonw to be too useful to omit it.
19 
20 
21 
22 #ifndef _TASK_H
23 #define _TASK_H
24 
25 
26 
27 #include "object.h"
28 
29 
30 
31 /*****************************************************************************/
32 /*                                   Data                                    */
33 /*****************************************************************************/
34 
35 
36 
37 // Priority class values
38 const unsigned PrioIdle         = 0;    // Run as idle thread
39 const unsigned PrioRegular      = 1;    // Run with regular priority
40 const unsigned PrioServer       = 2;    // Run as server thread
41 const unsigned PrioTimeCritical = 3;    // Run with time critical priority
42 
43 
44 
45 /*****************************************************************************/
46 /*                                class Task                                 */
47 /*****************************************************************************/
48 
49 
50 
51 class TaskHandle;
52 class Task: public Object {
53 
54 protected:
55     size_t      StackSize;
56     int         Running;
57     int         RunDown;
58     TaskHandle* Handle;
59 
60 private:
61     static void CreateFunc (void*);
62     // This function is started as a thread
63 
64     virtual void Init ();
65     // This function is called before Run()
66 
67     virtual void Run () = 0;
68     // This is the real "worker function" of the task
69 
70     virtual void Done ();
71     // This function is called after Run()
72 
73 public:
74     Task (size_t aStackSize = 0x8000);
75     // Constructor. The Stacksize value may be honored or not.
76 
77     virtual ~Task ();
78     // Destruktor
79 
80     virtual void Start ();
81     // Start the task
82 
83     virtual void Stop (unsigned WaitTime = 2000);
84     // Stop the task. WaitTime is the time, the routine waits for the thread
85     // to terminate after the RunDown flag has been set. The Run() function
86     // should poll this flag in regular intervalls. If the thread does not
87     // recognize this flag, termination is forced. BEWARE: Forced termination
88     // of a thread from outside is inherently dangerous, since the thread may
89     // have no chance to cleanup resources.
90     // The effects of this function differ from one supported operating
91     // system to another, when the thread does not honor the RunDown flag and
92     // must be killed: Under OS/2, the thread will catch the termination,
93     // execute the Done() function and terminate itself. Under NT, the thread
94     // will be killed immidiately without any chance for a cleanup.
95 
96     int IsRunning () const;
97     // Return 1 if the task is currently running
98 
99     virtual void SetPriority (unsigned Priority, int Delta = 0);
100     // Allows to set the priority of the task. BEWARE: Setting the priority
101     // from outside the thread may not be possible from each supported
102     // operating system.
103 
104 };
105 
106 
107 
IsRunning()108 inline int Task::IsRunning () const
109 // Return 1 if the task is currently running
110 {
111     return Running;
112 }
113 
114 
115 
116 // End of TASK.H
117 
118 #endif
119