Commit 833ecbf0 authored by Christopher Dykes's avatar Christopher Dykes Committed by Facebook Github Bot

Add portability support for PThread's TLS API

Summary:
This is the last piece needed to get Folly working on Windows without PThreads.
Updating Folly's test suite to support compiling without PThreads will come next.

Reviewed By: yfeldblum

Differential Revision: D4894048

fbshipit-source-id: 6076317e1364aef82a5d3cb306bea7c2226b3cdc
parent 71c789bb
...@@ -489,6 +489,7 @@ libfolly_la_SOURCES = \ ...@@ -489,6 +489,7 @@ libfolly_la_SOURCES = \
portability/Malloc.cpp \ portability/Malloc.cpp \
portability/Memory.cpp \ portability/Memory.cpp \
portability/OpenSSL.cpp \ portability/OpenSSL.cpp \
portability/PThread.cpp \
portability/Sockets.cpp \ portability/Sockets.cpp \
portability/Stdio.cpp \ portability/Stdio.cpp \
portability/Stdlib.cpp \ portability/Stdlib.cpp \
......
/*
* Copyright 2017 Facebook, Inc.
*
* 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.
*/
#include <folly/portability/PThread.h>
#if !FOLLY_HAVE_PTHREAD && _WIN32
#include <unordered_map>
#include <utility>
namespace folly {
namespace portability {
namespace pthread {
static thread_local struct PThreadLocalMap {
PThreadLocalMap() = default;
~PThreadLocalMap() {
for (auto kv : keyMap) {
// Call destruction callbacks if they exist.
if (kv.second.second != nullptr) {
kv.second.second(kv.second.first);
}
}
}
int createKey(pthread_key_t* key, void (*destructor)(void*)) {
auto ret = TlsAlloc();
if (ret == TLS_OUT_OF_INDEXES) {
return -1;
}
*key = ret;
keyMap.emplace(*key, std::make_pair(nullptr, destructor));
return 0;
}
int deleteKey(pthread_key_t key) {
if (!TlsFree(key)) {
return -1;
}
keyMap.erase(key);
return 0;
}
void* getKey(pthread_key_t key) {
return TlsGetValue(key);
}
int setKey(pthread_key_t key, void* value) {
if (!TlsSetValue(key, value)) {
return -1;
}
keyMap[key].first = value;
return 0;
}
std::unordered_map<pthread_key_t, std::pair<void*, void (*)(void*)>> keyMap{};
} s_tls_key_map;
int pthread_key_create(pthread_key_t* key, void (*destructor)(void*)) {
return s_tls_key_map.createKey(key, destructor);
}
int pthread_key_delete(pthread_key_t key) {
return s_tls_key_map.deleteKey(key);
}
void* pthread_getspecific(pthread_key_t key) {
return s_tls_key_map.getKey(key);
}
int pthread_setspecific(pthread_key_t key, const void* value) {
// Yes, the PThread API really is this bad -_-...
return s_tls_key_map.setKey(key, const_cast<void*>(value));
}
}
}
}
#endif
...@@ -16,6 +16,34 @@ ...@@ -16,6 +16,34 @@
#pragma once #pragma once
#include <folly/portability/Config.h>
#if !FOLLY_HAVE_PTHREAD
#ifndef _WIN32
#error Building Folly without pthreads is only supported on Windows.
#endif
#include <folly/portability/Windows.h>
#include <cstdint>
namespace folly {
namespace portability {
namespace pthread {
using pthread_key_t = DWORD;
int pthread_key_create(pthread_key_t* key, void (*destructor)(void*));
int pthread_key_delete(pthread_key_t key);
void* pthread_getspecific(pthread_key_t key);
int pthread_setspecific(pthread_key_t key, const void* value);
}
}
}
/* using override */ using namespace folly::portability::pthread;
#else
#include <pthread.h> #include <pthread.h>
#ifdef _WIN32 #ifdef _WIN32
...@@ -95,3 +123,4 @@ struct hash<pthread_t> { ...@@ -95,3 +123,4 @@ struct hash<pthread_t> {
}; };
} }
#endif #endif
#endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment