]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
tls: addeed a thread local storage infrastructure
authorYehuda Sadeh <yehuda@hq.newdream.net>
Mon, 9 Feb 2009 22:59:07 +0000 (14:59 -0800)
committerYehuda Sadeh <yehuda@hq.newdream.net>
Mon, 9 Feb 2009 23:01:21 +0000 (15:01 -0800)
(cherry picked from commit 1b6b128f4e2aaab016575f48f2ec50d4abe002c9)

src/Makefile.am
src/common/tls.cc [new file with mode: 0644]
src/common/tls.h [new file with mode: 0644]

index 26818e7c1c23b937de971a0adbd26cd875a1562a..0d78f72878908fdf000883ee9cdd4ac9d13b5cc4 100644 (file)
@@ -191,6 +191,7 @@ libcommon_a_SOURCES = \
        mon/MonClient.cc \
        osd/OSDMap.cc \
        mds/MDSMap.cc \
+       common/tls.cc \
        config.cc
 
 libcrush_a_SOURCES = \
diff --git a/src/common/tls.cc b/src/common/tls.cc
new file mode 100644 (file)
index 0000000..b614b58
--- /dev/null
@@ -0,0 +1,63 @@
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "common/tls.h"
+
+#include "config.h"
+
+#undef dout
+#undef derr
+#define  dout(l)    if (l<=g_conf.debug_lockdep) *_dout << g_clock.now() << " " << pthread_self() << " tls: "
+#define  derr(l)    if (l<=g_conf.debug_lockdep) *_derr << g_clock.now() << " " << pthread_self() << " tls: "
+
+
+pthread_key_t   _tls_key = 0;
+
+static int _initialized = 0;
+
+static void _tls_destructor(void *value)
+{
+  free(value);
+  pthread_setspecific(_tls_key, NULL);
+}
+
+void *tls_get_ptr()
+{
+  void *val;
+
+  if (!_initialized) {
+    derr(0) << "tls not initialized" << std::endl;
+    return NULL;
+  }
+
+  val  = pthread_getspecific(_tls_key);
+
+  if (!val) {
+    int ret;
+    val = malloc(sizeof(struct TlsData));
+    ret = pthread_setspecific(_tls_key, val);
+
+    if (ret)
+      return NULL;
+  }
+
+  return val;
+}
+
+int tls_init()
+{
+  int ret;
+
+  if (!_initialized) {
+    ret = pthread_key_create(&_tls_key, _tls_destructor);
+  }
+  return ret; 
+}
+
+void tls_finalize()
+{
+  if (_initialized)
+    pthread_key_delete(_tls_key);
+}
diff --git a/src/common/tls.h b/src/common/tls.h
new file mode 100644 (file)
index 0000000..c792208
--- /dev/null
@@ -0,0 +1,14 @@
+#ifndef __TLS_H
+#define __TLS_H
+
+struct TlsData {
+  int disable_assert;
+};
+
+
+void *tls_get_ptr();
+int tls_init();
+void tls_finalize();
+
+
+#endif