Skip to content

Commit

Permalink
[gpr] Move sync and time out of gpr target
Browse files Browse the repository at this point in the history
  • Loading branch information
yashykt committed Jan 24, 2025
1 parent 24e391b commit 3340aa1
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 109 deletions.
2 changes: 0 additions & 2 deletions src/core/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,6 @@ grpc_cc_library(
"util/env.h",
],
deps = [
"no_destruct",
"sync",
"tchar",
"//:gpr_platform",
],
Expand Down
8 changes: 0 additions & 8 deletions src/core/util/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,6 @@ void SetOrUnsetEnv(const char* name, const std::optional<T>& value) {
}
}

// getenv/setenv/unsetenv are not thread-safe, and calls from multiple threads
// can cause data races. This is generally not an issue since we do not expect
// setenv/unsetenv to be invoked after initialization, but for tests, we use
// setenv/unsetenv to test different environments and that causes crashes.
// This method synchronizes calls to getenv/setenv/unsetenv. We do not do this
// outside of tests to avoid adding a global mutex in our channel's path.
void SetTestOnlyEnvSynchronize();

} // namespace grpc_core

#endif // GRPC_SRC_CORE_UTIL_ENV_H
35 changes: 3 additions & 32 deletions src/core/util/linux/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,56 +32,27 @@
#include <stdlib.h>

#include "src/core/util/env.h"
#include "src/core/util/no_destruct.h"
#include "src/core/util/sync.h"

namespace grpc_core {

namespace {
NoDestruct<Mutex> g_mu;
bool g_test_only = false;
} // namespace

void SetTestOnlyEnvSynchronize() { g_test_only = true; }

std::optional<std::string> GetEnv(const char* name)
ABSL_NO_THREAD_SAFETY_ANALYSIS {
std::optional<std::string> GetEnv(const char* name) {
char* result = nullptr;
if (g_test_only) {
g_mu->Lock();
}
#if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 17)
result = secure_getenv(name);
#else
result = getenv(name);
#endif
if (g_test_only) {
g_mu->Unlock();
}
if (result == nullptr) return std::nullopt;
return result;
}

void SetEnv(const char* name,
const char* value) ABSL_NO_THREAD_SAFETY_ANALYSIS {
if (g_test_only) {
g_mu->Lock();
}
void SetEnv(const char* name, const char* value) {
int res = setenv(name, value, 1);
if (g_test_only) {
g_mu->Unlock();
}
if (res != 0) abort();
}

void UnsetEnv(const char* name) ABSL_NO_THREAD_SAFETY_ANALYSIS {
if (g_test_only) {
g_mu->Lock();
}
void UnsetEnv(const char* name) {
int res = unsetenv(name);
if (g_test_only) {
g_mu->Unlock();
}
if (res != 0) abort();
}

Expand Down
35 changes: 3 additions & 32 deletions src/core/util/posix/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,51 +23,22 @@
#include <stdlib.h>

#include "src/core/util/env.h"
#include "src/core/util/no_destruct.h"
#include "src/core/util/sync.h"

namespace grpc_core {

namespace {
NoDestruct<Mutex> g_mu;
bool g_test_only = false;
} // namespace

void SetTestOnlyEnvSynchronize() { g_test_only = true; }

std::optional<std::string> GetEnv(const char* name)
ABSL_NO_THREAD_SAFETY_ANALYSIS {
if (g_test_only) {
g_mu->Lock();
}
std::optional<std::string> GetEnv(const char* name) {
char* result = getenv(name);
if (g_test_only) {
g_mu->Unlock();
}
if (result == nullptr) return std::nullopt;
return result;
}

void SetEnv(const char* name,
const char* value) ABSL_NO_THREAD_SAFETY_ANALYSIS {
if (g_test_only) {
g_mu->Lock();
}
void SetEnv(const char* name, const char* value) {
int res = setenv(name, value, 1);
if (g_test_only) {
g_mu->Unlock();
}
if (res != 0) abort();
}

void UnsetEnv(const char* name) ABSL_NO_THREAD_SAFETY_ANALYSIS {
if (g_test_only) {
g_mu->Lock();
}
void UnsetEnv(const char* name) {
int res = unsetenv(name);
if (g_test_only) {
g_mu->Unlock();
}
if (res != 0) abort();
}

Expand Down
38 changes: 3 additions & 35 deletions src/core/util/windows/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,13 @@

#include <memory>

#include "absl/cleanup/cleanup.h"
#include "src/core/util/env.h"
#include "src/core/util/no_destruct.h"
#include "src/core/util/sync.h"
#include "src/core/util/tchar.h"

namespace grpc_core {

namespace {
NoDestruct<Mutex> g_mu;
bool g_test_only = false;
} // namespace

void SetTestOnlyEnvSynchronize() { g_test_only = true; }

std::optional<std::string> GetEnv(const char* name)
ABSL_NO_THREAD_SAFETY_ANALYSIS {
std::optional<std::string> GetEnv(const char* name) {
auto tname = CharToTchar(name);
if (g_test_only) {
g_mu->Lock();
}
auto cleanup = absl::MakeCleanup([]() {
if (g_test_only) {
g_mu->Unlock();
}
});
DWORD ret = GetEnvironmentVariable(tname.c_str(), NULL, 0);
if (ret == 0) return std::nullopt;
std::unique_ptr<TCHAR[]> tresult(new TCHAR[ret]);
Expand All @@ -59,27 +40,14 @@ std::optional<std::string> GetEnv(const char* name)
return TcharToChar(tresult.get());
}

void SetEnv(const char* name,
const char* value) ABSL_NO_THREAD_SAFETY_ANALYSIS {
if (g_test_only) {
g_mu->Lock();
}
void SetEnv(const char* name, const char* value) {
BOOL res = SetEnvironmentVariable(CharToTchar(name).c_str(),
CharToTchar(value).c_str());
if (g_test_only) {
g_mu->Unlock();
}
if (!res) abort();
}

void UnsetEnv(const char* name) ABSL_NO_THREAD_SAFETY_ANALYSIS {
if (g_test_only) {
g_mu->Lock();
}
void UnsetEnv(const char* name) {
BOOL res = SetEnvironmentVariable(CharToTchar(name).c_str(), NULL);
if (g_test_only) {
g_mu->Unlock();
}
if (!res) abort();
}

Expand Down

0 comments on commit 3340aa1

Please sign in to comment.