2 * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 namespace smartcard_service_api
30 virtual void lock() = 0;
31 virtual void unlock() = 0;
34 class PMutex : public Lock
37 pthread_mutex_t mutex;
40 PMutex() { pthread_mutex_init(&mutex, NULL); }
41 ~PMutex() { pthread_mutex_destroy(&mutex); }
43 inline void lock() { pthread_mutex_lock(&mutex); }
44 inline void unlock() { pthread_mutex_unlock(&mutex); }
46 #define TOKENPASTE(x, y) x ## y
47 #define TOKENPASTE2(x, y) TOKENPASTE(x, y)
48 #define SCOPE_LOCK(X) \
49 if (const AutoLockHelper& TOKENPASTE2(lock_, __LINE__) = makeAutoLock(X))
51 class EXPORT AutoLockHelper
54 inline operator bool() const
61 class EXPORT AutoLock : public AutoLockHelper
67 AutoLock(const AutoLock &_lock) { lock = _lock.lock; }
68 AutoLock(T& _lock) : lock(&_lock) { lock->lock(); }
69 ~AutoLock() { lock->unlock(); }
73 inline AutoLock<T> makeAutoLock(T& lock)
75 return AutoLock<T>(lock);
78 } /* namespace smartcard_service_api */
79 #endif /* SCOPELOCK_H_ */