Lisp, CLOS: adding a slot to a process class

My program gets multithreading errors, so I want to expand the captured capture macro to keep track of the lock stack that the process is receiving. I want to do this by simply adding a processing slot to save the lock stack.

Unfortunately, I do not understand how to add a slot at runtime without destroying what is already there. make-class completely overrides the class. I do not want this, since I do not know what another slot process has already done.

How to add a slot? In particular, I would like to add these two slots:

(lock-stack :documentation "stores a list of all locks of the process. Only used for debugging" :type list :initform nil :accessor lock-stack-acc ) (lock-stack-error-found :documentation "indicates that an error on the locks was already found. Only used for debugging" :type boolean :initform nil :accessor lock-stack-error-found-acc ) 
+2
source share
1 answer

Someone from GoogleGroups linked me to the answer: https://groups.google.com/group/comp.lang.lisp/msg/7e24e8417cd1b6e6?dmode=source

 (defun direct-slot-defn->initarg (slot-defn) (list :name (slot-definition-name slot-defn) :readers (slot-definition-readers slot-defn) :writers (slot-definition-writers slot-defn) :initform (slot-definition-initform slot-defn) :initargs (slot-definition-initargs slot-defn) :initfunction (slot-definition-initfunction slot-defn))) (defun add-slot-to-class (class name &key (initform nil) accessors readers writers initargs (initfunction (constantly nil))) (check-type class symbol) (let ((new-slots (list (list :name name :readers (union accessors readers) :writers (union writers (mapcar #'(lambda (x) (list 'setf x)) accessors) :test #'equal) :initform initform :initargs initargs :initfunction initfunction)))) (dolist (slot-defn (class-direct-slots (find-class class))) (push (direct-slot-defn->initarg slot-defn) new-slots)) (ensure-class class :direct-slots new-slots))) 
+1
source

Source: https://habr.com/ru/post/1486370/


All Articles