2014-12-13 00:57:06 +08:00
|
|
|
/****************************************************************************
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2014-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2014-12-13 00:57:06 +08:00
|
|
|
Author: Justin Graham (https://github.com/mannewalis)
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-03-24 15:13:05 +08:00
|
|
|
#ifndef CC_ALLOCATOR_MUTEX_H
|
|
|
|
#define CC_ALLOCATOR_MUTEX_H
|
2015-03-24 20:23:51 +08:00
|
|
|
/// @cond DO_NOT_SHOW
|
2015-03-24 15:13:05 +08:00
|
|
|
|
2014-12-13 00:57:06 +08:00
|
|
|
#include "platform/CCPlatformMacros.h"
|
|
|
|
|
2016-04-28 09:49:55 +08:00
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_TIZEN || CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_LINUX
|
2014-12-13 00:57:06 +08:00
|
|
|
#include "pthread.h"
|
|
|
|
#define MUTEX pthread_mutex_t
|
|
|
|
#define MUTEX_INIT(m) \
|
|
|
|
pthread_mutexattr_t mta; \
|
|
|
|
pthread_mutexattr_init(&mta); \
|
|
|
|
pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_RECURSIVE); \
|
|
|
|
pthread_mutex_init(&m, &mta)
|
|
|
|
#define MUTEX_LOCK(m) \
|
|
|
|
pthread_mutex_lock(&m);
|
|
|
|
#define MUTEX_UNLOCK(m) \
|
|
|
|
pthread_mutex_unlock(&m);
|
2015-05-09 00:19:13 +08:00
|
|
|
#elif CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT
|
2014-12-13 00:57:06 +08:00
|
|
|
#include "windows.h"
|
|
|
|
#define MUTEX HANDLE
|
2014-12-24 10:45:29 +08:00
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
|
2014-12-13 00:57:06 +08:00
|
|
|
#define MUTEX_INIT(m) \
|
|
|
|
m = CreateMutex(0, FALSE, 0)
|
|
|
|
#define MUTEX_LOCK(m) \
|
|
|
|
WaitForSingleObject(m, INFINITE)
|
2015-05-09 00:19:13 +08:00
|
|
|
#elif CC_TARGET_PLATFORM == CC_PLATFORM_WINRT
|
2014-12-24 10:45:29 +08:00
|
|
|
#define MUTEX_INIT(m) \
|
2014-12-31 13:24:05 +08:00
|
|
|
m = CreateMutexEx(NULL,FALSE,0,NULL);
|
2014-12-24 10:45:29 +08:00
|
|
|
#define MUTEX_LOCK(m) \
|
|
|
|
WaitForSingleObjectEx(m, INFINITE, false)
|
|
|
|
#endif
|
2014-12-13 00:57:06 +08:00
|
|
|
#define MUTEX_UNLOCK(m) \
|
|
|
|
ReleaseMutex(m)
|
|
|
|
#else
|
2014-12-23 08:43:40 +08:00
|
|
|
#message "Unsupported platform for AllocatorMutex, Locking semantics will not be supported"
|
2014-12-23 08:46:16 +08:00
|
|
|
#define MUTEX
|
|
|
|
#define MUTEX_INIT(...)
|
|
|
|
#define MUTEX_LOCK(...)
|
|
|
|
#define MUTEX_UNLOCK(...)
|
2014-12-13 00:57:06 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
NS_CC_ALLOCATOR_BEGIN
|
|
|
|
|
|
|
|
// so we cannot use std::mutex because it allocates memory
|
|
|
|
// which causes an infinite loop of death and exceptions.
|
|
|
|
class AllocatorMutex
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
AllocatorMutex()
|
|
|
|
{
|
|
|
|
MUTEX_INIT(_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void lock()
|
|
|
|
{
|
|
|
|
MUTEX_LOCK(_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void unlock()
|
|
|
|
{
|
|
|
|
MUTEX_UNLOCK(_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
MUTEX _mutex;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define LOCK(m) m.lock()
|
|
|
|
#define UNLOCK(m) m.unlock()
|
|
|
|
|
2014-12-18 00:54:06 +08:00
|
|
|
// @param implementation that provides a mutex with locking semantics.
|
|
|
|
struct locking_semantics
|
|
|
|
{
|
|
|
|
AllocatorMutex _mutex;
|
|
|
|
CC_ALLOCATOR_INLINE void lock()
|
|
|
|
{
|
2014-12-18 00:56:19 +08:00
|
|
|
LOCK(_mutex);
|
2014-12-18 00:54:06 +08:00
|
|
|
}
|
|
|
|
CC_ALLOCATOR_INLINE void unlock()
|
|
|
|
{
|
2014-12-18 00:56:19 +08:00
|
|
|
UNLOCK(_mutex);
|
2014-12-18 00:54:06 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// @param implementation that provides lockless semantics that should optimize away.
|
|
|
|
struct lockless_semantics
|
|
|
|
{
|
|
|
|
CC_ALLOCATOR_INLINE void lock() {}
|
|
|
|
CC_ALLOCATOR_INLINE void unlock() {}
|
|
|
|
};
|
|
|
|
|
2014-12-13 00:57:06 +08:00
|
|
|
NS_CC_ALLOCATOR_END
|
|
|
|
NS_CC_END
|
|
|
|
|
2015-03-24 15:13:05 +08:00
|
|
|
/// @endcond
|
2014-12-13 00:57:06 +08:00
|
|
|
#endif//CC_ALLOCATOR_MUTEX_H
|