In process locking implemented, not sqlite locking.
This commit is contained in:
+25
-2
@@ -27,8 +27,10 @@
|
||||
ks-abort-transaction
|
||||
)
|
||||
|
||||
(define (any? a) #t)
|
||||
|
||||
(define-struct keystore
|
||||
(file path dbh)
|
||||
(file path dbh lock-sem (in-lock #:mutable) (entered #:mutable))
|
||||
#:transparent
|
||||
)
|
||||
|
||||
@@ -46,7 +48,7 @@
|
||||
(let ((dbh (sqlite3-connect #:database path #:mode 'create)))
|
||||
(query-exec dbh "CREATE TABLE IF NOT EXISTS keystore(key varchar primary key, value varchar, str_key varchar);")
|
||||
(query-exec dbh "CREATE INDEX IF NOT EXISTS keystore_idx on keystore(str_key);")
|
||||
(make-keystore file path dbh))))
|
||||
(make-keystore file path dbh (make-semaphore 1) #f 0))))
|
||||
|
||||
(define/contract (ks-close ksh)
|
||||
(-> keystore? boolean?)
|
||||
@@ -54,6 +56,27 @@
|
||||
(disconnect dbh)
|
||||
#t))
|
||||
|
||||
(define/contract (ks-with-lock ksh proc)
|
||||
(-> keystore? procedure? any?)
|
||||
(dynamic-wind
|
||||
(λ ()
|
||||
(cond
|
||||
((eq? (keystore-in-lock ksh) (current-thread))
|
||||
(set-keystore-entered! ksh (+ keystore-entered ksh) 1))
|
||||
(else
|
||||
(semaphore-wait (keystore-lock-sem ksh))
|
||||
(set-keystore-entered! ksh 1)
|
||||
))
|
||||
(set-keystore-in-lock! ksh (current-thread)))
|
||||
proc
|
||||
(λ ()
|
||||
(set-keystore-entered! (- (keystore-entered ksh) 1))
|
||||
(when (= (keystore-entered ksh) 0)
|
||||
(set-keystore-in-lock! ksh #f)
|
||||
(semaphore-post (keystore-lock-sem ksh))))
|
||||
)
|
||||
)
|
||||
|
||||
(define/contract (ks-set! ksh key value)
|
||||
(-> keystore? any/c any/c boolean?)
|
||||
(ks-set!* (keystore-dbh ksh) (key->string key) (format "~a" key) (value->string value)))
|
||||
|
||||
Reference in New Issue
Block a user