[xml] win32 native threads improvement



Igor,

Here is an updated version of my patch to improve the
win32 native thread support (avoiding to start a separate
thread to free per-thread data).

It's more correct and also simpler.

IMHO, it's now quite robust. For instance, it also
works behind Apache/mod_python, where libxml
gets called by threads that where created *before*
the libxml DLL was loaded the first time...
That one was not so easy to catch ;-(

Would you care to review it? With that version you
should be able to use native threads instead of ctls
by default.

TIA.

-sbi

Index: threads.c
===================================================================
RCS file: /cvs/gnome/gnome-xml/threads.c,v
retrieving revision 1.14
diff -c -r1.14 threads.c
*** threads.c   10 Dec 2002 15:19:07 -0000      1.14
--- threads.c   15 May 2003 18:49:11 -0000
***************
*** 92,98 ****
  static __declspec(thread) xmlGlobalState tlstate;
  static __declspec(thread) int tlstate_inited = 0;
  #else /* HAVE_COMPILER_TLS */
! static DWORD globalkey;
  #endif /* HAVE_COMPILER_TLS */
  static DWORD mainthread;
  static int run_once_init = 1;
--- 92,98 ----
  static __declspec(thread) xmlGlobalState tlstate;
  static __declspec(thread) int tlstate_inited = 0;
  #else /* HAVE_COMPILER_TLS */
! static DWORD globalkey = TLS_OUT_OF_INDEXES;
  #endif /* HAVE_COMPILER_TLS */
  static DWORD mainthread;
  static int run_once_init = 1;
***************
*** 324,329 ****
--- 324,330 ----
  
  #ifdef HAVE_WIN32_THREADS
  #if !defined(HAVE_COMPILER_TLS)
+ #if defined(LIBXML_STATIC)
  typedef struct _xmlGlobalStateCleanupHelperParams
  {
      HANDLE thread;
***************
*** 339,344 ****
--- 340,346 ----
      free(params);
      _endthread();
  }
+ #endif /* LIBXML_STATIC */
  #endif /* HAVE_COMPILER_TLS */
  #endif /* HAVE_WIN32_THREADS */
  
***************
*** 381,393 ****
--- 383,399 ----
      }
      if ((globalval = (xmlGlobalState *) TlsGetValue(globalkey)) == NULL) {
        xmlGlobalState *tsd = xmlNewGlobalState();
+ #if defined(LIBXML_STATIC)
        xmlGlobalStateCleanupHelperParams *p = 
            (xmlGlobalStateCleanupHelperParams *) malloc(sizeof(xmlGlobalStateCleanupHelperParams));
        p->memory = tsd;
        DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), 
                GetCurrentProcess(), &p->thread, 0, TRUE, DUPLICATE_SAME_ACCESS);
+ #endif
        TlsSetValue(globalkey, tsd);
+ #if defined(LIBXML_STATIC)
        _beginthread(xmlGlobalStateCleanupHelper, 0, p);
+ #endif
  
        return (tsd);
      }
***************
*** 535,537 ****
--- 541,566 ----
      mainthread = GetCurrentThreadId();
  #endif
  }
+ 
+ #if defined(HAVE_WIN32_THREADS) && !defined(LIBXML_STATIC)
+ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
+     switch(fdwReason)
+     {
+     case DLL_THREAD_DETACH:
+         if (globalkey != TLS_OUT_OF_INDEXES) {
+             xmlGlobalState *globalval = (xmlGlobalState *)TlsGetValue(globalkey);
+             if (globalval) {
+                 xmlFreeGlobalState(globalval);
+                 TlsSetValue(globalkey,NULL);
+             }
+         }
+         break;
+     case DLL_PROCESS_DETACH:
+         if (globalkey != TLS_OUT_OF_INDEXES) {
+             TlsFree(globalkey);
+             globalkey = TLS_OUT_OF_INDEXES;
+         }
+         break;
+     }
+ }
+ #endif


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