[libvirt] [PATCH 1/2] Thread-local global variables

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This patch adds macros for doing thread-local global variables.  It is
implemented using POSIX threads, and if those aren't available then
the variables just turn into real global variables.

Rich.

-- 
Richard Jones, Emerging Technologies, Red Hat  http://et.redhat.com/~rjones
virt-p2v converts physical machines to virtual machines.  Boot with a
live CD or over the network (PXE) and turn machines into Xen guests.
http://et.redhat.com/~rjones/virt-p2v
Index: src/threads.h
===================================================================
RCS file: src/threads.h
diff -N src/threads.h
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ src/threads.h	30 Jul 2008 16:06:48 -0000
@@ -0,0 +1,67 @@
+/*
+ * Wrappers around POSIX thread functions.
+ *
+ * Copyright (C) 2008 Red Hat, Inc.
+ *
+ * See COPYING.LIB for the License of this software
+ *
+ * Author: Richard W.M. Jones <rjones@xxxxxxxxxx>
+ */
+
+#ifndef __VIR_THREADS_H__
+#define __VIR_THREADS_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef HAVE_PTHREAD_H
+
+/* These macros and functions are used to make thread-local
+ * global variables (ie. the variable is global but different
+ * values can be stored in it in each thread).  The variables
+ * always have type 'void *'.
+ *
+ * THREAD_LOCAL(name)
+ *    defines a thread-local variable called 'name'.
+ * THREAD_LOCAL_INIT(name)
+ *    must be called before using the variable (you may call this
+ *    macro as many times as you like).  The value of the variable
+ *    is still undefined until you call THREAD_LOCAL_SET.
+ * THREAD_LOCAL_SET(name,value)
+ *    sets the value of the variable to 'value'.
+ * THREAD_LOCAL_GET(name)
+ *    returns the value of the variable.
+ *
+ * If the system doesn't have POSIX threads, then this just turns
+ * into an ordinary global variable.
+ */
+#define THREAD_LOCAL(name)                                      \
+    static pthread_once_t _##name##_once = PTHREAD_ONCE_INIT;   \
+    static pthread_key_t _##name##_key;                         \
+    static void _initialize_##name (void)                       \
+    {                                                           \
+        (void) pthread_key_create (&_##name##_key, NULL);       \
+    }
+
+#define THREAD_LOCAL_INIT(name)                                 \
+    (void) pthread_once (&_##name##_once, _initialize_##name)
+
+#define THREAD_LOCAL_SET(name,value)                    \
+    pthread_setspecific (_##name##_key, (value))
+
+#define THREAD_LOCAL_GET(name) pthread_getspecific (_##name##_key)
+
+#else /* !HAVE_PTHREAD_H */
+
+#define THREAD_LOCAL(name) static void *name;
+#define THREAD_LOCAL_INIT(name)
+#define THREAD_LOCAL_SET(name,value) name = (value)
+#define THREAD_LOCAL_GET(name) name
+
+#endif /* !HAVE_PTHREAD_H */
+
+#ifdef __cplusplus
+}
+#endif
+#endif /* ! __VIR_THREADS_H__ */
Index: src/internal.h
===================================================================
RCS file: /data/cvs/libvirt/src/internal.h,v
retrieving revision 1.75
diff -u -r1.75 internal.h
--- src/internal.h	26 Jun 2008 09:37:51 -0000	1.75
+++ src/internal.h	30 Jul 2008 16:06:48 -0000
@@ -12,6 +12,9 @@
 #include <sys/syslimits.h>
 #endif
 
+/* Functions which need POSIX threads.  There are more
+ * thread functions available in threads.h include file.
+ */
 #ifdef HAVE_PTHREAD_H
 #include <pthread.h>
 #define PTHREAD_MUTEX_T(v) pthread_mutex_t v
Index: src/Makefile.am
===================================================================
RCS file: /data/cvs/libvirt/src/Makefile.am,v
retrieving revision 1.86
diff -u -r1.86 Makefile.am
--- src/Makefile.am	11 Jul 2008 16:23:36 -0000	1.86
+++ src/Makefile.am	30 Jul 2008 16:06:48 -0000
@@ -42,6 +42,7 @@
                 domain_conf.c domain_conf.h                     \
                 capabilities.c capabilities.h			\
 		xml.c xml.h					\
+		threads.h					\
 		event.c event.h					\
 		xen_unified.c xen_unified.h			\
 		xen_internal.c xen_internal.h			\
--
Libvir-list mailing list
Libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list

[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]