• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

ThreadWeaver

  • sources
  • kde-4.12
  • kdelibs
  • threadweaver
  • Weaver
Thread.cpp
Go to the documentation of this file.
1 /* -*- C++ -*-
2 
3  This file implements the Thread class.
4 
5  Thread is not a part of the public interface of the ThreadWeaver library.
6 
7  $ Author: Mirko Boehm $
8  $ Copyright: (C) 2004-2013 Mirko Boehm $
9  $ Contact: mirko@kde.org
10  http://www.kde.org
11  http://creative-destruction.me $
12 
13  This library is free software; you can redistribute it and/or
14  modify it under the terms of the GNU Library General Public
15  License as published by the Free Software Foundation; either
16  version 2 of the License, or (at your option) any later version.
17 
18  This library is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  Library General Public License for more details.
22 
23  You should have received a copy of the GNU Library General Public License
24  along with this library; see the file COPYING.LIB. If not, write to
25  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26  Boston, MA 02110-1301, USA.
27 
28  $Id: Thread.cpp 25 2005-08-14 12:41:38Z mirko $
29 */
30 
31 #include "Thread.h"
32 
33 #include <QtCore/QMutex>
34 #include <QtCore/QDebug>
35 
36 #include "ThreadWeaver.h"
37 #include "WeaverImpl.h"
38 #include "Job.h"
39 #include "DebuggingAids.h"
40 
41 using namespace ThreadWeaver;
42 
43 class Thread::Private
44 {
45 public:
46  explicit Private(WeaverImpl* theParent)
47  : parent(theParent)
48  , id(makeId())
49  , job(0)
50  {
51  Q_ASSERT(parent);
52  }
53 
54  WeaverImpl *parent;
55  const unsigned int id;
56  Job* job;
57  QMutex mutex;
58 
59  static unsigned int makeId()
60  {
61  static unsigned int s_id;
62  static QMutex sm_mutex;
63  QMutexLocker l (&sm_mutex);
64  return ++s_id;
65  }
66 };
67 
68 Thread::Thread (WeaverImpl *parent)
69  : QThread() // no parent, because the QObject hierarchy of this thread
70  // does not have a parent (see QObject::pushToThread)
71  , d(new Private(parent))
72 {
73 }
74 
75 Thread::~Thread()
76 {
77  delete d;
78 }
79 
80 unsigned int Thread::id()
81 {
82  return d->id; //id is const
83 }
84 
85 void Thread::run()
86 {
87  debug(3, "Thread::run [%u]: running.\n", id());
88  emit(started ( this));
89 
90  while (true) {
91  debug(3, "Thread::run [%u]: trying to execute the next job.\n", id());
92  Job* oldJob = 0;
93  {
94  QMutexLocker l(&d->mutex); Q_UNUSED(l);
95  oldJob = d->job; d->job = 0;
96  }
97  // this is the *only* assignment to m_job in the Thread class!
98  Job* newJob = d->parent->applyForWork(this, oldJob);
99 
100  if (newJob == 0) {
101  break;
102  } else {
103  {
104  QMutexLocker l(&d->mutex); Q_UNUSED(l);
105  d->job = newJob;
106  }
107  emit(jobStarted(this, newJob));
108  newJob->execute (this);
109  emit(jobDone(newJob));
110  }
111  }
112  debug ( 3, "Thread::run [%u]: exiting.\n", id() );
113 }
114 
115 void Thread::msleep(unsigned long msec)
116 {
117  QThread::msleep(msec);
118 }
119 
120 void Thread::requestAbort ()
121 {
122  QMutexLocker l(&d->mutex); Q_UNUSED(l);
123  if (d->job) {
124  d->job->requestAbort();
125  } else {
126  qDebug ( "Thread::requestAbort: not running." );
127  }
128 }
129 
130 #include "Thread.moc"
Job.h
ThreadWeaver::Thread::requestAbort
void requestAbort()
Request the abortion of the current job.
Definition: Thread.cpp:120
ThreadWeaver::Thread::msleep
void msleep(unsigned long msec)
Provide the msleep() method (protected in QThread) to be available for executed jobs.
Definition: Thread.cpp:115
ThreadWeaver.h
ThreadWeaver::Job::execute
virtual void execute(Thread *)
Perform the job.
Definition: Job.cpp:122
Thread.h
ThreadWeaver::Thread::started
void started(ThreadWeaver::Thread *)
The thread has been started.
DebuggingAids.h
ThreadWeaver::Thread::jobDone
void jobDone(ThreadWeaver::Job *)
The thread finished to execute a job.
ThreadWeaver::Thread::run
void run()
Overload to execute the assigned jobs.
Definition: Thread.cpp:85
WeaverImpl.h
ThreadWeaver::Thread::id
unsigned int id()
Returns the thread id.
Definition: Thread.cpp:80
ThreadWeaver::debug
void debug(int severity, const char *cformat,...)
This method prints a text message on the screen, if debugging is enabled.
Definition: DebuggingAids.h:112
ThreadWeaver::Job
A Job is a simple abstraction of an action that is to be executed in a thread context.
Definition: Job.h:65
ThreadWeaver::Thread::jobStarted
void jobStarted(ThreadWeaver::Thread *, ThreadWeaver::Job *)
The thread started to process a job.
QThread
ThreadWeaver::Thread::~Thread
~Thread()
The destructor.
Definition: Thread.cpp:75
ThreadWeaver::WeaverImpl
A WeaverImpl is the manager of worker threads (Thread objects) to which it assigns jobs from its queu...
Definition: WeaverImpl.h:51
ThreadWeaver::Thread::Thread
Thread(WeaverImpl *parent=0)
Create a thread.
Definition: Thread.cpp:68
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:48:53 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

ThreadWeaver

Skip menu "ThreadWeaver"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal