2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "image-load-thread.h"
30 LoadingTask::LoadingTask(uint32_t id, BitmapLoader loader )
36 ImageLoadThread::ImageLoadThread( EventThreadCallback* trigger )
41 ImageLoadThread::~ImageLoadThread()
43 // add an empty task would stop the thread from conditional wait.
51 void ImageLoadThread::Run()
53 while( LoadingTask* task = NextTaskToProcess())
56 AddCompletedTask( task );
60 void ImageLoadThread::AddTask( LoadingTask* task )
62 bool wasEmpty = false;
65 // Lock while adding task to the queue
66 ConditionalWait::ScopedLock lock( mConditionalWait );
67 wasEmpty = mLoadQueue.Empty();
68 mLoadQueue.PushBack( task );
73 // wake up the image loading thread
74 mConditionalWait.Notify();
78 LoadingTask* ImageLoadThread::NextCompletedTask()
80 // Lock while popping task out from the queue
81 Mutex::ScopedLock lock( mMutex );
83 if( mCompleteQueue.Empty() )
88 Vector< LoadingTask* >::Iterator next = mCompleteQueue.Begin();
89 LoadingTask* nextTask = *next;
90 mCompleteQueue.Erase( next );
95 bool ImageLoadThread::CancelTask( uint32_t loadingTaskId )
97 // Lock while remove task from the queue
98 ConditionalWait::ScopedLock lock( mConditionalWait );
100 for( Vector< LoadingTask* >::Iterator iter = mLoadQueue.Begin(); iter != mLoadQueue.End(); iter++ )
102 if( (*iter)->id == loadingTaskId )
105 mLoadQueue.Erase( iter );
114 void ImageLoadThread::CancelAll()
116 // Lock while remove task from the queue
117 ConditionalWait::ScopedLock lock( mConditionalWait );
119 for( Vector< LoadingTask* >::Iterator iter = mLoadQueue.Begin(); iter != mLoadQueue.End(); iter++ )
126 LoadingTask* ImageLoadThread::NextTaskToProcess()
128 // Lock while popping task out from the queue
129 ConditionalWait::ScopedLock lock( mConditionalWait );
131 while( mLoadQueue.Empty() )
133 mConditionalWait.Wait( lock );
136 Vector< LoadingTask* >::Iterator next = mLoadQueue.Begin();
137 LoadingTask* nextTask = *next;
138 mLoadQueue.Erase( next );
143 void ImageLoadThread::AddCompletedTask( LoadingTask* task )
145 // Lock while adding task to the queue
146 Mutex::ScopedLock lock( mMutex );
147 mCompleteQueue.PushBack( task );
149 // wake up the main thread
155 } // namespace Internal
157 } // namespace Toolkit