Is Common Lisp bordeaux thread blocking equivalent Java synchronization?

I come from a Java background trying to wrap around regular Lisp code that uses with-recursive-lock-held

. I have now studied operating systems in computer science at university, so I am familiar with the concept of thread locking at a theoretical level. My question is more practical.

Suppose we have the following generic Lisp code :

(progn 
  (defun make-recursive-lock (&optional name)
    (sb-thread:make-mutex :name (or name "printv")))
  (defmacro with-recursive-lock-held ((place) &body body)
    `(sb-thread:with-recursive-lock (,place)
      ,@body)))

      

It looks to me like it opens and holds a lock at the operating system thread level.

If I try to express this idea in Java (wrap my head around it) - I get something like:

public class SyncronizedExample {
    private long protectedLong = 0;
    private Object sync1 = new Object();

    public void inc1() {
        synchronized(sync1) {
            protectedLong++;
        }
    }
}

      

(There is an assumption that I am using "old school" Java sync and not the newer Locks from java.util.concurrent.*

- go with me on this - I'm trying to keep things as simple as possible).

(I also assume that the Common Lisp example is a macro, and the Java example is only a data structure with synchronization around it and that they are not directly comparable. This is partly because macros are not possible in Java, but also because which I assume you are a smart reader and can look at ideas, not syntax.)

My question is: Is Common Lisp bordeaux thread blocking equivalent Java synchronization?

+3


source to share


1 answer


In the following aspects, yes:

  • They work at the flow level.
  • They provide reentrant locking (however, you can choose not to use recursive locking in Lisp).

In the following aspects, maybe:

  • The implementation will vary in detail. SBCL uses CAS to capture the mutex, I don't know how other Common Lisp implementations do it. As far as I know, Java doesn't know.


In the following aspects, no:

  • You used one lock holder per SyncronizedExample

    (sic) on the Java side. Using a class variable instead will make the example more similar.

Besides

  • Use &optional (name "printv")

    to specify a default value for an optional parameter.
  • I see no need for Lisp forms to wrap progn

    .
  • Use a library bordeaux-threads

    (available from Quicklisp) as portability for low-level stream code.
  • Instead of swapping Lisp calls in make-mutex

    and out with-recursive-lock

    in redundant wrappers, you can show how you call them.
  • You can show how you might call your Java code.
0


source







All Articles