diff --git a/scsh/resource.scm b/scsh/resource.scm index 2b9e2d2..14feda0 100644 --- a/scsh/resource.scm +++ b/scsh/resource.scm @@ -1,12 +1,22 @@ +(define *resource-counter* 0) + (define-record-type resource :resource - (make-resource align! lock) + (really-make-resource count align! lock) resource? + (count resource-count) (align! resource-align!) (lock resource-lock)) +(define (make-resource align! lock) + (set! *resource-counter* (+ *resource-counter* 1)) + (really-make-resource *resource-counter* align! lock)) + (define (with-resources-aligned resources thunk) - (let ((locks (map resource-lock resources))) - (apply obtain-all-or-none locks) + (let ((locks (map resource-lock + (sort-list resources (lambda (r1 r2) + (< (resource-count r1) + (resource-count r2))))))) + (for-each obtain-lock locks) (for-each (lambda (align!) (align!)) (map resource-align! resources)) diff --git a/scsh/scsh-package.scm b/scsh/scsh-package.scm index 99d657d..0335d2b 100644 --- a/scsh/scsh-package.scm +++ b/scsh/scsh-package.scm @@ -38,7 +38,7 @@ define-record-types handle locks - scsh-utilities) + sort) (files resource)) (define-structure weak-tables weak-tables-interface