anchor.diff:  %%%
   %%% Utilizes a two level queue and will monitor a process for communication.
   %%% In the event of communication, it will mark it as a comunication-bound
  -%%% process.
  +%%% process. This attempts to be a fairly accurate clone of CML's scheduler,
  +%%% evem dowm to the function names.
   %%%
   -module(erlam_sched_single_cml).
   -behaviour(erlam_scheduler).
  
positive.code: spawn_process( Process, State ) -> enqueueAndSwitchCurThread( Process, State ).
  
  %%% ==========================================================================
  %%% Private functionality
  %%% ==========================================================================
  
  
negative.code:             {ok, running, State#state{ curThread=nil, curReduct=0 }}
      end.
  
  %% @private
  %% @doc We have caused a yield by touching a channel. We have no atomic sections
  %%   in ErLam so just mark the thread and enqueue it, then dequeue and set it
  %%   as the current thread.
  %% @end
  yield( Proc, State ) ->
      {ok, NewState} = markAndEnqueue( Proc, State ),
      {ok, Top, Next} = dequeue1( NewState ), %should always be valid due to enq.
      {ok, Next2} = setCurThread( Top, Next ),
      {ok, running, Next2}.
  
  %% @private
  %% @doc Push current thread to end of queue and promote a process if need be.
  preempt( #state{ curThread=T } = State ) ->
      case isMarked( T ) of
          true  -> NT = unmarkTid( T ),
                   {ok, NState} = promote( State ),
                   enqueue1( NT, NState );
          false -> enqueue2( T, State )
      end.
  
  %%% ==========================================================================
  %%% Queue Functionality
  %%% ==========================================================================
  
  %% @private
  %% @doc Enqueue the process on the primary queue.
  -spec enqueue1( #process{}, #state{} ) -> {ok, #state{}}.
  enqueue1( Process, #state{ rdyQ1=Q1 } = State ) -> 
      { ok, State#state{rdyQ1=queue:in(Process,Q1)}}.
  
  %% @private
  %% @doc Enqueue the process on the secondary queue.
  -spec enqueue2( #process{}, #state{} ) -> {ok, #state{}}.
  enqueue2( Process, #state{ rdyQ2=Q2 } = State ) ->
      { ok, State#state{rdyQ2=queue:in(Process,Q2)}}.
  
  %% @private
  %% @doc Enqueue the process and mark it as communication bound.
  -spec markAndEnqueue( #process{}, #state{} ) -> {ok, #state{}}.
  markAndEnqueue( Process, State ) -> enqueue1( markTid(Process), State ).
  
  %% @private
  %% @doc Enqueue the old current thread if there is one, and then set this
  %%   new one to be the current thread.
  %% @end
  -spec enqueueAndSwitchCurThread( #process{}, #state{} ) -> {ok, #state{}}.
  enqueueAndSwitchCurThread( Process, #state{curThread=T}=State ) ->
      case T of
          nil ->
              setCurThread( Process, State );
          _   -> 
              {ok, NewState} = enqueue1( T, State ), % New process takes over
              setCurThread( Process, NewState )
      end.
  
  