Luís Oliveira recently published a blog post (http://kvardek-du.kerno.org/2012/06/augmenting-bordeaux-threads-with-atomic....) iscussing the possibility of extending BT with atomic operations (like compare-and-swap). He put together a very handy table about implementation support for this operation, which gives the impression that it might indeed be a good time to wrap this feature in a portable API.
I'm working on a library that happens to use compare-and-swap, and wrote the following macro:
(defmacro compare-and-swap (place old-value new-value) #+sbcl (let ((old-val-var (gensym "OLD-VALUE-"))) ` (let ((,old-val-var ,old-value)) (eq ,old-val-var (sb-ext:compare-and-swap ,place ,old-val-var ,new-value)))) #+ccl `(ccl::conditional-store ,place ,old-value ,new-value) #+lispworks `(system:compare-and-swap ,place ,old-value ,new-value) #+allegro `(excl:atomic-conditional-setf ,place ,new-value ,old-value) #-(or allegro lispworks ccl sbcl) `(error "Not supported."))
I've tested this in all the mentioned implementations. I'm considering putting together a patch to add this macro to BT itself, and I'd like to know if this would be wanted, if there's any issues with the API itself, etc.
There are two big issues I see:
1. On CCL, conditional-store is an internal, unexported operation. It's also pretty narrow in what it can handle, and even breaks in situations such as typed struct slot access. rme was kind enough to create a ticket for this on the Clozure CL tracker, so this issue may be resolved in the (near?) future: http://trac.clozure.com/ccl/ticket/994
2. CAS support across implementations has a lot of 'but's associated with it. The only thing that seems to be supported across the listed implementations is svref and struct access. There's also weird behavior on some implementations (notably SBCL) when it comes to accessing dynamic variables (where it can only access the global binding, not the local one). Is this acceptable? Should a patch include compile-time warnings or errors when we detect (when possible) that CAS is being used on an unsupported place? Should it be left up to users to use it in the right case? Should it break for anything other than svref and structslot, which are the only ones supported across the board?
-- Josh Marchán
On Fri, Jul 20, 2012 at 10:00 AM, Josh Marchán sykopomp@sykosomatic.org wrote:
- CAS support across implementations has a lot of 'but's associated with
it. The only thing that seems to be supported across the listed implementations is svref and struct access. There's also weird behavior on some implementations (notably SBCL) when it comes to accessing dynamic variables (where it can only access the global binding, not the local one). Is this acceptable? Should a patch include compile-time warnings or errors when we detect (when possible) that CAS is being used on an unsupported place? Should it be left up to users to use it in the right case? Should it break for anything other than svref and structslot, which are the only ones supported across the board?
My API suggestion is to issue 1- an error if you're using the form in a way that is not supported on the current platform. 2- a warning if you're using the form in a way that is supported on the current platform but not universally.
The error and warning should use a defined condition, so that they can be suitably handled.
—♯ƒ • François-René ÐVB Rideau •Reflection&Cybernethics• http://fare.tunes.org It is a sorry society where honest people need to carry a gun, but a sorrier society where they need to carry one but can't.
In article 20120720140001.GA1193@Zushakon, Josh Marchán sykopomp@sykosomatic.org wrote:
There's also weird behavior on some implementations (notably SBCL) when it comes to accessing dynamic variables (where it can only access the global binding, not the local one).
I don't know where you're getting that. SYMBOL-VALUE works with dynamic variables and their current dynamic binding, if any.
Paul Khuong
On Sat, Jul 21, 2012 at 09:42:30AM -0400, Paul Khuong wrote:
In article 20120720140001.GA1193@Zushakon, Josh March?n sykopomp@sykosomatic.org wrote:
There's also weird behavior on some implementations (notably SBCL) when it comes to accessing dynamic variables (where it can only access the global binding, not the local one).
I don't know where you're getting that. SYMBOL-VALUE works with dynamic variables and their current dynamic binding, if any.
You're right. I hadn't tested it myself. I got that from Luis' post:
(2012-06-10 update: Nikodemus wrote in to point out that (cas (symbol-value '*foo*) ...) on SBCL modifies the special variable's global value rather than its dynamic binding. Fixed table accordingly.)
bordeaux-threads-devel@common-lisp.net