Lisp HUG Maillist Archive

Thread-safe slot accessors in CLOS

I've found myself doing the following periodically:

(defclass foo ()
   ((bar :initarg :bar :initform nil :reader foo-bar)))

(defmethod (setf foo-bar) (new-value (foo foo))
   (sys:atomic-exchange (slot-value foo 'bar) new-value))

I do this because bar is being used in another thread (obviously), and I 
want it to be safely setf-able. I'm just wondering if there's a nice 
LW-y way to do this from within the class definition? For example:

(defclass foo ()
   ((bar :initarg :bar :initform nil :accessor foo-bar :safety :atomic)))

Has an option like this ever been considered for LW? Or perhaps making 
an option so the :writer is automatically wrapped in some manner?

Jeff M.

_______________________________________________
Lisp Hug - the mailing list for LispWorks users
lisp-hug@lispworks.com
http://www.lispworks.com/support/lisp-hug.html


Re: Thread-safe slot accessors in CLOS

Interesting question... I have always worked on the assumption that simple slot and location references were essentially atomic. When it involves the coordination of several items that need to be kept in sync then locking would be necessary. But isn't a CLOS slot essentially already atomic?

- DM


On May 12, 2014, at 07:59 AM, Jeffrey Massung <massung@gmail.com> wrote:


I've found myself doing the following periodically:

(defclass foo ()
 ((bar :initarg :bar :initform nil :reader foo-bar)))

(defmethod (setf foo-bar) (new-value (foo foo))
 (sys:atomic-exchange (slot-value foo 'bar) new-value))

I do this because bar is being used in another thread (obviously), and I want it to be safely setf-able. I'm just wondering if there's a nice LW-y way to do this from within the class definition? For example:

(defclass foo ()
 ((bar :initarg :bar :initform nil :accessor foo-bar :safety :atomic)))

Has an option like this ever been considered for LW? Or perhaps making an option so the :writer is automatically wrapped in some manner?

Jeff M.

_______________________________________________
Lisp Hug - the mailing list for LispWorks users
lisp-hug@lispworks.com
http://www.lispworks.com/support/lisp-hug.html



Dr. David McClain
dbm@refined-audiometrics.com



Re: Thread-safe slot accessors in CLOS


On 12 May 2014, at 15:59, Jeffrey Massung <massung@gmail.com> wrote:
> 
> I do this because bar is being used in another thread (obviously), and I want it to be safely setf-able. I'm just wondering if there's a nice LW-y way to do this from within the class definition? For example:
> 
> (defclass foo ()
>  ((bar :initarg :bar :initform nil :accessor foo-bar :safety :atomic)))
> 
> Has an option like this ever been considered for LW? Or perhaps making an option so the :writer is automatically wrapped in some manner?

I think David McClain's point might answer this.  But if it doesn't and if you were really feeling your oats, I think you could implement something like this using the CLOS MOP: you'd need a metaclass, and then to have the reader/writer methods be some subclass of the standard one, and to check for slot options at class definition time.  I haven't tried doing this, but it looks possible.


_______________________________________________
Lisp Hug - the mailing list for LispWorks users
lisp-hug@lispworks.com
http://www.lispworks.com/support/lisp-hug.html


Re: Thread-safe slot accessors in CLOS

Unable to parse email body. Email id is 13036

Updated at: 2020-12-10 08:34 UTC