aboutsummaryrefslogtreecommitdiff
path: root/symtab.cc
diff options
context:
space:
mode:
authorShinichiro Hamaji <shinichiro.hamaji@gmail.com>2016-02-09 17:35:28 +0900
committerShinichiro Hamaji <shinichiro.hamaji@gmail.com>2016-02-09 17:37:07 +0900
commitd18c4275b6ad20c462a30fbb95bf93df33449415 (patch)
treee5297e80992fc976b85c36d11963207579a81084 /symtab.cc
parentac50ff4d3826924532f0a6180efe39a40860db55 (diff)
[C++] Make it possible to check the thread-safety of Symtab
Intern is used everywhere, we can easily introduce race conditions by using it.
Diffstat (limited to 'symtab.cc')
-rw-r--r--symtab.cc17
1 files changed, 17 insertions, 0 deletions
diff --git a/symtab.cc b/symtab.cc
index d8d7e93..af0b620 100644
--- a/symtab.cc
+++ b/symtab.cc
@@ -14,8 +14,13 @@
// +build ignore
+//#define ENABLE_TID_CHECK
+
#include "symtab.h"
+#ifdef ENABLE_TID_CHECK
+#include <pthread.h>
+#endif
#include <string.h>
#include <unordered_map>
@@ -35,6 +40,10 @@ Symbol::Symbol(int v)
class Symtab {
public:
Symtab() {
+#ifdef ENABLE_TID_CHECK
+ tid_ = pthread_self();
+#endif
+
CHECK(g_symbols == NULL);
g_symbols = &symbols_;
@@ -72,6 +81,11 @@ class Symtab {
}
Symbol Intern(StringPiece s) {
+#ifdef ENABLE_TID_CHECK
+ if (tid_ != pthread_self())
+ abort();
+#endif
+
if (s.size() <= 1) {
return Symbol(s.empty() ? 0 : (unsigned char)s[0]);
}
@@ -81,6 +95,9 @@ class Symtab {
private:
unordered_map<StringPiece, Symbol> symtab_;
vector<string*> symbols_;
+#ifdef ENABLE_TID_CHECK
+ pthread_t tid_;
+#endif
};
static Symtab* g_symtab;