aboutsummaryrefslogtreecommitdiff
path: root/modules/singleton
diff options
context:
space:
mode:
Diffstat (limited to 'modules/singleton')
-rw-r--r--modules/singleton/pom.xml16
-rw-r--r--modules/singleton/src/main/clojure/clojure/contrib/singleton.clj54
2 files changed, 70 insertions, 0 deletions
diff --git a/modules/singleton/pom.xml b/modules/singleton/pom.xml
new file mode 100644
index 00000000..47b8975d
--- /dev/null
+++ b/modules/singleton/pom.xml
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http//www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
+ http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.clojure.contrib</groupId>
+ <artifactId>parent</artifactId>
+ <version>1.3.0-SNAPSHOT</version>
+ <relativePath>../parent</relativePath>
+ </parent>
+ <artifactId>singleton</artifactId>
+ <dependencies>
+ </dependencies>
+</project> \ No newline at end of file
diff --git a/modules/singleton/src/main/clojure/clojure/contrib/singleton.clj b/modules/singleton/src/main/clojure/clojure/contrib/singleton.clj
new file mode 100644
index 00000000..2545d9c7
--- /dev/null
+++ b/modules/singleton/src/main/clojure/clojure/contrib/singleton.clj
@@ -0,0 +1,54 @@
+;;; singleton.clj: singleton functions
+
+;; by Stuart Sierra, http://stuartsierra.com/
+;; April 14, 2009
+
+;; Copyright (c) Stuart Sierra, 2009. All rights reserved. The use
+;; and distribution terms for this software are covered by the Eclipse
+;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
+;; which can be found in the file epl-v10.html at the root of this
+;; distribution. By using this software in any fashion, you are
+;; agreeing to be bound by the terms of this license. You must not
+;; remove this notice, or any other, from this software.
+
+
+;; Change Log:
+;;
+;; April 14, 2009: added per-thread-singleton, renamed singleton to
+;; global-singleton
+;;
+;; April 9, 2009: initial version
+
+
+(ns
+ ^{:author "Stuart Sierra",
+ :doc "Singleton functions"}
+ clojure.contrib.singleton)
+
+(defn global-singleton
+ "Returns a global singleton function. f is a function of no
+ arguments that creates and returns some object. The singleton
+ function will call f just once, the first time it is needed, and
+ cache the value for all subsequent calls.
+
+ Warning: global singletons are often unsafe in multi-threaded code.
+ Consider per-thread-singleton instead."
+ [f]
+ (let [instance (atom nil)
+ make-instance (fn [_] (f))]
+ (fn [] (or @instance (swap! instance make-instance)))))
+
+(defn per-thread-singleton
+ "Returns a per-thread singleton function. f is a function of no
+ arguments that creates and returns some object. The singleton
+ function will call f only once for each thread, and cache its value
+ for subsequent calls from the same thread. This allows you to
+ safely and lazily initialize shared objects on a per-thread basis.
+
+ Warning: due to a bug in JDK 5, it may not be safe to use a
+ per-thread-singleton in the initialization function for another
+ per-thread-singleton. See
+ http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5025230"
+ [f]
+ (let [thread-local (proxy [ThreadLocal] [] (initialValue [] (f)))]
+ (fn [] (.get thread-local))))