From 9d9fae75b549862810d0330729d7878e24a9d8e9 Mon Sep 17 00:00:00 2001 From: Leo Franchi Date: Sun, 17 Jun 2012 22:34:31 +0200 Subject: [PATCH] Set future on artist/album immediately after creating promise --- src/libtomahawk/Album.cpp | 10 +++++++++- src/libtomahawk/Album.h | 5 +++++ src/libtomahawk/Artist.cpp | 10 +++++++++- src/libtomahawk/Artist.h | 6 ++++++ src/libtomahawk/database/IdThreadWorker.cpp | 11 +++++------ src/libtomahawk/database/IdThreadWorker.h | 4 ++-- 6 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/libtomahawk/Album.cpp b/src/libtomahawk/Album.cpp index 0995a2f0f..18627a518 100644 --- a/src/libtomahawk/Album.cpp +++ b/src/libtomahawk/Album.cpp @@ -151,9 +151,17 @@ void Album::loadId( bool autoCreate ) { Q_ASSERT( m_waitingForId ); - m_idFuture = IdThreadWorker::getAlbumId( m_ownRef.toStrongRef(), autoCreate ); + IdThreadWorker::getAlbumId( m_ownRef.toStrongRef(), autoCreate ); } + +void +Album::setIdFuture( boost::unique_future< unsigned int > future ) +{ + m_idFuture = boost::move( future ); +} + + unsigned int Album::id() const { diff --git a/src/libtomahawk/Album.h b/src/libtomahawk/Album.h index f0d03e33e..1dc51d5f7 100644 --- a/src/libtomahawk/Album.h +++ b/src/libtomahawk/Album.h @@ -36,6 +36,8 @@ #include "Collection.h" #include "infosystem/InfoSystem.h" +class IdThreadWorker; + namespace Tomahawk { @@ -82,6 +84,7 @@ private slots: private: Q_DISABLE_COPY( Album ) QString infoid() const; + void setIdFuture( boost::unique_future< unsigned int > future ); mutable bool m_waitingForId; mutable boost::unique_future< unsigned int > m_idFuture; @@ -107,6 +110,8 @@ private: static QHash< QString, album_ptr > s_albumsByName; static QHash< unsigned int, album_ptr > s_albumsById; + + friend class ::IdThreadWorker; }; } // ns diff --git a/src/libtomahawk/Artist.cpp b/src/libtomahawk/Artist.cpp index 16b70efc2..58e3942c5 100644 --- a/src/libtomahawk/Artist.cpp +++ b/src/libtomahawk/Artist.cpp @@ -230,7 +230,15 @@ void Artist::loadId( bool autoCreate ) { Q_ASSERT( m_waitingForFuture ); - m_idFuture = IdThreadWorker::getArtistId( m_ownRef.toStrongRef(), autoCreate ); + + IdThreadWorker::getArtistId( m_ownRef.toStrongRef(), autoCreate ); +} + + +void +Artist::setIdFuture( boost::unique_future< unsigned int > future ) +{ + m_idFuture = boost::move(future); } diff --git a/src/libtomahawk/Artist.h b/src/libtomahawk/Artist.h index b738b06cd..d9d39fd3d 100644 --- a/src/libtomahawk/Artist.h +++ b/src/libtomahawk/Artist.h @@ -33,6 +33,8 @@ #include +class IdThreadWorker; + namespace Tomahawk { @@ -97,6 +99,8 @@ private: Artist(); QString infoid() const; + void setIdFuture( boost::unique_future< unsigned int > future ); + mutable bool m_waitingForFuture; mutable boost::unique_future< unsigned int > m_idFuture; mutable unsigned int m_id; @@ -133,6 +137,8 @@ private: static QHash< QString, artist_ptr > s_artistsByName; static QHash< unsigned int, artist_ptr > s_artistsById; + + friend class ::IdThreadWorker; }; } // ns diff --git a/src/libtomahawk/database/IdThreadWorker.cpp b/src/libtomahawk/database/IdThreadWorker.cpp index 7c5ccd12a..1d264f576 100644 --- a/src/libtomahawk/database/IdThreadWorker.cpp +++ b/src/libtomahawk/database/IdThreadWorker.cpp @@ -22,6 +22,7 @@ #include "Album.h" #include "Database.h" #include "DatabaseImpl.h" +#include "Source.h" #define ID_THREAD_DEBUG 0 @@ -90,10 +91,11 @@ internalGet( const artist_ptr& artist, const album_ptr& album, bool autoCreate, } -boost::unique_future< unsigned int > +void IdThreadWorker::getArtistId( const artist_ptr& artist, bool autoCreate ) { QueueItem* item = internalGet( artist, album_ptr(), autoCreate, ArtistType ); + artist->setIdFuture( item->promise.get_future() ); #if ID_THREAD_DEBUG qDebug() << "QUEUEING ARTIST:" << artist->name(); @@ -106,15 +108,14 @@ IdThreadWorker::getArtistId( const artist_ptr& artist, bool autoCreate ) #if ID_THREAD_DEBUG qDebug() << "DONE WOKE UP THREAD:" << artist->name(); #endif - - return item->promise.get_future(); } -boost::unique_future< unsigned int > +void IdThreadWorker::getAlbumId( const album_ptr& album, bool autoCreate ) { QueueItem* item = internalGet( artist_ptr(), album, autoCreate, AlbumType ); + album->setIdFuture( item->promise.get_future() ); #if ID_THREAD_DEBUG qDebug() << "QUEUEING ALUBM:" << album->artist()->name() << album->name(); @@ -126,8 +127,6 @@ IdThreadWorker::getAlbumId( const album_ptr& album, bool autoCreate ) #if ID_THREAD_DEBUG qDebug() << "DONE WOKE UP THREAD:" << album->artist()->name() << album->name(); #endif - - return item->promise.get_future(); } diff --git a/src/libtomahawk/database/IdThreadWorker.h b/src/libtomahawk/database/IdThreadWorker.h index 1f025ceba..b3d18d69e 100644 --- a/src/libtomahawk/database/IdThreadWorker.h +++ b/src/libtomahawk/database/IdThreadWorker.h @@ -42,8 +42,8 @@ public: void run(); void stop(); - static boost::unique_future< unsigned int > getArtistId( const Tomahawk::artist_ptr& artist, bool autoCreate = false ); - static boost::unique_future< unsigned int > getAlbumId( const Tomahawk::album_ptr& album, bool autoCreate = false ); + static void getArtistId( const Tomahawk::artist_ptr& artist, bool autoCreate = false ); + static void getAlbumId( const Tomahawk::album_ptr& album, bool autoCreate = false ); private: Database* m_db;