1 /* 2 * Copyright 2006-2008 The FLWOR Foundation. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #pragma once 17 #ifndef ZORBA_RCLOCK_H 18 #define ZORBA_RCLOCK_H 19 20 #include <iostream> 21 #include <cstdlib> 22 23 24 namespace zorba 25 { 26 27 28 #ifndef ZORBA_FOR_ONE_THREAD_ONLY 29 30 /******************************************************************************* 31 32 ********************************************************************************/ 33 34 #if defined ZORBA_HAVE_PTHREAD_SPINLOCK 35 36 class RCLock 37 { 38 pthread_spinlock_t theLock; 39 40 public: RCLock()41 RCLock() 42 { 43 if (0 != pthread_spin_init(&theLock, PTHREAD_PROCESS_PRIVATE)) 44 { 45 std::cerr << "Failed to initialize spinlock" << std::endl; 46 abort(); 47 } 48 } 49 RCLock(const RCLock &)50 RCLock(const RCLock&) 51 { 52 if (0 != pthread_spin_init(&theLock, PTHREAD_PROCESS_PRIVATE)) 53 { 54 std::cerr << "Failed to initialize spinlock" << std::endl; 55 abort(); 56 } 57 } 58 ~RCLock()59 ~RCLock() 60 { 61 if (0 != pthread_spin_destroy(&theLock)) 62 { 63 std::cerr << "Failed to destroy spinlock" << std::endl; 64 abort(); 65 } 66 } 67 acquire()68 void acquire() 69 { 70 if (0 != pthread_spin_lock(&theLock)) 71 { 72 std::cerr << "Failed to acquire spinlock" << std::endl; 73 abort(); 74 } 75 } 76 release()77 void release() 78 { 79 if (0 != pthread_spin_unlock(&theLock)) 80 { 81 std::cerr << "Failed to release spinlock" << std::endl; 82 abort(); 83 } 84 } 85 }; 86 87 88 #elif defined ZORBA_HAVE_PTHREAD_MUTEX 89 90 class RCLock 91 { 92 mutable pthread_mutex_t theLock; 93 94 public: 95 96 RCLock() 97 { 98 pthread_mutexattr_t attr; 99 pthread_mutexattr_init(&attr); 100 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK | PTHREAD_PROCESS_PRIVATE); 101 if (0 != pthread_mutex_init(&theLock, &attr)) 102 { 103 std::cerr << "Failed to initialize RCLock" << std::endl; 104 abort(); 105 } 106 pthread_mutexattr_destroy(&attr); 107 } 108 109 110 RCLock(const RCLock&) 111 { 112 pthread_mutexattr_t attr; 113 pthread_mutexattr_init(&attr); 114 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK | PTHREAD_PROCESS_PRIVATE); 115 if (0 != pthread_mutex_init(&theLock, &attr)) 116 { 117 std::cerr << "Failed to initialize RCLock" << std::endl; 118 abort(); 119 } 120 pthread_mutexattr_destroy(&attr); 121 } 122 123 124 ~RCLock() 125 { 126 if (0 != pthread_mutex_destroy(&theLock)) 127 { 128 std::cerr << "Failed to destroy RCLock" << std::endl; 129 abort(); 130 } 131 } 132 133 void acquire() 134 { 135 if (0 != pthread_mutex_lock(&theLock)) 136 { 137 std::cerr << "Failed to acquire RCLock" << std::endl; 138 abort(); 139 } 140 141 } 142 143 void release() 144 { 145 if (0 != pthread_mutex_unlock(&theLock)) 146 { 147 std::cerr << "Failed to release RCLock" << std::endl; 148 abort(); 149 } 150 } 151 }; 152 153 154 #elif defined WIN32 || defined WINCE 155 156 class RCLock 157 { 158 public: 159 RCLock() { } 160 161 RCLock(const RCLock& ) { } 162 163 ~RCLock() { } 164 165 void acquire() { } 166 167 void release() { } 168 }; 169 170 #else 171 172 #error must have pthread mutex or phread spinlock, or windows OS 173 174 #endif // ZORBA_HAVE_PTHREAD or WIN32 175 176 #endif // ZORBA_FOR_ONE_THREAD_ONLY 177 178 } // namespace zorba 179 180 #endif // ZORBA_RCLOCK 181 182 /* 183 * Local variables: 184 * mode: c++ 185 * End: 186 */ 187 /* vim:set et sw=2 ts=2: */ 188