Return-Path: Delivered-To: apmail-apr-dev-archive@www.apache.org Received: (qmail 65308 invoked from network); 28 Jul 2008 00:04:21 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 28 Jul 2008 00:04:21 -0000 Received: (qmail 77492 invoked by uid 500); 28 Jul 2008 00:04:19 -0000 Delivered-To: apmail-apr-dev-archive@apr.apache.org Received: (qmail 77443 invoked by uid 500); 28 Jul 2008 00:04:19 -0000 Mailing-List: contact dev-help@apr.apache.org; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Id: Delivered-To: mailing list dev@apr.apache.org Received: (qmail 77432 invoked by uid 99); 28 Jul 2008 00:04:19 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 27 Jul 2008 17:04:19 -0700 X-ASF-Spam-Status: No, hits=-0.0 required=10.0 tests=SPF_HELO_PASS,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of bojan@rexursive.com designates 203.171.74.242 as permitted sender) Received: from [203.171.74.242] (HELO beauty.rexursive.com) (203.171.74.242) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 28 Jul 2008 00:03:25 +0000 Received: from [10.1.120.24] (shrek.rexursive.com [10.1.120.24]) by beauty.rexursive.com (Postfix) with ESMTP id 0B2F7407AA for ; Mon, 28 Jul 2008 10:03:20 +1000 (EST) Subject: Re: Reslist v. no threads From: Bojan Smojver To: APR Development List In-Reply-To: <20080727135018.536e0c22@grimnir> References: <20080727200959.80206wuycnjzlx1c@www.rexursive.com> <20080727135018.536e0c22@grimnir> Content-Type: multipart/mixed; boundary="=-cNpckGGlFNmJLaNF5Zmj" Date: Mon, 28 Jul 2008 10:03:19 +1000 Message-Id: <1217203399.2812.5.camel@shrek.rexursive.com> Mime-Version: 1.0 X-Mailer: Evolution 2.22.3.1 (2.22.3.1-1.fc9) X-Virus-Checked: Checked by ClamAV on apache.org --=-cNpckGGlFNmJLaNF5Zmj Content-Type: text/plain Content-Transfer-Encoding: 7bit On Sun, 2008-07-27 at 13:50 +0100, Nick Kew wrote: > +1. Here is a rough sketch, so please review. Please note the enforcement of min, smax and hmax to 1. -- Bojan --=-cNpckGGlFNmJLaNF5Zmj Content-Disposition: attachment; filename=apu-reslist_no_threads.patch Content-Type: text/x-patch; name=apu-reslist_no_threads.patch; charset=UTF-8 Content-Transfer-Encoding: 7bit Index: include/apr_reslist.h =================================================================== --- include/apr_reslist.h (revision 680210) +++ include/apr_reslist.h (working copy) @@ -28,8 +28,6 @@ #include "apr_errno.h" #include "apr_time.h" -#if APR_HAS_THREADS - /** * @defgroup APR_Util_RL Resource List Routines * @ingroup APR_Util @@ -78,6 +76,9 @@ * @param pool The pool from which to create this resoure list. Also the * same pool that is passed to the constructor and destructor * routines. + * @remark If APR has been compiled without thread support, hmax will be + * automatically set to 1 and values of min and smax will be forced to + * 1 for any non-zero value. */ APU_DECLARE(apr_status_t) apr_reslist_create(apr_reslist_t **reslist, int min, int smax, int hmax, @@ -144,6 +145,4 @@ /** @} */ -#endif /* APR_HAS_THREADS */ - #endif /* ! APR_RESLIST_H */ Index: misc/apr_reslist.c =================================================================== --- misc/apr_reslist.c (revision 680210) +++ misc/apr_reslist.c (working copy) @@ -24,8 +24,6 @@ #include "apr_thread_cond.h" #include "apr_ring.h" -#if APR_HAS_THREADS - /** * A single resource element. */ @@ -56,8 +54,10 @@ void *params; /* opaque data passed to constructor and destructor calls */ apr_resring_t avail_list; apr_resring_t free_list; +#if APR_HAS_THREADS apr_thread_mutex_t *listlock; apr_thread_cond_t *avail; +#endif }; /** @@ -141,7 +141,9 @@ apr_reslist_t *rl = data_; apr_res_t *res; +#if APR_HAS_THREADS apr_thread_mutex_lock(rl->listlock); +#endif while (rl->nidle > 0) { apr_status_t rv1; @@ -158,9 +160,11 @@ assert(rl->nidle == 0); assert(rl->ntotal == 0); +#if APR_HAS_THREADS apr_thread_mutex_unlock(rl->listlock); apr_thread_mutex_destroy(rl->listlock); apr_thread_cond_destroy(rl->avail); +#endif return rv; } @@ -176,7 +180,9 @@ apr_res_t *res; int created_one = 0; +#if APR_HAS_THREADS apr_thread_mutex_lock(reslist->listlock); +#endif /* Check if we need to create more resources, and if we are allowed to. */ while (reslist->nidle < reslist->min && reslist->ntotal < reslist->hmax) { @@ -184,7 +190,9 @@ rv = create_resource(reslist, &res); if (rv != APR_SUCCESS) { free_container(reslist, res); +#if APR_HAS_THREADS apr_thread_mutex_unlock(reslist->listlock); +#endif return rv; } /* Add it to the list */ @@ -192,17 +200,21 @@ /* Update our counters */ reslist->ntotal++; /* If someone is waiting on that guy, wake them up. */ +#if APR_HAS_THREADS rv = apr_thread_cond_signal(reslist->avail); if (rv != APR_SUCCESS) { apr_thread_mutex_unlock(reslist->listlock); return rv; } +#endif created_one++; } /* We don't need to see if we're over the max if we were under it before */ if (created_one) { +#if APR_HAS_THREADS apr_thread_mutex_unlock(reslist->listlock); +#endif return APR_SUCCESS; } @@ -223,12 +235,16 @@ rv = destroy_resource(reslist, res); free_container(reslist, res); if (rv != APR_SUCCESS) { +#if APR_HAS_THREADS apr_thread_mutex_unlock(reslist->listlock); +#endif return rv; } } +#if APR_HAS_THREADS apr_thread_mutex_unlock(reslist->listlock); +#endif return APR_SUCCESS; } @@ -249,6 +265,17 @@ return APR_EINVAL; } +#if !APR_HAS_THREADS + /* There can be only one resource when we have no threads. */ + if (min > 0) { + min = 1; + } + if (smax > 0) { + smax = 1; + } + hmax = 1; +#endif + rl = apr_pcalloc(pool, sizeof(*rl)); rl->pool = pool; rl->min = min; @@ -262,6 +289,7 @@ APR_RING_INIT(&rl->avail_list, apr_res_t, link); APR_RING_INIT(&rl->free_list, apr_res_t, link); +#if APR_HAS_THREADS rv = apr_thread_mutex_create(&rl->listlock, APR_THREAD_MUTEX_DEFAULT, pool); if (rv != APR_SUCCESS) { @@ -271,6 +299,7 @@ if (rv != APR_SUCCESS) { return rv; } +#endif rv = reslist_maint(rl); if (rv != APR_SUCCESS) { @@ -297,7 +326,9 @@ apr_res_t *res; apr_time_t now; +#if APR_HAS_THREADS apr_thread_mutex_lock(reslist->listlock); +#endif /* If there are idle resources on the available list, use * them right away. */ now = apr_time_now(); @@ -310,19 +341,24 @@ rv = destroy_resource(reslist, res); free_container(reslist, res); if (rv != APR_SUCCESS) { +#if APR_HAS_THREADS apr_thread_mutex_unlock(reslist->listlock); +#endif return rv; /* FIXME: this might cause unnecessary fails */ } continue; } *resource = res->opaque; free_container(reslist, res); +#if APR_HAS_THREADS apr_thread_mutex_unlock(reslist->listlock); +#endif return APR_SUCCESS; } /* If we've hit our max, block until we're allowed to create * a new one, or something becomes free. */ while (reslist->ntotal >= reslist->hmax && reslist->nidle <= 0) { +#if APR_HAS_THREADS if (reslist->timeout) { if ((rv = apr_thread_cond_timedwait(reslist->avail, reslist->listlock, reslist->timeout)) != APR_SUCCESS) { @@ -333,6 +369,9 @@ else { apr_thread_cond_wait(reslist->avail, reslist->listlock); } +#else + return APR_EAGAIN; +#endif } /* If we popped out of the loop, first try to see if there * are new resources available for immediate use. */ @@ -340,7 +379,9 @@ res = pop_resource(reslist); *resource = res->opaque; free_container(reslist, res); +#if APR_HAS_THREADS apr_thread_mutex_unlock(reslist->listlock); +#endif return APR_SUCCESS; } /* Otherwise the reason we dropped out of the loop @@ -353,7 +394,9 @@ *resource = res->opaque; } free_container(reslist, res); +#if APR_HAS_THREADS apr_thread_mutex_unlock(reslist->listlock); +#endif return rv; } } @@ -363,12 +406,16 @@ { apr_res_t *res; +#if APR_HAS_THREADS apr_thread_mutex_lock(reslist->listlock); +#endif res = get_container(reslist); res->opaque = resource; push_resource(reslist, res); +#if APR_HAS_THREADS apr_thread_cond_signal(reslist->avail); apr_thread_mutex_unlock(reslist->listlock); +#endif return reslist_maint(reslist); } @@ -383,9 +430,13 @@ { apr_uint32_t count; +#if APR_HAS_THREADS apr_thread_mutex_lock(reslist->listlock); +#endif count = reslist->ntotal - reslist->nidle; +#if APR_HAS_THREADS apr_thread_mutex_unlock(reslist->listlock); +#endif return count; } @@ -394,12 +445,14 @@ void *resource) { apr_status_t ret; +#if APR_HAS_THREADS apr_thread_mutex_lock(reslist->listlock); +#endif ret = reslist->destructor(resource, reslist->params, reslist->pool); reslist->ntotal--; +#if APR_HAS_THREADS apr_thread_cond_signal(reslist->avail); apr_thread_mutex_unlock(reslist->listlock); +#endif return ret; } - -#endif /* APR_HAS_THREADS */ --=-cNpckGGlFNmJLaNF5Zmj--