2003-08-13 08:46:58 -04:00
|
|
|
(define *resource-counter* 0)
|
|
|
|
|
2003-04-28 04:33:46 -04:00
|
|
|
(define-record-type resource :resource
|
2003-08-13 08:46:58 -04:00
|
|
|
(really-make-resource count align! lock)
|
2003-04-28 04:33:46 -04:00
|
|
|
resource?
|
2003-08-13 08:46:58 -04:00
|
|
|
(count resource-count)
|
2003-04-28 04:33:46 -04:00
|
|
|
(align! resource-align!)
|
|
|
|
(lock resource-lock))
|
|
|
|
|
2003-08-13 08:46:58 -04:00
|
|
|
(define (make-resource align! lock)
|
|
|
|
(set! *resource-counter* (+ *resource-counter* 1))
|
|
|
|
(really-make-resource *resource-counter* align! lock))
|
|
|
|
|
2003-04-28 04:33:46 -04:00
|
|
|
(define (with-resources-aligned resources thunk)
|
2003-08-13 08:46:58 -04:00
|
|
|
(let ((locks (map resource-lock
|
|
|
|
(sort-list resources (lambda (r1 r2)
|
|
|
|
(< (resource-count r1)
|
|
|
|
(resource-count r2)))))))
|
|
|
|
(for-each obtain-lock locks)
|
2003-04-28 04:33:46 -04:00
|
|
|
(for-each
|
|
|
|
(lambda (align!) (align!))
|
|
|
|
(map resource-align! resources))
|
|
|
|
(let ((val (with-handler
|
|
|
|
(lambda (cond more)
|
|
|
|
(for-each release-lock locks)
|
|
|
|
(more))
|
|
|
|
thunk)))
|
|
|
|
(for-each release-lock locks)
|
|
|
|
val)))
|