简体   繁体   中英

How to check if libxml2 is initialized

According to this document about multithreading, it is mandatory to call xmlInitParser() before using the library concurrently. Here's the problem. I coded a library(not an executable) using libxml2 and it should guarantee concurrency. So I decided to call the init function on my library's init function. However, the document says the function is not reentrant. So it could be problematic if other libraries or the program linking my library calls the function beforehand.

I couldn't find a function or a way to check if the parser(or libxml2 should I say) is initialised. What should I do? Call the function regardless and hope for the best? I'm going to test if the function is reentrant after all, after I post this but that doesn't really tally.

For clarification, in summary:

  • Is xmlInitParser() actually reentrant?
  • Any way to check if libxml2 is initialised?
  • (OR) How to safely use libxml2 concurrently under the premise that another software could be using it concurrently as well.

After looking at the sourcecode (taken from here ) it seems possible to call the function multiple times:

static int xmlParserInitialized = 0;

void
xmlInitParser(void) {
    if (xmlParserInitialized != 0)
     return;

 #ifdef LIBXML_THREAD_ENABLED
     __xmlGlobalInitMutexLock();
     if (xmlParserInitialized == 0) {
 #endif
     /* ... the actual initialization ... */
     xmlParserInitialized = 1;
 #ifdef LIBXML_THREAD_ENABLED
     }
     __xmlGlobalInitMutexUnlock();
 #endif
 }

You are concerned about multiple other libraries calling xmlInitParser() concurrently. The System V ABI implies that libraries are loaded one after another (see the section "Initialization and Termination Functions").
[Assuming none of the other libraries creates threads (that call xmlInitParser() )] this means that you do not have to worry about it.

If you really want to be safe you should link libxml statically in your library, so you have your own private copy that other libraries cannot interfere with.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM