*/
APR_DECLARE(void) apr_pool_tag(apr_pool_t *pool, const char *tag);
+#if APR_HAS_THREADS
+/**
+ * Add a mutex to a pool to make it suitable to use from multiple threads.
+ * @param pool The pool to add the mutex to
+ * @param mutex The mutex
+ * @remark The mutex does not protect the destroy operation just the low level allocs.
+ */
+APR_DECLARE(void) apr_pool_mutex_set(apr_pool_t *pool,
+ apr_thread_mutex_t *mutex);
+#endif
+
/*
* User data management
* key names is a typical way to help ensure this uniqueness.
*
*/
+
APR_DECLARE(apr_status_t) apr_pool_userdata_set(
const void *data,
const char *key,
apr_abortfunc_t abort_fn;
apr_hash_t *user_data;
const char *tag;
-
+#if APR_HAS_THREADS
+ apr_thread_mutex_t *user_mutex;
+#endif
#if !APR_POOL_DEBUG
apr_memnode_t *active;
apr_memnode_t *self; /* The node containing the pool itself */
#endif /* APR_POOL_DEBUG */
#ifdef NETWARE
apr_os_proc_t owner_proc;
+
#endif /* defined(NETWARE) */
};
APR_DECLARE(void *) apr_palloc(apr_pool_t *pool, apr_size_t size)
{
apr_memnode_t *active, *node;
- void *mem;
+ void *mem = NULL;
apr_size_t free_index;
-
+#if APR_HAS_THREADS
+ if (pool->user_mutex) apr_thread_mutex_lock(pool->user_mutex);
+#endif
size = APR_ALIGN_DEFAULT(size);
active = pool->active;
mem = active->first_avail;
active->first_avail += size;
- return mem;
+ goto end;
}
node = active->next;
if (pool->abort_fn)
pool->abort_fn(APR_ENOMEM);
- return NULL;
+ mem = NULL;
+ goto end;
}
}
active->free_index = (APR_UINT32_TRUNC_CAST)free_index;
node = active->next;
if (free_index >= node->free_index)
- return mem;
+ goto end;
do {
node = node->next;
list_remove(active);
list_insert(active, node);
+ end:
+#if APR_HAS_THREADS
+ if (pool->user_mutex) apr_thread_mutex_unlock(pool->user_mutex);
+#endif
return mem;
}
APR_DECLARE(void) apr_pool_clear(apr_pool_t *pool)
{
apr_memnode_t *active;
-
+#if APR_HAS_THREADS
+ if (pool->user_mutex) apr_thread_mutex_lock(pool->user_mutex);
+#endif
/* Destroy the subpools. The subpools will detach themselves from
* this pool thus this loop is safe and easy.
*/
active->first_avail = pool->self_first_avail;
if (active->next == active)
- return;
+ goto end;
*active->ref = NULL;
allocator_free(pool->allocator, active->next);
active->next = active;
active->ref = &active->next;
+
+ end:
+#if APR_HAS_THREADS
+ if (pool->user_mutex) apr_thread_mutex_unlock(pool->user_mutex);
+#endif
}
+#if APR_HAS_THREADS
+APR_DECLARE(void) apr_pool_mutex_set(apr_pool_t *pool,
+ apr_thread_mutex_t *mutex)
+{
+ pool->user_mutex = mutex;
+}
+#endif
+
APR_DECLARE(void) apr_pool_destroy(apr_pool_t *pool)
{
apr_memnode_t *active;
pool->subprocesses = NULL;
pool->user_data = NULL;
pool->tag = NULL;
-
+#if APR_HAS_THREADS
+ pool->user_mutex = NULL;
+#endif
#ifdef NETWARE
pool->owner_proc = (apr_os_proc_t)getnlmhandle();
#endif /* defined(NETWARE) */
apr_memnode_t *active, *node;
apr_size_t free_index;
+#if APR_HAS_THREADS
+ if (pool->user_mutex) apr_thread_mutex_lock(pool->user_mutex);
+#endif
+
ps.node = active = pool->active;
ps.pool = pool;
ps.vbuff.curpos = ps.node->first_avail;
pool->abort_fn(APR_ENOMEM);
}
- return NULL;
+ strp = NULL;
+ goto end;
}
}
if (pool->abort_fn)
pool->abort_fn(APR_ENOMEM);
- return NULL;
+ strp = NULL;
+ goto end;
}
strp = ps.vbuff.curpos;
/*
* Link the node in if it's a new one
*/
- if (!ps.got_a_new_node)
- return strp;
+ if (!ps.got_a_new_node)
+ goto end;
active = pool->active;
node = ps.node;
node = active->next;
if (free_index >= node->free_index)
- return strp;
+ goto end;
do {
node = node->next;
list_remove(active);
list_insert(active, node);
+ end:
+
+#if APR_HAS_THREADS
+ if (pool->user_mutex) apr_thread_mutex_unlock(pool->user_mutex);
+#endif
+
return strp;
}