~ubuntu-branches/ubuntu/trusty/tomahawk/trusty-proposed

« back to all changes in this revision

Viewing changes to src/libtomahawk/database/IdThreadWorker.cpp

  • Committer: Package Import Robot
  • Author(s): Harald Sitter
  • Date: 2013-03-07 21:50:13 UTC
  • Revision ID: package-import@ubuntu.com-20130307215013-6gdjkdds7i9uenvs
Tags: upstream-0.6.0+dfsg
ImportĀ upstreamĀ versionĀ 0.6.0+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
 
2
 *
 
3
 *   Copyright 2012 Leo Franchi <lfranchi@kde.org>
 
4
 *
 
5
 *   Tomahawk is free software: you can redistribute it and/or modify
 
6
 *   it under the terms of the GNU General Public License as published by
 
7
 *   the Free Software Foundation, either version 3 of the License, or
 
8
 *   (at your option) any later version.
 
9
 *
 
10
 *   Tomahawk is distributed in the hope that it will be useful,
 
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 
13
 *   GNU General Public License for more details.
 
14
 *
 
15
 *   You should have received a copy of the GNU General Public License
 
16
 *   along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
 
17
 */
 
18
 
 
19
#include "IdThreadWorker.h"
 
20
 
 
21
#include "Artist.h"
 
22
#include "Album.h"
 
23
#include "Database.h"
 
24
#include "DatabaseImpl.h"
 
25
#include "Source.h"
 
26
 
 
27
#define ID_THREAD_DEBUG 0
 
28
 
 
29
#include <QtCore/qfutureinterface.h>
 
30
 
 
31
using namespace Tomahawk;
 
32
 
 
33
namespace {
 
34
    enum QueryType {
 
35
        ArtistType,
 
36
        AlbumType
 
37
    };
 
38
}
 
39
 
 
40
 
 
41
static QWaitCondition s_waitCond;
 
42
static QMutex s_mutex;
 
43
 
 
44
struct QueueItem
 
45
{
 
46
    QFutureInterface<unsigned int> promise;
 
47
    artist_ptr artist;
 
48
    album_ptr album;
 
49
    QueryType type;
 
50
    bool create;
 
51
};
 
52
 
 
53
// TODO Q_GLOBAL_STATIC
 
54
QQueue< QueueItem* > IdThreadWorker::s_workQueue = QQueue< QueueItem* >();
 
55
 
 
56
IdThreadWorker::IdThreadWorker( Database* db )
 
57
    : QThread()
 
58
    , m_db( db )
 
59
    , m_stop( false )
 
60
{
 
61
}
 
62
 
 
63
 
 
64
IdThreadWorker::~IdThreadWorker()
 
65
{
 
66
    wait();
 
67
}
 
68
 
 
69
 
 
70
void
 
71
IdThreadWorker::stop()
 
72
{
 
73
    {
 
74
        QMutexLocker l( &s_mutex );
 
75
        m_stop = true;
 
76
    }
 
77
 
 
78
    s_waitCond.wakeOne();
 
79
}
 
80
 
 
81
 
 
82
QueueItem*
 
83
internalGet( const artist_ptr& artist, const album_ptr& album, bool autoCreate, QueryType type )
 
84
{
 
85
    QueueItem* item = new QueueItem;
 
86
    item->artist = artist;
 
87
    item->album = album;
 
88
    item->type = type;
 
89
    item->create = autoCreate;
 
90
    item->promise.reportStarted();
 
91
 
 
92
    return item;
 
93
}
 
94
 
 
95
 
 
96
void
 
97
IdThreadWorker::getArtistId( const artist_ptr& artist, bool autoCreate )
 
98
{
 
99
    QueueItem* item = internalGet( artist, album_ptr(), autoCreate, ArtistType );
 
100
    artist->setIdFuture( item->promise.future() );
 
101
 
 
102
#if ID_THREAD_DEBUG
 
103
    qDebug() << "QUEUEING ARTIST:" << artist->name();
 
104
#endif
 
105
 
 
106
    s_mutex.lock();
 
107
    s_workQueue.enqueue( item );
 
108
    s_mutex.unlock();
 
109
    s_waitCond.wakeOne();
 
110
#if ID_THREAD_DEBUG
 
111
    qDebug() << "DONE WOKE UP THREAD:" << artist->name();
 
112
#endif
 
113
}
 
114
 
 
115
 
 
116
void
 
117
IdThreadWorker::getAlbumId( const album_ptr& album, bool autoCreate )
 
118
{
 
119
    QueueItem* item = internalGet( artist_ptr(), album, autoCreate, AlbumType );
 
120
    album->setIdFuture(  item->promise.future() );
 
121
 
 
122
#if ID_THREAD_DEBUG
 
123
    qDebug() << "QUEUEING ALUBM:" << album->artist()->name() << album->name();
 
124
#endif
 
125
    s_mutex.lock();
 
126
    s_workQueue.enqueue( item );
 
127
    s_mutex.unlock();
 
128
    s_waitCond.wakeOne();
 
129
#if ID_THREAD_DEBUG
 
130
    qDebug() << "DONE WOKE UP THREAD:" << album->artist()->name() << album->name();
 
131
#endif
 
132
}
 
133
 
 
134
 
 
135
void
 
136
IdThreadWorker::run()
 
137
{
 
138
    m_impl = Database::instance()->impl();
 
139
 
 
140
    while ( !m_stop )
 
141
    {
 
142
        s_mutex.lock();
 
143
#if ID_THREAD_DEBUG
 
144
        qDebug() << "IdWorkerThread waiting on condition...";
 
145
#endif
 
146
        s_waitCond.wait( &s_mutex );
 
147
#if ID_THREAD_DEBUG
 
148
        qDebug() << "IdWorkerThread WOKEN UP";
 
149
#endif
 
150
 
 
151
        while ( !s_workQueue.isEmpty() )
 
152
        {
 
153
            QueueItem* item = s_workQueue.dequeue();
 
154
            s_mutex.unlock();
 
155
 
 
156
#if ID_THREAD_DEBUG
 
157
            qDebug() << "WITH CONTENTS:" << (item->type == ArtistType ? item->artist->name() :  item->album->artist()->name() + " _ " + item->album->name());
 
158
#endif
 
159
            if ( item->type == ArtistType )
 
160
            {
 
161
                unsigned int id = m_impl->artistId( item->artist->name(), item->create );
 
162
                item->promise.reportFinished( &id );
 
163
 
 
164
                item->artist->id();
 
165
                delete item;
 
166
            }
 
167
            else if ( item->type == AlbumType )
 
168
            {
 
169
                unsigned int artistId = m_impl->artistId( item->album->artist()->name(), item->create );
 
170
                unsigned int albumId = m_impl->albumId( artistId, item->album->name(), item->create );
 
171
                item->promise.reportFinished( &albumId );
 
172
 
 
173
                item->album->id();
 
174
                delete item;
 
175
            }
 
176
 
 
177
            s_mutex.lock();
 
178
        }
 
179
 
 
180
        s_mutex.unlock();
 
181
    }
 
182
}