chickadee » timed-resource

Outdated egg!

This is an egg for CHICKEN 4, the unsupported old release. You're almost certainly looking for the CHICKEN 5 version of this egg, if it exists.

If it does not exist, there may be equivalent functionality provided by another egg; have a look at the egg index. Otherwise, please consider porting this egg to the current version of CHICKEN.

timed-resource

Documentation

Provides an abstraction for a managed object, known as a resource, with a fixed lifetime. This might be used to create a file port that will automatically close if unused for some amount of time.

make-timed-resource

make-timed-resource OPENER CLOSER TIMEOUT #!optional NAMEprocedure

Returns a new timed-resource object that covers a resource with a lifetime of TIMEOUT seconds. The resource is acquired by the OPENER and released by the CLOSER. An acquired resource will be released automatically at the end of its' life.

OPENER
(procedure () *) ; returns the resource-object
CLOSER
(procedure (*)) ; takes the resource-object (returned by OPENER)
TIMEOUT
number ; seconds
NAME
* ; prefix for the gensym of the created timed-resource

No attempt is made to serialize access to a resource, this is up to the caller; just don't share the thing.

Exceptions occurring during resource acquisition or release abort the operation. Exceptions during resource release at shutdown are just displayed but no further action is taken.

with-timed-resource

with-timed-resource TIMED-RESOURCE ACTIONprocedure

Returns the result of invoking ACTION with the resource covered by the TIMED-RESOURCE. The acquired resource will not be released during the extent of the with-timed-resource invocation.

ACTION is a (procedure (timed-resource) *).

timed-resource?

check-timed-resource

error-timed-resource

timed-resource? OBJECTprocedure

Is the OBJECT a timed-resource?

timed-resource-name

timed-resource-name TIMED-RESOURCEprocedure

Returns unique id for TIMED-RESOURCE.

default-timed-resource-timeout

default-timed-resource-timeoutprocedure
default-timed-resource-timeout SECONDSprocedure

Gets & sets the number of seconds to wait for a thread to quit.

Default is #f.

Currently only used at shutdown.

Examples

(use timed-resource srfi-4)

;; Returns a blob of random bits.
;;
;; (random-blob [BITS])
;; BITS - the number of random bits, default is 128

(define random-blob
  (let (
    (*tr-random-dev*
      (make-timed-resource
        ;random device (*nix only)
        (lambda () (open-input-file "/dev/random" #:binary))
        (lambda (port) (close-output-port port))
        ;open for 10 seconds
        10.0))
    (round-bytes
      (lambda (x) (fx/ (fx+ x 7) 8))) )
    (lambda (#!optional (bits 128))
      (with-timed-resource *tr-random-dev*
        (lambda (port)
          (u8vector->blob (read-u8vector (round-bytes bits))))) ) ) )

Usage

(require-extension timed-resource)

Requirements

thread-utils synch record-variants check-errors

setup-helper

Author

Kon Lovett

Version history

1.1.0
Add types.
1.0.2
Use compiled setup-helper (& un-hide).
1.0.1
Fix for reporting an expected condition, timed-resource-closed, during shutdown.
1.0.0
Moved from srfi-27 into own extension.

License

Copyright (C) 2010-2017 Kon Lovett. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ASIS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Contents »