Dear Juho,
thank you very much for your enlightening remarks.
> Currently hash-tables are protected by spinlocks, so two threads can't
> be accessing the same table at the same time. This is however not a
> documented property of the public interface, and is likely to change
> in the very near future to a situation where: multiple concurrent
> readers on the same hash-table are safe, but having a writer thread +
> another concurrent thread doing either reads or writes is unsafe. Thus
> application level locks will be needed in the latter case.
Ok, I understand that. I assume the code enlarging the hash on demand will be
triggered in the same thread that intended to add an entry to the hash; so
there will be no problem there either.
> That can't happen. There will be a reference to ARR in a register of
> thread 2, which the gc will treat as a conservative pointer, and thus
> not move the array.
I see. That's great.
> There's just one gc, not individual gcs per thread.
Does the garbage collector suspend all other threads while it is working?
> You're going to lose horribly. All threads that can run Lisp code must
> have been created by sbcl, for all kinds of reasons.
Ok, so I'll have to subclass C++ classes like this one in order to make VTK or
other libraries fully functional in Lisp:
http://www.vtk.org/doc/nightly/html/classvtkMultiThreader.html
Best regards,
Bruno Daniel

Bruno Daniel <bruno.daniel@...> writes:
> Dear developers,
>
> is it safe to create a hash table in one thread and allow it to self-expand
> (e.g. by (make-hash-table :test #'eql :size 10000
> :rehash-size 2d0 :rehash-threshold 0.5))
> and then use it from another thread? What happens if it is expanded while some
> new entry is added from another thread?
Currently hash-tables are protected by spinlocks, so two threads can't
be accessing the same table at the same time. This is however not a
documented property of the public interface, and is likely to change
in the very near future to a situation where: multiple concurrent
readers on the same hash-table are safe, but having a writer thread +
another concurrent thread doing either reads or writes is unsafe. Thus
application level locks will be needed in the latter case.
> What happens if the garbage collector is moving a data structure that is
> simultaneously accessed from another thread? Say you have created an array
> by (setq arr (make-array 100)) and the garbage collector is moving
> it while another thread executes (setf (aref arr 3) (list 1 2))?
That can't happen. There will be a reference to ARR in a register of
thread 2, which the gc will treat as a conservative pointer, and thus
not move the array.
> If you don't pass an array to any other thread, will it stay untouched by the
> garbage collector runs of other threads?
There's just one gc, not individual gcs per thread.
> What is the answer to these questions if a foreign library creates threads on
> its own and then calls some Lisp functions via callbacks?
You're going to lose horribly. All threads that can run Lisp code must
have been created by sbcl, for all kinds of reasons.
--
Juho Snellman

Dear developers,
is it safe to create a hash table in one thread and allow it to self-expand
(e.g. by (make-hash-table :test #'eql :size 10000
:rehash-size 2d0 :rehash-threshold 0.5))
and then use it from another thread? What happens if it is expanded while some
new entry is added from another thread?
What happens if the garbage collector is moving a data structure that is
simultaneously accessed from another thread? Say you have created an array
by (setq arr (make-array 100)) and the garbage collector is moving
it while another thread executes (setf (aref arr 3) (list 1 2))?
If you don't pass an array to any other thread, will it stay untouched by the
garbage collector runs of other threads?
What is the answer to these questions if a foreign library creates threads on
its own and then calls some Lisp functions via callbacks?
Best regards
Bruno Daniel

Dear Nikodemus,
thank you very much for your clarification.
> Most C libraries using callbacks have a separate "data" argument
> to the callback you can use.
Unfortunately this isn't an option for my application. What I want to do is
to improve Swig's support for Common Lisp (using CFFI). I already fixed the
overloaded functions and methods problem. Now I'm working on enabling Lisp to
subclass C++ classes in a way that makes the resulting objects usable in
C++ libraries just as if they had been suclassed in C++ itself, and the
solution shouldn't be too inefficient.
My solution would be the following: Let Swig add a C++ subclass that receives
some Lisp callback pointers for the methods to override in the constructor.
The subclass will have overriding methods that just delegate to the callbacks
in case the pointers are non-zero and to the upper-class methods otherwise.
This might be fine, but what about additional slots in the subclass? As the
overriding methods, these are to be defined in Lisp, but how do I get the
overriding methods to reach them when they are called? Since the methods must
keep the signatures of the overridden methods, I can't add a data argument, so
only the self-argument ("this") remains. But this is a C++ object and it can't
keep references to Lisp objects since these may be moved around by the garbage
collector. I don't want to switch off the garbage collector, so I turned to
closures which also don't work out as we have seen.
In the meantime I had another idea that is not too inefficient: Instead of a
reference I'll keep an index in the C++ object. It's an index into a
self-expanding Lisp array that references all the structs for additional slots
of the C++-class-subclassing objects. The C++-destructor will remove the
pointer to the struct from the array and make the index value reusable for
later objects. Then Lisp's garbage collector can reclaim the formerly
referenced struct.
Some tricks are necessary in order to avoid too inefficient searching for free
indices upon object creation, but I think I can deal with that. Slot access
for this particular kind of objects will be slowed down a bit by two function
calls and an array access, but this will happen only once per method call
since I intend to use the following convenience macro. It's analogous to
with-slots:
(defmacro with-dslots (vars (obj class) &body body)
"Like WITH-SLOTS but for things of STRUCTURE-CLASS only. Allows efficent
uses of optimized DEFSTRUCT slot accessors outside of method bodies;
inspired by
http://www.koders.com/lisp/fidC34AFB44CE058B8B2706165FF96C9DD4F7434C1C.aspx";
(with-gensyms (obj-evaluated)
(labels
((slot-ref (obj1 slot class)
`(,(intern (concatenate 'string (string class) "-" (string slot)))
,obj1))
(varform (v)
(etypecase v
(symbol
`(,v ,(slot-ref obj-evaluated v class)))
(list
`(,(car v) ,(slot-ref obj-evaluated (cadr v) class))))))
`(let ((,obj-evaluated ,obj))
(symbol-macrolet ,(mapcar #'varform vars)
;;(declare (type ,class ,obj))
,@body)))))
;; Example:
(defstruct test-struct
(a 0 :type mword)
(b 0 :type mword))
(let ((teststr (make-test-struct :a 3 :b 4)))
(with-dslots (a b) (teststr test-struct)
(print (list a b))))
;; This prints (3 4) .
Best regards
Bruno Daniel

On 9/18/07, Bruno Daniel <bruno.daniel@...> wrote:
> I thought it over and now I understand why this function is undocumented:
> Using closures as callbacks will result in a serious memory leak: Since the
> C code might have stored the function pointer anywhere, Lisp will never be
> able to garbage collect the closure.
Quite so. Constructing the callback for a closure is also
horrendously expensive compared to non-closures:
(defun foo (x)
(alien-lambda () ... x ...))
(defun bar ()
(alien-lambda () ...))
FOO needs to round-trip through the compiler every time, whereas
BAR does that only on the first call.
...but the reason for this being undocumented is just a feature
of me having run out of steam & time while working on this.
> I must find another solution for my application.
Most C libraries using callbacks have a separate "data" argument
to the callback you can use.
Cheers,
-- Nikodemus

> Dear developers,
> I just tried it out and it seems to work. Here's the code with some
> corrections (CFFI has to be installed and loaded):
I thought it over and now I understand why this function is undocumented:
Using closures as callbacks will result in a serious memory leak: Since the
C code might have stored the function pointer anywhere, Lisp will never be
able to garbage collect the closure.
I must find another solution for my application.
Best regards
Bruno Daniel

What should happen when calling get-mutex on an unavailable mutex without
waitp? The docstring does not seem to be clear on this, and I seem to
be getting different behaviour on darwin with sb-thread and linux.
Jonathon McKitrick
--
My other computer is your Windows box.