[libvirt] [PATCH 1/6] virterror: Make a thread local error state structure

Cole Robinson crobinso at redhat.com
Thu Nov 5 18:17:42 UTC 2009


Move virLastError into that structure. This should be a no op refactor.

Signed-off-by: Cole Robinson <crobinso at redhat.com>
---
 src/util/virterror.c |   52 +++++++++++++++++++++++++++++++++++--------------
 1 files changed, 37 insertions(+), 15 deletions(-)

diff --git a/src/util/virterror.c b/src/util/virterror.c
index c8e8623..7b7543b 100644
--- a/src/util/virterror.c
+++ b/src/util/virterror.c
@@ -22,7 +22,12 @@
 #include "threads.h"
 #include "util.h"
 
-virThreadLocal virLastErr;
+virThreadLocal virThreadErrorStateIdx;
+
+typedef struct virThreadErrorState {
+    virErrorPtr lastError;
+} virThreadErrorState;
+typedef virThreadErrorState *virThreadErrorStatePtr;
 
 virErrorFunc virErrorHandler = NULL;     /* global error handler */
 void *virUserData = NULL;        /* associated data */
@@ -182,13 +187,18 @@ static const char *virErrorDomainName(virErrorDomain domain) {
  * release the error object stored in the thread local
  */
 static void
-virLastErrFreeData(void *data)
+virThreadErrorStateFree(void *data)
 {
-    virErrorPtr err = data;
-    if (!err)
+    virThreadErrorStatePtr state = data;
+    if (!state)
         return;
-    virResetError(err);
-    VIR_FREE(err);
+
+    if (state->lastError) {
+        virResetError(state->lastError);
+        VIR_FREE(state->lastError);
+    }
+
+    VIR_FREE(state);
 }
 
 
@@ -202,7 +212,8 @@ virLastErrFreeData(void *data)
 int
 virErrorInitialize(void)
 {
-    return virThreadLocalInit(&virLastErr, virLastErrFreeData);
+    return virThreadLocalInit(&virThreadErrorStateIdx,
+                               virThreadErrorStateFree);
 }
 
 
@@ -253,19 +264,30 @@ virCopyError(virErrorPtr from,
     return ret;
 }
 
-static virErrorPtr
-virLastErrorObject(void)
+static virThreadErrorStatePtr
+virThreadErrorStateObject(void)
 {
-    virErrorPtr err;
-    err = virThreadLocalGet(&virLastErr);
-    if (!err) {
-        if (VIR_ALLOC(err) < 0)
+    virThreadErrorStatePtr state;
+    state = virThreadLocalGet(&virThreadErrorStateIdx);
+    if (!state) {
+        if (VIR_ALLOC(state) < 0)
             return NULL;
-        virThreadLocalSet(&virLastErr, err);
+        if (VIR_ALLOC(state->lastError) < 0)
+            return NULL;
+        virThreadLocalSet(&virThreadErrorStateIdx, state);
     }
-    return err;
+    return state;
 }
 
+static virErrorPtr
+virLastErrorObject(void)
+{
+    virThreadErrorStatePtr state = virThreadErrorStateObject();
+    if (!state)
+        return NULL;
+
+    return state->lastError;
+}
 
 /**
  * virGetLastError:
-- 
1.6.5.1




More information about the libvir-list mailing list