Skip to content

Commit 62c515e

Browse files
committed
Add mutex.clj
1 parent 55e17ad commit 62c515e

File tree

1 file changed

+37
-0
lines changed
  • src/main/clojure/clojure/core/async/impl

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
;; Copyright (c) Rich Hickey and contributors. All rights reserved.
2+
;; The use and distribution terms for this software are covered by the
3+
;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
4+
;; which can be found in the file epl-v10.html at the root of this distribution.
5+
;; By using this software in any fashion, you are agreeing to be bound by
6+
;; the terms of this license.
7+
;; You must not remove this notice, or any other, from this software.
8+
9+
(ns ^{:skip-wiki true}
10+
clojure.core.async.impl.mutex
11+
(:import [System.Threading Mutex])) ;;; [java.util.concurrent.locks Lock ReentrantLock]
12+
13+
;;;(defn mutex []
14+
;;; (let [m (ReentrantLock.)]
15+
;;; (reify
16+
;;; Lock
17+
;;; (lock [_] (.lock m))
18+
;;; (unlock [_] (.unlock m)))))
19+
20+
21+
;; The easiest solution is to define a protocol to give us the same methods (lock, unlock) as java.util.concurrent.locks.Lock,
22+
;; then have mutex reify the protocol wrapping a System.Threading.Mutex.
23+
24+
(defprotocol ILock
25+
"Providing the same affordance as java.util.concurrent.locks.Lock"
26+
(lock [_] "Lock the lock")
27+
(unlock [_] "Unlock the lock"))
28+
29+
(deftype Lock [^Mutex m]
30+
ILock
31+
(lock [x] (.WaitOne m))
32+
(unlock [x] (.ReleaseMutex m)))
33+
34+
35+
(defn mutex []
36+
(Lock. (Mutex.)))
37+

0 commit comments

Comments
 (0)