[sane-devel] Some thoughts on pthread support...

Gerhard Jaeger gerhard at gjaeger.de
Wed Oct 15 07:50:26 BST 2003


Hi list,

as you might remember ;-)))) I've updated the sanei_thread lib to support
pthreads and Henning worked on the test-backend to make it work correctly
with the pthreads. Here are some ideas/thoughts on that work, that might touch
other developers too:

- the difference between pthreads and a forked process is the process context
  where those two incarnations life. The function running as thread lives in
  the same "world" where the man process lives and therefore shares its
  signal "behaviour. The function running in the forked child processs not!
--> Thats why we get some trouble, when terminating the thread (mostly a
    reader thread) and killing its pipe to the main-process. Killing means
    pthread_cancel (which is not very nice, but necessary if the reader-thread
    will not respond, or sleeps as in the test-backend). So killing also the
    pipe will result in a SIGPIPE, which defaults to terminating the whole
    process if not handled or blocked...
    Suggestion for sanei_thread: As this is almost everytime used to create
                                 a reader process, the sanei_thread_begin
                                 function should also block the SIGPIPE when
                                 running in the pthread version

--> In the pthreaded version, for sanei_kill, we use the pthread_cancel
    functionality, why? This is the function designed to "kill" a pthread.
    We cannot simply use pthread_kill(id, SIGTERM), as this will not only kill
    the thread, but also the whole process (remember, we live in one world).
    Anyway, pthread_cancel, should be used rarely. In general, the reader
    thread should be designed that way, that it ends simply by returning,
    even if canceled, so it has the chance to shutdown gracefully. This 
    indeed is not necessary when running as own process, as all the cleanup
    work can be done by the OS, but this should be done, when running in
    threaded envrionment. (Okay the pthreads in Linux are in fact own
    processes, but only there AFAIK)....
    Suggestion for reader_process: Design it that way, that the main process
                                   is able to cancel the process by sending a,
                                   let's say command, which can be detected by
                                   the reader-process, to cause it, to
                                   shutdown gracefully. i.e. send a SIGUSRx
                                   and handle it. So there's no need of
                                   pthread_cancel (only if it hangs)...


Well in the end, we also have to say, that using pthreads under Linux or 
maybe other OS that are able to handle fork correctly (I mean clone all, even
FS-handles), is not really necessary. But when we'd like to make SANE work
even on MacOSX or OS/2, then we should be aware of these issues.


Just my two cents (maybe a little more ;-)
Ciao
  Gerhard




                                   
   




More information about the sane-devel mailing list