task.h
Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
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 
00048     Task()
00049         : state(INIT)
00050     {
00051     }
00052 
00053 
00054     virtual ~Task()
00055     {
00056     }
00057 
00058 
00059     virtual void execute() = 0;
00060 
00061 
00062     virtual void* get_user_data() = 0;
00063 
00064 
00065     virtual unsigned int get_id() = 0;
00066 
00067 
00068     TaskType get_type() const { return type; }
00069 
00070 
00071     inline bool is_aborted() const { return state == ABORTED; }
00072     void abort() { state = ABORTED; }
00073 
00074 
00075     void reset() { state = INIT; }
00076 
00077 
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 
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 
00097 
00098 
00099     void notify_end()
00100     {
00101         
00102         
00103         if (!is_aborted() && !is_paused())
00104         {
00105             state = FINISHED;
00106         }
00107         cancel_cond.signal();
00108     }
00109 
00110 protected:
00111     TaskType  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;          
00121     Cond      cancel_cond;    
00122 
00123     friend class Thread;
00124 };
00125 
00126 };
00127 
00128 #endif
00129