21 #include <tqapplication.h>
26 namespace ThreadWeaver {
32 : TQObject (parent, name),
34 m_mutex (new TQMutex (true) ),
69 TQMutexLocker l (m_mutex);
75 TQMutexLocker l (m_mutex);
81 TQMutexLocker l (m_mutex);
89 case Event::JobStarted:
92 case Event::JobFinished:
111 m_wc =
new TQWaitCondition;
114 thread()->
post (KPIM::ThreadWeaver::Event::JobSPR,
this);
126 m_wc =
new TQWaitCondition;
135 TQMutexLocker l(m_mutex);
144 const int Event::Type = TQEvent::User + 1000;
146 Event::Event ( Action action,
Thread *thread,
Job *job)
147 : TQCustomEvent ( type () ),
179 unsigned int Thread::sm_Id;
192 unsigned int Thread::makeId()
194 static TQMutex mutex;
195 TQMutexLocker l (&mutex);
213 debug ( 3,
"Thread::run [%u]: trying to execute the next job.\n",
id() );
221 post ( Event::JobStarted, job );
223 post ( Event::JobFinished, job );
227 post ( Event::ThreadExiting );
232 m_parent->
post ( a,
this, j);
235 void Thread::msleep(
unsigned long msec)
237 TQThread::msleep(msec);
240 Weaver::Weaver(TQObject* parent,
const char* name,
241 int inventoryMin,
int inventoryMax)
242 : TQObject(parent, name),
244 m_inventoryMin(inventoryMin),
245 m_inventoryMax(inventoryMax),
246 m_shuttingDown(false),
249 m_mutex ( new TQMutex(true) )
253 for (
int count = 0; count < m_inventoryMin; ++count)
256 m_inventory.append(th);
260 emit (threadCreated (th) );
270 debug ( 1,
"Weaver dtor: destroying inventory.\n" );
287 if ( !th->finished() )
293 emit (threadDestroyed (th) );
302 debug ( 1,
"Weaver dtor: done\n" );
308 debug ( 3 ,
"Weaver::lock: lock (mutex is %s).\n",
309 ( m_mutex->locked() ?
"locked" :
"not locked" ) );
317 debug ( 3 ,
"Weaver::unlock: unlock (mutex is %s).\n",
318 ( m_mutex->locked() ?
"locked" :
"not locked" ) );
323 TQMutexLocker l (m_mutex);
343 for (
Job * job = jobs.first(); job; job = jobs.next() )
355 TQMutexLocker l (m_mutex);
361 TQMutexLocker l (m_mutex);
381 debug (2,
"Weaver::suspend: queueing resumed.\n" );
394 if ( e->type() >= TQEvent::User )
401 switch (
event->action() )
403 case Event::JobFinished:
404 if (
event->job() !=0 )
409 case Event::Finished:
415 case Event::ThreadSuspended:
418 emit (threadSuspended (
event->thread() ) );
421 case Event::ThreadBusy:
424 emit (threadBusy (
event->thread() ) );
431 if (
event->job() !=0 )
433 event->job()->processEvent (
event);
436 debug ( 0,
"Weaver::event: Strange: received unknown user event.\n" );
441 return TQObject::event ( e );
448 TQApplication::postEvent (
this, e);
453 TQMutexLocker l (m_mutex);
460 bool lastjob =
false;
471 debug ( 3,
"Weaver::applyForWork: job done, %i jobs left, "
472 "%i active jobs left.\n",
479 post (Event::Finished);
480 debug ( 3,
"Weaver::applyForWork: last job.\n" );
487 debug ( 2,
"Weaver::applyForWork: queueing suspended.\n" );
507 debug ( 3,
"Weaver::applyForWork: job assigned, "
508 "%i jobs in queue (%i active).\n",
512 post (Event::ThreadBusy, th);
518 post (Event::ThreadSuspended, th);
527 TQMutexLocker l (m_mutex);
533 TQMutexLocker l (m_mutex);
541 debug (2,
"Weaver::finish: not done, waiting.\n" );
544 debug (1,
"Weaver::finish: done.\n\n\n" );
550 #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.
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.
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.
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.