[M3devel] Modula-3 threads vs. pthreads?

Jay K jay.krell at cornell.edu
Mon Sep 7 18:03:21 CEST 2009


To answer part of my question the same as you did, this (buggy -- race conditions) program demonstrates that Alert is catchable, whereas apparently pthread_cancel is not.


MODULE Main;
IMPORT IO, Thread;

PROCEDURE ThreadMain (<* UNUSED *> closure: Thread.Closure): REFANY =
VAR i := 0;
BEGIN
  TRY
    LOOP
      TRY
        Thread.AlertPause(0.01D0);
        IO.PutInt(i);
        IO.Put("\n");
      EXCEPT Thread.Alerted =>
        IO.Put("Thread.Alerted 1\n");
        INC(i, 1);
        IF i = 10 THEN
          (*RAISE Thread.Alerted;*)
          RETURN NIL;
        END;
      ELSE
        IO.Put("Thread.Alerted 3?\n");
      END
    END;
  FINALLY
    IO.Put("Thread.Alerted 2\n");
  END;
  RETURN NIL;
END ThreadMain;

VAR a := Thread.Fork(NEW(Thread.Closure, apply := ThreadMain));
BEGIN
FOR i := 1 TO 10 DO
  Thread.Pause(1.0D0);
  Thread.Alert(a);
END;
EVAL Thread.Join(a);
END Main.

Thanks,
 - Jay


CC: m3devel at elegosoft.com
From: hosking at cs.purdue.edu
To: jay.krell at cornell.edu
Subject: Re: 
Date: Mon, 7 Sep 2009 11:17:50 -0400

Jay,
Before you go off half cocked and brew up some new threads system might I suggest that it would be better to find and fix the current problem.  I am sceptical that there is a much thinner M3 thread implementation on top of pthreads than we currently have.  I am in the process of tracking down the problem on I386_DARWIN (I see at least one thing broken right now, and will have a fix soon).
In answer to your question, the wait list is to deal with M3 alerts, which are not the same as pthread cancellation.
 Antony Hosking | Associate Professor | Computer Science | Purdue University305 N. University Street | West Lafayette | IN 47907 | USAOffice +1 765 494 6001 | Mobile +1 765 427 5484 
On 7 Sep 2009, at 05:33, Jay K wrote:Tony, can you..um, trick question sort of, briefly explain the differences between pthreads and Modula-3 threads?
The "trick" part is that, to an audience (me) who is not all that familiar with the nuances of either?
I'll see if I can find my green book and other reference material (online).


I understand the basics of threading very well, if that helps.
Eh, not much to it really.


I'm not super familiar with condition variables, as Win32 didn't historically have them.
But I think I understand them.


Specific questions:
  Can Modula-3 threads be implemented more directly upon pthreads?


  Why do we need to maintain a waiting list?


  Is "alert" the same as "cancel" in pthread vocabulary? Or almost the same?


  I understand..there might be a difficult problem..does Modula-3 allow catching an alert? I think so.
  Does Posix allow canceling a cancel? I think not.


  Posix allows, what I think of as try/finally, for alert. Modula-3 probably does too. Even though you can't stop the cancel, you can run "cleanup" code that runs before the cancel completes.


However mapping Modula-3 to Posix here would be tricky. As I see things..any function with a try/finally would have to be contorted into a form that passes a pointer to itself, along with parameters, to C code that does pthread_cleanup_push/pop around calling the function pointer. At least given the Darwin implementation. Other implementations might simply be able to call push at the start and pop at the end. But on Darwin these functions are macros where push introduces a local variable that pop references. I guess maybe one could to the "reverse engineering" approach (just by reading the header). But it'd still be ugly.


I have to do more research here.


I'm seriously consider "writing" a ThreadPThreadDirect.m3 that is thinner than the current ThreadPThread.m3 and see how it fairs, specifically if the Juno hangs go away.
"writing" being an exaggeration because it should be very small.


Thanks,
 - Jay


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://m3lists.elegosoft.com/pipermail/m3devel/attachments/20090907/35b72404/attachment-0002.html>


More information about the M3devel mailing list