00001 /* 00002 * File Name: pdf_thread.h 00003 */ 00004 00005 /* 00006 * This file is part of uds-plugin-pdf. 00007 * 00008 * uds-plugin-pdf is free software: you can redistribute it and/or modify 00009 * it under the terms of the GNU General Public License as published by 00010 * the Free Software Foundation, either version 2 of the License, or 00011 * (at your option) any later version. 00012 * 00013 * uds-plugin-pdf is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU General Public License 00019 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00020 */ 00021 00022 /** 00023 * Copyright (C) 2008 iRex Technologies B.V. 00024 * All rights reserved. 00025 */ 00026 00027 #ifndef PDF_THREAD_H 00028 #define PDF_THREAD_H 00029 00030 #include <list> 00031 #include <glib.h> 00032 #include <string> 00033 #include <vector> 00034 00035 #include "mutex.h" 00036 #include "condition.h" 00037 #include "task.h" 00038 00039 namespace pdf 00040 { 00041 00042 class Thread 00043 { 00044 public: 00045 /// @brief Constructors and destructors. 00046 Thread(); 00047 ~Thread(); 00048 00049 /// @brief Run thread. 00050 bool start(); 00051 00052 /// @brief Stop current thread. 00053 void stop(bool cancel_all_tasks = true); 00054 00055 /// @brief Append the task to the end of the task queue. 00056 bool append_task(Task* new_task); 00057 00058 /// @brief Insert the task to the head of the task queue. 00059 /// @param abort_current This flag tells the thread that after inserting 00060 /// the new task to the head of the queue, abort current running task 00061 /// immediately. We should NOT give user a capability to abort the 00062 /// running task because the aborted task may generate a new task, and 00063 /// put it to the end of the queue, which may be executed by worker 00064 /// thread immediately. That is NOT we want. 00065 bool prepend_task(Task* new_task, bool abort_current); 00066 00067 /// @brief Cancel all of the tasks including current one 00068 /// @param user_data if this value is not null, thread will clear all of the 00069 /// tasks with this value. 00070 void cancel_tasks(void* user_data = 0); 00071 00072 /// @brief Remove all tasks in the task queue. 00073 void clear_all(void* user_data = 0, TaskType t = TASK_INVALID); 00074 00075 /// @brief Abort or remove a given task 00076 bool abort_task(void* user_data, TaskType t, unsigned int id); 00077 00078 private: 00079 /// @brief Abort current task if thread is executing a task. 00080 bool abort_current_task(Task *new_task); 00081 00082 /// @brief Thread functions. 00083 static gpointer thread_func(gpointer args); 00084 gpointer non_static_thread_func(); 00085 00086 private: 00087 enum ThreadCmd 00088 { 00089 CMD_NONE, ///< No external commond 00090 CMD_TERMINATE, ///< Terminate thread, abort current task and quit at once 00091 CMD_STOP ///< Stop thread, wait until all tasks are executed 00092 }; 00093 00094 typedef std::list<Task*> TaskQueue; 00095 typedef TaskQueue::iterator TaskQueueIter; 00096 private: 00097 GThread *thread; 00098 TaskQueue task_queue; 00099 ThreadCmd thread_cmd; 00100 Task* running_task; 00101 00102 /// @brief Mutexes and conditions 00103 Mutex queue_mutex; 00104 Mutex cancel_task_mutex; 00105 Mutex running_task_mutex; 00106 Cond queue_cond; 00107 }; 00108 00109 }; 00110 #endif // THREAD_H 00111