4

I want to implement the context switching between threads using Xilkernel, but there is no primitive POSIX-compliant that allows to stop and then resume the execution of a thread.

Is there anyone who can help me?

Paebbels
  • 15,573
  • 13
  • 70
  • 139
khaledrmse
  • 41
  • 1

1 Answers1

1

I do context switch for FPGA using this C code. If you find it useful and want ta get more of the surrounding code, just ask me.

/*
 * threadswitch - change thread
 * 
 * The thread stack-pointer is supplied as a parameter.
 * The old thread's stack-pointer value is saved to the array
 * thread_info_array, and a new thread is selected from the array.
 * The stack pointer of the new thread is returned.
 */
unsigned int * threadswitch( unsigned int * old_sp )
{
  unsigned int * new_sp;

  number_of_thread_switches += 1; /* Increase thread-switch counter. */

  /* Print line 1 of an informational message. */
  printf( "\nPerforming thread-switch number %d. The system has been running for %d ticks.\n",
          number_of_thread_switches,
          get_internal_globaltime() );

  /* Save the stack pointer of the old thread. */
  thread_info_array[ currently_running_thread ].thread_sp = old_sp;

  /* Print part 1 of a message saying which threads are involved this time. */
  printf( "Switching from thread-ID %d ",
          thread_info_array[ currently_running_thread ].thread_id );

  /* Perform the scheduling decision (round-robin). */
  currently_running_thread += 1;
  if( currently_running_thread >= current_thread_count )
  {
    currently_running_thread = 0;
  }

  /* Print part 2 of the informational message. */
  printf( "to thread-ID %d.\n",
          thread_info_array[ currently_running_thread ].thread_id );

  /* Get the stack pointer of the new thread. */
  new_sp = thread_info_array[ currently_running_thread ].thread_sp;

  /* Return. */
  return( new_sp );
}
Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424