We now have a current svn snapshot of libqxt in our contrib dir, and
[quassel.git] / src / contrib / libqxt-2007-10-24 / src / core / qxtjob.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) Qxt Foundation. Some rights reserved.
4 **
5 ** This file is part of the QxtCore module of the Qt eXTension library
6 **
7 ** This library is free software; you can redistribute it and/or modify it
8 ** under the terms of th Common Public License, version 1.0, as published by
9 ** IBM.
10 **
11 ** This file is provided "AS IS", without WARRANTIES OR CONDITIONS OF ANY
12 ** KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY
13 ** WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR
14 ** FITNESS FOR A PARTICULAR PURPOSE.
15 **
16 ** You should have received a copy of the CPL along with this file.
17 ** See the LICENSE file and the cpl1.0.txt file included with the source
18 ** distribution for more information. If you did not receive a copy of the
19 ** license, contact the Qxt Foundation.
20 **
21 ** <http://libqxt.sourceforge.net>  <foundation@libqxt.org>
22 **
23 ****************************************************************************/
24
25 /**
26 \class QxtJob QxtJob
27
28 \ingroup QxtCore
29
30 \brief Execute a Job on a QThread. once or multiple times.
31
32 QxtJob allows easily starting jobs on different threads.\n
33 exec() will ask for the QThread to run the job on.
34 The Qthread needs an event loop. 4.3 and later versions of Qt have 
35 a non virtual QThread with a default event loop, allowing easy deployment of jobs.
36
37 \code
38 QThread thread;
39 thread.start();
40 LockJob().exec(&thread);
41 \endcode
42 */
43
44 #include "qxtjob_p.h"
45 #include <cassert>
46 #include <QThread>
47
48 class Thread : public QThread\r
49 {\r
50 public:\r
51     static void usleep(unsigned long usecs)\r
52     {\r
53         QThread::usleep(usecs);\r
54     }\r
55 };
56
57 QxtJob::QxtJob()
58 {
59     QXT_INIT_PRIVATE(QxtJob);
60     qxt_d().running.set(false);
61     connect(&qxt_d(),SIGNAL(done()),this,SIGNAL(done()));
62 }
63 /*!
64 execute the Job on \p onthread \n
65 */
66 void QxtJob::exec(QThread * onthread)
67 {
68     qxt_d().moveToThread(onthread);
69     connect(this,SIGNAL(subseed()),&qxt_d(),SLOT(inwrap_d()),Qt::QueuedConnection);
70
71     qxt_d().running.set(true);
72     emit(subseed());
73 }
74 /*!
75 The dtor joins.
76 Means it blocks until the job is finished
77 */
78 QxtJob::~QxtJob()
79 {
80     join();
81 }
82 /*!
83 block until the Job finished \n
84 Note that the current thread will be blocked. \n
85 If you use this, you better be damn sure you actually want a thread.\n
86 Maybe you actualy want to use QxtSignalWaiter.
87 */
88 void QxtJob::join()
89 {
90     while(qxt_d().running.get()==true)
91     {
92         /**
93         oh yeah that sucks ass, 
94         but since any kind of waitcondition will just fail due to undeterminnism,
95         we have no chance then polling.
96         And no, a mutex won't work either.
97         using join for anything else then testcases sounds kindof retarded anyway.
98         */
99         Thread::usleep(1000);
100     }
101
102 }
103 void QxtJobPrivate::inwrap_d()
104 {
105     synca.wakeAll();
106     qxt_p().run();
107     running.set(false);
108     emit(done());
109 }
110
111
112
113
114
115
116
117
118