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 #include "ArExport.h"
00028
00029 #ifndef WIN32
00030 #include <pthread.h>
00031 #include <unistd.h>
00032 #endif
00033 #include "ariaOSDef.h"
00034 #include "ArASyncTask.h"
00035 #include "ArLog.h"
00036 #include "ArRecurrentTask.h"
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 ArRecurrentTask::ArRecurrentTask()
00049 {
00050 setThreadName("ArRecurrentTask");
00051 running = go_req = killed = false;
00052 create();
00053 }
00054
00055
00056
00057 ArRecurrentTask::~ArRecurrentTask()
00058 {
00059 kill();
00060 }
00061
00062
00063
00064
00065
00066
00067
00068 void *
00069 ArRecurrentTask::runThread(void *ptr)
00070 {
00071 threadStarted();
00072 #ifndef WIN32
00073 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
00074 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
00075 #endif
00076 while (myRunning)
00077 {
00078 bool doit;
00079
00080 while (myRunning)
00081 {
00082 lock();
00083 doit = go_req;
00084 unlock();
00085 if (doit)
00086 break;
00087
00088 #ifndef WIN32
00089 usleep(10000);
00090 #else
00091 Sleep(10);
00092 #endif
00093 }
00094 if (!myRunning)
00095 break;
00096 lock();
00097 go_req = false;
00098 running = true;
00099 unlock();
00100 task();
00101 lock();
00102 running = false;
00103 unlock();
00104 }
00105 return NULL;
00106 }
00107
00108 void ArRecurrentTask::go()
00109 {
00110 lock();
00111 go_req = true;
00112 running = true;
00113 killed = false;
00114 unlock();
00115 }
00116
00117 int ArRecurrentTask::done()
00118 {
00119 lock();
00120 bool is_running = running;
00121 bool is_killed = killed;
00122 unlock();
00123 if (is_running) return 0;
00124 if (is_killed) return 2;
00125 else return 1;
00126 }
00127
00128 void ArRecurrentTask::reset()
00129 {
00130 lock();
00131 go_req = false;
00132 if (running)
00133 {
00134 killed = true;
00135 running = false;
00136 unlock();
00137 cancel();
00138 create();
00139 }
00140 else
00141 unlock();
00142 }
00143
00144 void ArRecurrentTask::kill()
00145 {
00146 lock();
00147 go_req = false;
00148 killed = true;
00149 running = false;
00150 unlock();
00151 cancel();
00152 }