Lisp, CLOS: adding a slot to a lock class

I am trying to add a new slot to a lock class. This is useful because I have many locks in the hierarchy, and if I keep the parent lock for each lock, it becomes easier to detect debugging problems.

Unfortunately, this, apparently, cannot be done using the class provide function. I can add slots for the process, but not for blocking, because it is listed as an inline class. See my previous question here how to do this for a process: lisp, CLOS: adding a slot to a process class

Do you know how to solve this problem? If this is not possible, the only alternative I can think of is to preserve the hierarchical relationship of locks in the hash table, but since some locks are created at runtime and in different processes, I will need to add another lock only to access the hash table where metadata is stored on locks.

It seems terribly ineffective to me. Do you have an idea?

edit: for clarification, I am using Clozure Common Lisp.

+4
source share
2 answers

You can specify a metaclass using the class option :metaclass in the form of defclass.

 CL-USER> (defclass hierarchical-lock (lock) ((parent :initarg :parent :reader parent)) (:metaclass built-in-class)) #<BUILT-IN-CLASS HIERARCHICAL-LOCK> 

However, although you can do this and get a class, I'm not sure how you are going to create it. Trying to use make-instance fails:

 CL-USER> (make-instance 'hierarchical-lock) ; There is no applicable method for the generic function: ; #<STANDARD-GENERIC-FUNCTION MAKE-INSTANCE #x30200002676F> ; when called with arguments: ; (#<BUILT-IN-CLASS HIERARCHICAL-LOCK>) ; [Condition of type SIMPLE-ERROR] 

make-lock implemented in l0-aprims.lisp as

 (defun make-lock (&optional name) "Create and return a lock object, which can be used for synchronization between threads." (%make-lock (%make-recursive-lock-ptr) name)) 

You can keep track of the %make-lock implementation until you get to the implementation details at a low level, but it's clear that locks don't work out the same way as regular CLOS instances.

In addition to Rainer Joswig’s suggestion in a comment on this answer that you let CCL developers know that you think locks are CLOS objects, you can always use some aggregation and define your own hierarchical-lock , which has a slot for its primitive lock and slot for parent. By the magic of common functions, you can implement methods for common functions that work with locks, so your hierarchical-lock behaves like a built-in lock. (This assumes that the locking API is defined in terms of common functions.)

+3
source

You can use a subclass with such a slot.

 (defclass hierarchical-lock (lock) ((parent :initarg :parent :reader parent))) 
0
source

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


All Articles