task.h

Go to the documentation of this file.
00001 /*
00002  * File Name: task.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 TASK_H
00028 #define TASK_H
00029 
00030 #include <stdio.h>
00031 #include "condition.h"
00032 #include "mutex.h"
00033 
00034 namespace pdf
00035 {
00036 
00037 enum TaskType
00038 {
00039     TASK_RENDER = 0,
00040     TASK_SEARCH,
00041     TASK_INVALID
00042 };
00043 
00044 class Task
00045 {
00046 public:
00047     /// Constructor of task.
00048     Task()
00049         : state(INIT)
00050     {
00051     }
00052 
00053     /// Destructor of task.
00054     virtual ~Task()
00055     {
00056     }
00057 
00058     /// Execute task with task context.
00059     virtual void execute() = 0;
00060 
00061     /// get the user data
00062     virtual void* get_user_data() = 0;
00063 
00064     /// get task id
00065     virtual unsigned int get_id() = 0;
00066 
00067     /// get the type of task
00068     TaskType get_type() const { return type; }
00069 
00070     /// Check if the task is aborted or not.
00071     inline bool is_aborted() const { return state == ABORTED; }
00072     void abort() { state = ABORTED; }
00073 
00074     /// Reset the status to "INIT"
00075     void reset() { state = INIT; }
00076 
00077     /// Pause current task, it can be used in search
00078     inline bool is_paused() const { return state == PAUSE; }
00079     void pause() { state = PAUSE; }
00080 
00081 private:
00082     inline bool is_finished() const { return state == FINISHED; }
00083 
00084     /// Abort the task and wait until it's aborted.
00085     void abort_and_wait(Mutex &mutex)
00086     {
00087         ScopeMutex lock(&mutex);
00088         if (is_aborted() || is_finished())
00089         {
00090             return;
00091         }
00092         state = ABORTED;
00093         cancel_cond.wait(mutex.get_gmutex());
00094     }
00095 
00096     /// Emit the end signal to wake up the waiting thread.
00097     /// It's necessary to change the state, which can protect the working thread
00098     /// from waiting forever.
00099     void notify_end()
00100     {
00101         // Make sure the state is either FINISHED or ABORTED.
00102         // So abort_and_wait will never wait when the task is ended.
00103         if (!is_aborted() && !is_paused())
00104         {
00105             state = FINISHED;
00106         }
00107         cancel_cond.signal();
00108     }
00109 
00110 protected:
00111     TaskType  type;           ///< Task type.
00112 
00113 private:
00114     static const int INIT     = 0;
00115     static const int FINISHED = 1;
00116     static const int ABORTED  = 2;
00117     static const int PAUSE    = 3;
00118 
00119 private:
00120     int       state;          ///< Task state.
00121     Cond      cancel_cond;    ///< Maybe should use a better name.
00122 
00123     friend class Thread;
00124 };
00125 
00126 };
00127 
00128 #endif
00129 
Generated by  doxygen 1.6.2-20100208