| 1 | // ePDFView - A lightweight PDF Viewer. |
|---|
| 2 | // Copyright (C) 2006 Emma's Software. |
|---|
| 3 | // |
|---|
| 4 | // This program is free software; you can redistribute it and/or modify |
|---|
| 5 | // it under the terms of the GNU General Public License as published by |
|---|
| 6 | // the Free Software Foundation; either version 2 of the License, or |
|---|
| 7 | // (at your option) any later version. |
|---|
| 8 | // |
|---|
| 9 | // This program is distributed in the hope that it will be useful, |
|---|
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 12 | // GNU General Public License for more details. |
|---|
| 13 | // |
|---|
| 14 | // You should have received a copy of the GNU General Public License |
|---|
| 15 | // along with this program; if not, write to the Free Software |
|---|
| 16 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 17 | |
|---|
| 18 | #include "epdfview.h" |
|---|
| 19 | #include <unistd.h> |
|---|
| 20 | |
|---|
| 21 | using namespace ePDFView; |
|---|
| 22 | |
|---|
| 23 | /// The queue of jobs to run in background. |
|---|
| 24 | GAsyncQueue *IJob::m_JobsQueue = NULL; |
|---|
| 25 | |
|---|
| 26 | /// |
|---|
| 27 | /// @brief Clears the list of jobs. |
|---|
| 28 | /// |
|---|
| 29 | /// This is mainly used for the test suites when they are going |
|---|
| 30 | /// to delete the document class and don't want to generate segmentation |
|---|
| 31 | /// faults. It just pops all queued jobs without run them. |
|---|
| 32 | /// |
|---|
| 33 | void |
|---|
| 34 | IJob::clearQueue (void) |
|---|
| 35 | { |
|---|
| 36 | IJob *job = NULL; |
|---|
| 37 | do |
|---|
| 38 | { |
|---|
| 39 | job = (IJob *)g_async_queue_try_pop (m_JobsQueue); |
|---|
| 40 | } |
|---|
| 41 | while ( NULL != job ); |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | /// |
|---|
| 45 | /// @brief The job dispatcher. |
|---|
| 46 | /// |
|---|
| 47 | /// This function is the one that will run in a thread. What it does is |
|---|
| 48 | /// just pop queued jobs from the queue and then runs them. |
|---|
| 49 | /// |
|---|
| 50 | /// @param data The asynchronous queue where the jobs will be enqueued. |
|---|
| 51 | /// |
|---|
| 52 | gpointer |
|---|
| 53 | IJob::dispatcher (gpointer data) |
|---|
| 54 | { |
|---|
| 55 | while (true) |
|---|
| 56 | { |
|---|
| 57 | IJob *job = (IJob *)g_async_queue_pop (m_JobsQueue); |
|---|
| 58 | if ( job->run () ) |
|---|
| 59 | { |
|---|
| 60 | delete job; |
|---|
| 61 | } |
|---|
| 62 | } |
|---|
| 63 | sleep(0); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | /// |
|---|
| 67 | /// @brief Initialises the job dispatcher. |
|---|
| 68 | /// |
|---|
| 69 | /// This function must be the first called before any other jobs-related |
|---|
| 70 | /// function, as it does initialised the thread subsystem, created the |
|---|
| 71 | /// threaded dispatcher() function and initialised the job queue. |
|---|
| 72 | /// |
|---|
| 73 | void |
|---|
| 74 | IJob::init () |
|---|
| 75 | { |
|---|
| 76 | if ( !g_thread_supported () ) |
|---|
| 77 | { |
|---|
| 78 | g_thread_init (NULL); |
|---|
| 79 | } |
|---|
| 80 | m_JobsQueue = g_async_queue_new (); |
|---|
| 81 | GError *error = NULL; |
|---|
| 82 | if ( NULL == g_thread_create (IJob::dispatcher, NULL, FALSE, &error) ) |
|---|
| 83 | { |
|---|
| 84 | g_error ("Couldn't create the dispatcher thread: %s\n", error->message); |
|---|
| 85 | } |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | /// |
|---|
| 89 | /// @brief Adds a new job to the queue. |
|---|
| 90 | /// |
|---|
| 91 | /// It adds a new job to the queued jobs that will be dispatched by |
|---|
| 92 | /// the dispatch() function. |
|---|
| 93 | /// |
|---|
| 94 | /// @param job The job to add to the queue. |
|---|
| 95 | /// |
|---|
| 96 | void |
|---|
| 97 | IJob::enqueue (IJob *job) |
|---|
| 98 | { |
|---|
| 99 | g_assert ( NULL != job && "Tried to queue a NULL job."); |
|---|
| 100 | |
|---|
| 101 | g_async_queue_push (m_JobsQueue, (gpointer)job); |
|---|
| 102 | } |
|---|