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

ThreadWeaver

  • 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, 2005 Mirko Boehm $
9  $ Contact: mirko@kde.org
10  http://www.kde.org
11  http://www.hackerbuero.org $
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 #include "Thread_p.h"
33 
34 #include <QtCore/QMutex>
35 #include <QtCore/QDebug>
36 
37 #include "ThreadWeaver.h"
38 #include "WeaverImpl.h"
39 #include "Job.h"
40 #include "DebuggingAids.h"
41 
42 using namespace ThreadWeaver;
43 
44 class Thread::Private
45 {
46 public:
47  explicit Private ( WeaverImpl* theParent )
48  : parent ( theParent )
49  , runhelper ( 0 )
50  , id ( makeId() )
51  {}
52 
53  WeaverImpl *parent;
54 
55  ThreadRunHelper* runhelper;
56 
57  const unsigned int id;
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 
69 ThreadWeaver::ThreadRunHelper::ThreadRunHelper()
70  : QObject ( 0 )
71  , m_job( 0 )
72 {
73 }
74 
75 void ThreadWeaver::ThreadRunHelper::run ( WeaverImpl *parent, Thread* th )
76 {
77  Q_ASSERT ( thread() == th );
78  emit ( started ( th) );
79 
80  while (true)
81  {
82  debug ( 3, "Thread::run [%u]: trying to execute the next job.\n", th->id() );
83 
84  // this is the *only* assignment to m_job in the Thread class!
85  Job* tmp = m_job; m_job = 0;
86 
87  Job* job = parent->applyForWork ( th, tmp );
88 
89  if (job == 0)
90  {
91  break;
92  } else {
93  m_job = job;
94  emit ( jobStarted ( th, m_job ) );
95  m_job->execute (th);
96  emit ( jobDone ( m_job ) );
97  }
98  }
99 }
100 
101 void ThreadWeaver::ThreadRunHelper::requestAbort()
102 {
103  Job* job = m_job;
104  if ( job )
105  {
106  job->requestAbort();
107  }
108 }
109 
110 Thread::Thread (WeaverImpl *parent)
111  : QThread () // no parent, because the QObject hierarchy of this thread
112  // does not have a parent (see QObject::pushToThread)
113  , d ( new Private ( parent ) )
114 {
115 }
116 
117 Thread::~Thread()
118 {
119  delete d;
120 }
121 
122 unsigned int Thread::id()
123 {
124  return d->id;
125 }
126 
127 void Thread::run()
128 {
129 // disabled while testing movetothread...
130 // Q_ASSERT ( thread() != this ); // this is created and owned by the main thread
131  debug ( 3, "Thread::run [%u]: running.\n", id() );
132 
133  ThreadRunHelper helper;
134  d->runhelper = &helper;
135 
136  connect ( &helper, SIGNAL (started(ThreadWeaver::Thread*)),
137  SIGNAL (started(ThreadWeaver::Thread*)) );
138  connect ( &helper, SIGNAL (jobStarted(ThreadWeaver::Thread*,ThreadWeaver::Job*)),
139  SIGNAL (jobStarted(ThreadWeaver::Thread*,ThreadWeaver::Job*)) );
140  connect ( &helper, SIGNAL (jobDone(ThreadWeaver::Job*)),
141  SIGNAL (jobDone(ThreadWeaver::Job*)) );
142  helper.run( d->parent, this );
143 
144  d->runhelper = 0;
145  debug ( 3, "Thread::run [%u]: exiting.\n", id() );
146 }
147 
148 void Thread::msleep(unsigned long msec)
149 {
150  QThread::msleep(msec);
151 }
152 
153 
154 void Thread::requestAbort ()
155 {
156  if ( d->runhelper )
157  {
158  d->runhelper->requestAbort();
159  } else {
160  qDebug ( "Thread::requestAbort: not running." );
161  }
162 }
163 
164 #include "Thread.moc"
165 #include "Thread_p.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Fri Jul 12 2013 08:52:14 by doxygen 1.8.3.1 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-4.10.5 API Reference

Skip menu "kdelibs-4.10.5 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
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
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