tidy Win32 stuff

This commit is contained in:
guoling
2019-12-18 15:51:02 +08:00
parent c3956e9c55
commit d383aa71ec
7 changed files with 120 additions and 84 deletions
+1 -1
View File
@@ -89,7 +89,7 @@ add_library(core
crc32/zlib/crc32.h
crc32/zlib/crc32.cpp
MMKVPredef.h
)
ThreadLock_Win32.cpp)
target_include_directories(core PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
+1
View File
@@ -21,6 +21,7 @@
#include "MemoryFile.h"
#ifdef MMKV_ANDROID
# include "InterProcessLock.h"
# include "MMBuffer.h"
# include "MMKVLog.h"
+8 -7
View File
@@ -2,7 +2,7 @@
* Tencent is pleased to support the open source community by making
* MMKV available.
*
* Copyright (C) 2018 THL A29 Limited, a Tencent company.
* Copyright (C) 2019 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License"); you may not use
@@ -18,16 +18,17 @@
* limitations under the License.
*/
#include "InterProcessLock.h"
#include "MMBuffer.h"
#include "MMKVLog.h"
#include "MemoryFile.h"
#include "ScopedLock.hpp"
using namespace std;
#ifdef MMKV_WIN32
# include "InterProcessLock.h"
# include "MMBuffer.h"
# include "MMKVLog.h"
# include "ScopedLock.hpp"
using namespace std;
namespace mmkv {
static bool getFileSize(MMKV_FILE_HANDLE fd, size_t &size);
+13 -72
View File
@@ -19,22 +19,23 @@
*/
#include "ThreadLock.h"
#include "MMKVLog.h"
#include <atomic>
#include <cassert>
#ifdef MMKV_LINUX
# include <sys/syscall.h>
# include <unistd.h>
#if MMKV_USING_PTHREAD
# include "MMKVLog.h"
# include <atomic>
# ifdef MMKV_LINUX
# include <sys/syscall.h>
# include <unistd.h>
static pid_t gettid() {
return syscall(SYS_gettid);
}
#endif
# endif
using namespace std;
namespace mmkv {
#if MMKV_USING_PTHREAD
ThreadLock::ThreadLock() {
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
@@ -72,38 +73,11 @@ void ThreadLock::unlock() {
MMKVError("fail to unlock %p, ret=%d, errno=%s", &m_lock, ret, strerror(errno));
}
}
#else
ThreadLock::ThreadLock() : m_lock{0} {}
ThreadLock::~ThreadLock() {
DeleteCriticalSection(&m_lock);
}
void ThreadLock::initialize() {
// TODO: a better spin count?
if (!InitializeCriticalSectionAndSpinCount(&m_lock, 1024)) {
MMKVError("fail to init critical section:%d", GetLastError());
}
}
void ThreadLock::lock() {
EnterCriticalSection(&m_lock);
}
bool ThreadLock::try_lock() {
auto ret = TryEnterCriticalSection(&m_lock);
return ret != 0;
}
void ThreadLock::unlock() {
LeaveCriticalSection(&m_lock);
}
#endif
#if MMKV_USING_PTHREAD
void ThreadLock::ThreadOnce(ThreadOnceToken *onceToken, void (*callback)()) {
pthread_once(onceToken, callback);
}
# ifndef NDEBUG
static uint64_t gettid() {
# ifdef MMKV_MAC
@@ -118,6 +92,7 @@ static uint64_t gettid() {
# endif
}
# endif
void ThreadLock::Sleep(int ms) {
constexpr auto MILLI_SECOND_MULTIPLIER = 1000;
constexpr auto NANO_SECOND_MULTIPLIER = MILLI_SECOND_MULTIPLIER * MILLI_SECOND_MULTIPLIER;
@@ -130,41 +105,7 @@ void ThreadLock::Sleep(int ms) {
}
nanosleep(&duration, nullptr);
}
#else
void ThreadLock::ThreadOnce(ThreadOnceToken *onceToken, void (*callback)()) {
if (!onceToken || !callback) {
assert(onceToken);
assert(callback);
return;
}
while (true) {
auto expected = ThreadOnceUninitialized;
atomic_compare_exchange_weak(onceToken, &expected, ThreadOnceInitializing);
switch (expected) {
case ThreadOnceInitialized:
return;
case ThreadOnceUninitialized:
callback();
onceToken->store(ThreadOnceInitialized);
return;
case ThreadOnceInitializing: {
// another thread is initializing, let's wait for 1ms
ThreadLock::Sleep(1);
break;
}
default: {
MMKVError("should never happen:%d", expected);
assert(0);
return;
}
}
}
}
void ThreadLock::Sleep(int ms) {
::Sleep(ms);
}
#endif
} // namespace mmkv
#endif // MMKV_USING_PTHREAD
+93
View File
@@ -0,0 +1,93 @@
/*
* Tencent is pleased to support the open source community by making
* MMKV available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of
* the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* 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.
*/
#include "ThreadLock.h"
#if !(MMKV_USING_PTHREAD)
# include "MMKVLog.h"
# include <atomic>
# include <cassert>
namespace mmkv {
ThreadLock::ThreadLock() : m_lock{0} {}
ThreadLock::~ThreadLock() {
DeleteCriticalSection(&m_lock);
}
void ThreadLock::initialize() {
// TODO: a better spin count?
if (!InitializeCriticalSectionAndSpinCount(&m_lock, 1024)) {
MMKVError("fail to init critical section:%d", GetLastError());
}
}
void ThreadLock::lock() {
EnterCriticalSection(&m_lock);
}
bool ThreadLock::try_lock() {
auto ret = TryEnterCriticalSection(&m_lock);
return ret != 0;
}
void ThreadLock::unlock() {
LeaveCriticalSection(&m_lock);
}
void ThreadLock::ThreadOnce(ThreadOnceToken *onceToken, void (*callback)()) {
if (!onceToken || !callback) {
assert(onceToken);
assert(callback);
return;
}
while (true) {
auto expected = ThreadOnceUninitialized;
atomic_compare_exchange_weak(onceToken, &expected, ThreadOnceInitializing);
switch (expected) {
case ThreadOnceInitialized:
return;
case ThreadOnceUninitialized:
callback();
onceToken->store(ThreadOnceInitialized);
return;
case ThreadOnceInitializing: {
// another thread is initializing, let's wait for 1ms
ThreadLock::Sleep(1);
break;
}
default: {
MMKVError("should never happen:%d", expected);
assert(0);
return;
}
}
}
}
void ThreadLock::Sleep(int ms) {
::Sleep(ms);
}
} // namespace mmkv
#endif // MMKV_USING_PTHREAD
+1 -1
View File
@@ -42,7 +42,7 @@
<ClCompile Include="MMKV.cpp" />
<ClCompile Include="MMKVLog.cpp" />
<ClCompile Include="PBUtility.cpp" />
<ClCompile Include="ThreadLock.cpp" />
<ClCompile Include="ThreadLock_Win32.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="aes\AESCrypt.h" />
+3 -3
View File
@@ -51,15 +51,15 @@
<ClCompile Include="MMKVLog.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="ThreadLock.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="MiniPBCoder.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="PBUtility.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="ThreadLock_Win32.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="CodedInputData.h">