c
c	msgdone.f 1.4 92/07/01 15:24:08
c
c  Copyright 1991 Intel Corporation, Inc.
c  See the "Copyright" file for complete rights and liability information.

      integer      iam, gotmsg1, gotmsg2, id1, id2, mtime
      character*80 buf1, buf2

c  Identify self.

      iam = mynode()

c  If node 0, then ...

      if(iam  .eq.  0) then
         print *,'Starting ...'

c    Waste some time.

         mtime = mclock()
1        continue
         if(mclock() .lt. mtime+50) goto 1

c    Create and send message type 1.

         buf1   = 'This is message type 1' 
         id1 = isend(1, buf1, len(buf1), -1, mypid())

c    Wait until message is sent and then report same.

         call msgwait(id1)
         write(*, 100) iam
100      format('Node ', i3, ' sent message type 1.')

c    Waste some more time.

         mtime = mclock()
2        continue
         if(mclock() .lt. mtime+100) goto 2

c    Create and send message type 2.

         buf2 = 'This is message type 2' 
         id2 = isend(2, buf2, len(buf2), -1, mypid())

c    Wait until message is sent and then report same.

         call msgwait(id2)
         write(*, 200) iam
200      format('Node ', i3, ' sent message type 2.')

c  If not node 0, then ...

      else

c    Post receives for the messages.

         id1 = irecv(1, buf1, len(buf1))
         id2 = irecv(2, buf2, len(buf2))

c    Set up loop exit condition.

         gotmsg1 = 0
         gotmsg2 = 0
10       if(gotmsg1 .eq. 1 .and. gotmsg2 .eq. 1) goto 20

c    Receive message type 1 if available.

         if(msgdone(id1) .eq. 1) then
            write(*, 300) iam
300         format('Node ', i3, ' received message type 1.')
            id1 = irecv(1, buf1, len(buf1))
            gotmsg1 = 1

c    Receive message type 2 if available.

         else if(msgdone(id2) .eq. 1) then
            write(*, 400) iam
400         format('Node ', i3, ' received message type 2.')
            id2 = irecv(2, buf2, len(buf2))
            gotmsg2 = 1

c    Flick if neither message is available.

         else
            write(*, 500) iam
500         format('Node ', i3, ' calling flick() ...')
            call flick()
         endif

         goto 10

      endif

20    end
