Index: /trunk/mod_cacher/xc_cacher.c
===================================================================
--- /trunk/mod_cacher/xc_cacher.c	(revision 1203)
+++ /trunk/mod_cacher/xc_cacher.c	(revision 1204)
@@ -41,5 +41,5 @@
 
 #define ENTER_LOCK_EX(x) \
-	xc_lock((x)->lck); \
+	LOCK((x)); \
 	zend_try { \
 		do
@@ -49,5 +49,5 @@
 		catched = 1; \
 	} zend_end_try(); \
-	xc_unlock((x)->lck)
+	UNLOCK((x))
 
 #define ENTER_LOCK(x) do { \
@@ -2651,10 +2651,11 @@
 		}
 		CHECK(allocator->vtable->init(shm, allocator, memsize), "Failed init allocator");
-		CHECK(cache->cached           = allocator->vtable->calloc(allocator, 1, sizeof(xc_cached_t)), "cache OOM");
-		CHECK(cache->cached->entries  = allocator->vtable->calloc(allocator, hentry->size, sizeof(xc_entry_t*)), "entries OOM");
+		CHECK(cache->cached           = allocator->vtable->calloc(allocator, 1, sizeof(xc_cached_t)), "create cache OOM");
+		CHECK(cache->cached->entries  = allocator->vtable->calloc(allocator, hentry->size, sizeof(xc_entry_t*)), "create entries OOM");
 		if (hphp) {
-			CHECK(cache->cached->phps = allocator->vtable->calloc(allocator, hphp->size, sizeof(xc_entry_data_php_t*)), "phps OOM");
-		}
-		CHECK(cache->lck              = xc_lock_init(NULL, 0), "can't create lock");
+			CHECK(cache->cached->phps = allocator->vtable->calloc(allocator, hphp->size, sizeof(xc_entry_data_php_t*)), "create phps OOM");
+		}
+		CHECK(cache->lck              = allocator->vtable->calloc(allocator, 1, xc_lock_size()), "create lock OOM");
+		CHECK(xc_lock_init(cache->lck, NULL, 1), "can't create lock");
 
 		cache->hcache  = hcache;
Index: /trunk/xcache/xc_lock.c
===================================================================
--- /trunk/xcache/xc_lock.c	(revision 1203)
+++ /trunk/xcache/xc_lock.c	(revision 1204)
@@ -11,4 +11,23 @@
 #	include <errno.h>
 #endif
+
+/* {{{ detect what lock to use */
+#undef XC_INTERPROCESS_LOCK_IMPLEMENTED
+#undef XC_LOCK_UNSUED
+
+#ifdef ZEND_WIN32
+#	define XC_INTERPROCESS_LOCK_IMPLEMENTED
+#	ifndef ZTS
+#		define XC_LOCK_UNSUED
+#	endif
+#endif
+
+#ifdef _POSIX_THREAD_PROCESS_SHARED
+#	include "../mod_cacher/xc_cache.h"
+#	define XC_INTERPROCESS_LOCK_IMPLEMENTED
+#endif
+/* }}} */
+
+#ifndef XC_INTERPROCESS_LOCK_IMPLEMENTED
 
 #ifndef ZEND_WIN32
@@ -74,8 +93,8 @@
 
 	if (type == LCK_UN) {
-		return UnlockFileEx((HANDLE)lck->fd, 0, 1, 0, &offset);
+		return UnlockFileEx(lck->fd, 0, 1, 0, &offset);
 	}
 	else {
-		return LockFileEx((HANDLE)lck->fd, type, 0, 1, 0, &offset);
+		return LockFileEx(lck->fd, type, 0, 1, 0, &offset);
 	}
 }
@@ -164,18 +183,5 @@
 }
 /* }}} */
-
-#undef XC_INTERPROCESS_LOCK_IMPLEMENTED
-#undef XC_LOCK_UNSUED
-
-#ifdef ZEND_WIN32
-#	define XC_INTERPROCESS_LOCK_IMPLEMENTED
-#	ifndef ZTS
-#		define XC_LOCK_UNSUED
-#	endif
-#endif
-
-#if defined(PTHREADS)
-#	define XC_INTERPROCESS_LOCK_IMPLEMENTED
-#endif
+#endif /* XC_INTERPROCESS_LOCK_IMPLEMENTED */
 
 struct _xc_lock_t {
@@ -187,4 +193,7 @@
 #	endif
 
+#	ifdef _POSIX_THREAD_PROCESS_SHARED
+	pthread_mutex_t pthread_mutex;
+#	endif
 #	ifndef XC_INTERPROCESS_LOCK_IMPLEMENTED
 #		ifdef ZTS
@@ -194,41 +203,95 @@
 #	endif
 #endif
+
+#ifndef NDEBUG
+	zend_bool locked;
+#endif
 };
 
-xc_lock_t *xc_lock_init(const char *pathname, int interprocess) /* {{{ */
-{
-#ifdef XC_LOCK_UNSUED
-	return (xc_lock_t *) 1;
-#else
-	xc_lock_t *lck = malloc(sizeof(xc_lock_t));
-#	ifdef ZTS
-#		if defined(PTHREADS)
+size_t xc_lock_size(void) /* {{{ */
+{
+	return sizeof(xc_lock_t);
+}
+/* }}} */
+xc_lock_t *xc_lock_init(xc_lock_t *lck, const char *pathname, unsigned char interprocess) /* {{{ */
+{
+#ifdef ZTS
+#	ifdef _POSIX_THREAD_PROCESS_SHARED
 	pthread_mutexattr_t psharedm;
 	pthread_mutexattr_init(&psharedm);
 	pthread_mutexattr_setpshared(&psharedm, PTHREAD_PROCESS_SHARED);
-	lck->tsrm_mutex = (pthread_mutex_t *) malloc(sizeof(pthread_mutex_t));
-	pthread_mutex_init(lck->tsrm_mutex, &psharedm);
-#		else
+	pthread_mutex_init(&lck->pthread_mutex, &psharedm);
+	lck->tsrm_mutex = &lck->pthread_mutex;
+#	else
 	lck->tsrm_mutex = tsrm_mutex_alloc();
-#		endif
+#	endif
+#endif
+
+#ifndef XC_INTERPROCESS_LOCK_IMPLEMENTED
+#	ifdef ZTS
+	lck->use_fcntl = interprocess;
+	if (lck->use_fcntl)
+#	endif
+		xc_fcntl_init(&lck->fcntl_lock, pathname);
+#endif
+
+#ifndef NDEBUG
+	lck->locked = 0;
+#endif
+
+	return lck;
+}
+/* }}} */
+void xc_lock_destroy(xc_lock_t *lck) /* {{{ */
+{
+#ifdef ZTS
+#	ifdef _POSIX_THREAD_PROCESS_SHARED
+	pthread_mutex_destroy(&lck->pthread_mutex);
+#	else
+	tsrm_mutex_free(lck->tsrm_mutex);
+#	endif
+	lck->tsrm_mutex = NULL;
+#endif
+
+#ifndef XC_INTERPROCESS_LOCK_IMPLEMENTED
+#	ifdef ZTS
+	if (lck->use_fcntl)
+#	endif
+		xc_fcntl_destroy(&lck->fcntl_lock);
+#endif
+}
+/* }}} */
+void xc_lock(xc_lock_t *lck) /* {{{ */
+{
+#ifdef XC_LOCK_UNSUED
+#else
+#	ifdef ZTS
+	if (tsrm_mutex_lock(lck->tsrm_mutex) < 0) {
+		zend_error(E_ERROR, "xc_lock failed errno:%d", errno);
+	}
 #	endif
 #	ifndef XC_INTERPROCESS_LOCK_IMPLEMENTED
 #		ifdef ZTS
-	lck->use_fcntl = interprocess;
 	if (lck->use_fcntl)
 #		endif
-		xc_fcntl_init(&lck->fcntl_lock, pathname);
-#	endif
-	return lck;
-#endif
-}
-/* }}} */
-void xc_lock_destroy(xc_lock_t *lck) /* {{{ */
+		xc_fcntl_lock(&lck->fcntl_lock);
+#	endif
+#endif
+
+#ifndef NDEBUG
+	assert(!lck->locked);
+	lck->locked = 1;
+	assert(lck->locked);
+#endif
+}
+/* }}} */
+void xc_rdlock(xc_lock_t *lck) /* {{{ */
 {
 #ifdef XC_LOCK_UNSUED
-	/* do nothing */
-#else
-#	ifdef ZTS
-	tsrm_mutex_free(lck->tsrm_mutex);
+#else
+#	ifdef ZTS
+	if (tsrm_mutex_lock(lck->tsrm_mutex) < 0) {
+		zend_error(E_ERROR, "xc_rdlock failed errno:%d", errno);
+	}
 #	endif
 #	ifndef XC_INTERPROCESS_LOCK_IMPLEMENTED
@@ -236,54 +299,29 @@
 	if (lck->use_fcntl)
 #		endif
-		xc_fcntl_destroy(&lck->fcntl_lock);
-#	endif
-	free(lck);
-#endif
-}
-/* }}} */
-void xc_lock(xc_lock_t *lck) /* {{{ */
-{
+		xc_fcntl_lock(&lck->fcntl_lock);
+#	endif
+#endif
+
+#ifndef NDEBUG
+	assert(!lck->locked);
+	lck->locked = 1;
+	assert(lck->locked);
+#endif
+}
+/* }}} */
+void xc_unlock(xc_lock_t *lck) /* {{{ */
+{
+#ifndef NDEBUG
+	assert(lck->locked);
+	lck->locked = 0;
+	assert(!lck->locked);
+#endif
+
 #ifdef XC_LOCK_UNSUED
 #else
-#	ifdef ZTS
-	if (tsrm_mutex_lock(lck->tsrm_mutex) < 0) {
-		zend_error(E_ERROR, "xc_lock failed errno:%d", errno);
-	}
-#	endif
 #	ifndef XC_INTERPROCESS_LOCK_IMPLEMENTED
 #		ifdef ZTS
 	if (lck->use_fcntl)
 #		endif
-		xc_fcntl_lock(&lck->fcntl_lock);
-#	endif
-#endif
-}
-/* }}} */
-void xc_rdlock(xc_lock_t *lck) /* {{{ */
-{
-#ifdef XC_LOCK_UNSUED
-#else
-#	ifdef ZTS
-	if (tsrm_mutex_lock(lck->tsrm_mutex) < 0) {
-		zend_error(E_ERROR, "xc_rdlock failed errno:%d", errno);
-	}
-#	endif
-#	ifndef XC_INTERPROCESS_LOCK_IMPLEMENTED
-#		ifdef ZTS
-	if (lck->use_fcntl)
-#		endif
-		xc_fcntl_lock(&lck->fcntl_lock);
-#	endif
-#endif
-}
-/* }}} */
-void xc_unlock(xc_lock_t *lck) /* {{{ */
-{
-#ifdef XC_LOCK_UNSUED
-#else
-#	ifndef XC_INTERPROCESS_LOCK_IMPLEMENTED
-#		ifdef ZTS
-	if (lck->use_fcntl)
-#		endif
 		xc_fcntl_unlock(&lck->fcntl_lock);
 #	endif
Index: /trunk/xcache/xc_lock.h
===================================================================
--- /trunk/xcache/xc_lock.h	(revision 1203)
+++ /trunk/xcache/xc_lock.h	(revision 1204)
@@ -6,7 +6,10 @@
 #endif /* _MSC_VER > 1000 */
 
+#include <stdlib.h>
+
 typedef struct _xc_lock_t xc_lock_t;
 
-xc_lock_t *xc_lock_init(const char *pathname, int interprocess /* only with ZTS */);
+size_t xc_lock_size(void);
+xc_lock_t *xc_lock_init(xc_lock_t *lck, const char *pathname, unsigned char interprocess /* only with ZTS */);
 void xc_lock_destroy(xc_lock_t *lck);
 void xc_lock(xc_lock_t *lck);
