libtdepim

weaver.h
1/*
2 This file declares the Weaver, Job and Thread classes.
3
4 $ Author: Mirko Boehm $
5 $ Copyright: (C) 2004, Mirko Boehm $
6 $ Contact: mirko@kde.org
7 http://www.kde.org
8 http://www.hackerbuero.org $
9 $ License: LGPL with the following explicit clarification:
10 This code may be linked against any version of the TQt toolkit
11 from Troll Tech, Norway. $
12
13*/
14
15#ifndef WEAVER_H
16#define WEAVER_H
17
18extern "C"
19{
20#include <stdarg.h>
21#include <unistd.h>
22#include <stdio.h>
23}
24
25#include <tqobject.h>
26#include <tqptrlist.h>
27#include <tqthread.h>
28#include <tqwaitcondition.h>
29#include <tqmutex.h>
30#include <tqevent.h>
31
32#include <tdemacros.h>
33
34namespace KPIM {
35namespace ThreadWeaver {
36
52
53 TDE_EXPORT extern bool Debug;
54 TDE_EXPORT extern int DebugLevel;
55
56 TDE_EXPORT inline void setDebugLevel (bool debug, int level)
57 {
58 Debug = debug;
59 DebugLevel = level;
60 }
61
62 TDE_EXPORT inline void debug(int severity, const char * cformat, ...)
63#ifdef __GNUC__
64 __attribute__ ( (format (printf, 2, 3 ) ) )
65#endif
66;
67
68 TDE_EXPORT inline void debug(int severity, const char * cformat, ...)
69 {
70 if ( Debug == true && ( severity<=DebugLevel || severity == 0) )
71 {
72 static TQMutex mutex;
73 TQString text;
74
75 mutex.lock();
76 va_list ap;
77 va_start( ap, cformat );
78 vprintf (cformat, ap);
79 va_end (ap);
80 mutex.unlock();
81 }
82 }
83
84
85 class Thread;
86 class Job;
87
98
99 class TDE_EXPORT Event : public TQCustomEvent
100 {
101 public:
102 enum Action {
103 NoAction = 0,
104 Finished,
107 ThreadExiting,
108 ThreadBusy,
109 ThreadSuspended,
110 JobStarted,
111 JobFinished,
112 JobSPR,
114 };
115 Event ( Action = NoAction, Thread * = 0, Job *job = 0);
117 static int type ();
119 Thread* thread () const;
121 Job* job () const;
123 Action action () const;
124 private:
125 Action m_action;
126 Thread *m_thread;
127 Job *m_job;
128 static const int Type;
129 };
130
163 class TDE_EXPORT Job : public TQObject
164 {
165 TQ_OBJECT
166
167 public:
169 Job(TQObject* parent=0, const char* name=0);
170
172 virtual ~Job();
173
178 virtual void execute(Thread*);
179
181 virtual bool isFinished() const;
182
184 void wakeAPR ();
185
188 virtual void processEvent ( Event* );
189
190 signals:
192 void started ();
194 void done ();
207 void SPR ();
210 void APR ();
211 protected:
213 void lock();
215 void unlock();
219 virtual void run () = 0;
222 Thread *thread();
224 virtual void setFinished(bool status);
228 void triggerSPR ();
234 void triggerAPR ();
235
236 bool m_finished;
237
238 TQMutex *m_mutex;
239
240 Thread * m_thread;
241
242 TQWaitCondition *m_wc;
243 };
244
245 class Weaver;
246
249 class TDE_EXPORT Thread : public TQThread
250 {
251 public:
255 Thread(Weaver *parent);
256
258 ~Thread();
259
269 void run();
270
271 /* Provide the msleep() method (protected in TQThread) to be
272 available for executed jobs. */
273 void msleep(unsigned long msec);
274
279 unsigned int id() const;
280
282 void post (Event::Action, Job* = 0);
283
284 private:
285 Weaver *m_parent;
286
287 const unsigned int m_id;
288
289 static unsigned int sm_Id;
290
291 static unsigned int makeId();
292 };
293
296 class TDE_EXPORT Weaver : public TQObject
297 {
298 TQ_OBJECT
299
300 public:
301 Weaver (TQObject* parent=0, const char* name=0,
302 int inventoryMin = 4, // minimal number of provided threads
303 int inventoryMax = 32); // maximum number of provided threads
304 virtual ~Weaver ();
306 virtual void enqueue (Job*);
315 void enqueue (TQPtrList<Job> jobs);
325 virtual bool dequeue (Job*);
329 virtual void dequeue ();
332 // virtual void jobFinished(Thread *);
340 virtual void finish();
351 virtual void suspend (bool state);
353 bool isEmpty () const;
357 bool isIdle () const;
359 int queueLength ();
370 virtual Job* applyForWork (Thread *thread, Job *previous);
374 void lock ();
376 void unlock ();
381 void post (Event::Action, Thread* = 0, Job* = 0);
383 int threads () const;
384 signals:
391 void finished ();
396 void suspended ();
400 void jobDone (Job*);
401// The following signals are used mainly for debugging purposes.
402 void threadCreated (Thread *);
403 void threadDestroyed (Thread *);
404 void threadBusy (Thread *);
405 void threadSuspended (Thread *);
406
407 protected:
411 void assignJobs();
414 bool event ( TQEvent* );
416 TQPtrList<Thread> m_inventory;
418 TQPtrList<Job> m_assignments;
427 TQWaitCondition m_jobAvailable;
429 TQWaitCondition m_jobFinished;
443 private:
445 TQMutex *m_mutex;
446 };
447} // namespace ThreadWeaver
448} // namespace KPIM
449
450#endif // defined WEAVER_H
A class to represent the events threads generate and send to the Weaver object.
Definition weaver.h:100
@ Suspended
All jobs in the queue are done.
Definition weaver.h:105
@ ThreadStarted
Thread queueing halted.
Definition weaver.h:106
@ JobAPR
Synchronous Process Request.
Definition weaver.h:113
A Job is a simple abstraction of an action that is to be executed in a thread context.
Definition weaver.h:164
void triggerSPR()
Trigger a SPR.
Definition weaver.cpp:107
virtual void execute(Thread *)
Perform the job.
Definition weaver.cpp:52
void unlock()
Unlock this Job's mutex.
Definition weaver.cpp:47
Job(TQObject *parent=0, const char *name=0)
Construct a Job object.
Definition weaver.cpp:30
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.
Definition weaver.cpp:42
virtual bool isFinished() const
Returns true if the jobs's execute method finished.
Definition weaver.cpp:72
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).
Definition weaver.cpp:84
void APR()
Perform an Asynchronous Process Request.
void triggerAPR()
Trigger an APR.
Definition weaver.cpp:122
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.
Definition weaver.cpp:78
void wakeAPR()
Wake the thread after an APR has been processed.
Definition weaver.cpp:132
void done()
This signal is emitted when a job has been finished.
Thread * thread()
Return the thread that executes this job.
Definition weaver.cpp:66
The class Thread is used to represent the worker threads in the weaver's inventory.
Definition weaver.h:250
Thread(Weaver *parent)
Create a thread.
Definition weaver.cpp:180
unsigned int id() const
Returns the thread id.
Definition weaver.cpp:199
void run()
Overloaded to execute the assigned job.
Definition weaver.cpp:204
void post(Event::Action, Job *=0)
Post an event, will be received and processed by the Weaver.
Definition weaver.cpp:229
A weaver is the manager of worker threads (Thread objects) to which it assigns jobs from it's queue.
Definition weaver.h:297
virtual bool dequeue(Job *)
Remove a job from the queue.
Definition weaver.cpp:352
void jobDone(Job *)
This signal is emitted when a job is done.
void assignJobs()
Schedule enqueued jobs to be executed by idle threads.
Definition weaver.cpp:386
virtual void enqueue(Job *)
Add a job to be executed.
Definition weaver.cpp:326
virtual void finish()
Get notified when a thread has finished a job.
Definition weaver.cpp:536
bool m_suspend
If m_suspend is true, no new jobs will be assigned to threads.
Definition weaver.h:442
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...
Definition weaver.h:437
int threads() const
Returns the current number of threads in the inventory.
Definition weaver.cpp:320
virtual void suspend(bool state)
Suspend job execution if state = true, otherwise resume job execution if it was suspended.
Definition weaver.cpp:364
TQWaitCondition m_jobAvailable
Wait condition all idle or done threads wait for.
Definition weaver.h:427
void lock()
Lock the mutex for this weaver.
Definition weaver.cpp:305
void post(Event::Action, Thread *=0, Job *=0)
Post an event that is handled by this object, but in the main (GUI) thread.
Definition weaver.cpp:444
int m_inventoryMax
Stored setting .
Definition weaver.h:425
virtual Job * applyForWork(Thread *thread, Job *previous)
Assign a job to the calling thread.
Definition weaver.cpp:456
int m_inventoryMin
Stored setting.
Definition weaver.h:423
bool event(TQEvent *)
Check incoming events for user defined ones.
Definition weaver.cpp:391
int m_active
The number of jobs that are assigned to the worker threads, but not finished.
Definition weaver.h:421
bool isEmpty() const
Is the queue empty?
Definition weaver.cpp:450
TQWaitCondition m_jobFinished
Wait for a job to finish.
Definition weaver.h:429
bool isIdle() const
Is the weaver idle?
Definition weaver.cpp:530
bool m_shuttingDown
Indicates if the weaver is shutting down and exiting it's threads.
Definition weaver.h:432
TQPtrList< Job > m_assignments
The job queue.
Definition weaver.h:418
void finished()
This signal is emitted when the Weaver has finished ALL currently queued jobs.
int queueLength()
Returns the number of pending jobs.
Definition weaver.cpp:524
TQPtrList< Thread > m_inventory
The thread inventory.
Definition weaver.h:416
void suspended()
Thread queueing has been suspended.
TDEPIM classes for drag and drop of mails.