34 using namespace basis;
44 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), s)
63 class thread_counter :
public virtual root_object {
65 thread_counter() : _count(0) {}
88 : _thread_ready(false),
89 _thread_active(false),
95 _handle(new pthread_t),
102 FUNCDEF(
"constructor [one-shot]");
106 : _thread_ready(false),
107 _thread_active(false),
113 _handle(new pthread_t),
115 _sleep_time(sleep_timer),
120 FUNCDEF(
"constructor [periodic]");
129 WHACK(_next_activation);
151 _stop_thread =
false;
152 _thread_ready =
true;
153 _next_activation->
reset();
154 bool success =
false;
159 pthread_attr_t attribs;
160 int aret = pthread_attr_init(&attribs);
161 if (aret)
LOG(
"failed to init attribs.");
162 aret = pthread_attr_setdetachstate(&attribs, PTHREAD_CREATE_DETACHED);
163 if (aret)
LOG(
"failed to set detach state.");
166 ret = pthread_create(_handle, &attribs, periodic_thread_driver,
169 ret = pthread_create(_handle, &attribs, one_shot_thread_driver,
171 if (!ret) success =
true;
184 LOG(
"failed to create thread; trying again...");
189 LOG(
astring(
"failed to create thread, error is ")
190 + critical_events::system_error_text(error));
212 time_control::sleep_ms(10);
218 _thread_active =
false;
219 _thread_ready =
false;
226 void *ethread::one_shot_thread_driver(
void *hidden_pointer)
233 FUNCDEF(
"one_shot_thread_driver");
241 _current_threads().increment();
244 manager->_thread_active =
true;
249 _current_threads().decrement();
260 void *ethread::periodic_thread_driver(
void *hidden_pointer)
267 FUNCDEF(
"periodic_thread_driver");
275 _current_threads().increment();
279 while (!manager->_stop_thread) {
286 *manager->_next_activation =
time_stamp(manager->_sleep_time);
288 manager->_thread_active =
true;
289 manager->perform_activity(manager->_data);
290 manager->_thread_active =
false;
296 *manager->_next_activation =
time_stamp(manager->_sleep_time);
301 while (!manager->_stop_thread) {
302 int time_diff = int(manager->_next_activation->value()
304 if (time_diff < 0) time_diff = 0;
312 if (!manager->_stop_thread)
313 time_control::sleep_ms(time_diff);
314 if (
time_stamp() >= *manager->_next_activation)
319 manager->exempt_stop();
321 _current_threads().decrement();
Provides a dynamically resizable ASCII character string.
auto_synchronizer simplifies concurrent code by automatically unlocking.
Provides a platform-independent object for adding threads to a program.
bool start(void *thread_data)
causes the thread to start, if it has not already been started.
void cancel()
stops the thread but does not wait until it has terminated.
virtual void perform_activity(void *thread_data)=0
< invoked just after after start(), when the OS thread is created.
bool thread_started() const
returns true if the thread has been started.
void exempt_stop()
this special form of stop() does not wait for the thread to exit.
bool thread_finished() const
returns true if the thread has exited.
void stop()
tells the thread to shutdown and waits for the shutdown to occur.
ethread()
creates a single-shot thread object.
void reschedule(int delay=0)
causes a periodic thread to activate after "delay" milliseconds from now.
Represents a point in time relative to the operating system startup time.
void reset()
sets the stamp time back to now.
#define NULL_POINTER
The value representing a pointer to nothing.
#define DEFINE_CLASS_NAME(objname)
Defines the name of a class by providing a couple standard methods.
#define FUNCDEF(func_in)
FUNCDEF sets the name of a function (and plugs it into the callstack).
The guards collection helps in testing preconditions and reporting errors.
void WHACK(contents *&ptr)
deletion with clearing of the pointer.
A logger that sends to the console screen using the standard output device.
const int SNOOZE_FOR_RETRY
const int MINIMUM_SLEEP_PERIOD
const int MAXIMUM_SLEEP_PERIOD
const int MAXIMUM_CREATE_ATTEMPTS
A dynamic container class that holds any kind of object via pointers.
#define SAFE_STATIC(type, func_name, parms)
Statically defines a singleton object whose scope is the program's lifetime.
Aids in achievement of platform independence.