00001 /* 00002 * mace.cc : part of the Mace toolkit for building distributed systems 00003 * 00004 * Copyright (c) 2007, Charles Killian, Dejan Kostic, Ryan Braud, James W. Anderson, John Fisher-Ogden, Calvin Hubble, Duy Nguyen, Justin Burke, David Oppenheimer, Amin Vahdat, Adolfo Rodriguez, Sooraj Bhat 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions are met: 00009 * 00010 * * Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * * Redistributions in binary form must reproduce the above copyright 00013 * notice, this list of conditions and the following disclaimer in 00014 * the documentation and/or other materials provided with the 00015 * distribution. 00016 * * Neither the names of Duke University nor The University of 00017 * California, San Diego, nor the names of the authors or contributors 00018 * may be used to endorse or promote products derived from 00019 * this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00022 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00023 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00024 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 00025 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00026 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00027 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00028 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00029 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 00030 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 * 00032 * ----END-OF-LEGAL-STUFF---- */ 00033 #include <errno.h> 00034 #include "mace.h" 00035 #include "ScopedStackExecution.h" 00036 00037 static pthread_mutex_t getRecursiveMutex() { 00038 pthread_mutex_t mutex; 00039 pthread_mutexattr_t mutexattr; 00040 pthread_mutexattr_init(&mutexattr); 00041 pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE); 00042 pthread_mutex_init(&mutex, &mutexattr); 00043 return mutex; 00044 } 00045 00046 pthread_mutex_t BaseMaceService::agentlock = getRecursiveMutex(); 00047 bool BaseMaceService::_printLower = false; 00048 00049 BaseMaceService::BaseMaceService() 00050 { 00051 } 00052 00053 #define AGENT_LOCK_DEBUG 0 00054 #define AGENT_LOCK_TIME 0 00055 // ---------------------------------------------- 00056 // lock functions 00057 // ---------------------------------------------- 00058 00059 void BaseMaceService::Lock () 00060 { 00061 #if AGENT_LOCK_DEBUG > 1 00062 printf("%d Agent_lock in : %d %d %x %d %x %d \n", pthread_self(), 00063 agentlock.__m_reserved, 00064 agentlock.__m_count, 00065 agentlock.__m_owner, 00066 agentlock.__m_kind, 00067 agentlock.__m_lock.__status, 00068 agentlock.__m_lock.__spinlock 00069 ); 00070 // fflush(stdout); 00071 #endif 00072 pthread_mutex_lock(&(agentlock)); 00073 #if AGENT_LOCK_DEBUG > 0 00074 printf("%d Agent_lock out %d\n" , pthread_self(),lock_count); 00075 #endif 00076 } 00077 00078 // ---------------------------------------------- 00079 // unlock 00080 // ---------------------------------------------- 00081 00082 void BaseMaceService::Unlock () 00083 { 00084 #if AGENT_LOCK_DEBUG > 1 00085 printf("%d Agent_unlock in : %d %d %x %d %x %d \n", pthread_self(), 00086 agentlock.__m_reserved, 00087 agentlock.__m_count, 00088 agentlock.__m_owner, 00089 agentlock.__m_kind, 00090 agentlock.__m_lock.__status, 00091 agentlock.__m_lock.__spinlock 00092 ); 00093 #endif 00094 pthread_mutex_unlock(&(agentlock)); 00095 #if AGENT_LOCK_DEBUG > 0 00096 printf("%d Agent_unlock out %d %d\n", pthread_self(), lock_count,lock_owner ); 00097 #endif 00098 } 00099 00100 namespace mace { 00101 int ScopedStackExecution::stack = 0; 00102 ScopedStackExecution::ServiceList ScopedStackExecution::needDefer; 00103 }