Skip to content
This repository has been archived by the owner on Jul 26, 2021. It is now read-only.

Commit

Permalink
Thread safety for stable-pointers (#36)
Browse files Browse the repository at this point in the history
* Wrapping stable-pointer accesses with SBCL mutex

This works for me, in the sense that see no errors where nil gets funcall:ed. But it's a very naive approach, as I don't have a lot of experience with multi-threading.

* Wrapping stable-pointer accesses in mutex-locks.

This seems to work for me, in the sense that I don't see errors where nil gets funcall:ed. But I don't know if it might create other problems. Bordeaux-threads has to be loaded.

* Changed name sp-mutex -> stable-pointers lock

* bordeaux-threads now used by stable-pointers
  • Loading branch information
bpatrikm committed Mar 29, 2020
1 parent cebbe16 commit 303a236
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
3 changes: 2 additions & 1 deletion glib/cl-cffi-gtk-glib.asd
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
:depends-on (:cffi
:alexandria
:iterate
:trivial-features))
:trivial-features
:bordeaux-threads))

;;; --- End of file cl-cffi-gtk-glib.asd ---------------------------------------
29 changes: 17 additions & 12 deletions glib/glib.stable-pointer.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -32,36 +32,41 @@
;; free-stable-pointer. Stable pointers are used to pass references to lisp
;; objects to foreign code. thing is any object. The return value is an integer.

(let ((stable-pointers (make-array 0 :adjustable t :fill-pointer t)))
(let ((stable-pointers (make-array 0 :adjustable t :fill-pointer t))
(sp-mutex (bt:make-lock "stable-pointers lock")))

(defun allocate-stable-pointer (thing)
(flet ((find-fresh-id ()
(or ;; Search a free place for the pointer
(position nil stable-pointers)
;; Add a place for the pointer
(vector-push-extend nil stable-pointers))))
(let ((id (find-fresh-id)))
(setf (aref stable-pointers id) thing)
(make-pointer id))))
(bt:with-lock-held (sp-mutex)
(let ((id (find-fresh-id)))
(setf (aref stable-pointers id) thing)
(make-pointer id)))))

;; Frees the stable pointer previously allocated by allocate-stable-pointer

(defun free-stable-pointer (stable-pointer)
(setf (aref stable-pointers (pointer-address stable-pointer))
nil))
(bt:with-lock-held (sp-mutex)
(setf (aref stable-pointers (pointer-address stable-pointer))
nil)))

;; Returns the objects that is referenced by stable pointer previously
;; allocated by allocate-stable-pointer. May be called any number of times.

(defun get-stable-pointer-value (stable-pointer)
(let ((ptr-id (pointer-address stable-pointer)))
(when (<= 0 ptr-id (1- (length stable-pointers)))
(aref stable-pointers ptr-id))))
(bt:with-lock-held (sp-mutex)
(let ((ptr-id (pointer-address stable-pointer)))
(when (<= 0 ptr-id (1- (length stable-pointers)))
(aref stable-pointers ptr-id)))))

(defun set-stable-pointer-value (stable-pointer data)
(let ((ptr-id (pointer-address stable-pointer)))
(when (<= 0 ptr-id (1- (length stable-pointers)))
(setf (aref stable-pointers ptr-id) data))))
(bt:with-lock-held (sp-mutex)
(let ((ptr-id (pointer-address stable-pointer)))
(when (<= 0 ptr-id (1- (length stable-pointers)))
(setf (aref stable-pointers ptr-id) data)))))
)

;; Executes body with ptr bound to the stable pointer to result of evaluating
Expand Down

0 comments on commit 303a236

Please sign in to comment.