O'Reilly logo

Writing Apache Modules with Perl and C by Lincoln Stein, Doug MacEachern

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

11.8. Data Mutex Locking

Apache provides a cross-platform API for implementing data mutex locking. This is a mechanism implemented on most platforms to provide serialization when multiple concurrent threads need access to global data. The API was introduced with the Win32 port of Apache and will be adapted to future multithreaded versions of Apache for other platforms. While there is no need to implement data access serialization under multiprocess versions of Apache, this API will still work in such environments and is recommended for portability.[3]

[3] Under multiprocess versions of Apache, such as 1.3.x under Unix, using the mutex API does not introduce any overhead, as each function is simply defined as a no-op macro.

mutex *ap_create_mutex (char *name)

This function is used to allocate a mutex structure, which is required to implement runtime locking. This structure is normally a global variable, which is created during the module initialization phase.

static mutex *my_mutex = NULL;

static void my_module_init(server_rec *s, pool *p)
{
    if (!my_mutex) {
        my_mutex = ap_create_mutex(NULL);
    }
}

int ap_acquire_mutex (mutex *mutex_id)

The ap_acquire_mutex() function will acquire a lock. If a lock has already been acquired by another thread, it will block until it is released by the other thread.

int ap_release_mutex (mutex *mutex_id)

After locked data access is complete, the ap_release_mutex() function must be called so other threads are able to acquire locks.

static int my_handler(request_rec ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required