示例#1
0
文件: smp.c 项目: saumzy/clib
void clib_smp_lock_init (clib_smp_lock_t ** pl)
{
  clib_smp_lock_t * l;
  uword i, n_bytes, n_fifo_elts;

  /* No locking necessary if n_cpus <= 1.
     Null means no locking is necessary. */
  if (clib_smp_main.n_cpus < 2)
    {
      *pl = 0;
      return;
    }

  /* Need n_cpus - 1 elts in waiting fifo.  One CPU holds lock
     and others could potentially be waiting. */
  n_fifo_elts = clib_smp_main.n_cpus - 1;

  n_bytes = sizeof (l[0]) + n_fifo_elts * sizeof (l->waiting_fifo[0]);
  ASSERT_AND_PANIC (n_bytes % CLIB_CACHE_LINE_BYTES == 0);

  l = clib_mem_alloc_aligned (n_bytes, CLIB_CACHE_LINE_BYTES);

  memset (l, 0, n_bytes);
  l->n_waiting_fifo_elts = n_fifo_elts;

  for (i = 0; i < l->n_waiting_fifo_elts; i++)
    l->waiting_fifo[i].wait_type = CLIB_SMP_LOCK_WAIT_EMPTY;

  *pl = l;
}
示例#2
0
u8 *
vlib_thread_stack_init (uword thread_index)
{
  ASSERT (thread_index < vec_len (vlib_thread_stacks));
  vlib_thread_stacks[thread_index] = clib_mem_alloc_aligned
    (VLIB_THREAD_STACK_SIZE, clib_mem_get_page_size ());

  /*
   * Disallow writes to the bottom page of the stack, to
   * catch stack overflows.
   */
  if (mprotect (vlib_thread_stacks[thread_index],
		clib_mem_get_page_size (), PROT_READ) < 0)
    clib_unix_warning ("thread stack");
  return vlib_thread_stacks[thread_index];
}
示例#3
0
/**
 * Initialize segment in a private heap
 */
int
ssvm_master_init_private (ssvm_private_t * ssvm)
{
  ssvm_shared_header_t *sh;
  u32 pagesize = clib_mem_get_page_size ();
  u32 rnd_size = 0;
  u8 *heap;

  rnd_size = clib_max (ssvm->ssvm_size + (pagesize - 1), ssvm->ssvm_size);
  rnd_size &= ~(pagesize - 1);

#if USE_DLMALLOC == 0
  {
    mheap_t *heap_header;

    heap = mheap_alloc (0, rnd_size);
    if (heap == 0)
      {
	clib_unix_warning ("mheap alloc");
	return -1;
      }
    heap_header = mheap_header (heap);
    heap_header->flags |= MHEAP_FLAG_THREAD_SAFE;
  }
#else
  heap = create_mspace (rnd_size, 1 /* locked */ );
#endif

  ssvm->ssvm_size = rnd_size;
  ssvm->i_am_master = 1;
  ssvm->my_pid = getpid ();
  ssvm->requested_va = ~0;

  /* Allocate a [sic] shared memory header, in process memory... */
  sh = clib_mem_alloc_aligned (sizeof (*sh), CLIB_CACHE_LINE_BYTES);
  ssvm->sh = sh;

  clib_memset (sh, 0, sizeof (*sh));
  sh->heap = heap;
  sh->ssvm_va = pointer_to_uword (heap);
  sh->type = SSVM_SEGMENT_PRIVATE;

  return 0;
}