pdf::Thread Class Reference

#include <pdf_thread.h>

Collaboration diagram for pdf::Thread:
Collaboration graph
[legend]

Public Member Functions

 Thread ()
 Constructors and destructors.
 ~Thread ()
bool start ()
 Run thread.
void stop (bool cancel_all_tasks=true)
 Stop current thread.
bool append_task (Task *new_task)
 Append the task to the end of the task queue.
bool prepend_task (Task *new_task, bool abort_current)
 Insert the task to the head of the task queue.
void cancel_tasks (void *user_data=0)
 Cancel all of the tasks including current one.
void clear_all (void *user_data=0, TaskType t=TASK_INVALID)
 Remove all tasks in the task queue.
bool abort_task (void *user_data, TaskType t, unsigned int id)
 Abort or remove a given task.

Detailed Description

Definition at line 42 of file pdf_thread.h.


Constructor & Destructor Documentation

pdf::Thread::Thread (  ) 

Constructors and destructors.

Definition at line 41 of file pdf_thread.cpp.

00042     : thread(NULL)
00043     , thread_cmd(CMD_NONE)
00044     , running_task(0)
00045 {
00046 }

pdf::Thread::~Thread (  ) 

Definition at line 48 of file pdf_thread.cpp.

00049 {
00050 }


Member Function Documentation

bool pdf::Thread::abort_task ( void *  user_data,
TaskType  t,
unsigned int  id 
)

Abort or remove a given task.

Definition at line 293 of file pdf_thread.cpp.

References pdf::Task::abort(), pdf::Task::get_id(), pdf::Task::get_type(), and pdf::Task::get_user_data().

Referenced by pdf::PDFController::abort_search().

00294 {
00295     {
00296         ScopeMutex r(&running_task_mutex);
00297         if (running_task != 0 &&
00298             running_task->get_type() == t &&
00299             running_task->get_user_data() == user_data &&
00300             running_task->get_id() == id)
00301         {
00302             // if running task is the one, abort it
00303             running_task->abort();
00304             return true;
00305         }
00306     }
00307 
00308     // search the task in queue
00309     ScopeMutex m(&queue_mutex);
00310     TaskQueueIter idx = task_queue.begin();
00311     while (idx != task_queue.end())
00312     {
00313         if ((*idx)->get_type() == t &&
00314             (*idx)->get_user_data() == user_data &&
00315             (*idx)->get_id() == id)
00316         {
00317             delete *idx;
00318             task_queue.erase(idx);
00319             return true;
00320         }
00321         idx++;
00322     }
00323 
00324     return false;
00325 }

Here is the call graph for this function:

Here is the caller graph for this function:

bool pdf::Thread::append_task ( Task new_task  ) 

Append the task to the end of the task queue.

Definition at line 204 of file pdf_thread.cpp.

References pdf::Cond::signal().

Referenced by pdf::PDFLibrary::thread_add_render_task().

00205 {
00206     if (thread_cmd == CMD_NONE)
00207     {
00208         // Append task to the end of the task queue.
00209         {
00210             ScopeMutex m(&queue_mutex);
00211             task_queue.push_back(new_task);
00212         }
00213 
00214         // Tells the worker thread that a new task is available.
00215         queue_cond.signal();
00216         return true;
00217     }
00218 
00219     return false;
00220 }

Here is the call graph for this function:

Here is the caller graph for this function:

void pdf::Thread::cancel_tasks ( void *  user_data = 0  ) 

Cancel all of the tasks including current one.

Parameters:
user_data if this value is not null, thread will clear all of the tasks with this value.

Definition at line 107 of file pdf_thread.cpp.

References clear_all(), and pdf::Task::get_user_data().

Referenced by pdf::PDFLibrary::remove_tasks_by_document().

00108 {
00109     clear_all(user_data);
00110 
00111     ScopeMutex r(&running_task_mutex);
00112     if (running_task != 0 &&
00113         running_task->get_user_data() == user_data)
00114     {
00115         running_task->abort_and_wait(cancel_task_mutex);
00116     }
00117 }

Here is the call graph for this function:

Here is the caller graph for this function:

void pdf::Thread::clear_all ( void *  user_data = 0,
TaskType  t = TASK_INVALID 
)

Remove all tasks in the task queue.

Definition at line 119 of file pdf_thread.cpp.

References pdf::TASK_INVALID.

Referenced by cancel_tasks(), stop(), and pdf::PDFLibrary::thread_cancel_render_tasks().

00120 {
00121     ScopeMutex m(&queue_mutex);
00122 
00123     TaskQueueIter idx = task_queue.begin();
00124     while (idx != task_queue.end())
00125     {
00126         bool is_remove = false;
00127         if (user_data == 0)
00128         {
00129             is_remove = true;
00130         }
00131         else if (user_data == (*idx)->get_user_data())
00132         {
00133             if (t == TASK_INVALID || (t == (*idx)->type))
00134             {
00135                 is_remove = true;
00136             }
00137         }
00138 
00139         if (is_remove)
00140         {
00141             delete *idx;
00142             idx = task_queue.erase(idx);
00143         }
00144         else
00145         {
00146             idx++;
00147         }
00148     }
00149 
00150     if (user_data == 0)
00151     {
00152         task_queue.clear();
00153     }
00154 }

Here is the caller graph for this function:

bool pdf::Thread::prepend_task ( Task new_task,
bool  abort_current 
)

Insert the task to the head of the task queue.

Parameters:
abort_current This flag tells the thread that after inserting the new task to the head of the queue, abort current running task immediately. We should NOT give user a capability to abort the running task because the aborted task may generate a new task, and put it to the end of the queue, which may be executed by worker thread immediately. That is NOT we want.

Definition at line 222 of file pdf_thread.cpp.

References pdf::Cond::signal().

Referenced by pdf::PDFLibrary::thread_add_render_task(), and pdf::PDFLibrary::thread_add_search_task().

00223 {
00224     if (thread_cmd == CMD_NONE)
00225     {
00226         // Insert task to the beginning of the task queue.
00227         ScopeMutex m(&queue_mutex);
00228         task_queue.push_front(new_task);
00229 
00230         // Tells the worker thread that a new task is available.
00231         queue_cond.signal();
00232 
00233         if (abort_current)
00234         {
00235             abort_current_task(new_task);
00236         }
00237 
00238         return true;
00239     }
00240 
00241     return false;
00242 }

Here is the call graph for this function:

Here is the caller graph for this function:

bool pdf::Thread::start (  ) 

Run thread.

Definition at line 156 of file pdf_thread.cpp.

00157 {
00158     if (thread != NULL)
00159     {
00160         // The thread has been started.
00161         return false;
00162     }
00163 
00164     // Reset the thread cmd. TODO: Replace this variable by a boolean.
00165     thread_cmd = CMD_NONE;
00166     
00167     thread = g_thread_create(thread_func, this, TRUE, NULL);
00168     return thread != NULL;
00169 }

void pdf::Thread::stop ( bool  cancel_all_tasks = true  ) 

Stop current thread.

TODO, still a problem here. running task must be associated with document. Can not use cancle all tasks directly.

Definition at line 173 of file pdf_thread.cpp.

References clear_all(), and pdf::Cond::signal().

Referenced by pdf::PDFLibrary::~PDFLibrary().

00174 {
00175     if (thread == NULL)
00176     {
00177         return;
00178     }
00179 
00180     thread_cmd = cancel_all_tasks ? CMD_TERMINATE : CMD_STOP;
00181 
00182     {
00183         ScopeMutex r(&running_task_mutex);
00184         if (running_task != 0)
00185         {
00186             if (cancel_all_tasks)
00187             {
00188                 running_task->abort_and_wait(cancel_task_mutex);
00189             }
00190         }
00191     }
00192 
00193     queue_cond.signal();
00194 
00195     // Wait for worker thread to die.
00196     g_thread_join(thread);
00197 
00198     clear_all();
00199 
00200     // Set the thread to be NULL
00201     thread = NULL;
00202 }

Here is the call graph for this function:

Here is the caller graph for this function:


The documentation for this class was generated from the following files:
Generated by  doxygen 1.6.2-20100208