Main Page | Modules | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | Related Pages

spin_mutex.h

00001 /*
00002     Copyright 2005-2008 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/tbb_machine.h"
00027 
00028 namespace tbb {
00029 
00031 
00036 class spin_mutex {
00038     unsigned char flag;
00039 
00040 public:
00042 
00043     spin_mutex() : flag(0) {}
00044 
00046     class scoped_lock : private internal::no_copy {
00047     private:
00049         spin_mutex* my_mutex; 
00050 
00052         internal::uintptr my_unlock_value;
00053 
00055         void internal_acquire( spin_mutex& m );
00056 
00058         bool internal_try_acquire( spin_mutex& m );
00059 
00061         void internal_release();
00062 
00063     public:
00065         scoped_lock() : my_mutex(NULL), my_unlock_value(0) {}
00066 
00068         scoped_lock( spin_mutex& m ) { 
00069 #if TBB_DO_THREADING_TOOLS||TBB_DO_ASSERT
00070             my_mutex=NULL;
00071             internal_acquire(m);
00072 #else
00073             my_unlock_value = __TBB_LockByte(m.flag);
00074             my_mutex=&m;
00075 #endif /* TBB_DO_THREADING_TOOLS||TBB_DO_ASSERT*/
00076         }
00077 
00079         void acquire( spin_mutex& m ) {
00080 #if TBB_DO_THREADING_TOOLS||TBB_DO_ASSERT
00081             internal_acquire(m);
00082 #else
00083             my_unlock_value = __TBB_LockByte(m.flag);
00084             my_mutex = &m;
00085 #endif /* TBB_DO_THREADING_TOOLS||TBB_DO_ASSERT*/
00086         }
00087 
00089         bool try_acquire( spin_mutex& m ) {
00090 #if TBB_DO_THREADING_TOOLS||TBB_DO_ASSERT
00091             return internal_try_acquire(m);
00092 #else
00093             bool result = __TBB_TryLockByte(m.flag);
00094             if( result ) {
00095                 my_unlock_value = 0;
00096                 my_mutex = &m;
00097             }
00098             return result;
00099 #endif /* TBB_DO_THREADING_TOOLS||TBB_DO_ASSERT*/
00100         }
00101 
00103         void release() {
00104 #if TBB_DO_THREADING_TOOLS||TBB_DO_ASSERT
00105             internal_release();
00106 #else
00107             __TBB_store_with_release(my_mutex->flag, static_cast<unsigned char>(my_unlock_value));
00108             my_mutex = NULL;
00109 #endif /* TBB_DO_THREADING_TOOLS||TBB_DO_ASSERT */
00110         }
00111 
00113         ~scoped_lock() {
00114             if( my_mutex ) {
00115 #if TBB_DO_THREADING_TOOLS||TBB_DO_ASSERT
00116                 internal_release();
00117 #else
00118                 __TBB_store_with_release(my_mutex->flag, static_cast<unsigned char>(my_unlock_value));
00119 #endif /* TBB_DO_THREADING_TOOLS||TBB_DO_ASSERT */
00120             }
00121         }
00122     };
00123 
00124     // Mutex traits
00125     static const bool is_rw_mutex = false;
00126     static const bool is_recursive_mutex = false;
00127     static const bool is_fair_mutex = false;
00128 
00129     friend class scoped_lock;
00130 };
00131 
00132 } // namespace tbb
00133 
00134 #endif /* __TBB_spin_mutex_H */

Copyright © 2005-2008 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.