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 ...

Get Writing Apache Modules with Perl and C now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.