00001 /** @file 00002 00003 A brief file description 00004 00005 @section license License 00006 00007 Licensed to the Apache Software Foundation (ASF) under one 00008 or more contributor license agreements. See the NOTICE file 00009 distributed with this work for additional information 00010 regarding copyright ownership. The ASF licenses this file 00011 to you under the Apache License, Version 2.0 (the 00012 "License"); you may not use this file except in compliance 00013 with the License. You may obtain a copy of the License at 00014 00015 http://www.apache.org/licenses/LICENSE-2.0 00016 00017 Unless required by applicable law or agreed to in writing, software 00018 distributed under the License is distributed on an "AS IS" BASIS, 00019 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00020 See the License for the specific language governing permissions and 00021 limitations under the License. 00022 */ 00023 00024 #ifndef _ink_mutex_h_ 00025 #define _ink_mutex_h_ 00026 00027 /*********************************************************************** 00028 00029 Fast Mutex 00030 00031 Uses atomic memory operations to minimize blocking. 00032 00033 00034 ***********************************************************************/ 00035 #include <stdio.h> 00036 00037 #include "ink_defs.h" 00038 00039 #if defined(POSIX_THREAD) 00040 #include <pthread.h> 00041 #include <stdlib.h> 00042 00043 typedef pthread_mutex_t ink_mutex; 00044 00045 // just a wrapper so that the constructor gets executed 00046 // before the first call to ink_mutex_init(); 00047 class x_pthread_mutexattr_t 00048 { 00049 public: 00050 pthread_mutexattr_t attr; 00051 x_pthread_mutexattr_t(); 00052 ~x_pthread_mutexattr_t() 00053 { 00054 } 00055 }; 00056 inline 00057 x_pthread_mutexattr_t::x_pthread_mutexattr_t() 00058 { 00059 pthread_mutexattr_init(&attr); 00060 #ifndef POSIX_THREAD_10031c 00061 pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); 00062 #endif 00063 } 00064 00065 extern class x_pthread_mutexattr_t _g_mattr; 00066 00067 static inline int 00068 ink_mutex_init(ink_mutex * m, const char *name) 00069 { 00070 (void) name; 00071 00072 #if defined(solaris) 00073 if ( pthread_mutex_init(m, NULL) != 0 ) { 00074 abort(); 00075 } 00076 #else 00077 if (pthread_mutex_init(m, &_g_mattr.attr) != 0) { 00078 abort(); 00079 } 00080 #endif 00081 return 0; 00082 } 00083 00084 static inline int 00085 ink_mutex_destroy(ink_mutex * m) 00086 { 00087 return pthread_mutex_destroy(m); 00088 } 00089 00090 static inline int 00091 ink_mutex_acquire(ink_mutex * m) 00092 { 00093 if (pthread_mutex_lock(m) != 0) { 00094 abort(); 00095 } 00096 return 0; 00097 } 00098 00099 static inline int 00100 ink_mutex_release(ink_mutex * m) 00101 { 00102 if (pthread_mutex_unlock(m) != 0) { 00103 abort(); 00104 } 00105 return 0; 00106 } 00107 00108 static inline int 00109 ink_mutex_try_acquire(ink_mutex * m) 00110 { 00111 return pthread_mutex_trylock(m) == 0; 00112 } 00113 00114 #endif /* #if defined(POSIX_THREAD) */ 00115 00116 struct ink_scoped_mutex 00117 { 00118 explicit ink_scoped_mutex(ink_mutex& m) : mtx(m) { 00119 ink_mutex_acquire(&mtx); 00120 } 00121 00122 ~ink_scoped_mutex() { 00123 ink_mutex_release(&mtx); 00124 } 00125 00126 private: 00127 ink_mutex& mtx; 00128 }; 00129 00130 #endif /* _ink_mutex_h_ */