cosmos72 / stmx

High performance Transactional Memory for Common Lisp
http://stmx.org/
241 stars 14 forks source link

Creating threads with (sb-thread:make-thread) - part 2 [was: Basic TFifo test fails.] #8

Closed cosmos72 closed 10 years ago

cosmos72 commented 10 years ago

Schmidh reported two different errors in issue #7. The first has been fixed (and the issue closed), the second is analyzed here:


Hi Massimiliano,

following code throws errors in SBCL:

(in-package #:tm-test)

(defvar test-fifo (make-instance 'tfifo))

(defun loop1 () (take test-fifo) (loop1))

(defun loop2 (n) (put test-fifo n) (loop2 (1+ n)))

(defun take-thread () (sb-thread:make-thread #'loop1 :name "BFS"))

(defun put-thread () (sb-thread:make-thread #'loop2 :name "BFS" :arguments '(1)))

There are two types of error messages popping up. The first is the most common one. The second one only showed up once:

I.) [omitted, see issue #7] II.)

BUG! Transaction 4892 called (retry), but no TVARs to wait for changes. This is a bug either in the STMX library or in the application code. Possible reason: some code analogous to (atomic (retry)) was executed. Such code is not allowed, because at least one TVAR or one TOBJ slot must be read before retrying an ATOMIC block. [Condition of type SIMPLE-ERROR]

Restarts: 0: [ABORT] Abort thread (#<THREAD "BFS" RUNNING {10060F0BC3}>)

Backtrace: 0: (STMX::LISTEN-TVARS-OF #<STMX::TLOG v-2 {1002CB4E33}>) 1: (STMX::WAIT-TLOG #<STMX::TLOG v5306 {1002CB4E33}>) 2: (STMX::%RUN-SW-ATOMIC #<CLOSURE (LAMBDA NIL :IN TAKE) {1002DF1C0B}>) 3: (LOOP1) 4: ((FLET #:WITHOUT-INTERRUPTS-BODY-1226 :IN SB-THREAD::INITIAL-THREAD-FUNCTION-TRAMPOLINE)) 5: ((FLET SB-THREAD::WITH-MUTEX-THUNK :IN SB-THREAD::INITIAL-THREAD-FUNCTION-TRAMPOLINE)) 6: ((FLET #:WITHOUT-INTERRUPTS-BODY-660 :IN SB-THREAD::CALL-WITH-MUTEX)) 7: (SB-THREAD::CALL-WITH-MUTEX #<CLOSURE (FLET SB-THREAD::WITH-MUTEX-THUNK :IN SB-THREAD::INITIAL-THREAD-FUNCTION-TRAMPOLINE) {7FFFF61F6C9B}> #<SB-THREAD:MUTEX "thread result lock" owner: #<SB-THREAD:THR.. 8: (SB-THREAD::INITIAL-THREAD-FUNCTION-TRAMPOLINE #<SB-THREAD:THREAD "BFS" RUNNING {10060F0BC3}> #S(SB-THREAD:SEMAPHORE :NAME "Thread setup semaphore" :%COUNT 0 :WAITCOUNT 0 :MUTEX #<SB-THREAD:MUTEX (fre.. 9: ("foreign function: call_into_lisp") 10: ("foreign function: new_thread_trampoline")

Any idea what I'm doing wrong?

Kind regards, Hans-J.

cosmos72 commented 10 years ago

As I said in issue #7, I could not yet reproduce this error. I will leave SBCL running on your example and see if it happens to me as well.

cosmos72 commented 10 years ago

Found :) Short answer: you must call (bt:make-thread), NOT (sb-thread:make-thread)

Long answer: new threads created with (sb-thread:make-thread) do not get per-thread initial local bindings configured in bordeaux-threads:default-special-bindings. STMX relies on such per-thread initial local bindings, so threads must be created with (bt:make-thread).

I am adding some sanity checks to detect this situation and report it.

ghost commented 10 years ago

Cool!

Will try with green threads (https://github.com/deliciousrobots/green-threads), too. Interface is very close to Bordeaux threads.

cosmos72 commented 10 years ago

Hello Hans,

STMX internally uses BORDEAUX-THREADS for mutexes, conditions, (bt:current-thread) and per-thread initial local bindings bt:default-special-bindings. To use some other thread implementation, you will need to modify STMX at least for the last two features.

Good luck :)

cosmos72 commented 10 years ago

"Dynamic bindings don't over well into green threads" - this could be a show-stopper...