thread_t *
thread_attach ()
{
  thread_t *thr = thread_allocate ();

  thr->thr_stack_size = (unsigned long) -1;
  thr->thr_status = RUNNABLE;
  thr->thr_priority = NORMAL_PRIORITY;
  thr->thr_attached = 1;

  return thr;
}
thread_t *
thread_create (
    thread_init_func initial_function,
    unsigned long stack_size,
    void *init_arg)
{
  thread_t *thr;

  assert (_main_thread != NULL);

  if (stack_size == 0)
    stack_size = THREAD_STACK_SIZE;

#if (SIZEOF_VOID_P == 8)
  stack_size *= 2;
#endif
#if defined (__x86_64 ) && defined (SOLARIS)
  /*GK: the LDAP on that platform requires that */
  stack_size *= 2;
#endif

  /* Any free threads with the right stack size? */
  for (thr = (thread_t *) _deadq.thq_head.thr_next;
       thr != (thread_t *) &_deadq.thq_head;
       thr = (thread_t *) thr->thr_hdr.thr_next)
    {
      if (thr->thr_stack_size >= stack_size)
	break;
    }

  if (thr == (thread_t *) &_deadq.thq_head)
    {
      /* No free fiber, create a new one */
      thr = thread_allocate ();
      _fiber_for_thread (thr, stack_size);
      _thread_num_total++;
    }
  else
    {
      /* Set new context for the thread */
      memcpy (thr->thr_context, thr->thr_init_context, sizeof (jmp_buf));
    }

  thr->thr_initial_function = initial_function;
  thr->thr_initial_argument = init_arg;
  thread_set_priority (thr, NORMAL_PRIORITY);
  _thread_init_attributes (thr);
  _fiber_status (thr, RUNNABLE);

  return thr;
}
示例#3
0
文件: piclang.c 项目: kd0kfo/picos
char PICLANG_load(file_handle_t fh)
{
  thread_id_t new_thread;
  file_t file;
  mount_t mount;
  picos_size_t block_counter;
  extern char ARG_buffer[ARG_SIZE];
  // Check to see if there is an available thread space
  new_thread = thread_allocate();
  picos_processes[new_thread].addr = SRAM_PICFS_FILE_ADDR + PCB_MAGIC_NUMBER_OFFSET*sizeof(picos_size_t);

  // setup arguments
  
  if(ARG_next > 1)
    {
      if(ARG_buffer[ARG_next-1] == 0)
	ARG_buffer[ARG_next-1] = ' ';
    }
  picos_processes[new_thread].nargs = ARG_count();
  ARG_next = 0;
  SRAM_write(SRAM_PICFS_ARG_SWAP_ADDR + ARG_SIZE*new_thread,ARG_buffer,ARG_SIZE);

  // setup file and cat the first block to SRAM
  picos_processes[new_thread].program_file = fh;
  ftab_read(fh*sizeof(file_t)+SRAM_PICFS_FTAB_ADDR,&file,sizeof(file_t));
  SRAM_read(file.mount_point*sizeof(mount_t)+SRAM_MTAB_ADDR,&mount,sizeof(mount_t));

  // Load PCB
  picos_processes[new_thread].block_size = mount.block_size;
  block_counter = 0;
  while(block_counter < PCB_SIZE)
    {    
      picfs_load(fh);
      SRAM_write(SRAM_PICFS_FILE_ADDR + block_counter,(void*)picfs_buffer,mount.block_size);
      block_counter += mount.block_size;
    }

  // Reset current page index 
  picos_processes[new_thread].current_page = -1;
  
  return PICLANG_resume(new_thread);
}