timed-resource

  1. timed-resource
  2. Documentation
    1. make-timed-resource
    2. call-with-timed-resource
    3. use-timed-resource
    4. timed-resource?
    5. check-timed-resource
    6. error-timed-resource
    7. timed-resource-open?
    8. timed-resource-name
    9. default-timed-resource-timeout
  3. Examples
  4. Usage
  5. Requirements
  6. Author
  7. Repository
  8. Version history
  9. License

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

[procedure] (make-timed-resource OPENER CLOSER TIMEOUT [NAME]) -> (or boolean timed-resource)

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. The acquired resource will be released automatically at the end of its' life.

During shutdown returns #f.

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.

call-with-timed-resource

[procedure] (call-with-timed-resource TIMED-RESOURCE ACTION) -> *

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 call-with-timed-resource invocation.

ACTION
(<(timed-resource-open) cached> -> *) ; protected use of resource.

use-timed-resource

[syntax] (use-timed-resource (TIMED-RESOURCE VAR) BODY ...) -> *

Returns the result of invoking BODY ... with the resource covered by the TIMED-RESOURCE. The acquired resource will be bound to VAR in the BODY .... The acquired resource will not be released during the extent of the use-timed-resource invocation.

timed-resource?

check-timed-resource

error-timed-resource

[procedure] (timed-resource? OBJECT) --> boolean

Is the OBJECT a timed-resource?

timed-resource-open?

[procedure] (timed-resource-open? TIMED-RESOURCE) -> boolean

Returns unique id for TIMED-RESOURCE.

timed-resource-name

[procedure] (timed-resource-name TIMED-RESOURCE) --> *

Returns unique id for TIMED-RESOURCE.

default-timed-resource-timeout

[procedure] (default-timed-resource-timeout) -> (or #f number)
[procedure] (default-timed-resource-timeout SECONDS) ->

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

Default is #f.

Currently only used at shutdown.

Examples

(import scheme (srfi-4) timed-resource)

;; Returns a blob from port.

(define (read-blob u8cnt #!optional (port (current-input-port)))
  (u8vector->blob (read-u8vector u8cnt port)) )

;; Returns a blob of random bits.
;; random device (*nix only)
;;
;; ((random-blob [SEC]) [CNT])
;; SEC - the number of seconds, default is 5.0
;; CNT - the number of random bytes, default is 16

(define (random-blob #!optional (lifetime 5.0))
  (let ((+tr+
          (make-timed-resource
            (cut open-input-file "/dev/random" #:binary)
            (cut close-output-port <>)
            lifetime)) )
    (lambda (#!optional (cnt 16))
      (print "\t** open? " (timed-resource-open? +tr+) " **")
      (use-timed-resource (+tr+ ip) (read-blob cnt ip)) ) ) )

Usage

(import timed-resource)

Requirements

thread-utils srfi-1 srfi-18 synch miscmacros record-variants check-errors

Author

Kon Lovett

Repository

This egg is hosted on the CHICKEN Subversion repository:

https://anonymous@code.call-cc.org/svn/chicken-eggs/release/5/timed-resource

If you want to check out the source code repository of this egg and you are not familiar with Subversion, see this page.

Version history

2.4.0
Remove deperecated with-timed-resource.
2.3.4
Fix egg category.
2.3.3
.
2.3.2
.
2.3.1
.
2.3.0
.
2.2.0
Add timed-resource-open?.
2.1.0
.
2.0.1
Fix make-timed-resource return.
2.0.0
C5 port.

License

Copyright (C) 2010-2022 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.