2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
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.
20 /* standard library header */
22 /* SLP library header */
27 namespace smartcard_service_api
33 virtual void lock() = 0;
34 virtual void unlock() = 0;
37 class PMutex : public Lock
40 pthread_mutex_t mutex;
43 PMutex() { pthread_mutex_init(&mutex, NULL); }
44 ~PMutex() { pthread_mutex_destroy(&mutex); }
46 inline void lock() { pthread_mutex_lock(&mutex); }
47 inline void unlock() { pthread_mutex_unlock(&mutex); }
49 #define TOKENPASTE(x, y) x ## y
50 #define TOKENPASTE2(x, y) TOKENPASTE(x, y)
51 #define SCOPE_LOCK(X) \
52 if (const AutoLockHelper& TOKENPASTE2(lock_, __LINE__) = makeAutoLock(X))
57 inline operator bool() const
64 class AutoLock : public AutoLockHelper
70 AutoLock(const AutoLock &_lock) { lock = _lock.lock; }
71 AutoLock(T& _lock) : lock(&_lock) { lock->lock(); }
72 ~AutoLock() { lock->unlock(); }
76 inline AutoLock<T> makeAutoLock(T& lock)
78 return AutoLock<T>(lock);
81 } /* namespace smartcard_service_api */
82 #endif /* SCOPELOCK_H_ */