From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Mark H Weaver Newsgroups: gmane.lisp.guile.devel Subject: [PATCH] Futures: Avoid creating the worker pool more than once Date: Tue, 06 Nov 2012 18:07:35 -0500 Message-ID: <87a9uu9vpk.fsf@tines.lan> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: ger.gmane.org 1352243275 20073 80.91.229.3 (6 Nov 2012 23:07:55 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 6 Nov 2012 23:07:55 +0000 (UTC) To: guile-devel@gnu.org Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Wed Nov 07 00:08:05 2012 Return-path: Envelope-to: guile-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1TVsFX-0002xm-TG for guile-devel@m.gmane.org; Wed, 07 Nov 2012 00:08:04 +0100 Original-Received: from localhost ([::1]:59162 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TVsFO-0006tl-BT for guile-devel@m.gmane.org; Tue, 06 Nov 2012 18:07:54 -0500 Original-Received: from eggs.gnu.org ([208.118.235.92]:37241) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TVsFL-0006tg-DS for guile-devel@gnu.org; Tue, 06 Nov 2012 18:07:52 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TVsFK-0004HK-BK for guile-devel@gnu.org; Tue, 06 Nov 2012 18:07:51 -0500 Original-Received: from world.peace.net ([96.39.62.75]:56184) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TVsFK-0004HC-6g for guile-devel@gnu.org; Tue, 06 Nov 2012 18:07:50 -0500 Original-Received: from 209-6-91-212.c3-0.smr-ubr1.sbo-smr.ma.cable.rcn.com ([209.6.91.212] helo=tines.lan) by world.peace.net with esmtpsa (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.72) (envelope-from ) id 1TVsFD-000199-8v; Tue, 06 Nov 2012 18:07:43 -0500 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-Received-From: 96.39.62.75 X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Original-Sender: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.devel:15118 Archived-At: --=-=-= Content-Type: text/plain Any objections to applying this fix? Mark --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0001-Futures-Avoid-creating-the-worker-pool-more-than-onc.patch Content-Description: [PATCH] Futures: Avoid creating the worker pool more than once >From ff2c84b118b68020805eae93f71c3a8fd13ca3d9 Mon Sep 17 00:00:00 2001 From: Mark H Weaver Date: Tue, 6 Nov 2012 18:03:39 -0500 Subject: [PATCH] Futures: Avoid creating the worker pool more than once. * module/ice-9/futures.scm (%create-workers!): Check to make sure the worker pool hasn't already been created within the critical section. --- module/ice-9/futures.scm | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/module/ice-9/futures.scm b/module/ice-9/futures.scm index 0f64b5c..afdf48f 100644 --- a/module/ice-9/futures.scm +++ b/module/ice-9/futures.scm @@ -158,13 +158,19 @@ touched." (define (%create-workers!) (lock-mutex %futures-mutex) - (set! %workers - (unfold (lambda (i) (>= i %worker-count)) - (lambda (i) - (call-with-new-thread process-futures)) - 1+ - 0)) - (set! create-workers! (lambda () #t)) + ;; setting 'create-workers!' to a no-op is an optimization, but it is + ;; still possible for '%create-workers!' to be called more than once + ;; from different threads. Therefore, to avoid creating %workers more + ;; than once (and thus creating too many threads), we check to make + ;; sure %workers is empty within the critical section. + (when (null? %workers) + (set! create-workers! (lambda () #t)) + (set! %workers + (unfold (lambda (i) (>= i %worker-count)) + (lambda (i) + (call-with-new-thread process-futures)) + 1+ + 0))) (unlock-mutex %futures-mutex)) (define create-workers! -- 1.7.10.4 --=-=-=--