X-Git-Url: https://gerrit.akraino.org/r/gitweb?a=blobdiff_plain;f=src%2Ftype3_AndroidCloud%2Fanbox-master%2Fexternal%2Fandroid-emugl%2Fshared%2Femugl%2Fcommon%2Fmutex.h;fp=src%2Ftype3_AndroidCloud%2Fanbox-master%2Fexternal%2Fandroid-emugl%2Fshared%2Femugl%2Fcommon%2Fmutex.h;h=6a161ae2b5c655caa25cf735d07c042397b2e64d;hb=e26c1ec581be598521517829adba8c8dd23a768f;hp=0000000000000000000000000000000000000000;hpb=6699c1aea74eeb0eb400e6299079f0c7576f716f;p=iec.git diff --git a/src/type3_AndroidCloud/anbox-master/external/android-emugl/shared/emugl/common/mutex.h b/src/type3_AndroidCloud/anbox-master/external/android-emugl/shared/emugl/common/mutex.h new file mode 100644 index 0000000..6a161ae --- /dev/null +++ b/src/type3_AndroidCloud/anbox-master/external/android-emugl/shared/emugl/common/mutex.h @@ -0,0 +1,95 @@ +// Copyright (C) 2014 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef EMUGL_MUTEX_H +#define EMUGL_MUTEX_H + +#ifdef _WIN32 +# define WIN32_LEAN_AND_MEAN 1 +# include +#else +# include +#endif + +namespace emugl { + +class ConditionVariable; + +// Simple wrapper class for mutexes. +class Mutex { +public: + // Constructor. + Mutex() { +#ifdef _WIN32 + ::InitializeCriticalSection(&mLock); +#else + ::pthread_mutex_init(&mLock, NULL); +#endif + } + + // Destructor. + ~Mutex() { +#ifdef _WIN32 + ::DeleteCriticalSection(&mLock); +#else + ::pthread_mutex_destroy(&mLock); +#endif + } + + // Acquire the mutex. + void lock() { +#ifdef _WIN32 + ::EnterCriticalSection(&mLock); +#else + ::pthread_mutex_lock(&mLock); +#endif + } + + // Release the mutex. + void unlock() { +#ifdef _WIN32 + ::LeaveCriticalSection(&mLock); +#else + ::pthread_mutex_unlock(&mLock); +#endif + } + + // Helper class to lock / unlock a mutex automatically on scope + // entry and exit. + class AutoLock { + public: + AutoLock(Mutex& mutex) : mMutex(&mutex) { + mMutex->lock(); + } + + ~AutoLock() { + mMutex->unlock(); + } + private: + Mutex* mMutex; + }; + +private: +#ifdef _WIN32 + CRITICAL_SECTION mLock; +#else + friend class ConditionVariable; + pthread_mutex_t mLock; +#endif + +}; + +} // namespace emugl + +#endif // EMUGL_MUTEX_H