1 #ifndef __DALI_SLP_PLATFORM_RESOURCE_THREAD_BASE_H__
2 #define __DALI_SLP_PLATFORM_RESOURCE_THREAD_BASE_H__
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 #include "resource-loader.h"
23 #include "resource-loading-client.h"
24 #include <dali/integration-api/platform-abstraction.h>
25 #include <dali/integration-api/resource-cache.h>
29 #include <boost/thread.hpp>
38 * Resource loader worker thread
40 class ResourceThreadBase : public ResourceLoadingClient
45 /** Client threads send work to resource threads through Requests, for which
46 * this type identifies the action to be taken on the resource thread. */
49 /** Pull a resource out of the platform's file system. */
51 /** Pull a resource over http protocol. */
53 /** Pull a resource out of a memory buffer. */
55 /** Push a resource's data out to the file system. */
59 typedef std::pair<Integration::ResourceRequest, RequestType> RequestInfo;
60 typedef std::deque<RequestInfo> RequestQueue;
61 typedef RequestQueue::iterator RequestQueueIter;
65 ResourceThreadBase(ResourceLoader& resourceLoader);
68 virtual ~ResourceThreadBase();
71 void TerminateThread();
75 * Add a resource request to the back of the queue
76 * @param[in] request The requested resource/file url and attributes
77 * @param[in] type Load or save flag
79 void AddRequest(const Integration::ResourceRequest& request, const RequestType type);
82 * Cancel a resource request. Removes the request from the queue.
83 * @param[in] resourceId ID of the resource to be canceled
85 void CancelRequest(Integration::ResourceId resourceId);
88 * Pause starting new work in the background, but keep that work queued.
93 * Resume processing tasks on background thread.
100 * Main control loop for the thread.
101 * The thread is terminated when this function exits
106 * Wait for an incoming resource request or termination
108 void WaitForRequests();
111 * Process the resource request at the head of the queue
113 void ProcessNextRequest();
116 * Install a logging function in to core for this thread.
118 void InstallLogging();
121 * Uninstall a logging function.
123 void UninstallLogging();
127 * @param[in] request The requested resource/file url and attributes
129 virtual void Load(const Integration::ResourceRequest& request) = 0;
132 * Download a resource
133 * @param[in] request The requested resource/file url and attributes
135 virtual void Download(const Integration::ResourceRequest& request);
138 * Decode a resource exactly as if it were being loaded but source its data
139 * from a memory buffer attached directly to the request object.
140 * @param[in] request The requested resource data and attributes
142 virtual void Decode(const Integration::ResourceRequest& request);
146 * @param[in] request The requested resource/file url and attributes
148 virtual void Save(const Integration::ResourceRequest& request) = 0;
151 * @brief Cancels current resource request if it matches the one latched to be cancelled.
153 * @copydoc ResourceLoadingClient::InterruptionPoint
155 virtual void InterruptionPoint() const;
158 ResourceLoader& mResourceLoader;
159 boost::thread* mThread; ///< thread instance
160 boost::condition_variable mCondition; ///< condition variable
161 boost::mutex mMutex; ///< used to protect mQueue
162 RequestQueue mQueue; ///< Request queue
164 Integration::ResourceId mCurrentRequestId; ///< Current request, set by worker thread
165 volatile Integration::ResourceId mCancelRequestId; ///< Request to be cancelled on thread: written by external thread and read by worker.
166 bool mPaused; ///< Whether to process work in mQueue
168 #if defined(DEBUG_ENABLED)
170 Integration::Log::Filter* mLogFilter;
172 }; // class ResourceThreadBase
174 } // namespace SlpPlatform
178 #endif // __DALI_SLPPLATFORM_RESOURCE_THREAD_BASE_H__