![]() |
pl-nk v0.4.5
Plonk|Plink|Plank are a set of cross-platform C/C++ frameworks for audio software development
|
00001 /* 00002 ------------------------------------------------------------------------------- 00003 This file is part of the Plink, Plonk, Plank libraries 00004 by Martin Robinson 00005 00006 http://code.google.com/p/pl-nk/ 00007 00008 Copyright University of the West of England, Bristol 2011-14 00009 All rights reserved. 00010 00011 Redistribution and use in source and binary forms, with or without 00012 modification, are permitted provided that the following conditions are met: 00013 00014 * Redistributions of source code must retain the above copyright 00015 notice, this list of conditions and the following disclaimer. 00016 * Redistributions in binary form must reproduce the above copyright 00017 notice, this list of conditions and the following disclaimer in the 00018 documentation and/or other materials provided with the distribution. 00019 * Neither the name of University of the West of England, Bristol nor 00020 the names of its contributors may be used to endorse or promote products 00021 derived from this software without specific prior written permission. 00022 00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 00024 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00025 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00026 DISCLAIMED. IN NO EVENT SHALL UNIVERSITY OF THE WEST OF ENGLAND, BRISTOL BE 00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00033 00034 This software makes use of third party libraries. For more information see: 00035 doc/license.txt included in the distribution. 00036 ------------------------------------------------------------------------------- 00037 */ 00038 00039 #ifndef PLANK_THREADSPINLOCK_H 00040 #define PLANK_THREADSPINLOCK_H 00041 00042 #include "../containers/atomic/plank_Atomic.h" 00043 #include "plank_Thread.h" 00044 00045 #define PLANK_THREADSPINLOCK_UNLOCKED 0 // assumes 0 is an invalid thread ID, should be OK on posix & windows 00046 00047 PLANK_BEGIN_C_LINKAGE 00048 00057 typedef struct PlankThreadSpinLock* PlankThreadSpinLockRef; 00058 00061 PlankThreadSpinLockRef pl_ThreadSpinLock_CreateAndInit(); 00062 00065 PlankThreadSpinLockRef pl_ThreadSpinLock_Create(); 00066 00070 PlankResult pl_ThreadSpinLock_Init (PlankThreadSpinLockRef p); 00071 00075 PlankResult pl_ThreadSpinLock_DeInit (PlankThreadSpinLockRef p); 00076 00080 PlankResult pl_ThreadSpinLock_Destroy (PlankThreadSpinLockRef p); 00081 00086 void pl_ThreadSpinLock_Lock (PlankThreadSpinLockRef p); 00087 00091 PlankB pl_ThreadSpinLock_Unlock (PlankThreadSpinLockRef p); 00092 00096 static PlankB pl_ThreadSpinLock_TryLock (PlankThreadSpinLockRef p); 00097 00101 void pl_ThreadSpinLock_Wait (PlankThreadSpinLockRef p); 00102 00103 void pl_ThreadSpinLock_WaitTimeout (PlankThreadSpinLockRef p, double time); 00104 00108 void pl_ThreadSpinLock_Signal (PlankThreadSpinLockRef p); 00109 00112 PLANK_END_C_LINKAGE 00113 00114 #if !DOXYGEN 00115 typedef struct PlankThreadSpinLock 00116 { 00117 PLANK_ALIGN(8) PlankAtomicL flag; 00118 } PlankThreadSpinLock; 00119 #endif 00120 00121 static inline PlankB pl_ThreadSpinLock_TryLock (PlankThreadSpinLockRef p) 00122 { 00123 PlankThreadID threadID = pl_ThreadCurrentID(); 00124 00125 if (pl_AtomicL_CompareAndSwap (&p->flag, PLANK_THREADSPINLOCK_UNLOCKED, threadID)) 00126 return PLANK_TRUE; 00127 else 00128 return pl_AtomicL_CompareAndSwap (&p->flag, threadID, threadID); // stay locked on same thread 00129 } 00130 00131 00132 #endif // PLANK_ThreadSpinLock_H