rlm@10
|
1 ;;; singleton.clj: singleton functions
|
rlm@10
|
2
|
rlm@10
|
3 ;; by Stuart Sierra, http://stuartsierra.com/
|
rlm@10
|
4 ;; April 14, 2009
|
rlm@10
|
5
|
rlm@10
|
6 ;; Copyright (c) Stuart Sierra, 2009. All rights reserved. The use
|
rlm@10
|
7 ;; and distribution terms for this software are covered by the Eclipse
|
rlm@10
|
8 ;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
|
rlm@10
|
9 ;; which can be found in the file epl-v10.html at the root of this
|
rlm@10
|
10 ;; distribution. By using this software in any fashion, you are
|
rlm@10
|
11 ;; agreeing to be bound by the terms of this license. You must not
|
rlm@10
|
12 ;; remove this notice, or any other, from this software.
|
rlm@10
|
13
|
rlm@10
|
14
|
rlm@10
|
15 ;; Change Log:
|
rlm@10
|
16 ;;
|
rlm@10
|
17 ;; April 14, 2009: added per-thread-singleton, renamed singleton to
|
rlm@10
|
18 ;; global-singleton
|
rlm@10
|
19 ;;
|
rlm@10
|
20 ;; April 9, 2009: initial version
|
rlm@10
|
21
|
rlm@10
|
22
|
rlm@10
|
23 (ns
|
rlm@10
|
24 ^{:author "Stuart Sierra",
|
rlm@10
|
25 :doc "Singleton functions"}
|
rlm@10
|
26 clojure.contrib.singleton)
|
rlm@10
|
27
|
rlm@10
|
28 (defn global-singleton
|
rlm@10
|
29 "Returns a global singleton function. f is a function of no
|
rlm@10
|
30 arguments that creates and returns some object. The singleton
|
rlm@10
|
31 function will call f just once, the first time it is needed, and
|
rlm@10
|
32 cache the value for all subsequent calls.
|
rlm@10
|
33
|
rlm@10
|
34 Warning: global singletons are often unsafe in multi-threaded code.
|
rlm@10
|
35 Consider per-thread-singleton instead."
|
rlm@10
|
36 [f]
|
rlm@10
|
37 (let [instance (atom nil)
|
rlm@10
|
38 make-instance (fn [_] (f))]
|
rlm@10
|
39 (fn [] (or @instance (swap! instance make-instance)))))
|
rlm@10
|
40
|
rlm@10
|
41 (defn per-thread-singleton
|
rlm@10
|
42 "Returns a per-thread singleton function. f is a function of no
|
rlm@10
|
43 arguments that creates and returns some object. The singleton
|
rlm@10
|
44 function will call f only once for each thread, and cache its value
|
rlm@10
|
45 for subsequent calls from the same thread. This allows you to
|
rlm@10
|
46 safely and lazily initialize shared objects on a per-thread basis.
|
rlm@10
|
47
|
rlm@10
|
48 Warning: due to a bug in JDK 5, it may not be safe to use a
|
rlm@10
|
49 per-thread-singleton in the initialization function for another
|
rlm@10
|
50 per-thread-singleton. See
|
rlm@10
|
51 http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5025230"
|
rlm@10
|
52 [f]
|
rlm@10
|
53 (let [thread-local (proxy [ThreadLocal] [] (initialValue [] (f)))]
|
rlm@10
|
54 (fn [] (.get thread-local))))
|