|
Apache HTTP Server Version 2.0 ![]() Apache 2.0 Thread Safety IssuesWhen using any of the threaded mpms in Apache 2.0 it is important that every function called from Apache be thread safe. When linking in 3rd party extensions it can be difficult to determine whether the resulting server will be thread safe. Casual testing generally won't tell you this either as thread safety problems can lead to subtle race conditons that may only show up in certain conditions under heavy load. Global and static variablesWhen writing your module or when trying to determine if a module or 3rd party library is thread safe there are some common things to keep in mind. First, you need to recognize that in a threaded model each individual thread has its own program counter, stack and registers. Local variables live on the stack, so those are fine. You need to watch out for any static or global variables. This doesn't mean that you are absolutely not allowed to use static or global variables. There are times when you actually want something to affect all threads, but generally you need to avoid using them if you want your code to be thread safe. In the case where you have a global variable that needs to be global and accessed by all threads, be very careful when you update it. If, for example, it is an incrementing counter, you need to atomically increment it to avoid race conditions with other threads. You do this using a mutex (mutual exclusion). Lock the mutex, read the current value, increment it and write it back and then unlock the mutex. Any other thread that wants to modify the value has to first check the mutex and block until it is cleared. If you are using APR, have a look
at the errnoThis is a common global variable that holds the error number of the
last error that occurred. If one thread calls a low-level function that
sets errno and then another thread checks it, we are bleeding error
numbers from one thread into another. To solve this, make sure your
module or library defines
which means that accessing errno will call
Common standard troublesome functionsNot only do things have to be thread safe, but they also have to be
reentrant.
Common 3rd Party LibrariesThe following is a list of common libraries that are used by 3rd party
Apache modules. You can check to see if your module is using a potentially
unsafe library by using tools such as
In addition to these libraries you will need to have a look at any
libraries linked statically into the module. You can use Library ListPlease drop a note to dev@httpd.apache.org if you have additions or corrections to this list.
|
|
This apache manual Copyright © 1999-2003, The Apache Software Foundation.
Web Design Copyright © 1999-2003. Chrisranjana Software Solutions Pvt Ltd. syndicate rss feed |