Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members

/home/jeroen/FOX/fox/fox-1.7.33/include/FXSpinLock.h
00001 /********************************************************************************
00002 *                                                                               *
00003 *                           S p i n l o c k   C l a s s                         *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 2004,2012 by Jeroen van der Zijp.   All Rights Reserved.        *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or modify          *
00009 * it under the terms of the GNU Lesser General Public License as published by   *
00010 * the Free Software Foundation; either version 3 of the License, or             *
00011 * (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                 *
00016 * GNU Lesser General Public License for more details.                           *
00017 *                                                                               *
00018 * You should have received a copy of the GNU Lesser General Public License      *
00019 * along with this program.  If not, see <http://www.gnu.org/licenses/>          *
00020 ********************************************************************************/
00021 #ifndef FXSPINLOCK_H
00022 #define FXSPINLOCK_H
00023 
00024 
00025 namespace FX {
00026 
00027 
00037 class FXAPI FXSpinLock {
00038 private:
00039   volatile FXuval data[4];
00040 private:
00041   FXSpinLock(const FXSpinLock&);
00042   FXSpinLock &operator=(const FXSpinLock&);
00043 public:
00044 
00046   FXSpinLock();
00047 
00049   void lock();
00050 
00052   FXbool trylock();
00053 
00055   FXbool locked();
00056 
00058   void unlock();
00059 
00061   ~FXSpinLock();
00062   };
00063 
00064 
00073 class FXAPI FXScopedSpinLock {
00074 private:
00075   FXSpinLock& spn;
00076 private:
00077   FXScopedSpinLock();
00078   FXScopedSpinLock(const FXScopedSpinLock&);
00079   FXScopedSpinLock& operator=(const FXScopedSpinLock&);
00080 public:
00081 
00083   FXScopedSpinLock(FXSpinLock& s):spn(s){ lock(); }
00084 
00086   FXSpinLock& spinlock(){ return spn; }
00087 
00089   void lock(){ spn.lock(); }
00090 
00092   FXbool trylock(){ return spn.trylock(); }
00093 
00095   FXbool locked(){ return spn.locked(); }
00096 
00098   void unlock(){ spn.unlock(); }
00099 
00101   ~FXScopedSpinLock(){ unlock(); }
00102   };
00103 
00104 }
00105 
00106 #endif
00107 

Copyright © 1997-2011 Jeroen van der Zijp