///////////////////////////////////////////////////////////////////////////////
//                                                                           //
// Copyright (C) 2006 by Intel Coproration and Carnegie Mellon University    //
//                                                                           //
///////////////////////////////////////////////////////////////////////////////

#include <iostream>
#include <errno.h>
#include <string.h>

#include "MagicMoveTest.hxx"

#include "CatomWorld.hxx"
#include "CatomSim.hxx"

//#include <pthread.h>

CODE_MODULE_DECLARATION( MagicMoveTest, MagicMoveTest );

using namespace std;

// no longer needed; magic move auto locks by default
//static pthread_mutex_t local_lock = PTHREAD_MUTEX_INITIALIZER;

void MagicMoveTest::simulationStart() { signpost dprdebug_block_signpost(__FILE__,__LINE__,__FUNCTION__); 
  if ( hostCatom&0x01 && 
      (worldPtr->catomHash.find(hostCatom-1) != worldPtr->catomHash.end() ) ) {
    f = 1;
//    pthread_mutex_lock(&local_lock);
    worldPtr->catomHash[hostCatom]->C.moveTo( hostCatom-1, f );
//    pthread_mutex_unlock(&local_lock);
  } else f = 0;
  worldPtr->catomHash[hostCatom]->magnet_state[1] = 1;
}
void MagicMoveTest::newTick() { signpost dprdebug_block_signpost(__FILE__,__LINE__,__FUNCTION__); 
  if (f) {
    f++;
    if (f>NUM_FEATURES) f=1;
//    pthread_mutex_lock(&local_lock);
    worldPtr->catomHash[hostCatom]->C.moveTo( hostCatom-1, f, hostCatom&0x02 );
//    pthread_mutex_unlock(&local_lock);
  }
}

