示例#1
0
int main()
{
	int i;
	int tid[NUM_THREADS];
	pthread_t thread[NUM_THREADS];

	/** < Ensure that locks are cacheline aligned */
	assert(sizeof(lock_t) == 64);

	/** < Allocate the shared nodes */
	red_printf("Allocting %d nodes\n", NUM_NODES);
	nodes = (node_t *) malloc(NUM_NODES * sizeof(node_t));
	assert(nodes != NULL);
	
	for(i = 0; i < NUM_NODES; i ++) {
		nodes[i].a = rand();
		nodes[i].b = nodes[i].a + 1;
	}

	/** < Allocate the striped spinlocks */
	red_printf("Allocting %d locks\n", NUM_LOCKS);
	locks = (lock_t *) malloc(NUM_LOCKS * sizeof(lock_t));
	assert(locks != NULL);
	
	for(i = 0; i < NUM_LOCKS; i++) {
		pthread_spin_init(&locks[i].lock, 0);
	}
	
	/** < Launch several reader threads and a writer thread */
	for(i = 0; i < NUM_THREADS; i++) {
		tid[i] = i;
		red_printf("Launching reader thread with tid = %d\n", tid[i]);
		pthread_create(&thread[i], NULL, reader, &tid[i]);
	}

	for(i = 0; i < NUM_THREADS; i++) {
		pthread_join(thread[i], NULL);
	}

	exit(0);
}
示例#2
0
static struct server *
server_init(void)
{
    struct server *s = malloc(sizeof(struct server));
    if (s == NULL)
        dns_error(0, "out of memory in server_init");
    s->nfetcher = FETCHER_NUM;
    s->nquizzer = QUIZZER_NUM;
    s->authors = NULL;
    s->fetchers = NULL;
    s->pkg = 0;
    pthread_spin_init(&s->eventlist.lock, 0);
    //pthread_mutex_init(&s->lock,NULL);
    s->eventlist.head = NULL;
    if ((s->ludp = create_listen_ports(SERVER_PORT, UDP, (uchar *)SRV_ADDR)) < 0)
        dns_error(0, "can not open udp");
    set_sock_buff(s->ludp, 10);
    if ((s->ltcp = create_listen_ports(SERVER_PORT, TCP, (uchar *)SRV_ADDR)) < 0)
        dns_error(0, "can not open tcp");
    s->datasets =
        htable_create(NULL, dict_comp_str_equ, HASH_TABLE_SIZE,
                      MULTI_HASH);
    if (s->datasets == NULL)
        dns_error(0, "htable create");
    s->forward = htable_create(NULL, dict_comp_str_equ, 1024, 1);
    if (s->forward == NULL)
        dns_error(0, "create forward");
    s->qlist =
        htable_create(NULL, dict_comp_str_equ,
                      QLIST_TABLE_SIZE, 1);
    if (s->qlist == NULL)
        dns_error(0, "create qlist");
    s->ttlexp = create_rbtree(rbt_comp_ttl_gt, NULL);
    if (s->ttlexp == NULL)
        dns_error(0, "create ttl tree");
    s->recordsindb = 0;
    s->refreshflag = 0;
    s->lastrefresh = global_now;
    s->is_forward = 0;
    return s;
}
INLINE int
ptw32_spinlock_check_need_init (pthread_spinlock_t * lock)
{
    int result = 0;

    /*
     * The following guarded test is specifically for statically
     * initialised spinlocks (via PTHREAD_SPINLOCK_INITIALIZER).
     *
     * Note that by not providing this synchronisation we risk
     * introducing race conditions into applications which are
     * correctly written.
     */
    EnterCriticalSection (&ptw32_spinlock_test_init_lock);

    /*
     * We got here possibly under race
     * conditions. Check again inside the critical section
     * and only initialise if the spinlock is valid (not been destroyed).
     * If a static spinlock has been destroyed, the application can
     * re-initialise it only by calling pthread_spin_init()
     * explicitly.
     */
    if (*lock == PTHREAD_SPINLOCK_INITIALIZER)
    {
        result = pthread_spin_init (lock, PTHREAD_PROCESS_PRIVATE);
    }
    else if (*lock == NULL)
    {
        /*
         * The spinlock has been destroyed while we were waiting to
         * initialise it, so the operation that caused the
         * auto-initialisation should fail.
         */
        result = EINVAL;
    }

    LeaveCriticalSection (&ptw32_spinlock_test_init_lock);

    return (result);
}
示例#4
0
文件: 1-1.c 项目: 8l/rose
int main()
{
	int rc = 0;

	printf("main: initialize spin lock\n");
	if(pthread_spin_init(&spinlock, PTHREAD_PROCESS_PRIVATE) != 0)
	{
		printf("main: Error at pthread_spin_init()\n");
		return PTS_UNRESOLVED;
	}

	printf("main: attempt spin lock\n");

	/* We should get the lock */	
	if(pthread_spin_lock(&spinlock) != 0)
	{
		printf("Unresolved: main cannot get spin lock when no one owns the lock\n");
		return PTS_UNRESOLVED;
	} 
	
	printf("main: acquired spin lock\n");
	
	printf("main: unlock spin lock\n");	
	if(pthread_spin_unlock(&spinlock) != 0)
	{
		printf("main: Error at pthread_spin_unlock()\n");
		return PTS_UNRESOLVED;
	}

	printf("main: destroy spin lock\n");
	rc = pthread_spin_destroy(&spinlock);
	if(rc != 0)
	{
		printf("Test FAILED: Error at pthread_spin_destroy()"
			"Return code : %d\n", rc);
		return PTS_FAIL;
	}

	printf("Test PASSED\n");
	return PTS_PASS;
}
示例#5
0
文件: 4-1.c 项目: Nan619/ltp-ddt
int main()
{
	int rc = 0;
	pthread_t child_thread;

#ifdef PTHREAD_PROCESS_PRIVATE
	pshared = PTHREAD_PROCESS_PRIVATE;
#else
	pshared = -1;
#endif

	printf("main: initialize spin lock\n");

	rc = pthread_spin_init(&spinlock, pshared);
	if (rc != 0) {
		printf("Test FAILED:  Error at pthread_spin_init()\n");
		return PTS_FAIL;
	}

	printf("main: attempt spin lock\n");

	/* We should get the lock */
	if (pthread_spin_lock(&spinlock) != 0) {
		printf
		    ("Error: main cannot get spin lock when no one owns the lock\n");
		return PTS_UNRESOLVED;
	}

	printf("main: acquired spin lock\n");

	printf("main: create thread\n");
	if (pthread_create(&child_thread, NULL, fn_chld, NULL) != 0) {
		printf("main: Error creating child thread\n");
		return PTS_UNRESOLVED;
	}

	/* Wait for thread to end execution */
	pthread_join(child_thread, NULL);

	return PTS_PASS;
}
示例#6
0
int
test_spin2(void)
#endif
{
  pthread_t t;

  assert(pthread_spin_init(&lock, PTHREAD_PROCESS_PRIVATE) == 0);

  assert(pthread_spin_lock(&lock) == 0);

  assert(pthread_create(&t, NULL, func, NULL) == 0);
  assert(pthread_join(t, NULL) == 0);

  assert(pthread_spin_unlock(&lock) == 0);

  assert(pthread_spin_destroy(&lock) == 0);

  assert(washere == 1);

  return 0;
}
示例#7
0
/*
 * Initialize timer data
 */
int
usdf_timer_init(struct usdf_fabric *fp)
{
	int i;

	pthread_spin_init(&fp->fab_timer_lock, PTHREAD_PROCESS_PRIVATE);

	fp->fab_timer_buckets = calloc(USDF_NUM_TIMER_BUCKETS,
			sizeof(struct usdf_timer_bucket));
	if (fp->fab_timer_buckets == NULL) {
		return -FI_ENOMEM;
	}

	for (i = 0; i < USDF_NUM_TIMER_BUCKETS; ++i) {
		LIST_INIT(&fp->fab_timer_buckets[i]);
	}

	fp->fab_cur_bucket = 0;
	fp->fab_cur_bucket_ms = usdf_get_ms();
	return 0;
}
示例#8
0
int
test_spin3(void)
#endif
{
  pthread_t t;

  wasHere = 0;
  assert(pthread_spin_init(&spin, PTHREAD_PROCESS_PRIVATE) == 0);
  assert(pthread_spin_lock(&spin) == 0);
  assert(pthread_create(&t, NULL, unlocker, (void *) 0) == 0);
  assert(pthread_join(t, NULL) == 0);
  /*
   * Our spinlocks don't record the owner thread so any thread can unlock the spinlock,
   * but nor is it an error for any thread to unlock a spinlock that is not locked.
   */
  assert(pthread_spin_unlock(&spin) == 0);
  assert(pthread_spin_destroy(&spin) == 0);
  assert(wasHere == 2);

  return 0;
}
示例#9
0
int ssa_set_ssa_signal_handler()
{
	struct sigaction action;
	int ret;
#if 0
	/*
	 *  addr2line utility doesn't work with alternative stack
	 */
	stack_t our_stack;

	our_stack.ss_sp = (void *) malloc(SIGSTKSZ);
	our_stack.ss_size = SIGSTKSZ;
	our_stack.ss_flags = 0;

	if (sigaltstack(&our_stack, NULL) != 0)
		return 1;
#endif
	ret = pthread_spin_init(&signal_handler_lock, 0);
	if (ret)
		return ret;

	ret = get_exe_path();
	if (ret)
		return ret;

	action.sa_sigaction = ssa_signal_handler;
	sigemptyset(&action.sa_mask);

	action.sa_flags = SA_SIGINFO | SA_ONSTACK;

	if (sigaction(SIGSEGV, &action, NULL) != 0)
		return 1;
	if (sigaction(SIGFPE,  &action, NULL) != 0)
		return 1;
	if (sigaction(SIGILL,  &action, NULL) != 0)
		return 1;

	return 0;
}
void bianca_init() {
        int i;
       
        _BIANCAperror = (void (*)(const char*)) dlsym (RTLD_NEXT, "perror");
        _BIANCAclose = (int (*)(int)) dlsym (RTLD_NEXT, "close");
        _BIANCAexit = (void (*)(int)) dlsym (RTLD_NEXT, "exit");
        _BIANCAwrite = (int (*)(int, const void*, size_t)) dlsym (RTLD_NEXT, "write");
        _BIANCA__builtin_puts = (int (*)(const char*)) dlsym (RTLD_NEXT, "__builtin_puts");
        _BIANCApthread_create = (int (*)(pthread_t*, const pthread_attr_t*, void *(*start_routine)(void*), void*)) dlsym (RTLD_NEXT, "pthread_create");
        _BIANCAprintf = (int (*)(const char*,...)) dlsym (RTLD_NEXT, "printf");
        _BIANCApthread_join = (int (*)(pthread_t, void **value_ptr)) dlsym (RTLD_NEXT, "pthread_join");

        pthread_spin_init(&lock, NULL);
       
        /* Initialisation de la table des identités */
        for (i = 0; i < 100; i++) { hashid.threads[i] = (pthread_t) -1; }
        hashid.size = 0;
       
        /* Initialisation du réseau */
        global_table = init(id(pthread_self()), P0_1_ENTRY);
        hashid.processes[0] = find_process_id(&global_table,0);
}
示例#11
0
int main()
{
	pthread_t tcb1, tcb2;
	int rv;

	pthread_spin_init(&spin,
			  PTHREAD_PROCESS_PRIVATE /* PTHREAD_PROCESS_SHARED */
			  );

	rv = pthread_create(&tcb1, NULL, thread1, NULL);
	if (rv)
		puts("Failed to create thread");

	rv = pthread_create(&tcb2, NULL, thread2, NULL);
	if (rv)
		puts("Failed to create thread");

	pthread_join(tcb1, NULL);
	pthread_join(tcb2, NULL);
	puts(" Exit Main");
	return 0;
}
示例#12
0
文件: 3-1.c 项目: 8l/rose
static void* fn_chld(void *arg)
{ 
	int rc = 0;

	/* Initialize spin lock */
	if(pthread_spin_init(&spinlock, PTHREAD_PROCESS_PRIVATE) != 0)
	{
		printf("main: Error at pthread_spin_init()\n");
		exit(PTS_UNRESOLVED);
	}

	/* Lock the spinlock */
	printf("thread: attempt spin lock\n");
	rc = pthread_spin_lock(&spinlock);
	if(rc != 0)
	{
		printf("Error: thread failed to get spin lock error code:%d\n" , rc);
		exit(PTS_UNRESOLVED);
	}
	printf("thread: acquired spin lock\n");
	
	/* Wait for main to try and unlock this spinlock */
	sem = INMAIN;
	while(sem == INMAIN)
		sleep(1);

	/* Cleanup just in case */
	pthread_spin_unlock(&spinlock);
	
	if(pthread_spin_destroy(&spinlock) != 0)
	{
		printf("Error at pthread_spin_destroy()");
		exit(PTS_UNRESOLVED);
	}	

	pthread_exit(0);
	return NULL;
}
示例#13
0
文件: spinlock.c 项目: DesiD/CsInfoPa
int main(int argc, char *argv[]) {

    int seed;

    // inizializza il seme
    seed=time(NULL);
    srand(seed);

    printf("Pid:%d\n",getpid());

    // ancora non vogliamo uscire
    wanna_exit=0;
    // imposta il signal handler
    signal(SIGUSR1,sig_user_exit);

    // inizializza lo spinlock
    pthread_spin_init(&spinlock,PTHREAD_PROCESS_PRIVATE);

    // conservati il tuo thread id (anche se qui non serve)
    pthread[0]=pthread_self();
    // crea il secondo thread
    if (pthread_create(&pthread[1],NULL,(void *(*)(void *))do_something,(void *)((long int)1)))
        printf("Error creating thread!\n");
    // vai alla funzione relativa alla gestione dello spinlock
    do_something(0);

    // se hai finito aspetta il secondo thread
    // simile al wait del fork, per evitare di produrre "thread zombie"
    // e spreco di risorse, a meno che non venga utilizzato
    // pthread_detach e il thread abbia gli attributi corretti
    pthread_join(pthread[1],NULL);
    // libera le risorse relative allo spinlock
    pthread_spin_destroy(&spinlock);

    printf("All done\n");
    // esci
    exit(EXIT_SUCCESS);
}
示例#14
0
文件: tst-spin3.c 项目: ArcEye/RTAI
static int
do_test (void)
{
  pthread_spinlock_t s;

  if (pthread_spin_init (&s, PTHREAD_PROCESS_PRIVATE) != 0)
    {
      puts ("spin_init failed");
      return 1;
    }

  if (pthread_spin_lock (&s) != 0)
    {
      puts ("1st spin_lock failed");
      return 1;
    }

  /* Set an alarm for 1 second.  The wrapper will expect this.  */
  alarm (1);

#ifdef ORIGINAL_TEST /* ORIGINAL */
  /* This call should never return.  */
  pthread_spin_lock (&s);

  puts ("2nd spin_lock returned");
#else /* !ORIGINAL */
  int r = pthread_spin_lock (&s);
  if (!r) {
    puts ("2nd spin_lock succeeded");
  } else if (r != EDEADLOCK) {
    puts ("2nd spin_lock failed but did not EDEADLOCKed");
  }
// needed to avoid freezing linux
  pthread_soft_real_time_np();
  while (pthread_spin_trylock (&s) == EBUSY) rt_sleep(nano2count(10000));
#endif /* ORIGINAL */
  return 1;
}
INLINE int
ptw32_spinlock_check_need_init (pthread_spinlock_t * lock)
{
  int result = 0;
  ptw32_mcs_local_node_t node;

  /*
   * The following guarded test is specifically for statically
   * initialised spinlocks (via PTHREAD_SPINLOCK_INITIALIZER).
   */
  ptw32_mcs_lock_acquire(&ptw32_spinlock_test_init_lock, &node);

  /*
   * We got here possibly under race
   * conditions. Check again inside the critical section
   * and only initialise if the spinlock is valid (not been destroyed).
   * If a static spinlock has been destroyed, the application can
   * re-initialise it only by calling pthread_spin_init()
   * explicitly.
   */
  if (*lock == PTHREAD_SPINLOCK_INITIALIZER)
    {
      result = pthread_spin_init (lock, PTHREAD_PROCESS_PRIVATE);
    }
  else if (*lock == NULL)
    {
      /*
       * The spinlock has been destroyed while we were waiting to
       * initialise it, so the operation that caused the
       * auto-initialisation should fail.
       */
      result = EINVAL;
    }

  ptw32_mcs_lock_release(&node);

  return (result);
}
int main (int argc, char *argv[])  {
  pthread_t thread[NTHREADS];
  int err;
  
  pthread_spin_init( &spinlock_global, 0);
  
  for(int i=0;i<NTHREADS;i++) {
    err=pthread_create(&(thread[i]),NULL,&func,NULL); 
    if(err!=0)
      error(err,"pthread_create");
  }
  for(int i=0; i<1000000000;i++) { /*...*/ }

  for(int i=NTHREADS-1;i>=0;i--) {
    err=pthread_join(thread[i],NULL);
    if(err!=0)
      error(err,"pthread_join");
  }

  printf("global: %ld\n",global);

  return(EXIT_SUCCESS);
}
示例#17
0
文件: sync.c 项目: cfallin/nk
nk_status nk_cond_create(nk_host *host, nk_cond **ret) {
  nk_status status;

  status = NK_ERR_NOMEM;
  nk_cond *c = nk_freelist_alloc(&host->cond_freelist);
  if (!c) {
    goto err;
  }

  if (pthread_spin_init(&c->lock, PTHREAD_PROCESS_PRIVATE)) {
    goto err;
  }

  c->host = host;
  QUEUE_INIT(&c->waiters);

  *ret = c;
  return NK_OK;
err:
  if (c) {
    nk_freelist_free(&host->cond_freelist, c);
  }
  return status;
}
示例#18
0
文件: sync.c 项目: cfallin/nk
nk_status nk_mutex_create(nk_host *host, nk_mutex **ret) {
  nk_status status;

  status = NK_ERR_NOMEM;
  nk_mutex *m = nk_freelist_alloc(&host->mutex_freelist);
  if (!m) {
    goto err;
  }

  if (pthread_spin_init(&m->lock, PTHREAD_PROCESS_PRIVATE)) {
    goto err;
  }

  m->host = host;
  QUEUE_INIT(&m->waiters);

  *ret = m;
  return NK_OK;
err:
  if (m) {
    nk_freelist_free(&host->mutex_freelist, m);
  }
  return status;
}
示例#19
0
void codec_async_mem_init(int frame_width, int frame_height, int channels)
{
	_codec_mem_pool_size = 80;
	pthread_spin_init(&_mem_spin, 0);

	mem = (codec_buffer_t *)malloc(_codec_mem_pool_size * sizeof(codec_buffer_t));

	for (int i = 0 ; i < _codec_mem_pool_size; i++) {
		mem[i].index = i;
		mem[i].data = (void *)malloc(frame_width * frame_height * channels * sizeof(uint8_t) +	// Reference Camera Frame, UINT8, 4 channels
										sizeof(short) +												// Depth Data availability flag
										frame_width * frame_height * sizeof(float) +				// Depth Data, float, 1 channels
										sizeof(int) +												// Depth Data aligned representation step offset
										sizeof(int)													// Frame Counter
									);

	}

	_mem_count = 0;
	_mem_head = mem;
	_mem_tail = mem;

	sem_init(&_sem_empty, 0, _codec_mem_pool_size);
}
示例#20
0
Driver_FILE::Driver_FILE(const int& _framesize_in_byte, std::string _conn_string): 
  Driver(_framesize_in_byte, _conn_string),
  empty_frame(Frame(_framesize_in_byte))
{
  
#ifdef __MACH__
  this->spinlock = PTHREAD_MUTEX_INITIALIZER;
#else
  pthread_spin_init(&this->spinlock, 0);
#endif
  
  assert(LOGICBLOCKSIZE <= this->framesize_in_byte && "Driver_FILE: O_DIRECT cannot deal with OBJECT that is smaller than LOGICBLOCKSIZE");

#ifdef __MACH__
    fd = open(this->conn_string.c_str(), O_RDWR | O_CREAT , (mode_t) 0600);
#else
    fd = open64(this->conn_string.c_str(), O_RDWR | O_CREAT | O_DIRECT, (mode_t) 0600);
#endif
    
  if(fd <= 0){
    assert(false && "Driver_FILE: Fail to openfile");
  }

}
示例#21
0
easy_mempool_t *easy_mempool_create(uint32_t size)
{
    easy_mempool_t          *ret = NULL;
    int32_t                 page_num = EASY_MEMPOOL_PAGE_MAX_NUM;
    int32_t                 size2alloc = sizeof(easy_mempool_t) + sizeof(easy_mempool_page_t) * page_num;

    if (NULL != (ret = (easy_mempool_t *)easy_mempool_g_allocator.memalign(EASY_MEMPOOL_ALIGNMENT, size2alloc))) {
        ret->cur_page_pos = 0;
        ret->direct_alloc_cnt = 0;
        ret->mem_total = 0;
        ret->mem_limit = INT64_MAX;
        ret->allocator = &easy_mempool_g_allocator;
        ret->page_size = size ? size : EASY_MEMPOOL_PAGE_SIZE;
        ret->page_num = page_num;
        ret->page_metas = (easy_mempool_page_meta_t *)((char *)ret + sizeof(easy_mempool_t));
        memset(ret->page_metas, 0, sizeof(easy_mempool_page_t) * page_num);
        ret->page_metas[ret->cur_page_pos].ref_cnt = 1;
        ret->free_num = 0;
        ret->free_list = NULL;
        pthread_spin_init(&(ret->free_list_lock), PTHREAD_PROCESS_PRIVATE);
    }

    return ret;
}
示例#22
0
LayerWta* createWtaLayer(size_t N, size_t *glob_idx, unsigned char statLevel) {
    LayerWta *l = (LayerWta*)malloc(sizeof(LayerWta));
    l->base = *createPoissonLayer(N, glob_idx, statLevel);
    l->base.need_steps_sync = true;

    l->base.calculateProbability = &calculateProbability_Wta;
    l->base.calculateSpike = &calculateSpike_Wta;
    l->base.calculateDynamics = &calculateDynamics_Wta;
    l->base.deleteLayer = &deleteLayer_Wta;
    l->base.configureLayer = &configureLayer_Wta;
    l->base.toStartValues = &toStartValues_Wta;
    l->base.propagateSpike = &propagateSpike_Wta;
    l->base.allocSynData = &allocSynData_Wta;
    l->base.deallocSynData = &deallocSynData_Wta;
    l->base.printLayer = &printLayer_Wta;
    l->base.serializeLayer = &serializeLayer_Wta;
    l->base.deserializeLayer= &deserializeLayer_Wta;
    l->base.saveStat= &saveStat_Wta;

    l->sum_prob = 0.0;
    l->b = (double*) malloc( sizeof(double) * N );
    pthread_spin_init(&sum_prob_spinlock, 0);
    return(l);
}
示例#23
0
文件: lock.c 项目: github188/netlib
static  int lock_spin_create(lock_t * lock)
{
	int ret = -1;
	if(NULL == lock)
	{
		dbg_printf("this is null \n");
		return(-1);
	}
	bzero(lock,sizeof(lock_t));

	ret = pthread_spin_init(&lock->object.spinlock,0);
	if(ret < 0 )
	{
		dbg_printf("pthread_spin_init fail\n");
		return(-2);
	}

	lock->lock = lock_spin_lock;
	lock->unlock = lock_spin_unlock;
	lock->trylock = lock_spin_trylock;
	lock->free = lock_spin_free;

	return(0);
}
示例#24
0
int MPID_nem_ib_init_cell_pool(int n)
{
    int mpi_errno = MPI_SUCCESS;
    /* MPID_nem_ib_cell_pool_t *pool; */

    /* pool = &MPID_nem_ib_cell_pool; */

    MPID_nem_ib_queue_init(&MPID_nem_ib_cell_pool.queue);

    MPID_nem_ib_queue_init(&alloc_cells_queue);

    mpi_errno = MPID_nem_ib_add_cells(n);

    if(mpi_errno) {
        MPIU_ERR_POP(mpi_errno);
    }

    pthread_spin_init(&MPID_nem_ib_cell_pool.lock, 0);

fn_exit:
    return mpi_errno;
fn_fail:
    goto fn_exit;
}
示例#25
0
 void init() {
     my_val = 0;
     pthread_assert( pthread_spin_init( &my_lock, PTHREAD_PROCESS_PRIVATE ), "pthread_spin_init failed" );
 }
示例#26
0
static void spin_lock_init(spinlock_t *lock)
{
	int r = pthread_spin_init(lock, 0);
	assert(!r);
}
示例#27
0
文件: cbuf.c 项目: arnolda/scafacos
void init_cbuf_lock()
{
    pthread_spin_init(&cbuf_lock, 0);
}
示例#28
0
int
init_mem_list(uint8_t type, int size, int length)
{
	int i = 0;
	mem_list_t *plist = &mem_list[i];
	
	if(!mem_list_init)
	{
		init_all_mem_list();
		mem_list_init = 1;
	}

	if((type == 0) || (size <= 0) || (length <= 0))
		return 0;

	plist = &mem_list[type];
	
	if(plist->valid)
		return 1;

	plist->valid = 1;
	pthread_spin_init(&plist->lock, 0);	
	plist->type = type;
	
	for(i = 0; i < length + 1; i++)
	{
		uint8_t *ptype = 0;
		mem_node_t *p = (mem_node_t *)malloc(sizeof(mem_node_t));
		if(!p)
			goto err;
	
		p->data = malloc(size + sizeof(uint8_t));
		if(!p->data)
		{
			free(p);
			goto err;
		}
		p->raw_data = p->data;
		ptype = p->data;
		*ptype = type;
		
		p->next = NULL;
		p->pre = NULL;
		
		if(!plist->head)
		{
			plist->head= p;
			plist->tail = p;
			continue;
		}

		p->next = plist->head->next;
		plist->head->next = p;
		p->pre = plist->head;
		if(p->next)
			p->next->pre = p;
	}
	
	while(plist->tail->next)
		plist->tail = plist->tail->next;
	plist->valid = 1;
	if(print_msg)
		printf("init %d node(s) of type %u success\n", length, type);	
	return 1;

err:
	while(plist->head)
	{
		mem_node_t *p = plist->head;
		plist->head = plist->head->next;
		if(p)
		{
			if(p->data)
				free(p->data);
			free(p);
		}
	}

	mem_list[type].valid = 0;
	mem_list[type].type  = 0;
	mem_list[type].head  = NULL;
	mem_list[type].tail   = NULL;	
	
	return 0;
}
示例#29
0
文件: thread.c 项目: xaiki/IceCast
void thread_spin_create (spin_t *spin)
{
    int x = pthread_spin_init (&spin->lock, PTHREAD_PROCESS_PRIVATE);
    if (x)
        abort();
}
示例#30
0
int
usdf_pep_open(struct fid_fabric *fabric, struct fi_info *info,
              struct fid_pep **pep_o, void *context)
{
    struct usdf_pep *pep;
    struct usdf_fabric *fp;
    struct sockaddr_in *sin;
    int ret;
    int optval;

    USDF_TRACE_SYS(EP_CTRL, "\n");

    if (!info) {
        USDF_DBG_SYS(EP_CTRL, "null fi_info struct is invalid\n");
        return -FI_EINVAL;
    }

    if (info->ep_attr->type != FI_EP_MSG) {
        return -FI_ENODEV;
    }

    if ((info->caps & ~USDF_MSG_CAPS) != 0) {
        return -FI_EBADF;
    }

    switch (info->addr_format) {
    case FI_SOCKADDR:
        if (((struct sockaddr *)info->src_addr)->sa_family != AF_INET) {
            USDF_WARN_SYS(EP_CTRL, "non-AF_INET src_addr specified\n");
            return -FI_EINVAL;
        }
        break;
    case FI_SOCKADDR_IN:
        break;
    default:
        USDF_WARN_SYS(EP_CTRL, "unknown/unsupported addr_format\n");
        return -FI_EINVAL;
    }

    if (info->src_addrlen &&
            info->src_addrlen != sizeof(struct sockaddr_in)) {
        USDF_WARN_SYS(EP_CTRL, "unexpected src_addrlen\n");
        return -FI_EINVAL;
    }

    fp = fab_ftou(fabric);

    pep = calloc(1, sizeof(*pep));
    if (pep == NULL) {
        return -FI_ENOMEM;
    }

    pep->pep_fid.fid.fclass = FI_CLASS_PEP;
    pep->pep_fid.fid.context = context;
    pep->pep_fid.fid.ops = &usdf_pep_ops;
    pep->pep_fid.ops = &usdf_pep_base_ops;
    pep->pep_fid.cm = &usdf_pep_cm_ops;
    pep->pep_fabric = fp;

    pep->pep_state = USDF_PEP_UNBOUND;
    pep->pep_sock = socket(AF_INET, SOCK_STREAM, 0);
    if (pep->pep_sock == -1) {
        ret = -errno;
        goto fail;
    }
    ret = fcntl(pep->pep_sock, F_GETFL, 0);
    if (ret == -1) {
        ret = -errno;
        goto fail;
    }
    ret = fcntl(pep->pep_sock, F_SETFL, ret | O_NONBLOCK);
    if (ret == -1) {
        ret = -errno;
        goto fail;
    }

    /* set SO_REUSEADDR to prevent annoying "Address already in use" errors
     * on successive runs of programs listening on a well known port */
    optval = 1;
    ret = setsockopt(pep->pep_sock, SOL_SOCKET, SO_REUSEADDR, &optval,
                     sizeof(optval));
    if (ret == -1) {
        ret = -errno;
        goto fail;
    }

    pep->pep_info = fi_dupinfo(info);
    if (!pep->pep_info) {
        ret = -FI_ENOMEM;
        goto fail;
    }

    if (info->src_addrlen == 0) {
        /* Copy the source address information from the device
         * attributes.
         */
        pep->pep_info->src_addrlen = sizeof(struct sockaddr_in);
        sin = calloc(1, pep->pep_info->src_addrlen);
        if (!sin) {
            USDF_WARN_SYS(EP_CTRL,
                          "calloc for src address failed\n");
            goto fail;
        }

        sin->sin_family = AF_INET;
        sin->sin_addr.s_addr = fp->fab_dev_attrs->uda_ipaddr_be;
        pep->pep_info->src_addr = sin;
    }

    memcpy(&pep->pep_src_addr, pep->pep_info->src_addr,
           pep->pep_info->src_addrlen);

    /* initialize connreq freelist */
    ret = pthread_spin_init(&pep->pep_cr_lock, PTHREAD_PROCESS_PRIVATE);
    if (ret != 0) {
        ret = -ret;
        goto fail;
    }
    TAILQ_INIT(&pep->pep_cr_free);
    TAILQ_INIT(&pep->pep_cr_pending);
    pep->pep_backlog = 10;
    ret = usdf_pep_grow_backlog(pep);
    if (ret != 0) {
        goto fail;
    }

    atomic_initialize(&pep->pep_refcnt, 0);
    atomic_inc(&fp->fab_refcnt);

    *pep_o = pep_utof(pep);
    return 0;

fail:
    if (pep != NULL) {
        usdf_pep_free_cr_lists(pep);
        if (pep->pep_sock != -1) {
            close(pep->pep_sock);
        }
        fi_freeinfo(pep->pep_info);
        free(pep);
    }
    return ret;
}