Hang in embedded libchicken

Diogo via <[email protected]>
Newsgroups gmane.lisp.scheme.chicken
Message-ID <[email protected]>
Dear mailing list,

the following issue has occurred when embedding CHICKEN 5.4.0 in a
multithreaded C program. I have reproduced the issue on macOS and
NetBSD 10. Either I am doing something wrong or this is a bug.

The program I am embedding just returns the squared input.

    ;; x.scm
    (import (chicken platform) (chicken foreign))
    
    (define-external (bar (int i)) int
        (* i i))
    
    (return-to-host)

The C program starts one extra thread and then both threads 
call the exported function bar. To ensure no concurrency issues
occur, I have protected the calls to bar with a pthread mutex.
    
    // y.c
    #include <chicken.h>
    #include <pthread.h>
    #include <stdio.h>
    #include <unistd.h>
    
    extern int bar(int);
    
    static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
    
    static int call(void) {
        pthread_mutex_lock(&m);
        int r = bar(1);
        pthread_mutex_unlock(&m);
        return r;
    }
    
    static void *run(void *_) {
        printf("value: %d\n", call());
        sleep(1);
        printf("value: %d\n", call());
        return 0;
    }
    
    int main(void) {
        CHICKEN_run(C_toplevel);
        pthread_t t;
        pthread_create(&t, 0, run, 0);
        run(0);
        pthread_join(t, 0);
        return 0;
    }
    
To build and run the test case, I used:

    csc -e -o test x.scm y.c
    ./test

From CHICKEN's documentation, I had the impression that this 
interface usage should be supported. Am I doing something silly?

Can anybody reproduce this issue on other systems?

Last comment: I have started this with gdb and one thread does
not leave C_save_and_reclaim, IIRC. I keeps calling some
mark_* and C_reclaim functions.  My libchicken does not have
symbols, so I couldn't look in more details what is happening,
but it looks like the thread never leaves the function. That
only happens if I call the interface with two or more threads.

-Diogo
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.