28#include <tqwaitcondition.h>
35namespace ThreadWeaver {
53 TDE_EXPORT
extern bool Debug;
54 TDE_EXPORT
extern int DebugLevel;
56 TDE_EXPORT
inline void setDebugLevel (
bool debug,
int level)
62 TDE_EXPORT
inline void debug(
int severity,
const char * cformat, ...)
64 __attribute__ ( (format (printf, 2, 3 ) ) )
68 TDE_EXPORT
inline void debug(
int severity,
const char * cformat, ...)
70 if ( Debug ==
true && ( severity<=DebugLevel || severity == 0) )
77 va_start( ap, cformat );
78 vprintf (cformat, ap);
99 class TDE_EXPORT Event :
public TQCustomEvent
123 Action action ()
const;
128 static const int Type;
163 class TDE_EXPORT
Job :
public TQObject
169 Job(TQObject* parent=0,
const char* name=0);
242 TQWaitCondition *m_wc;
249 class TDE_EXPORT
Thread :
public TQThread
273 void msleep(
unsigned long msec);
279 unsigned int id()
const;
287 const unsigned int m_id;
289 static unsigned int sm_Id;
291 static unsigned int makeId();
296 class TDE_EXPORT Weaver :
public TQObject
301 Weaver (TQObject* parent=0,
const char* name=0,
302 int inventoryMin = 4,
303 int inventoryMax = 32);
315 void enqueue (TQPtrList<Job> jobs);
351 virtual void suspend (
bool state);
402 void threadCreated (
Thread *);
403 void threadDestroyed (
Thread *);
404 void threadBusy (
Thread *);
405 void threadSuspended (
Thread *);
414 bool event ( TQEvent* );
A class to represent the events threads generate and send to the Weaver object.
@ Suspended
All jobs in the queue are done.
@ ThreadStarted
Thread queueing halted.
@ JobAPR
Synchronous Process Request.
A Job is a simple abstraction of an action that is to be executed in a thread context.
void triggerSPR()
Trigger a SPR.
virtual void execute(Thread *)
Perform the job.
void unlock()
Unlock this Job's mutex.
Job(TQObject *parent=0, const char *name=0)
Construct a Job object.
void SPR()
This signal is emitted when the job needs some operation done by the main thread (usually the creator...
void lock()
Lock this Job's mutex.
virtual bool isFinished() const
Returns true if the jobs's execute method finished.
virtual void run()=0
The method that actually performs the job.
virtual void processEvent(Event *)
Process events related to this job (created by the processing thread or the weaver or whoever).
void APR()
Perform an Asynchronous Process Request.
void triggerAPR()
Trigger an APR.
void started()
This signal is emitted when a thread starts to process a job.
virtual void setFinished(bool status)
Call with status = true to mark this job as done.
void wakeAPR()
Wake the thread after an APR has been processed.
void done()
This signal is emitted when a job has been finished.
Thread * thread()
Return the thread that executes this job.
The class Thread is used to represent the worker threads in the weaver's inventory.
Thread(Weaver *parent)
Create a thread.
unsigned int id() const
Returns the thread id.
void run()
Overloaded to execute the assigned job.
void post(Event::Action, Job *=0)
Post an event, will be received and processed by the Weaver.
A weaver is the manager of worker threads (Thread objects) to which it assigns jobs from it's queue.
virtual bool dequeue(Job *)
Remove a job from the queue.
void jobDone(Job *)
This signal is emitted when a job is done.
void assignJobs()
Schedule enqueued jobs to be executed by idle threads.
virtual void enqueue(Job *)
Add a job to be executed.
virtual void finish()
Get notified when a thread has finished a job.
bool m_suspend
If m_suspend is true, no new jobs will be assigned to threads.
bool m_running
m_running is set to true when a job is enqueued and set to false when the job finishes that was the l...
int threads() const
Returns the current number of threads in the inventory.
virtual void suspend(bool state)
Suspend job execution if state = true, otherwise resume job execution if it was suspended.
TQWaitCondition m_jobAvailable
Wait condition all idle or done threads wait for.
void lock()
Lock the mutex for this weaver.
void post(Event::Action, Thread *=0, Job *=0)
Post an event that is handled by this object, but in the main (GUI) thread.
int m_inventoryMax
Stored setting .
virtual Job * applyForWork(Thread *thread, Job *previous)
Assign a job to the calling thread.
int m_inventoryMin
Stored setting.
bool event(TQEvent *)
Check incoming events for user defined ones.
int m_active
The number of jobs that are assigned to the worker threads, but not finished.
bool isEmpty() const
Is the queue empty?
TQWaitCondition m_jobFinished
Wait for a job to finish.
bool isIdle() const
Is the weaver idle?
bool m_shuttingDown
Indicates if the weaver is shutting down and exiting it's threads.
TQPtrList< Job > m_assignments
The job queue.
void finished()
This signal is emitted when the Weaver has finished ALL currently queued jobs.
int queueLength()
Returns the number of pending jobs.
TQPtrList< Thread > m_inventory
The thread inventory.
void suspended()
Thread queueing has been suspended.
TDEPIM classes for drag and drop of mails.