unofficial mirror of bug-guix@gnu.org 
 help / color / mirror / code / Atom feed
From: "Clément Lassieur" <clement@lassieur.org>
To: 32234@debbugs.gnu.org
Subject: bug#32234: [PATCH 1/2] utils: Avoid deadlock when WITH-CRITICAL-SECTION calls are nested.
Date: Mon,  6 Aug 2018 21:27:35 +0200	[thread overview]
Message-ID: <20180806192736.1747-1-clement@lassieur.org> (raw)
In-Reply-To: <87k1ponc62.fsf@lassieur.org>

* src/cuirass/utils.scm (%critical-section-args): New parameter.
(make-critical-section): Put ARGS into a parameter, so that
CALL-WITH-CRITICAL-SECTION knows when it's called from the critical section.
In that case it would just apply PROC to ARGS.
(call-with-critical-section): If already in the critical section, apply PROC
to %CRITICAL-SECTION-ARGS instead of sending the message through the critical
section channel.
---
 src/cuirass/utils.scm | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)

diff --git a/src/cuirass/utils.scm b/src/cuirass/utils.scm
index 9e9ac36..6083890 100644
--- a/src/cuirass/utils.scm
+++ b/src/cuirass/utils.scm
@@ -94,6 +94,9 @@ delimited continuations and fibers."
         (conclusion)
         (apply throw args)))))
 
+(define %critical-section-args
+  (make-parameter #f))
+
 (define (make-critical-section . args)
   "Return a channel used to implement a critical section.  That channel can
 then be passed to 'join-critical-section', which will ensure sequential
@@ -104,19 +107,23 @@ dedicated fiber."
   (let ((channel (make-channel)))
     (spawn-fiber
      (lambda ()
-       (let loop ()
-         (match (get-message channel)
-           (((? channel? reply) . (? procedure? proc))
-            (put-message reply (apply proc args))))
-         (loop))))
+       (parameterize ((%critical-section-args args))
+         (let loop ()
+           (match (get-message channel)
+             (((? channel? reply) . (? procedure? proc))
+              (put-message reply (apply proc args))))
+           (loop)))))
     channel))
 
 (define (call-with-critical-section channel proc)
-  "Call PROC in the critical section corresponding to CHANNEL.  Return the
-result of PROC."
-  (let ((reply (make-channel)))
-    (put-message channel (cons reply proc))
-    (get-message reply)))
+  "Send PROC to the critical section through CHANNEL.  Return the result of
+PROC.  If already in the critical section, call PROC immediately."
+  (let ((args (%critical-section-args)))
+    (if args
+        (apply proc args)
+        (let ((reply (make-channel)))
+          (put-message channel (cons reply proc))
+          (get-message reply)))))
 
 (define-syntax-rule (with-critical-section channel (vars ...) exp ...)
   "Evaluate EXP... in the critical section corresponding to CHANNEL.
-- 
2.18.0

  parent reply	other threads:[~2018-08-06 19:29 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-07-21  9:57 bug#32234: Cuirass: The SQLite built in busy handler might block the Fibers scheduler Clément Lassieur
2018-07-23  9:18 ` Ludovic Courtès
2018-07-23 13:42   ` Clément Lassieur
2018-07-23 14:57     ` Ludovic Courtès
2018-07-23 16:18       ` Clément Lassieur
2018-07-23 20:11         ` Ludovic Courtès
2018-08-06 19:27 ` Clément Lassieur [this message]
2018-08-06 19:27   ` bug#32234: [PATCH 2/2] database: Serialize all database accesses in a thread Clément Lassieur
2018-08-06 19:35     ` Clément Lassieur
2018-08-19 14:06     ` Ludovic Courtès
2018-08-26 14:07       ` Clément Lassieur
2018-08-26 21:16         ` Ludovic Courtès
2018-08-27 13:47           ` Clément Lassieur
2018-08-27 12:41         ` Danny Milosavljevic
2018-08-27 13:18           ` Clément Lassieur
2018-08-27 14:23             ` Danny Milosavljevic
2018-08-27 15:05               ` Clément Lassieur

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://guix.gnu.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180806192736.1747-1-clement@lassieur.org \
    --to=clement@lassieur.org \
    --cc=32234@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).