trealla-prolog / trealla

A compact, efficient Prolog interpreter written in plain-old C.
MIT License
252 stars 11 forks source link

FYI: Android/Termux multi-threading issue #513

Closed flexoron closed 3 months ago

flexoron commented 3 months ago

FYI Android/Termux does not provide function pthread_cancel(t->tid). Instead, some ppl suggest to use the dangerous function (for example: pthread_kill(t->tid,9)). Anyway, this issue demands to think about how safe it is to kill a thread. Right now Trealla(Android) must be compiled like so: $ make NOTHREADS=1

infradig commented 3 months ago

Yes, dangerous. I'd vote to drop support for it in Prolog threads.

On Fri, Mar 15, 2024 at 10:20 AM flexoron @.***> wrote:

FYI Android/Termux does not provide function pthread_cancel(t->tid). Instead, some ppl suggest to use the dangerous function (for example: pthread_kill(t->tid,9)). Anyway, this issue demands to think about how safe it is to kill a thread. Right now Trealla(Android) must be compiled like so: $ make NOTHREADS=1

— Reply to this email directly, view it on GitHub https://github.com/trealla-prolog/trealla/issues/513, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFNKSEXCXBNWLFKIBZQUBD3YYI5DTAVCNFSM6AAAAABEXBXBYKVHI2DSMVQWIX3LMV43ASLTON2WKOZSGE4DONBYG4YDSMI . You are receiving this because you are subscribed to this thread.Message ID: @.***>

flexoron commented 3 months ago

Agreed! In Rhyme: Draining a phone's battery that fast is a thankless task.

flexoron commented 3 months ago
Thread hack

Supporting (porting) explorative programmers, those can do the following:

src/bif_threads.c:

Remove all 
#if !defined(__ANDROID__)

And insert in function do_cancel()

# if defined(__ANDROID__)
   pthread_kill(t->id,0);
# else
   pthread_cancel(t->id);
# endif

$ make
# Now all (Android/Termux) multi-threading tests are successful:
$ ./tpl samples/test_theads[1-6].pl

Comment: Advanced programmers know what to do and just cancel or kill a thread because another thread finished some work faster is a concept of a very stupid design strategy. A thread get an assignment and has to finish this job no matter how long it takes. There should be no need to cancel or kill a thread. Right?

infradig commented 3 months ago

Some Prologs that do multithreading don't provide thread_cancel (I believe). Some don't even trust Prolog application programmers to control mutexes (kind of agree as well with this).

flexoron commented 3 months ago
Google programmers don't have the courage to pthread_cancel a thread.
Solaris programmers explain why this is a challenging task.
Application programmers should try to avoid both (killing a process is a programming error (mostly)).

# Android/termux
$ make # OK
$ make test # OK
$ ./tpl samples/test_threads[1-6].pl  # OK
pmoura commented 3 months ago

Comment: Advanced programmers know what to do and just cancel or kill a thread because another thread finished some work faster is a concept of a very stupid design strategy. A thread get an assignment and has to finish this job no matter how long it takes. There should be no need to cancel or kill a thread. Right?

Wrong. Learn about competitive or-parallelism and its applications where the ability to cancel a thread is key.

infradig commented 3 months ago

Seems generally like a poor design paradigm, though maybe useful with genetic algos. All memory allocations (and that's just to start) must be protected by pthread_setcancelstate or leaks can occur. I would see it as last resort for unresponsive loops. Ultimately though, if you don't like it don't use it.

The alternative is to send a signal to do an exit for the thread, that would seem much cleaner.

pmoura commented 3 months ago

The alternative is to send a signal to do an exit for the thread, that would seem much cleaner.

That's exactly how I implement it. Threads are always cancelled by sending them a signal, using the thread_signal/2 predicate, not by calling a thread_cancel/1 predicate (which I don't use anywhere).

flexoron commented 3 months ago

@pmoura So, so and what signal you sending 1,2,3 or 5,6,7 or NUMBER 9? I was talking about just killing a thread. You stop a thread with purpose and this is something completely different. I'm sure we get it now.

flexoron commented 3 months ago

I am a zombie star programmer and sending signal 1 to tell its time to die and sending signal 2 to tell you ready to die and sending signal 3 to tell you die now and sending signal 9 but its too late. Thread committed suicide and I stay crime-free. Thank God.

pmoura commented 3 months ago

@pmoura So, so and what signal you sending 1,2,3 or 5,6,7 or NUMBER 9?

None. The thread_signal/2 predicate sends a goal to the thread engine to be called at the earliest opportunity. The goal is typically a call to throw/1 to abort (and thus cancelling) the thread.