Skip to content

Commit

Permalink
Merge pull request grpc#5682 from yang-g/tls_init_should_be_called
Browse files Browse the repository at this point in the history
Add debug support for checking tls_init is called
  • Loading branch information
jtattermusch committed Mar 18, 2016
2 parents 63978d4 + 5a39106 commit ceb6510
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion include/grpc/support/tls_gcc.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* Copyright 2015, Google Inc.
* Copyright 2015-2016, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -34,9 +34,51 @@
#ifndef GRPC_SUPPORT_TLS_GCC_H
#define GRPC_SUPPORT_TLS_GCC_H

#include <stdbool.h>

#include <grpc/support/log.h>

/* Thread local storage based on gcc compiler primitives.
#include tls.h to use this - and see that file for documentation */

#ifndef NDEBUG

struct gpr_gcc_thread_local {
intptr_t value;
bool *inited;
};

#define GPR_TLS_DECL(name) \
static bool name##_inited = false; \
static __thread struct gpr_gcc_thread_local name = {0, &(name##_inited)}

#define gpr_tls_init(tls) \
do { \
GPR_ASSERT(*((tls)->inited) == false); \
*((tls)->inited) = true; \
} while (0)

/* It is allowed to call gpr_tls_init after gpr_tls_destroy is called. */
#define gpr_tls_destroy(tls) \
do { \
GPR_ASSERT(*((tls)->inited)); \
*((tls)->inited) = false; \
} while (0)

#define gpr_tls_set(tls, new_value) \
do { \
GPR_ASSERT(*((tls)->inited)); \
(tls)->value = (new_value); \
} while (0)

#define gpr_tls_get(tls) \
({ \
GPR_ASSERT(*((tls)->inited)); \
(tls)->value; \
})

#else /* NDEBUG */

struct gpr_gcc_thread_local {
intptr_t value;
};
Expand All @@ -53,4 +95,6 @@ struct gpr_gcc_thread_local {
#define gpr_tls_set(tls, new_value) (((tls)->value) = (new_value))
#define gpr_tls_get(tls) ((tls)->value)

#endif /* NDEBUG */

#endif

0 comments on commit ceb6510

Please sign in to comment.