# context switch using message passing
# The context switch is similar to switch using semaphores.  The only
# difference is that message passing is used instead of semaphores.
# The reported number of times is twice the loop limit
# because two context switches occur on each pass.

resource context
  op up(); op down()               # export prevents semaphore implementation
body context()

  int  limit, factor, base, trials 

  getarg(1,factor)
  getarg(2,base)
  getarg(3,trials)
  limit = factor * base / 2

  process p1  {
    int  start, finish 

    for [ i = 1 to trials ] {
	start = age()
	for [ i = 1 to limit ] { send up(); receive down() }
	finish = age()
	printf("%d times   %d ms\n", 2*limit, finish-start)
    }
  }

  process p2  {
    for [ i = 1 to trials ] {
	for [ i = 1 to limit ] { receive up(); send down() }
    }
  }

end
