-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add MARL_USE_PTHREAD_THREAD_LOCAL option
Yet another attempt to fix this MSAN false positive that haunts Chrome.
- Loading branch information
1 parent
abd0336
commit 9c689c9
Showing
5 changed files
with
80 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2023 The Marl Authors. | ||
// | ||
// 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 | ||
// | ||
// https://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. | ||
|
||
// A wrapper around a thread_local variable, or a pthread key | ||
|
||
#ifndef marl_thread_local_h | ||
#define marl_thread_local_h | ||
|
||
#ifdef MARL_USE_PTHREAD_THREAD_LOCAL | ||
#include "debug.h" | ||
|
||
#include <pthread.h> | ||
#include <type_traits> | ||
|
||
template <typename T> | ||
class ThreadLocal { | ||
static_assert(std::is_pointer<T>::value, | ||
"The current implementation of ThreadLocal requires that T " | ||
"must be a pointer"); | ||
|
||
public: | ||
inline ThreadLocal(T v) { | ||
pthread_key_create(&key, NULL); | ||
pthread_setspecific(key, v); | ||
} | ||
inline ~ThreadLocal() { pthread_key_delete(key); } | ||
inline operator T() const { return static_cast<T>(pthread_getspecific(key)); } | ||
inline ThreadLocal& operator=(T v) { | ||
pthread_setspecific(key, v); | ||
return *this; | ||
} | ||
|
||
private: | ||
ThreadLocal(const ThreadLocal&) = delete; | ||
ThreadLocal& operator=(const ThreadLocal&) = delete; | ||
|
||
pthread_key_t key; | ||
}; | ||
|
||
#define MARL_DECLARE_THREAD_LOCAL(TYPE, NAME) static ThreadLocal<TYPE> NAME | ||
#define MARL_INSTANTIATE_THREAD_LOCAL(TYPE, NAME, VALUE) \ | ||
ThreadLocal<TYPE> NAME { \ | ||
VALUE \ | ||
} | ||
|
||
#else | ||
|
||
#define MARL_DECLARE_THREAD_LOCAL(TYPE, NAME) static thread_local TYPE NAME | ||
#define MARL_INSTANTIATE_THREAD_LOCAL(TYPE, NAME, VALUE) \ | ||
thread_local TYPE NAME { \ | ||
VALUE \ | ||
} | ||
|
||
#endif | ||
|
||
#endif // marl_thread_local_h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters