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 
18 extern "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 <kdemacros.h>
33 
34 namespace KPIM {
35 namespace ThreadWeaver {
36 
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 
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,
113  JobAPR
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;
421  int m_active;
427  TQWaitCondition m_jobAvailable;
429  TQWaitCondition m_jobFinished;
437  bool m_running;
442  bool m_suspend;
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
A Job is a simple abstraction of an action that is to be executed in a thread context.
Definition: weaver.h:164
void SPR()
This signal is emitted when the job needs some operation done by the main thread (usually the creator...
virtual void run()=0
The method that actually performs the job.
void APR()
Perform an Asynchronous Process Request.
void started()
This signal is emitted when a thread starts to process a job.
void done()
This signal is emitted when a job has been finished.
The class Thread is used to represent the worker threads in the weaver's inventory.
Definition: weaver.h:250
A weaver is the manager of worker threads (Thread objects) to which it assigns jobs from it's queue.
Definition: weaver.h:297
void jobDone(Job *)
This signal is emitted when a job is done.
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
TQWaitCondition m_jobAvailable
Wait condition all idle or done threads wait for.
Definition: weaver.h:427
int m_inventoryMax
Stored setting .
Definition: weaver.h:425
int m_inventoryMin
Stored setting.
Definition: weaver.h:423
int m_active
The number of jobs that are assigned to the worker threads, but not finished.
Definition: weaver.h:421
TQWaitCondition m_jobFinished
Wait for a job to finish.
Definition: weaver.h:429
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.
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.