common::Thread Class Reference

#include <thread.h>

Collaboration diagram for common::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 clear_all ()
 Remove all tasks in the task queue.

Detailed Description

Definition at line 103 of file thread.h.


Constructor & Destructor Documentation

common::Thread::Thread (  ) 

Constructors and destructors.

Definition at line 44 of file thread.cpp.

00047 : thread(NULL),
00048   thread_cmd(CMD_NONE),
00049   running_task(0)
00050 {
00051     queue_mutex = g_mutex_new();

common::Thread::~Thread (  ) 

Definition at line 53 of file thread.cpp.

00056 {
00057     g_mutex_free(queue_mutex);


Member Function Documentation

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

Append the task to the end of the task queue.

Definition at line 164 of file thread.cpp.

Referenced by text::TextController::paginate().

00167 {
00168     if (thread_cmd == CMD_NONE)
00169     {
00170         // Append task to the end of the task queue.
00171         g_mutex_lock(queue_mutex);
00172         task_queue.push_back(new_task);
00173         g_mutex_unlock(queue_mutex);
00174 
00175         // Tells the worker thread that a new task is available.
00176         g_cond_signal(queue_cond);
00177         return true;
00178     }
00179     else
00180     {
00181         delete new_task;
00182         return false;

Here is the caller graph for this function:

void common::Thread::clear_all (  ) 

Remove all tasks in the task queue.

Definition at line 109 of file thread.cpp.

Referenced by images::ImagesRenderer::render().

00112 {
00113     g_mutex_lock(queue_mutex);
00114 
00115     std::list<Task*>::iterator it = task_queue.begin();
00116     while (it != task_queue.end())
00117     {
00118         delete *it;
00119         it = task_queue.erase(it);
00120     }
00121 

Here is the caller graph for this function:

bool common::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 184 of file thread.cpp.

Referenced by text::TextController::pause_pagination(), text::TextController::render(), images::ImagesRenderer::render(), and text::TextController::search().

00187 {
00188     if (thread_cmd == CMD_NONE)
00189     {
00190         // Insert task to the beginning of the task queue.
00191         g_mutex_lock(queue_mutex);
00192         task_queue.push_front(new_task);
00193 
00194         if (abort_current)
00195         {
00196             abort_current_task();
00197         }
00198 
00199         g_mutex_unlock(queue_mutex);
00200 
00201         // Tells the worker thread that a new task is available.
00202         g_cond_signal(queue_cond);
00203 
00204         return true;
00205     }
00206     else
00207     {
00208         delete new_task;
00209         return false;

Here is the caller graph for this function:

bool common::Thread::start (  ) 

Run thread.

Definition at line 123 of file thread.cpp.

Referenced by images::ImagesRenderer::ImagesRenderer(), and text::TextController::start().

00126 {
00127     if (thread != NULL)
00128     {
00129         // The thread has been started.
00130         return false;
00131     }
00132 
00133     thread = g_thread_create(thread_func, this, TRUE, NULL);

Here is the caller graph for this function:

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

Stop current thread.

Definition at line 135 of file thread.cpp.

Referenced by text::TextController::stop(), images::ImagesRenderer::stop(), and images::ImagesRenderer::~ImagesRenderer().

00138 {
00139     if (thread == NULL)
00140     {
00141         return;
00142     }
00143 
00144     thread_cmd = cancel_all_tasks ? CMD_TERMINATE : CMD_STOP;
00145 
00146     if (running_task != 0)
00147     {
00148         if (cancel_all_tasks)
00149         {
00150             running_task->send_abort_request();
00151         }
00152     }
00153     else
00154     {
00155         // Woker thread is waiting for a task, wake it up.
00156         g_cond_signal(queue_cond);
00157     }
00158 
00159     // Wait for worker thread to die.
00160     g_thread_join(thread);
00161 
00162     // Set the thread to be NULL

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