00001 /* 00002 FSF: Flow Scheduling Framework library 00003 00004 Version 1.0 Copyright (C) 2010 Alexandre R.J. Francois 00005 00006 Previous versions Copyright (C) 2001-2005 University of Southern California 00007 00008 Author: Alexandre R.J. Francois - alexandrefrancois@yahoo.com 00009 www.alexandrefrancois.org 00010 00011 Modular Flow Scheduling Middleware 00012 mfsm.sourceForge.net 00013 00014 This library is free software; you can redistribute it and/or 00015 modify it under the terms of the GNU Lesser General Public 00016 License as published by the Free Software Foundation; either 00017 version 2.1 of the License, or (at your option) any later version. 00018 00019 This library 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 GNU 00022 Lesser General Public License for more details. 00023 00024 You should have received a copy of the GNU Lesser General Public 00025 License along with this library; if not, write to the Free Software 00026 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00027 */ 00028 00054 00055 00058 00059 00060 #ifndef FSF_H 00061 #define FSF_H 00062 00063 #include "pthread.h" 00064 #include "sched.h" 00065 #include "semaphore.h" 00066 00067 namespace fsf{ 00068 00071 class CMutex{ 00072 protected: 00073 pthread_mutex_t m_mutex; 00074 public: 00076 CMutex() { pthread_mutex_init(&m_mutex,NULL); } 00078 ~CMutex() { pthread_mutex_destroy(&m_mutex); } 00080 void lock() { pthread_mutex_lock(&m_mutex); } 00082 void unlock() { pthread_mutex_unlock(&m_mutex); } 00083 }; 00084 00087 class CSemaphore{ 00088 protected: 00089 sem_t m_semaphore; 00090 public: 00092 CSemaphore() { sem_init(&m_semaphore,0,0); } 00094 ~CSemaphore() { sem_destroy(&m_semaphore); } 00096 void wait() { sem_wait(&m_semaphore); } 00098 void tryWait() { sem_trywait(&m_semaphore); } 00100 void post() { sem_post(&m_semaphore); } 00101 }; 00102 00105 class CCondition{ 00106 protected: 00107 pthread_mutex_t m_mutex; 00108 pthread_cond_t m_cond; 00109 public: 00111 CCondition(){ 00112 pthread_mutex_init(&m_mutex,NULL); 00113 pthread_cond_init(&m_cond,NULL); 00114 } 00115 ~CCondition(){ 00116 pthread_mutex_destroy(&m_mutex); 00117 pthread_cond_destroy(&m_cond); 00118 } 00120 void lock() { pthread_mutex_lock(&m_mutex); } 00122 void unlock() { pthread_mutex_unlock(&m_mutex); } 00124 void wait() { pthread_cond_wait(&m_cond,&m_mutex); } 00126 void timedWait(const struct timespec *timeout) { pthread_cond_timedwait(&m_cond,&m_mutex,timeout); } 00128 void signal() { pthread_cond_signal(&m_cond); } 00130 void broadcast() { pthread_cond_broadcast(&m_cond); } 00131 }; 00132 00134 typedef double Time; 00135 00137 struct DeleteObject{ 00139 template<typename T> 00140 void operator()(const T* ptr) const { 00141 delete ptr; 00142 } 00143 }; 00144 00145 } // namespace fsf 00146 00147 #endif // FSF_H 00148