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: Re: [PATCH] Futures: Avoid creating the worker pool more than once Date: Wed, 07 Nov 2012 08:46:40 -0500 Message-ID: <87r4o58r0f.fsf@tines.lan> References: <87a9uu9vpk.fsf@tines.lan> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: ger.gmane.org 1352296088 25798 80.91.229.3 (7 Nov 2012 13:48:08 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 7 Nov 2012 13:48:08 +0000 (UTC) To: guile-devel@gnu.org Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Wed Nov 07 14:48:18 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 1TW5zK-00087I-BE for guile-devel@m.gmane.org; Wed, 07 Nov 2012 14:48:14 +0100 Original-Received: from localhost ([::1]:59015 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TW5zB-0000JC-6W for guile-devel@m.gmane.org; Wed, 07 Nov 2012 08:48:05 -0500 Original-Received: from eggs.gnu.org ([208.118.235.92]:55627) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TW5yj-00008I-Gg for guile-devel@gnu.org; Wed, 07 Nov 2012 08:48:02 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TW5y3-0005k3-Nb for guile-devel@gnu.org; Wed, 07 Nov 2012 08:47:37 -0500 Original-Received: from world.peace.net ([96.39.62.75]:57079) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TW5y3-0005jd-JL for guile-devel@gnu.org; Wed, 07 Nov 2012 08:46:55 -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 1TW5xw-0003AC-5t; Wed, 07 Nov 2012 08:46:48 -0500 In-Reply-To: <87a9uu9vpk.fsf@tines.lan> (Mark H. Weaver's message of "Tue, 06 Nov 2012 18:07:35 -0500") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.2 (gnu/linux) 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:15120 Archived-At: --=-=-= Content-Type: text/plain Here's an improved version the patch that gracefully handles the case where creation of the worker pool is unsuccessful due to an exception or cancelled thread. What do you think? 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 b0d936a348b916e73e9071abeb7baae3d7c126d3 Mon Sep 17 00:00:00 2001 From: Mark H Weaver Date: Wed, 7 Nov 2012 08:39:42 -0500 Subject: [PATCH] Futures: Avoid creating the worker pool more than once. * module/ice-9/futures.scm (%create-workers!): Use 'with-mutex' in case an exception is thrown. Within the critical section, check to make sure the worker pool hasn't already been created by another thread. --- module/ice-9/futures.scm | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/module/ice-9/futures.scm b/module/ice-9/futures.scm index 0f64b5c..7fbccf6 100644 --- a/module/ice-9/futures.scm +++ b/module/ice-9/futures.scm @@ -19,6 +19,7 @@ (define-module (ice-9 futures) #:use-module (srfi srfi-1) #:use-module (srfi srfi-9) + #:use-module (ice-9 threads) #:use-module (ice-9 q) #:export (future make-future future? touch)) @@ -157,15 +158,20 @@ touched." (define %workers '()) (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)) - (unlock-mutex %futures-mutex)) + (with-mutex + %futures-mutex + ;; 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! %workers + (unfold (lambda (i) (>= i %worker-count)) + (lambda (i) (call-with-new-thread process-futures)) + 1+ + 0)) + (set! create-workers! (lambda () #t))))) (define create-workers! (lambda () (%create-workers!))) -- 1.7.10.4 --=-=-=--