![]() |
Main Page
Class Hierarchy
Alphabetical List
Compound List
File List
Compound Members
![]() |
00001 /******************************************************************************** 00002 * * 00003 * Synchronisation mechanism * 00004 * * 00005 ********************************************************************************* 00006 * Copyright (C) 2003 by Mathew Robertson. All Rights Reserved. * 00007 ********************************************************************************* 00008 * This library is free software; you can redistribute it and/or * 00009 * modify it under the terms of the GNU Lesser General Public * 00010 * License as published by the Free Software Foundation; either * 00011 * version 2.1 of the License, or (at your option) any later version. * 00012 * * 00013 * This library is distributed in the hope that it will be useful, * 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00016 * Lesser General Public License for more details. * 00017 * * 00018 * You should have received a copy of the GNU Lesser General Public * 00019 * License along with this library; if not, write to the Free Software * 00020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * 00021 ********************************************************************************/ 00022 #ifndef FXSYNCHRONISE_H 00023 #define FXSYNCHRONISE_H 00024 00025 namespace FXEX { 00026 00027 /** 00028 * The following macros serialises a section of code in FXRunnable and 00029 * derived classes (ie FXThread) 00030 */ 00031 #define FXSYNCHRONISE FXSynchronise _fxsynchronise_lock(*this); 00032 00033 /** 00034 * A Synchronise class serialises access to the class resources, 00035 * during the current block scope. This has the advantage that 00036 * mutex.unlock() will be called automatically when an exception 00037 * is thrown, ie. the 'synchronise' goes out of scope. 00038 * ie. auto lock/unlock of mutex 00039 * 00040 * As an alternative to: 00041 * { 00042 * mutex.lock(); 00043 * ..... 00044 * mutex.unlock(); 00045 * } 00046 * 00047 * you can use a single instance of the FXSynchronise class: 00048 * { 00049 * FXSynchronise(mutex); 00050 * .... 00051 * } 00052 * 00053 * for cases in FXRunnable derived classes, you could use the FXSYNCHRONISE 00054 * macro in FXThread derived clases: 00055 * { 00056 * FXSYNCHRONISE 00057 * ... 00058 * } 00059 */ 00060 class FXAPI FXSynchronise { 00061 private: 00062 FXLockable& lockable; 00063 00064 private: 00065 // dummy copy constructor and operator= to prevent copying 00066 FXSynchronise(const FXSynchronise&); 00067 FXSynchronise& operator=(const FXSynchronise&); 00068 00069 public: 00070 /// on construction, lock the lockable 00071 inline FXSynchronise(FXLockable& l) : lockable(l) { lockable.lock(); } 00072 00073 /// lock the mutex using the FXSYNCHRONISE macro 00074 inline FXSynchronise(FXRunnable& r) : lockable(*r.mutex) { lockable.lock(); } 00075 00076 /// unlock the lockable on destruction 00077 inline ~FXSynchronise() { lockable.unlock(); } 00078 }; 00079 00080 } // namespace FXEX 00081 #endif // FXSYNCHRONISE_H