00001 /* 00002 * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. By 00003 * downloading, copying, installing or using the software you agree to 00004 * this license. If you do not agree to this license, do not download, 00005 * install, copy or use the software. 00006 * 00007 * Intel Open Source License 00008 * 00009 * Copyright (c) 2004 Intel Corporation. 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 00013 * met: 00014 * 00015 * Redistributions of source code must retain the above copyright 00016 * notice, this list of conditions and the following disclaimer. 00017 * 00018 * Redistributions in binary form must reproduce the above copyright 00019 * notice, this list of conditions and the following disclaimer in the 00020 * documentation and/or other materials provided with the distribution. 00021 * 00022 * Neither the name of the Intel Corporation nor the names of its 00023 * contributors may be used to endorse or promote products derived from 00024 * this software without specific prior written permission. 00025 * 00026 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00027 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00028 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00029 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR 00030 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00031 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00032 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00033 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00034 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00035 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00036 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00037 */ 00038 00039 #include "SpinLock.h" 00040 #include "../debug/StackTrace.h" 00041 00042 namespace oasys { 00043 00044 bool SpinLock::warn_on_contention_(true); 00045 #ifndef NDEBUG 00046 atomic_t SpinLock::total_spins_(0); 00047 atomic_t SpinLock::total_yields_(0); 00048 #endif 00049 00050 int 00051 SpinLock::lock(const char* lock_user) 00052 { 00053 if (is_locked_by_me()) { 00054 lock_count_.value++; 00055 return 0; 00056 } 00057 00058 atomic_incr(&lock_waiters_); 00059 00060 int nspins = 0; 00061 (void)nspins; 00062 while (atomic_cmpxchg32(&lock_count_, 0, 1) != 0) 00063 { 00064 Thread::spin_yield(); 00065 00066 #ifndef NDEBUG 00067 atomic_incr(&total_spins_); 00068 if (warn_on_contention_ && ++nspins > 1000000) { 00069 fprintf(stderr, 00070 "warning: spin lock held by %s reached spin limit\n", 00071 lock_holder_name_); 00072 StackTrace::print_current_trace(false); 00073 nspins = 0; 00074 } 00075 #endif 00076 } 00077 00078 atomic_decr(&lock_waiters_); 00079 00080 ASSERT(lock_count_.value == 1); 00081 00082 lock_holder_ = Thread::current(); 00083 lock_holder_name_ = lock_user; 00084 00085 return 0; 00086 }; 00087 00088 int 00089 SpinLock::unlock() 00090 { 00091 ASSERT(is_locked_by_me()); 00092 00093 if (lock_count_.value > 1) { 00094 lock_count_.value--; 00095 return 0; 00096 } 00097 00098 lock_holder_ = 0; 00099 lock_holder_name_ = 0; 00100 lock_count_.value = 0; 00101 00102 if (lock_waiters_.value != 0) { 00103 #ifndef NDEBUG 00104 atomic_incr(&total_yields_); 00105 #endif 00106 Thread::spin_yield(); 00107 } 00108 00109 00110 return 0; 00111 }; 00112 00113 int 00114 SpinLock::try_lock(const char* lock_user) 00115 { 00116 if (is_locked_by_me()) { 00117 lock_count_.value++; 00118 return 0; 00119 } 00120 00121 int got_lock = atomic_cmpxchg32(&lock_count_, 0, 1); 00122 00123 if (got_lock) { 00124 ASSERT(lock_holder_ == 0); 00125 00126 lock_holder_ = Thread::current(); 00127 lock_holder_name_ = lock_user; 00128 00129 return 0; // success 00130 00131 } else { 00132 return 1; // already locked 00133 } 00134 }; 00135 00136 } // namespace oasys