return false;
}
-void* ArchiveFile::taskManagerThread(void *data)
+void ArchiveFile::taskManagerThread(gpointer data, gpointer user_data)
{
LoggerD("Entered");
ArchiveFileHolder* archive_file_holder = static_cast<ArchiveFileHolder*>(data);
if (!archive_file_holder) {
LoggerE("archive_file_holder is null");
- return NULL;
+ return;
}
if (!archive_file_holder->ptr){
LoggerE("archive_file is null");
delete archive_file_holder;
archive_file_holder = NULL;
- return NULL;
+ return;
}
PlatformResult result(ErrorCode::NO_ERROR);
delete archive_file_holder;
archive_file_holder = NULL;
- return NULL;
+ return;
}
PlatformResult ArchiveFile::addOperation(OperationCallbackData* callback)
size = m_task_queue.size();
}
if(1 == size) {
- pthread_t thread;
ArchiveFileHolder* holder = new(std::nothrow) ArchiveFileHolder();
if(!holder) {
LoggerE("Memory allocation error");
return PlatformResult(ErrorCode::UNKNOWN_ERR, "Memory allocation error");
}
holder->ptr = shared_from_this();
- if (pthread_create(&thread, NULL, taskManagerThread,
- static_cast<void*>(holder))) {
- LoggerE("Thread creation failed");
- delete holder;
- holder = NULL;
- return PlatformResult(ErrorCode::UNKNOWN_ERR, "Thread creation failed");
- }
- if (pthread_detach(thread)) {
- LoggerE("Thread detachment failed");
+ // operations would be executed asynchronously on one background thread
+ // (no risk of parallel operations on file)
+ if (!g_thread_pool_push(ArchiveManager::getInstance().getThreadPool(),
+ static_cast<gpointer>(holder), NULL)) {
+ LoggerE("Thread creation failed");
+ delete holder;
+ holder = NULL;
+ return PlatformResult(ErrorCode::UNKNOWN_ERR, "Thread creation failed");
}
}
return PlatformResult(ErrorCode::NO_ERROR);
static gboolean getEntriesTaskCompleteCB(void *data);
static gboolean getEntryByNameTaskCompleteCB(void *data);
- static void* taskManagerThread(void *data);
+ static void taskManagerThread(gpointer data, gpointer user_data);
common::PlatformResult addOperation(OperationCallbackData* callback);
static gboolean callErrorCallback(void* data);
m_next_unique_id(0)
{
LoggerD("Initialize ArchiveManager");
+ // create thread pool with max threads = 1 to make API calls async but
+ // only one call at time
+ m_pool = g_thread_pool_new(ArchiveFile::taskManagerThread, NULL, 1, true, NULL);
}
ArchiveManager::~ArchiveManager()
{
LoggerD("Deinitialize ArchiveManager");
+ //finish only current task and wait for thread to stop
+ g_thread_pool_free(m_pool, true, true);
}
ArchiveManager& ArchiveManager::getInstance()
return instance;
}
+GThreadPool* ArchiveManager::getThreadPool()
+{
+ return m_pool;
+}
+
void ArchiveManager::abort(long operation_id)
{
LoggerD("Entered");
#include <map>
#include <memory>
#include <string>
+#include <glib.h>
#include "archive_file.h"
#include "archive_callback_data.h"
long addPrivData(ArchiveFilePtr archive_file_ptr);
common::PlatformResult getPrivData(long handle, ArchiveFilePtr* archive_file);
common::PlatformResult open(OpenCallbackData* callback);
+ GThreadPool* getThreadPool();
private:
ArchiveManager();
ArchiveFileMap m_priv_map;
long m_next_unique_id;
+
+ //! Handler for thread pool
+ GThreadPool* m_pool;
};
} // archive