ddc11f8419a47a89bdaae421cc093762d499afa5
[feisty_meow.git] / nucleus / library / processes / ethread.h
1 #ifndef ETHREAD_CLASS
2 #define ETHREAD_CLASS
3
4 /*****************************************************************************\
5 *                                                                             *
6 *  Name   : ethread (easy thread)                                             *
7 *  Author : Chris Koeritz                                                     *
8 *                                                                             *
9 *******************************************************************************
10 * Copyright (c) 1998-$now By Author.  This program is free software; you can  *
11 * redistribute it and/or modify it under the terms of the GNU General Public  *
12 * License as published by the Free Software Foundation; either version 2 of   *
13 * the License or (at your option) any later version.  This is online at:      *
14 *     http://www.fsf.org/copyleft/gpl.html                                    *
15 * Please send any updates to: fred@gruntose.com                               *
16 \*****************************************************************************/
17
18 #include <basis/contracts.h>
19 #include <timely/time_stamp.h>
20
21 #include <signal.h>
22
23 namespace processes {
24
25 //! Provides a platform-independent object for adding threads to a program.
26 /*!
27   This greatly simplifies creating and managing threads by hiding all the
28   operating system details.  The user just needs to override one virtual
29   function in their derived object to perform the main activity of their
30   thread.  The thread can be a one time invocation or it can run periodically.
31   Control over the thread remains in the hands of the program that started
32   it.
33 */
34
35 class ethread : public virtual basis::root_object
36 {
37 public:
38   ethread();
39     //!< creates a single-shot thread object.
40     /*!< the OS-level thread is not started until the start() method is
41     invoked.  this constructor creates a thread that will only execute
42     once; when start() is called, the thread starts up and performs its
43     activity.  it will then stop.  to run it again, start() must be invoked
44     again.  however, if the perform_activity() method just keeps running,
45     then the single-shot thread can live as long as needed.  it is important
46     for such a thread to periodically check should_exit() to avoid having
47     the program hang-up when it's supposed to be shutting down. */
48
49   enum timed_thread_types { TIGHT_INTERVAL, SLACK_INTERVAL };
50
51   ethread(int sleep_timer, timed_thread_types how = SLACK_INTERVAL);
52     //!< creates a managed thread object that runs on a periodic interval.
53     /*!< the thread will activate every "sleep_timer" milliseconds.  when
54     start() is invoked, the thread's action (via the perform_activity()
55     method) will be performed at regular intervals (using the specified value
56     for "sleep_timer").  the thread will continue activating until the stop()
57     method is called.  a faster interval is used internally during sleep
58     periods such that calling stop() will not consume the whole "sleep_timer"
59     period.  if the "how" is TIGHT_INTERVAL, then the thread will activate
60     every "sleep_timer" milliseconds, as accurately as possible.  if the "how"
61     is SLACK_INTERVAL, then the thread will activate after a delay of
62     "sleep_timer" milliseconds from its last activation.  the latter mode
63     allows the thread to consume its entire intended operation time knowing
64     that there will still be slack time between when it is active.  the
65     former mode requires the thread to only run for some amount of time less
66     than its "sleep_timer"; otherwise it will hog a lot of the CPU. */
67
68   virtual ~ethread();
69
70   DEFINE_CLASS_NAME("ethread");
71
72   bool start(void *thread_data);
73     //!< causes the thread to start, if it has not already been started.
74     /*!< if the thread has terminated previously, then this will restart the
75     thread.  true is returned if the thread could be started.  false is
76     returned if the thread could not be started or if it is already running. */
77
78   void stop();
79     //!< tells the thread to shutdown and waits for the shutdown to occur.
80     /*!< this will cause the OS thread to terminate once the current (if any)
81     perform_activity() invocation completes.  the thread may be restarted
82     with start(). */
83
84   void cancel() { _stop_thread = true; }
85     //!< stops the thread but does not wait until it has terminated.
86     /*!< this is appropriate for use within the perform_activity() method. */
87
88 //  virtual void pre_thread();
89     //!< invoked just after after start(), when the OS thread is created.
90     /*!< the call comes in _from_ the thread itself, so the derived method
91     must be thread-safe. */
92 //  virtual void post_thread();
93     //!< this is invoked just before the thread is to be terminated.
94     /*!< the call also comes in from the thread itself, so the implementation
95     must be thread-safe. */
96
97   virtual void perform_activity(void *thread_data) = 0;
98     //!< carries out the main activity of the thread.
99     /*!< this is called repeatedly by the main thread management function and
100     so should return as soon as possible.  if it does not return fairly
101     regularly, then the thread shutdown process will not occur until the
102     function exits on its own. */
103
104   void exempt_stop();
105     //!< this special form of stop() does not wait for the thread to exit.
106     /*!< it is required in certain weird OS situations where the thread does
107     not exit properly and stop() would cause an infinite wait.  don't use it
108     unless you are SURE that this is the case. */
109
110   void reschedule(int delay = 0);
111     //!< causes a periodic thread to activate after "delay" milliseconds from now.
112     /*!< this resets the normal activation period, but after the next
113     activation occurs, the normal activation interval takes over again. */
114
115   int sleep_time() const { return _sleep_time; }
116     //!< returns the current periodic thread interval.
117     /*!< this is only meaningful for periodic threads. */
118
119   void sleep_time(int new_sleep) { _sleep_time = new_sleep; }
120     //!< adjusts the period for the thread to the "new_sleep" interval.
121     /*!< this is only meaningful for periodic threads. */
122
123   // these functions report on the thread state.
124
125   bool thread_started() const { return _thread_ready; }
126     //!< returns true if the thread has been started.
127     /*!< this does not mean it is necessarily active. */
128
129   bool thread_finished() const { return !_thread_ready; }
130     //!< returns true if the thread has exited.
131     /*!< This can happen either by the thread responding to the stop() or
132     cancel() methods or when the thread stops of its own accord.  if this
133     returns true, it means that the thread will not start up again unless
134     the user invokes start(). */
135
136   bool thread_active() const { return _thread_active; }
137     //!< returns true if the thread is currently performing its activity.
138     /*!< this information is not necessarily relevant even at the point it is
139     returned (because of the nature of multethreading), so don't believe this
140     information for anything important. */
141
142   bool should_stop() const { return _stop_thread; }
143     //!< reports whether the thread should stop right now.
144     /*!< this returns true due to an invocation of stop() or cancel(). */
145
146 private:
147   bool _thread_ready;  //!< is the thread ready to run (or running)?
148   bool _thread_active;  //!< is the thread currently performing?
149   bool _stop_thread;  //!< true if the thread should stop now.
150   void *_data;  //!< holds the thread's link back to whatever.
151 #ifndef _MSC_VER
152   pthread_t *_handle;  //!< thread structure for our thread.
153 #else
154   uintptr_t _handle;  //!< thread handle for the active thread, or zero.
155 #endif
156   int _sleep_time;  //!< threads perform at roughly this interval.
157   bool _periodic;  //!< true if this thread should run repeatedly.
158   timely::time_stamp *_next_activation;  //!< the next time perform_activity is called.
159   timed_thread_types _how;  //!< how is the period evaluated?
160
161   // the OS level thread functions.
162 #ifndef _MSC_VER
163   static void *periodic_thread_driver(void *hidden_pointer);
164   static void *one_shot_thread_driver(void *hidden_pointer);
165 #else
166   static void periodic_thread_driver(void *hidden_pointer);
167   static void one_shot_thread_driver(void *hidden_pointer);
168 #endif
169
170   // forbidden.
171   ethread(const ethread &);
172   ethread &operator =(const ethread &);
173 };
174
175 } //namespace.
176
177 #endif
178