change the #:get-lock-file argument of call-with-file-lock/timeout to #:lock-file

This commit is contained in:
Robby Findler 2011-08-11 09:28:01 -05:00
parent 78a999537d
commit 997813680d
4 changed files with 18 additions and 23 deletions

View File

@ -175,7 +175,7 @@
[else 'exists])) [else 'exists]))
(define (call-with-file-lock/timeout fn kind thunk failure-thunk (define (call-with-file-lock/timeout fn kind thunk failure-thunk
#:get-lock-file [get-lock-file (λ () (make-lock-file-name fn))] #:lock-file [lock-file #f]
#:delay [delay 0.01] #:delay [delay 0.01]
#:max-delay [max-delay 0.2]) #:max-delay [max-delay 0.2])
@ -187,18 +187,14 @@
(raise-type-error 'call-with-file-lock/timeout "procedure (arity 0)" thunk)) (raise-type-error 'call-with-file-lock/timeout "procedure (arity 0)" thunk))
(unless (and (procedure? thunk) (= (procedure-arity thunk) 0)) (unless (and (procedure? thunk) (= (procedure-arity thunk) 0))
(raise-type-error 'call-with-file-lock/timeout "procedure (arity 0)" failure-thunk)) (raise-type-error 'call-with-file-lock/timeout "procedure (arity 0)" failure-thunk))
(unless (or (path-string? get-lock-file) (unless (path-string? lock-file)
(and (procedure? get-lock-file) (= (procedure-arity get-lock-file) 0))) (raise-type-error 'call-with-file-lock/timeout "path-string? or #f" lock-file))
(raise-type-error 'call-with-file-lock/timeout "procedure (arity 0) returning a path-string? or path-string?" get-lock-file))
(unless (and (real? delay) (not (negative? delay))) (unless (and (real? delay) (not (negative? delay)))
(raise-type-error 'call-with-file-lock/timeout "non-negative real" delay)) (raise-type-error 'call-with-file-lock/timeout "non-negative real" delay))
(unless (and (real? max-delay) (not (negative? max-delay))) (unless (and (real? max-delay) (not (negative? max-delay)))
(raise-type-error 'call-with-file-lock/timeout "non-negative real" max-delay)) (raise-type-error 'call-with-file-lock/timeout "non-negative real" max-delay))
(define real-lock-file (define real-lock-file (or lock-file (make-lock-file-name fn)))
(if (procedure? get-lock-file) (get-lock-file) get-lock-file))
(unless (path-string? real-lock-file)
(raise-type-error 'call-with-file-lock/timeout "procedure (arity 0) returning a path-string? or path-string?" get-lock-file))
(let loop ([delay delay]) (let loop ([delay delay])
(call-with-file-lock (call-with-file-lock
kind kind

View File

@ -1056,30 +1056,29 @@ with the given @racket[lock-there], instead.}
[kind (or/c 'shared 'exclusive)] [kind (or/c 'shared 'exclusive)]
[thunk (-> any)] [thunk (-> any)]
[failure-thunk (-> any)] [failure-thunk (-> any)]
[#:get-lock-file get-lock-file (or/c path-string? (-> path-string?)) (make-lock-filename filename)] [#:lock-file lock-file (or/c #f path-string?) #f]
[#:delay delay (and/c real? (not/c negative?)) 0.01] [#:delay delay (and/c real? (not/c negative?)) 0.01]
[#:max-delay max-delay (and/c real? (not/c negative?)) 0.2]) [#:max-delay max-delay (and/c real? (not/c negative?)) 0.2])
any]{ any]{
Obtains a lock for the filename returned from @racket[(get-lock-file)] and then Obtains a lock for the filename @racket[lock-file] and then
calls @racket[thunk]. When @racket[thunk] returns, calls @racket[thunk]. The @racket[filename] argument specifies
a file path prefix that is only used
to generate the lock filename when @racket[lock-file] is @racket[#f].
Specifically, when @racket[lock-file] is @racket[#f], then
@racket[call-with-file-lock/timeout] uses @racket[make-lock-file-name] to build the
lock filename.
When @racket[thunk] returns,
@racket[call-with-file-lock] releases the lock, returning the result of @racket[call-with-file-lock] releases the lock, returning the result of
@racket[thunk]. The @racket[call-with-file-lock/timeout] function will retry @racket[thunk]. The @racket[call-with-file-lock/timeout] function will retry
after @racket[#:delay] seconds and continue retrying with exponential backoff after @racket[delay] seconds and continue retrying with exponential backoff
until delay reaches @racket[#:max-delay]. If until delay reaches @racket[max-delay]. If
@racket[call-with-file-lock/timeout] fails to obtain the lock, @racket[call-with-file-lock/timeout] fails to obtain the lock,
@racket[failure-thunk] is called in tail position. The @racket[kind] argument @racket[failure-thunk] is called in tail position. The @racket[kind] argument
specifies whether the lock is @racket['shared] or @racket['exclusive] specifies whether the lock is @racket['shared] or @racket['exclusive]
in the sense of @racket[port-try-file-lock?]. in the sense of @racket[port-try-file-lock?].
The @racket[filename] argument specifies a file path prefix that is only used
to generate the lock filename, when @racket[#:get-lock-file] is not present.
The @racket[call-with-file-lock/timeout] function uses a separate lock file to
prevent race conditions on @racket[filename], when @racket[filename] has not yet
been created. On the Windows platfom, the @racket[call-with-file-lock/timeout]
function uses a separate lock file (@racket["_LOCKfilename"]), because a lock
on @racket[filename] would interfere with replacing @racket[filename] via
@racket[rename-file-or-directory].
} }
@examples[ @examples[

View File

@ -1162,7 +1162,7 @@
(t:-> -Pathlike ManyUniv)) (t:-> -Pathlike ManyUniv))
(tc-e (call-with-file-lock/timeout #f 'exclusive (lambda () 'res) (lambda () 'err) (tc-e (call-with-file-lock/timeout #f 'exclusive (lambda () 'res) (lambda () 'err)
#:get-lock-file (lambda () "lock") #:lock-file "lock"
#:delay .01 #:delay .01
#:max-delay .2) (one-of/c 'res 'err)) #:max-delay .2) (one-of/c 'res 'err))

View File

@ -835,7 +835,7 @@
(one-of/c 'shared 'exclusive) (one-of/c 'shared 'exclusive)
(-> a) (-> a)
(-> a) (-> a)
#:get-lock-file (-> -Pathlike) #f #:lock-file (-opt -Pathlike) #f
#:delay -Real #f #:delay -Real #f
#:max-delay -Real #f #:max-delay -Real #f
a))] a))]