20 #include <tqapplication.h>
25 namespace ThreadWeaver {
31 : TQObject (parent, name),
33 m_mutex (new TQMutex (true) ),
68 TQMutexLocker l (m_mutex);
74 TQMutexLocker l (m_mutex);
80 TQMutexLocker l (m_mutex);
88 case Event::JobStarted:
91 case Event::JobFinished:
110 m_wc =
new TQWaitCondition;
113 thread()->
post (KPIM::ThreadWeaver::Event::JobSPR,
this);
125 m_wc =
new TQWaitCondition;
134 TQMutexLocker l(m_mutex);
143 const int Event::Type = TQEvent::User + 1000;
145 Event::Event ( Action action,
Thread *thread,
Job *job)
146 : TQCustomEvent ( type () ),
178 unsigned int Thread::sm_Id;
191 unsigned int Thread::makeId()
193 static TQMutex mutex;
194 TQMutexLocker l (&mutex);
212 debug ( 3,
"Thread::run [%u]: trying to execute the next job.\n",
id() );
220 post ( Event::JobStarted, job );
222 post ( Event::JobFinished, job );
226 post ( Event::ThreadExiting );
231 m_parent->
post ( a,
this, j);
234 void Thread::msleep(
unsigned long msec)
236 TQThread::msleep(msec);
239 Weaver::Weaver(TQObject* parent,
const char* name,
240 int inventoryMin,
int inventoryMax)
241 : TQObject(parent, name),
243 m_inventoryMin(inventoryMin),
244 m_inventoryMax(inventoryMax),
245 m_shuttingDown(false),
248 m_mutex ( new TQMutex(true) )
252 for (
int count = 0; count < m_inventoryMin; ++count)
255 m_inventory.append(th);
259 emit (threadCreated (th) );
269 debug ( 1,
"Weaver dtor: destroying inventory.\n" );
286 if ( !th->finished() )
292 emit (threadDestroyed (th) );
301 debug ( 1,
"Weaver dtor: done\n" );
307 debug ( 3 ,
"Weaver::lock: lock (mutex is %s).\n",
308 ( m_mutex->locked() ?
"locked" :
"not locked" ) );
316 debug ( 3 ,
"Weaver::unlock: unlock (mutex is %s).\n",
317 ( m_mutex->locked() ?
"locked" :
"not locked" ) );
322 TQMutexLocker l (m_mutex);
342 for (
Job * job = jobs.first(); job; job = jobs.next() )
354 TQMutexLocker l (m_mutex);
360 TQMutexLocker l (m_mutex);
380 debug (2,
"Weaver::suspend: queueing resumed.\n" );
393 if ( e->type() >= TQEvent::User )
400 switch (
event->action() )
402 case Event::JobFinished:
403 if (
event->job() !=0 )
408 case Event::Finished:
414 case Event::ThreadSuspended:
417 emit (threadSuspended (
event->thread() ) );
420 case Event::ThreadBusy:
423 emit (threadBusy (
event->thread() ) );
430 if (
event->job() !=0 )
432 event->job()->processEvent (
event);
435 debug ( 0,
"Weaver::event: Strange: received unknown user event.\n" );
440 return TQObject::event ( e );
447 TQApplication::postEvent (
this, e);
452 TQMutexLocker l (m_mutex);
459 bool lastjob =
false;
470 debug ( 3,
"Weaver::applyForWork: job done, %i jobs left, "
471 "%i active jobs left.\n",
478 post (Event::Finished);
479 debug ( 3,
"Weaver::applyForWork: last job.\n" );
486 debug ( 2,
"Weaver::applyForWork: queueing suspended.\n" );
506 debug ( 3,
"Weaver::applyForWork: job assigned, "
507 "%i jobs in queue (%i active).\n",
511 post (Event::ThreadBusy, th);
517 post (Event::ThreadSuspended, th);
526 TQMutexLocker l (m_mutex);
532 TQMutexLocker l (m_mutex);
540 debug (2,
"Weaver::finish: not done, waiting.\n" );
543 debug (1,
"Weaver::finish: done.\n\n\n" );
549 #include "weaver.moc"
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_active
The number of jobs that are assigned to the worker threads, but not finished.
int queueLength()
Returns the number of pending jobs.
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...
TQWaitCondition m_jobFinished
Wait for a job to finish.
The class Thread is used to represent the worker threads in the weaver's inventory.
virtual void dequeue()
Remove all queued jobs.
Thread(Weaver *parent)
Create a thread.
A class to represent the events threads generate and send to the Weaver object.
virtual ~Job()
Destructor.
virtual void processEvent(Event *)
Process events related to this job (created by the processing thread or the weaver or whoever).
Job(TQObject *parent=0, const char *name=0)
Construct a Job object.
void jobDone(Job *)
This signal is emitted when a job is done.
void triggerSPR()
Trigger a SPR.
void suspended()
Thread queueing has been suspended.
virtual void execute(Thread *)
Perform the job.
@ JobAPR
Synchronous Process Request.
bool isEmpty() const
Is the queue empty?
virtual void finish()
Get notified when a thread has finished a job.
void assignJobs()
Schedule enqueued jobs to be executed by idle threads.
void done()
This signal is emitted when a job has been finished.
Job * job() const
The associated job.
void unlock()
Unlock this Job's mutex.
virtual void suspend(bool state)
Suspend job execution if state = true, otherwise resume job execution if it was suspended.
void run()
Overloaded to execute the assigned job.
@ Suspended
All jobs in the queue are done.
void finished()
This signal is emitted when the Weaver has finished ALL currently queued jobs.
void lock()
Lock this Job's mutex.
virtual Job * applyForWork(Thread *thread, Job *previous)
Assign a job to the calling thread.
TQWaitCondition m_jobAvailable
Wait condition all idle or done threads wait for.
void triggerAPR()
Trigger an APR.
@ ThreadStarted
Thread queueing halted.
virtual bool isFinished() const
Returns true if the jobs's execute method finished.
unsigned int id() const
Returns the thread id.
bool m_shuttingDown
Indicates if the weaver is shutting down and exiting it's threads.
TQPtrList< Thread > m_inventory
The thread inventory.
void APR()
Perform an Asynchronous Process Request.
Thread * thread()
Return the thread that executes this job.
static int type()
Return the (custom defined) event type.
int threads() const
Returns the current number of threads in the inventory.
void started()
This signal is emitted when a thread starts to process a job.
bool isIdle() const
Is the weaver idle? The weaver is idle if no jobs are queued and no jobs are processed by the threads...
virtual void run()=0
The method that actually performs the job.
void SPR()
This signal is emitted when the job needs some operation done by the main thread (usually the creator...
TDEPIM classes for drag and drop of mails.
TQPtrList< Job > m_assignments
The job queue.
void wakeAPR()
Wake the thread after an APR has been processed.
Action action() const
The action.
A weaver is the manager of worker threads (Thread objects) to which it assigns jobs from it's queue.
virtual void enqueue(Job *)
Add a job to be executed.
A Job is a simple abstraction of an action that is to be executed in a thread context.
bool event(TQEvent *)
Check incoming events for user defined ones.
void lock()
Lock the mutex for this weaver.
bool m_suspend
If m_suspend is true, no new jobs will be assigned to threads.
void post(Event::Action, Job *=0)
Post an event, will be received and processed by the Weaver.
virtual void setFinished(bool status)
Call with status = true to mark this job as done.
Thread * thread() const
The ID of the sender thread.