notepad_commandqueue.h

Go to the documentation of this file.
00001 #ifndef NOTEPAD_COMMANDQUEUE_H__
00002 #define NOTEPAD_COMMANDQUEUE_H__
00003 
00004 /**
00005  * File Name  : notepad_commandqueue.h
00006  *
00007  * Description: command queue class for notepad prerender thread. 
00008  *              and helper classes mutex, scopemutex, counting semaphore.
00009  */
00010 
00011 /*
00012  * This file is part of notepad.
00013  *
00014  * notepad is free software: you can redistribute it and/or modify
00015  * it under the terms of the GNU General Public License as published by
00016  * the Free Software Foundation, either version 2 of the License, or
00017  * (at your option) any later version.
00018  *
00019  * notepad is distributed in the hope that it will be useful,
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00022  * GNU General Public License for more details.
00023  *
00024  * You should have received a copy of the GNU General Public License
00025  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00026  */
00027 
00028 /**
00029  * Copyright (C) 2010 IREX Technologies B.V.
00030  * All rights reserved.
00031  */
00032 
00033 #include <list>
00034 #include <glib.h>
00035 #include <string>
00036 #include <vector>
00037 #include <set>
00038 
00039 
00040 namespace notepad
00041 {
00042 
00043 
00044     // helper classes
00045     class Mutex
00046     {
00047         public:
00048             Mutex()
00049             {
00050                 if (!g_thread_supported ()) g_thread_init (NULL);
00051                 mutex_ = g_mutex_new(); 
00052             }
00053             ~Mutex() { g_mutex_free(mutex_); }
00054 
00055             void lock() { g_mutex_lock(mutex_); }
00056             void unlock() { g_mutex_unlock(mutex_); }
00057             GMutex* get_gmutex() { return mutex_; }
00058         private:
00059             GMutex *mutex_;
00060     };
00061 
00062     class MutexLocker
00063     {
00064         public:
00065             MutexLocker(Mutex *m) : mutex_(m) { mutex_->lock(); }
00066             ~MutexLocker() { mutex_->unlock(); }
00067         private:
00068             Mutex* mutex_;
00069     };
00070 
00071 
00072     class CSemaphore
00073     {
00074         public:
00075             CSemaphore():s(0) { m = g_mutex_new(); c = g_cond_new(); };
00076             CSemaphore(gint v):s(v) { m = g_mutex_new(); c = g_cond_new(); };
00077             ~CSemaphore() { g_mutex_free(m); m=0; g_cond_free(c); c=0; };
00078 
00079             void p()
00080             {
00081                 while ( s == 0 ) 
00082                 {
00083                     g_cond_wait(c, m);
00084                 }
00085                 --s;
00086             }
00087 
00088             void v()
00089             {
00090                 ++s;
00091                 g_cond_signal(c);
00092             }
00093 
00094 
00095             bool try_p() 
00096             { 
00097                 bool ret = false;
00098                 GTimeVal tv = {0,1};
00099                 while ( s == 0 ) 
00100                 {
00101                     ret = g_cond_timed_wait(c, m, &tv);
00102                 }
00103                 --s;
00104                 return ret;
00105             }
00106 
00107             void set(unsigned int n)
00108             {
00109                 s = n;
00110                 g_cond_signal(c);
00111             }
00112 
00113         private: 
00114             int s;
00115             GCond* c;
00116             GMutex* m;
00117     };
00118 
00119 
00120     class Command
00121     {
00122         public:
00123             Command() {}
00124             virtual ~Command() {}
00125             virtual bool execute() = 0; // must be implemented by derived command
00126     };
00127 
00128 
00129 
00130     // The command queue
00131     class CmdQueue
00132     {
00133         public:
00134             CmdQueue();
00135             ~CmdQueue();
00136 
00137             bool start();
00138             void stop();
00139             void add(Command* new_command);
00140             void flush();
00141 
00142         private:
00143             static gpointer thread_func(gpointer args);
00144             gpointer non_static_thread_func();
00145             void empty_queue();
00146 
00147         private:
00148             typedef std::list<Command*>    CommandQueue;
00149             typedef CommandQueue::iterator CommandQueueIter;
00150         private:
00151             GThread*         thread;
00152             CommandQueue     the_queue;
00153             Command*         current_command;
00154             CSemaphore       sem;
00155             bool             stop_flag;
00156             bool             flush_flag;
00157 
00158             // Mutexes and conditions
00159             Mutex            queue_mutex;
00160     };
00161 
00162 };
00163 
00164 
00165 
00166 #endif // NOTEPAD_COMMANDQUEUE_H__
Generated by  doxygen 1.6.2-20100208