feisty meow concerns codebase 2.140
ethread.h
Go to the documentation of this file.
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
23namespace processes {
24
26
35class ethread : public virtual basis::root_object
36{
37public:
38 ethread();
40
50
51 ethread(int sleep_timer, timed_thread_types how = SLACK_INTERVAL);
53
68 virtual ~ethread();
69
71
72 bool start(void *thread_data);
74
78 void stop();
80
84 void cancel() { _stop_thread = true; }
86
88// virtual void pre_thread();
90
92// virtual void post_thread();
94
97 virtual void perform_activity(void *thread_data) = 0;
99
104 void exempt_stop();
106
110 void reschedule(int delay = 0);
112
115 int sleep_time() const { return _sleep_time; }
119 void sleep_time(int new_sleep) { _sleep_time = new_sleep; }
121
123 // these functions report on the thread state.
124
125 bool thread_started() const { return _thread_ready; }
127
129 bool thread_finished() const { return !_thread_ready; }
136 bool thread_active() const { return _thread_active; }
138
142 bool should_stop() const { return _stop_thread; }
144
146private:
147 bool _thread_ready;
148 bool _thread_active;
149 bool _stop_thread;
150 void *_data;
151//#ifndef _MSC_VER
152 pthread_t *_handle;
153//#else
154 //uintptr_t _handle; //!< thread handle for the active thread, or zero.
155//#endif
156 int _sleep_time;
157 bool _periodic;
158 timely::time_stamp *_next_activation;
159 timed_thread_types _how;
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
Provides a platform-independent object for adding threads to a program.
Definition ethread.h:36
int sleep_time() const
returns the current periodic thread interval.
Definition ethread.h:114
bool start(void *thread_data)
causes the thread to start, if it has not already been started.
Definition ethread.cpp:145
void cancel()
stops the thread but does not wait until it has terminated.
Definition ethread.h:84
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.
Definition ethread.h:122
DEFINE_CLASS_NAME("ethread")
void exempt_stop()
this special form of stop() does not wait for the thread to exit.
Definition ethread.cpp:213
bool thread_finished() const
returns true if the thread has exited.
Definition ethread.h:125
void stop()
tells the thread to shutdown and waits for the shutdown to occur.
Definition ethread.cpp:194
ethread()
creates a single-shot thread object.
Definition ethread.cpp:86
void reschedule(int delay=0)
causes a periodic thread to activate after "delay" milliseconds from now.
Definition ethread.cpp:140
virtual ~ethread()
Definition ethread.cpp:125
bool thread_active() const
returns true if the thread is currently performing its activity.
Definition ethread.h:131
bool should_stop() const
reports whether the thread should stop right now.
Definition ethread.h:136
Represents a point in time relative to the operating system startup time.
Definition time_stamp.h:38