thread.h

Go to the documentation of this file.
00001 /*
00002  * File Name: thread.h
00003  */
00004 
00005 /*
00006  * This file is part ofuds-plugin-common.
00007  *
00008  * uds-plugin-common 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-common 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 THREAD_H
00028 #define THREAD_H
00029 
00030 #include <list>
00031 #include <glib.h>
00032 
00033 namespace common
00034 {
00035 
00036 enum ThreadCmd
00037 {
00038     CMD_NONE,      ///< No external commond
00039     CMD_TERMINATE, ///< Terminate thread, abort current task and quit at once
00040     CMD_STOP       ///< Stop thread, wait until all tasks are executed
00041 };
00042 
00043 template <typename T>
00044 class SafeData
00045 {
00046 public:
00047     /// @brief Constructors and destructors
00048     SafeData()
00049     {
00050         g_static_rw_lock_init(&rw_lock);
00051     }
00052     ~SafeData()
00053     {
00054         g_static_rw_lock_free(&rw_lock);
00055     }
00056 
00057     /// @brief Safely get the value
00058     T get_value()
00059     {
00060         
00061         g_static_rw_lock_reader_lock(&rw_lock);
00062         T tmp = data;
00063         g_static_rw_lock_reader_unlock(&rw_lock);
00064 
00065         return tmp;
00066     }
00067 
00068     /// @brief Safely set the value
00069     void set_value(const T& new_value)
00070     {
00071         g_static_rw_lock_writer_lock(&rw_lock);
00072         data = new_value;
00073         g_static_rw_lock_writer_unlock(&rw_lock);
00074     }
00075 
00076 private:
00077     GStaticRWLock rw_lock;
00078     T data;
00079 };
00080 
00081 class Task
00082 {
00083 public:
00084     /// @brief Constructors and destructors.
00085     Task();
00086     virtual ~Task();
00087 
00088     /// @brief Execute task with task context.
00089     virtual void execute() = 0;
00090 
00091     /// @brief Send abort request to the current running task.
00092     void send_abort_request();
00093 
00094     bool is_aborted() const
00095     {
00096         return aborted;
00097     }
00098 
00099 private:
00100     bool aborted;
00101 };
00102 
00103 class Thread
00104 {
00105 public:
00106     /// @brief Constructors and destructors.
00107     Thread();
00108     ~Thread();
00109 
00110     /// @brief Run thread.
00111     bool start();
00112 
00113     /// @brief Stop current thread.
00114     void stop(bool cancel_all_tasks = true);
00115 
00116     /// @brief Append the task to the end of the task queue.
00117     bool append_task(Task* new_task);
00118 
00119     /// @brief Insert the task to the head of the task queue.
00120     /// @param abort_current This flag tells the thread that after inserting
00121     /// the new task to the head of the queue, abort current running task
00122     /// immediately. We should NOT give user a capability to abort the 
00123     /// running task because the aborted task may generate a new task, and
00124     /// put it to the end of the queue, which may be executed by worker
00125     /// thread immediately. That is NOT we want.
00126     bool prepend_task(Task* new_task, bool abort_current);
00127 
00128     /// @brief Remove all tasks in the task queue.
00129     void clear_all();
00130 
00131 private:
00132     /// @brief Abort current task if thread is executing a task.
00133     bool abort_current_task();
00134 
00135     /// @brief Thread functions.
00136     static gpointer thread_func(gpointer args);
00137     gpointer non_static_thread_func();
00138 
00139 private:
00140     GThread          *thread;
00141     std::list<Task*> task_queue;
00142     ThreadCmd        thread_cmd;
00143     Task*            running_task;
00144 
00145     /// @brief Mutexes and conditions
00146     GMutex *queue_mutex;
00147     GCond  *queue_cond;
00148 };
00149 
00150 }; // namespace common
00151 
00152 #endif // THREAD_H
Generated by  doxygen 1.6.2-20100208