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"
A class to represent the events threads generate and send to the Weaver object.
Thread * thread() const
The ID of the sender thread.
static int type()
Return the (custom defined) event type.
Action action() const
The action.
@ Suspended
All jobs in the queue are done.
@ ThreadStarted
Thread queueing halted.
@ JobAPR
Synchronous Process Request.
Job * job() const
The associated job.
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.
virtual ~Job()
Destructor.
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.
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 dequeue()
Remove all queued jobs.
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.
virtual Job * applyForWork(Thread *thread, Job *previous)
Assign a job to the calling thread.
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? The weaver is idle if no jobs are queued and no jobs are processed by the threads...
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.