You are looking at historical revision 18622 of this page. It may differ significantly from its current revision.

Pool

Description

A library providing a pool datatype and functions for handling them in a thread-safe manner.

Author

Moritz Heidkamp

Documentation

A pool is a set of values which are supposed to be used by multiple threads in an arbitrary order. It is commonly used for scarce resources which must be used by many consumers at the same time, e.g. database connections shared in a multi threaded web server.

Procedures

[procedure] (make-pool values)

Returns a pool of the given list of values.

[procedure] (call-with-value-from-pool pool proc)

Locks one value in the given pool (possibly waiting in case all values are currently locked) and calls proc with this value as its argument.

[procedure] (call-with-value-from-pool-in-thread pool proc)

Does the same as call-with-value-from-pool but runs proc in a new thread and returns the respective thread.

Examples

(use pool srfi-18)

;; create a pool with some values
(define pool (make-pool '(foo bar baz)))

;; we want to use a value from the pool
(call-with-value-from-pool pool 
  ;; a is the first available value from the pool locked by a mutex
  ;; while this lambda is executed
  (lambda (a)
    ;; so this would print "foo"
    (print a)
    (thread-join! (call-with-value-from-pool-in-thread pool
		    (lambda (b)
		      ;; since the first value in the pool is still locked and we
		      ;; are now in a different thread, we get the next available
		      ;; value, in this case "bar", again locked by a mutex
		      (print b)))))

  (call-with-value-from-pool pool 
    (lambda (c)
      ;; we are back in the primordial thread here so we'll get foo
      ;; again since we already have a lock on it
      (print c))))

License

 Copyright (c) 2010, Moritz Heidkamp
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are
 met:
 
 Redistributions of source code must retain the above copyright
 notice, this list of conditions and the following disclaimer.
 
 Redistributions in binary form must reproduce the above copyright
 notice, this list of conditions and the following disclaimer in the
 documentation and/or other materials provided with the distribution.
 
 Neither the name of the author nor the names of its contributors may
 be used to endorse or promote products derived from this software
 without specific prior written permission.
 
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 OF THE POSSIBILITY OF SUCH DAMAGE.