|
1 /* |
|
2 * Copyright (C) 2010 Apple Inc. All rights reserved. |
|
3 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) |
|
4 * |
|
5 * Redistribution and use in source and binary forms, with or without |
|
6 * modification, are permitted provided that the following conditions |
|
7 * are met: |
|
8 * 1. Redistributions of source code must retain the above copyright |
|
9 * notice, this list of conditions and the following disclaimer. |
|
10 * 2. Redistributions in binary form must reproduce the above copyright |
|
11 * notice, this list of conditions and the following disclaimer in the |
|
12 * documentation and/or other materials provided with the distribution. |
|
13 * |
|
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
|
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
|
16 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
|
18 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
|
24 * THE POSSIBILITY OF SUCH DAMAGE. |
|
25 */ |
|
26 |
|
27 #include "WorkQueue.h" |
|
28 |
|
29 #include <QLocalSocket> |
|
30 #include <QObject> |
|
31 #include <QThread> |
|
32 #include <wtf/Threading.h> |
|
33 |
|
34 class WorkQueue::WorkItemQt : public QObject { |
|
35 Q_OBJECT |
|
36 public: |
|
37 WorkItemQt(WorkQueue* workQueue, WorkItem* workItem) |
|
38 : m_queue(workQueue) |
|
39 , m_source(0) |
|
40 , m_signal(0) |
|
41 , m_workItem(workItem) |
|
42 { |
|
43 } |
|
44 |
|
45 WorkItemQt(WorkQueue* workQueue, QObject* source, const char* signal, WorkItem* workItem) |
|
46 : m_queue(workQueue) |
|
47 , m_source(source) |
|
48 , m_signal(signal) |
|
49 , m_workItem(workItem) |
|
50 { |
|
51 connect(m_source, m_signal, SLOT(execute()), Qt::QueuedConnection); |
|
52 } |
|
53 |
|
54 ~WorkItemQt() |
|
55 { |
|
56 delete m_workItem; |
|
57 } |
|
58 |
|
59 Q_SLOT void execute() |
|
60 { |
|
61 if (m_queue->m_isValid) |
|
62 m_workItem->execute(); |
|
63 } |
|
64 |
|
65 virtual void timerEvent(QTimerEvent*) |
|
66 { |
|
67 execute(); |
|
68 delete this; |
|
69 } |
|
70 |
|
71 WorkQueue* m_queue; |
|
72 QObject* m_source; |
|
73 const char* m_signal; |
|
74 WorkItem* m_workItem; |
|
75 }; |
|
76 |
|
77 void WorkQueue::connectSignal(QObject* o, const char* signal, PassOwnPtr<WorkItem> workItem) |
|
78 { |
|
79 WorkQueue::WorkItemQt* itemQt = new WorkQueue::WorkItemQt(this, o, signal, workItem.leakPtr()); |
|
80 itemQt->moveToThread(m_workThread); |
|
81 m_signalListeners.add(o, itemQt); |
|
82 } |
|
83 |
|
84 void WorkQueue::disconnectSignal(QObject* o, const char* name) |
|
85 { |
|
86 HashMap<QObject*, WorkItemQt*>::iterator it = m_signalListeners.find(o); |
|
87 for (; it != m_signalListeners.end(); ++it) { |
|
88 if (strcmp(it->second->m_signal, name)) |
|
89 continue; |
|
90 delete it->second; |
|
91 m_signalListeners.remove(it); |
|
92 return; |
|
93 } |
|
94 } |
|
95 |
|
96 void WorkQueue::moveSocketToWorkThread(QLocalSocket* socket) |
|
97 { |
|
98 ASSERT(m_workThread); |
|
99 ASSERT(socket); |
|
100 |
|
101 socket->setParent(0); |
|
102 socket->moveToThread(m_workThread); |
|
103 } |
|
104 |
|
105 void WorkQueue::platformInitialize(const char*) |
|
106 { |
|
107 m_workThread = new QThread(); |
|
108 m_workThread->start(); |
|
109 } |
|
110 |
|
111 void WorkQueue::platformInvalidate() |
|
112 { |
|
113 m_workThread->exit(); |
|
114 m_workThread->wait(); |
|
115 delete m_workThread; |
|
116 deleteAllValues(m_signalListeners); |
|
117 } |
|
118 |
|
119 void WorkQueue::scheduleWork(PassOwnPtr<WorkItem> item) |
|
120 { |
|
121 WorkQueue::WorkItemQt* itemQt = new WorkQueue::WorkItemQt(this, item.leakPtr()); |
|
122 itemQt->startTimer(0); |
|
123 itemQt->moveToThread(m_workThread); |
|
124 } |
|
125 |
|
126 #include "WorkQueueQt.moc" |