Example #1
0
// all threads start here, NetThreads 1.0 has 8 threads (4 per CPU)
// instructions from the 4 thread on a CPU are interleaved in a
// round-robin fashion. use compiler/bin/mips-mips-elf-objdump -h to
// see the memory layout of your compiled application and support.h to
// see where some controls are memory-mapped.
int main(void)  
{   
  int mytid = nf_tid();
  
  if(mytid != 0)
    {
      nf_stall_a_bit();
      nf_lock(LOCK_INIT); // should not get it
    }
  else
    {
#ifndef DEBUG
      nf_lock(LOCK_INIT); // should get it on the first attempt
      nf_pktout_init();
      nf_pktin_init();
#endif
      sp_init_mem_single();  // initialize the multithreaded memory allocator

      // perform memory allocation for initialization purposes
      // only use sp_free() and sp_malloc()
      // two implementations of these functions exists. If you prefer the STANDARD_MALLOC
      // from ../common/memory.c, you should not perform sp_init_mem_single() nor sp_init_mem_pool().

      // finalize the initialization of the multithreaded memory allocator
      // since each thread allocates from its private head and adds to its heap
      // upon memory free, the heaps can get unbalanced if care is not taken
      sp_init_mem_pool();  
    }
  nf_unlock(LOCK_INIT); 


  while(1)
    { 
      // get the time if you need it
      uint t = nf_time();  // 32-bit unsigned wrap-around time


      nf_lock(LOCK_DS0);   // valid lock identifiers are integers from 0 to 15 inclusively
      // do some synchronized action here
      nf_unlock(LOCK_DS0);

      t_addr* next_packet = nf_pktin_pop();  // get the next available packet, this is non-blocking call
      if (nf_pktin_is_valid(next_packet)) { // test if we have a packet

	// process the packet

        nf_pktin_free(next_packet);  // free this packet from the input memory
      }
    }

  // rever reached
  return 0;
}
int main(void)
{
  t_addr* next_packet;
  int i = 0;
  
  int mytid = nf_tid();

  if(mytid != 0)
    {
      nf_stall_a_bit();
      nf_lock(LOCK_INIT); // should not get it
    }
  else
    {
      nf_lock(LOCK_INIT); // should get it immediately
      nf_pktout_init();
      nf_pktin_init();      
    }

  nf_unlock(LOCK_INIT); 


  while(1) 
    {
      next_packet = nf_pktin_pop();
      if (nf_pktin_is_valid(next_packet)) {
        i++;
        iddle_time(NUMBER_OF_LOOPS);
        log("thread %d sending packet %d from %p\n", nf_tid(), i, next_packet);
        process_pkt(next_packet);
      }
    }

  while(1); 

  return 0;
}
 int main() 
 {
   int mytid = nf_tid();
   
   if(mytid != 0)
     {
       nf_stall_a_bit();
       nf_lock(LOCK_INIT); // should not get it
     }
   else
     {
       nf_lock(LOCK_INIT); // should get it on the first attempt
       nf_pktout_init();
       nf_pktin_init();
     }
   nf_unlock(LOCK_INIT); 

   //only run this program on thread 0
   //if (nf_tid() != 0) 
   //{
   //   while (1) {}
   //}

   //// initialize
   //nf_pktout_init();
   //nf_pktin_init();

   // allocate an output buffer
   t_addr *pkt = nf_pktout_alloc(PKT_SIZE);

   // setup the ioq_header
   fill_ioq((struct ioq_header*)pkt, 2, sizeof(struct netfpga_to_driver));

   // get a pointer to the payload struct
   struct netfpga_to_driver* n2d = 
     (struct netfpga_to_driver*) (pkt + sizeof(struct ioq_header));

   // initialize the message
   memset(n2d->str, 0, STR_SIZE); 
   memcpy(n2d->str, mystr, strlen(mystr));
   //n2d->str[strlen(mystr)+1] = nf_tid() + 0x30; //thread id in ascii
   n2d->str[strlen(mystr)+1] = mytid + 0x30; //thread id in ascii

   // send it
   nf_pktout_send(pkt, pkt + PKT_SIZE); 
   //int i = 0;
   //int mytid;
   //struct netfpga_to_driver* n2d;
   //t_addr *pkt;
   //while(i <= 1)
   //{
   //   mytid = nf_tid();
   //   
   //   if(mytid != 0)
   //     {
   //       nf_stall_a_bit();
   //       nf_lock(LOCK_INIT); // should not get it
   //     }
   //   else
   //     {
   //       nf_lock(LOCK_INIT); // should get it on the first attempt
   //       nf_pktout_init();
   //       nf_pktin_init();
   //     }
   //   nf_unlock(LOCK_INIT); 

   //   // allocate an output buffer
   //   pkt = nf_pktout_alloc(PKT_SIZE);

   //   // setup the ioq_header
   //   fill_ioq((struct ioq_header*)pkt, 2, sizeof(struct netfpga_to_driver));

   //   // get a pointer to the payload struct
   //   n2d = 
   //     (struct netfpga_to_driver*) (pkt + sizeof(struct ioq_header));

   //   // initialize the message
   //   memset(n2d->str, 0, STR_SIZE); 
   //   memcpy(n2d->str, mystr, strlen(mystr));
   //   //n2d->str[strlen(mystr)+1] = nf_tid() + 0x30; //thread id in ascii
   //   n2d->str[strlen(mystr)+1] = mytid + 0x30; //thread id in ascii

   //   // send it
   //   nf_pktout_send(pkt, pkt + PKT_SIZE); 
   //   i = i + 1;
   //}
//#else
//   int mytid = nf_tid();
//   
//   if(mytid != 0)
//     {
//       nf_stall_a_bit();
//       nf_lock(LOCK_INIT); // should not get it
//     }
//   else
//     {
//       nf_lock(LOCK_INIT); // should get it on the first attempt
//       nf_pktout_init();
//       nf_pktin_init();
//     }
//   nf_unlock(LOCK_INIT); 
//   t_addr *pkt = nf_pktout_alloc(PKT_SIZE);
//
//   // setup the ioq_header
//   fill_ioq((struct ioq_header*)pkt, 2, sizeof(struct netfpga_to_driver));
//
//   // get a pointer to the payload struct
//   struct netfpga_to_driver* n2d = 
//     (struct netfpga_to_driver*) (pkt + sizeof(struct ioq_header));
//
//   // initialize the message
//   memset(n2d->str, 0, STR_SIZE); 
//   memcpy(n2d->str, mystr, strlen(mystr));
//   //n2d->str[strlen(mystr)+1] = nf_tid() + 0x30; //thread id in ascii
//   n2d->str[strlen(mystr)+1] = mytid + 0x30; //thread id in ascii
//   nf_pktout_send(pkt, pkt + PKT_SIZE); 
//#endif

   return 0;
 }