scsh-0.5/scsh/flock.scm

151 lines
4.6 KiB
Scheme
Raw Permalink Normal View History

1995-10-13 23:34:21 -04:00
;;; Scsh
;;; Posix advisory record-locking for file descriptors.
;;; These procs may only be applied to integer file descriptors;
;;; they may not be applied to ports.
;;; Copyright (c) 1995 by David Albertz and Olin Shivers. See file COPYING.
1995-10-13 23:34:21 -04:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; C syscall interface
;;;;;;;;;;;;;;;;;;;;;;;
(foreign-source
"#include <sys/types.h>"
"#include <unistd.h>"
"#include <errno.h>"
1995-10-13 23:34:21 -04:00
"#include <fcntl.h>"
""
"/* Make sure foreign-function stubs interface to the C funs correctly: */"
"#include \"flock1.h\""
""
1995-10-13 23:34:21 -04:00
"#define errno_or_false(x) (((x) == -1) ? ENTER_FIXNUM(errno) : SCHFALSE)"
"" "")
(define-foreign %set-lock (set_lock (integer fd)
(integer cmd)
(integer type)
(integer whence)
(integer start)
(integer len))
(to-scheme integer errno_or_false))
(define-foreign %get-lock (get_lock (integer fd)
(integer cmd)
(integer type)
(integer whence)
(integer start)
(integer len))
(to-scheme integer errno_or_false)
integer ; lock type
integer ; whence
integer ; start
integer ; len
integer) ; pid
;;; The LOCK record type
;;;;;;;;;;;;;;;;;;;;;;;;
(define-record %lock-region
exclusive?
start ; integer
len ; Positive integer or #f
whence ; seek/set, seek/delta, or seek/end.
proc ; Process holding lock
1995-10-13 23:34:21 -04:00
)
(define lock-region? %lock-region?)
(define lock-region:exclusive? %lock-region:exclusive?)
(define lock-region:whence %lock-region:whence)
(define lock-region:start %lock-region:start)
(define lock-region:len %lock-region:len)
(define lock-region:proc %lock-region:proc)
1995-10-13 23:34:21 -04:00
(define set-lock-region:exclusive? set-%lock-region:exclusive?)
(define set-lock-region:whence set-%lock-region:whence)
(define set-lock-region:start set-%lock-region:start)
(define set-lock-region:len set-%lock-region:len)
(define set-lock-region:proc set-%lock-region:proc)
;;; Backwards compatibility for one or two releases.
(define lock-region:pid
(deprecated-proc (lambda (lr)
(cond ((lock-region:proc lr) => proc:pid)
(else #f)))
'lock-region:pid
"Use lock-region:proc instead."))
1995-10-13 23:34:21 -04:00
(define (make-lock-region exclusive? start len . maybe-whence)
(let ((whence (:optional maybe-whence seek/set)))
(make-%lock-region exclusive? start len whence #f)))
1995-10-13 23:34:21 -04:00
;;; Internal middleman routine
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (call-lock-region proc cmd fdes lock)
(check-arg lock-region? lock proc)
(let ((lock-type (if (lock-region:exclusive? lock) lock/write lock/read)))
(proc fdes cmd lock-type
(lock-region:whence lock)
(lock-region:start lock)
(lock-region:len lock))))
;;; The main routines
;;;;;;;;;;;;;;;;;;;;;
(define-errno-syscall (lock-region fdes lock)
(lambda (fdes lock)
(call-lock-region %set-lock fcntl/set-record-lock fdes lock)))
1995-10-13 23:34:21 -04:00
;;; Return true/false indicating success/failure.
(define (lock-region/no-block fdes lock)
(cond ((call-lock-region %set-lock fcntl/set-record-lock-no-block fdes lock)
1995-10-13 23:34:21 -04:00
=> (lambda (errno)
(cond ((or (= errno errno/again) (= errno errno/acces)) #f)
1996-08-24 04:52:34 -04:00
((= errno errno/intr) (lock-region/no-block fdes lock))
(else (errno-error errno lock-region/no-block fdes lock)))))
1995-10-13 23:34:21 -04:00
(else #t)))
;;; Return first lock that conflicts w/LOCK; if none, return #f.
1995-10-13 23:34:21 -04:00
(define (get-lock-region fdes lock)
(receive (err type whence start len pid)
(call-lock-region %get-lock fcntl/get-record-lock fdes lock)
1996-08-24 04:52:34 -04:00
(cond ((not err)
(and (not (= type lock/release))
(make-%lock-region (= type lock/write) start len whence
(pid->proc pid 'create))))
1996-08-24 04:52:34 -04:00
((= err errno/intr) (get-lock-region fdes lock))
(else (errno-error err get-lock-region fdes lock)))))
1995-10-13 23:34:21 -04:00
1996-08-24 04:52:34 -04:00
(define-errno-syscall (unlock-region fdes lock)
(lambda (fdes lock)
(%set-lock fdes fcntl/set-record-lock lock/release
(lock-region:whence lock)
(lock-region:start lock)
(lock-region:len lock))))
1995-10-13 23:34:21 -04:00
;;; Locks with dynamic extent -- with and without sugar
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Throwing out frees the lock. Don't throw back in.
(define (with-region-lock* fd lock thunk)
(let ((returned? #f))
(dynamic-wind (lambda ()
(if returned?
(error "Can't throw back into a with-region-lock" lock)
(lock-region fd lock)))
thunk
(lambda ()
(unlock-region fd lock)
(set! returned? #t)))))
(define-syntax with-region-lock
(syntax-rules ()
((with-region-lock fd lock body ...)
(with-region-lock* fd lock (lambda () body ...)))))