spin_mutex.h

00001 /*
00002     Copyright 2005-2009 Intel Corporation.  All Rights Reserved.
00003 
00004     The source code contained or described herein and all documents related
00005     to the source code ("Material") are owned by Intel Corporation or its
00006     suppliers or licensors.  Title to the Material remains with Intel
00007     Corporation or its suppliers and licensors.  The Material is protected
00008     by worldwide copyright laws and treaty provisions.  No part of the
00009     Material may be used, copied, reproduced, modified, published, uploaded,
00010     posted, transmitted, distributed, or disclosed in any way without
00011     Intel's prior express written permission.
00012 
00013     No license under any patent, copyright, trade secret or other
00014     intellectual property right is granted to or conferred upon you by
00015     disclosure or delivery of the Materials, either expressly, by
00016     implication, inducement, estoppel or otherwise.  Any license under such
00017     intellectual property rights must be express and approved by Intel in
00018     writing.
00019 */
00020 
00021 #ifndef __TBB_spin_mutex_H
00022 #define __TBB_spin_mutex_H
00023 
00024 #include <cstddef>
00025 #include "tbb_stddef.h"
00026 #include "tbb_machine.h"
00027 #include "tbb_profiling.h"
00028 
00029 namespace tbb {
00030 
00032 
00037 class spin_mutex {
00039     unsigned char flag;
00040 
00041 public:
00043 
00044     spin_mutex() : flag(0) {
00045 #if TBB_USE_THREADING_TOOLS
00046         internal_construct();
00047 #endif
00048     }
00049 
00051     class scoped_lock : internal::no_copy {
00052     private:
00054         spin_mutex* my_mutex; 
00055 
00057         internal::uintptr my_unlock_value;
00058 
00060         void __TBB_EXPORTED_METHOD internal_acquire( spin_mutex& m );
00061 
00063         bool __TBB_EXPORTED_METHOD internal_try_acquire( spin_mutex& m );
00064 
00066         void __TBB_EXPORTED_METHOD internal_release();
00067 
00068     public:
00070         scoped_lock() : my_mutex(NULL), my_unlock_value(0) {}
00071 
00073         scoped_lock( spin_mutex& m ) { 
00074 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
00075             my_mutex=NULL;
00076             internal_acquire(m);
00077 #else
00078             my_unlock_value = __TBB_LockByte(m.flag);
00079             my_mutex=&m;
00080 #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT*/
00081         }
00082 
00084         void acquire( spin_mutex& m ) {
00085 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
00086             internal_acquire(m);
00087 #else
00088             my_unlock_value = __TBB_LockByte(m.flag);
00089             my_mutex = &m;
00090 #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT*/
00091         }
00092 
00094         bool try_acquire( spin_mutex& m ) {
00095 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
00096             return internal_try_acquire(m);
00097 #else
00098             bool result = __TBB_TryLockByte(m.flag);
00099             if( result ) {
00100                 my_unlock_value = 0;
00101                 my_mutex = &m;
00102             }
00103             return result;
00104 #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT*/
00105         }
00106 
00108         void release() {
00109 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
00110             internal_release();
00111 #else
00112             __TBB_store_with_release(my_mutex->flag, static_cast<unsigned char>(my_unlock_value));
00113             my_mutex = NULL;
00114 #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT */
00115         }
00116 
00118         ~scoped_lock() {
00119             if( my_mutex ) {
00120 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
00121                 internal_release();
00122 #else
00123                 __TBB_store_with_release(my_mutex->flag, static_cast<unsigned char>(my_unlock_value));
00124 #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT */
00125             }
00126         }
00127     };
00128 
00129     void __TBB_EXPORTED_METHOD internal_construct();
00130 
00131     // Mutex traits
00132     static const bool is_rw_mutex = false;
00133     static const bool is_recursive_mutex = false;
00134     static const bool is_fair_mutex = false;
00135 
00136     friend class scoped_lock;
00137 };
00138 
00139 __TBB_DEFINE_PROFILING_SET_NAME(spin_mutex)
00140 
00141 } // namespace tbb
00142 
00143 #endif /* __TBB_spin_mutex_H */

Copyright © 2005-2009 Intel Corporation. All Rights Reserved.

Intel, Pentium, Intel Xeon, Itanium, Intel XScale and VTune are registered trademarks or trademarks of Intel Corporation or its subsidiaries in the United States and other countries.

* Other names and brands may be claimed as the property of others.