Ejemplo n.º 1
2
/** command "show logging" */
int _dessert_cli_cmd_logging(struct cli_def* cli, char* command, char* argv[], int argc) {
    pthread_rwlock_rdlock(&_dessert_logrbuf_len_lock);
    int i = 0;
    int max = _dessert_logrbuf_len - 1;
    char* line;

    if(_dessert_logrbuf_len < 1) {
        cli_print(
            cli,
            "logging to ringbuffer is disabled - use \"logging ringbuffer [int]\" in config-mode first");
        pthread_rwlock_unlock(&_dessert_logrbuf_len_lock);
        return CLI_ERROR;
    }

    if(argc == 1) {
        int max2 = (int) strtol(argv[0], NULL, 10);

        if(max2 > 0) {
            max = max2;
        }
    }

    /* where to start and print? */
    if(max > _dessert_logrbuf_used) {
        max = _dessert_logrbuf_used;
    }

    i = _dessert_logrbuf_cur - max - 1;

    if(i < 0) {
        i += _dessert_logrbuf_len;
    }

    while(max > 0) {
        i++;
        max--;

        if(i == _dessert_logrbuf_len) {
            i = 0;
        }

        line = _dessert_logrbuf + (DESSERT_LOGLINE_MAX * i);
        cli_print(cli, "%s", line);
    }

    pthread_rwlock_unlock(&_dessert_logrbuf_len_lock);

    return CLI_OK;
}
Ejemplo n.º 2
0
void rw_lock_read_lock(rw_lock_t* lock)
{
#ifdef __MINGW32__
	EnterCriticalSection(&lock->readlock);

	EnterCriticalSection(&lock->lock);
	lock->readers++;
	ResetEvent(lock->writelock);
	LeaveCriticalSection(&lock->lock);

	LeaveCriticalSection(&lock->readlock);
#else
	pthread_rwlock_rdlock(lock);
#endif
}
Ejemplo n.º 3
0
void * writer() {
    for (int i = 0; i < 5; i++) {
        pthread_rwlock_rdlock(&rwlock);

        pthread_rwlock_trywrlock(&rwlock);

        value += 1;

        pthread_rwlock_unlock(&rwlock);

        sleep(i);
    }

    pthread_exit(NULL);
}
Ejemplo n.º 4
0
struct list * list_lookup(int lookup_id)
{
   struct list *curr;
   int rv;
   rv=pthread_rwlock_rdlock(&rw);   // 只讀表,因此以讀模式申請讀寫鎖 
   check_error(rv, "lookup: read lock");
   for (curr=list_head; curr !=NULL; curr=curr->next) // 查詢記錄位置 
         if (curr->id >= lookup_id) 
            break;
   if (curr != NULL && curr->id != lookup_id)
      curr = (struct list *)NULL;    // 沒有找到
   rv = pthread_rwlock_unlock(&rw);   // 釋放讀寫鎖 
   check_error(rv, "add: write unlock");
   return (curr);
}
Ejemplo n.º 5
0
static int cache_get(lua_State * L) {
	size_t len1,len2;
	const char * key_l=lua_tolstring(L,1,&len1);
	char * val=NULL;
	pthread_rwlock_rdlock(&rwlock);
	val=hashtable_get(ht,key_l,&len2);
	//printf("%p %s %u\n",val,val,len2);
	if(val==NULL) {
		pthread_rwlock_unlock(&rwlock);
		return 0;
	}
	lua_pushlstring(L,val,len2);
	pthread_rwlock_unlock(&rwlock);
	return 1;
}
Ejemplo n.º 6
0
struct gate_info *gate_info_manager_t::get_best_gate_incref(uint64_t uid)
{
    struct gate_info *info = NULL;

    pthread_rwlock_rdlock(&rwlock);
    size_t sz = gate_infos.size();
    if (sz > 0) {
        int idx = (int)(uid % (uint64_t)sz);
        info = gate_infos[idx];
        gate_info_incref(info);
    }
    pthread_rwlock_unlock(&rwlock);

    return info;
}
Ejemplo n.º 7
0
void runpath_list_fprintf(runpath_list_type * list ) {
  pthread_rwlock_rdlock( &list->lock );
  {
    FILE * stream = util_mkdir_fopen( list->export_file , "w");
    const char * line_fmt = runpath_list_get_line_fmt( list );
    int index;
    vector_sort( list->list , runpath_node_cmp );
    for (index =0; index < vector_get_size( list->list ); index++) {
      const runpath_node_type * node = runpath_list_iget_node__( list , index );
      runpath_node_fprintf( node , line_fmt , stream );
    }
    fclose( stream );
  }
  pthread_rwlock_unlock( &list->lock );
}
Ejemplo n.º 8
0
/*
 * Find a job for the given thread ID.
 */
struct job *
job_find(struct queue *qp, pthread_t id)
{
  struct job *jp;

  if (pthread_rwlock_rdlock(&qp->q_lock) != 0)
    return NULL;

  for (jp = qp->q_head; jp != NULL; jp = jp->j_next)
    if (pthread_equal(jp->j_id, id))
      break;

  pthread_rwlock_unlock(&qp->q_lock);
  return jp;
}
Ejemplo n.º 9
0
//---------------------------------------------------------------------------------------------------------------------
int pmip_cache_iterate(int (*func) (void *, void *), void *arg)
{
    int err;
    int mutex_return_code;
    mutex_return_code = pthread_rwlock_rdlock(&pmip_lock);
    if (mutex_return_code != 0) {
        dbg("pthread_rwlock_rdlock(&pmip_lock) %s\n", strerror(mutex_return_code));
    }
    err = pmip_hash_iterate(&g_pmip_hash, func, arg);
    mutex_return_code = pthread_rwlock_unlock(&pmip_lock);
    if (mutex_return_code != 0) {
        dbg("pthread_rwlock_unlock(&pmip_lock) %s\n", strerror(mutex_return_code));
    }
    return err;
}
Ejemplo n.º 10
0
  void RateLimiter::Release(int counter_index, const char * key, uint64_t amount) {
    TSReleaseAssert(!pthread_rwlock_rdlock(&rwlock_keymap_));

    std::map<const char *,LimiterState *>::iterator it = keymap_.find(key);

    TSReleaseAssert(!pthread_rwlock_unlock(&rwlock_keymap_));
    TSReleaseAssert( it != keymap_.end() );
    LimiterState * state = it->second;


    TSMutexLock(update_mutex_);
    state->set_taken(counter_index, state->taken(counter_index) - amount);
    dbg("released amount, currently taken %f", state->taken(counter_index));
    TSMutexUnlock(update_mutex_);
  }
//---------------------------------------------------------------------------------------------------------------------
pmip_entry_t *pmip_cache_get(const struct in6_addr * our_addr, const struct in6_addr * peer_addr)
{
	pmip_entry_t *bce;
	assert(peer_addr && our_addr);
	pthread_rwlock_rdlock(&pmip_lock);
	bce = hash_get(&g_pmip_hash, our_addr, peer_addr);
	if (bce) {
		pthread_rwlock_wrlock(&bce->lock);
		//dbg("PMIP cache entry is found for: %x:%x:%x:%x:%x:%x:%x:%x with type %d\n", NIP6ADDR(&bce->mn_hw_address), (bce->type));
	} else {
		pthread_rwlock_unlock(&pmip_lock);
		//dbg("PMIP cache entry is NOT found for %x:%x:%x:%x:%x:%x:%x:%x <-> %x:%x:%x:%x:%x:%x:%x:%x\n", NIP6ADDR(our_addr), NIP6ADDR(peer_addr));
	}
	return bce;
}
Ejemplo n.º 12
0
/* Looks for the photoset specified in the argument.
 * Returns pointer to the stored cached_information
 * or 0 if not found.
 */
cached_information *photoset_lookup(const char *photoset) {
    cached_photoset *cps;
    cached_information *ci_copy = NULL;

    pthread_rwlock_rdlock(&cache_lock);
    if(check_cache())
        goto fail;

    cps = g_hash_table_lookup(photoset_ht, photoset);
    if(cps)
        ci_copy = copy_cached_info(&(cps->ci));

fail: pthread_rwlock_unlock(&cache_lock);
    return ci_copy;
}
Ejemplo n.º 13
0
/**
 * \brief Bind to a variable
 *
 * Subscribe to the give variable and automatically populate its value in the float whose reference is passed
 *
 * \param name Name of the variable to subscribe to
 * \param store_to Pointer to a float to store the value of the variable when it is updated
 * \return 0 on success
 */
int Var_bind(char* name, float* store_to) {
    Subscription* s;

    Var_subscribe(name);

    pthread_rwlock_rdlock(&subscriptions_lock); {
        s = Dictionary_get(subscriptions, name);
        s->writeback = store_to;
    }
    pthread_rwlock_unlock(&subscriptions_lock);

    (*s->writeback) = s->current;
    
    return 0;
}
Ejemplo n.º 14
0
static void
__dcethread_child_fork(void)
{
    unsigned int i;

    pthread_rwlock_rdlock(&atfork_lock);

    for (i = 0; i < atfork_handlers_len; i++)
    {
	if (atfork_handlers[i].child_fork)
	    atfork_handlers[i].child_fork(atfork_handlers[i].user_state);
    }

    pthread_rwlock_unlock(&atfork_lock);
}
Ejemplo n.º 15
0
/* Force log context acquisition through a function */
static rpmlogCtx rpmlogCtxAcquire(int write)
{
    static struct rpmlogCtx_s _globalCtx = { PTHREAD_RWLOCK_INITIALIZER,
					     RPMLOG_UPTO(RPMLOG_NOTICE),
					     0, NULL, NULL, NULL, NULL };
    rpmlogCtx ctx = &_globalCtx;

    /* XXX: errors should be handled */
    if (write)
	pthread_rwlock_wrlock(&ctx->lock);
    else
	pthread_rwlock_rdlock(&ctx->lock);

    return ctx;
}
Ejemplo n.º 16
0
/**
 * TryReadLock
 *
 * This will attempt to aquire the read lock. If it is not able to do so false will be returned.
 * If the lock is aquired true will be returned.
 *
 * If the lock is not aquired you should not make a call to Unlock as the lock is not owned.
 **/
bool RWLock::TryReadLock()
{
	int ret = pthread_rwlock_rdlock(&m_lock);
	if (ret == 0)
		return true;

	switch(ret)
	{
		case EBUSY:
			return false;
		default:
			abort();
	}
	return false;
}
Ejemplo n.º 17
0
void
glthread_rwlock_rdlock (gl_rwlock_t *lock)
{
  if (!lock->initialized)
    {
      if (pthread_mutex_lock (&lock->guard) != 0)
	abort ();
      if (!lock->initialized)
	glthread_rwlock_init (lock);
      if (pthread_mutex_unlock (&lock->guard) != 0)
	abort ();
    }
  if (pthread_rwlock_rdlock (&lock->rwlock) != 0)
    abort ();
}
Ejemplo n.º 18
0
NOEXPORT void dyn_lock_function(int mode, struct CRYPTO_dynlock_value *value,
        const char *file, int line) {
    (void)file; /* squash the unused parameter warning */
    (void)line; /* squash the unused parameter warning */
    if(mode&CRYPTO_LOCK) {
        /* either CRYPTO_READ or CRYPTO_WRITE (but not both) are needed */
        if(!(mode&CRYPTO_READ)==!(mode&CRYPTO_WRITE))
            fatal("Invalid locking mode");
        if(mode&CRYPTO_WRITE)
            pthread_rwlock_wrlock(&value->rwlock);
        else
            pthread_rwlock_rdlock(&value->rwlock);
    } else
        pthread_rwlock_unlock(&value->rwlock);
}
Ejemplo n.º 19
0
static ucs_async_mode_t ucs_async_handler_mode(int id)
{
    ucs_async_mode_t mode;
    khiter_t hash_it;

    pthread_rwlock_rdlock(&ucs_async_global_context.handlers_lock);
    hash_it = ucs_async_handler_kh_get(id);
    if (ucs_async_handler_kh_is_end(hash_it)) {
        mode = UCS_ASYNC_MODE_POLL;
    } else {
        mode = kh_value(&ucs_async_global_context.handlers, hash_it)->mode;
    }
    pthread_rwlock_unlock(&ucs_async_global_context.handlers_lock);
    return mode;
}
Ejemplo n.º 20
0
void
CPacBioUtility::AcquireLock(bool bExclusive)
{
#ifdef _WIN32
if(bExclusive)
	AcquireSRWLockExclusive(&m_hRwLock);
else
	AcquireSRWLockShared(&m_hRwLock);
#else
if(bExclusive)
	pthread_rwlock_wrlock(&m_hRwLock);
else
	pthread_rwlock_rdlock(&m_hRwLock);
#endif
}
Ejemplo n.º 21
0
/***********************
 *    Read-Write Lock
 ***********************/
BOOL _RWLockLockR(pthread_rwlock_t *lock, DWORD timeout)
{
	if(timeout == INFINITE)
		return pthread_rwlock_rdlock(lock) == 0;
	else
	{
		while(!pthread_rwlock_tryrdlock(lock) && timeout > 0)
		{
			usleep(10000);
			if(timeout > 10) timeout -= 10;
			else return FALSE;
		}
		return TRUE;
	}
}
Ejemplo n.º 22
0
// 3. PUT_FILE_CLEANUP
static void proc_cmd_put_file_cleanup(struct mg_connection* conn, const struct mg_request_info* request_info)
{
	char str_command[100];
	logger_remotem("PUT_FILE with CLEANUP: delete all files in %s", UPLOAD_DIRECTORY);

	// Clean up upload directory
	sprintf(str_command, "exec rm -rf %s/*", ODI_UPLOAD);
	pthread_rwlock_rdlock(&rwlock);
	system(str_command);
	pthread_rwlock_unlock(&rwlock);

	// Process upload file
	proc_cmd_put_file(conn, request_info);
	logger_remotem("PUT_FILE with CLEANUP: SUCCESS");
}
Ejemplo n.º 23
0
void* run1(void* arglist)
{
    pthread_t id = pthread_self();
    printf("[run1] TID=%d\n", (int)id);

    printf("[run1] started\n");

    pthread_rwlock_rdlock(&lock);
    printf("[run1] a read lock is obtained\n");

    pthread_rwlock_unlock(&lock);
    printf("[run1] a read lock is released\n");

    return NULL;
}
Ejemplo n.º 24
0
struct job* job_find(struct queue *qb,pthread_t id)
{
    struct job *jb;

	if(pthread_rwlock_rdlock(&qb->q_lock) != 0)
           return NULL;

	for(jb=qb->q_head;jb != NULL;jp = jb->j_next)
			if(pthread_equal(jb->j_id,id))
					break;
	pthread_rwlock_unlock(&qb->q_lock);

	return jb;

}
Ejemplo n.º 25
0
/* Returns a copy of the port's statistics description. */
struct ofl_port_stats * MALLOC_ATTR
pcap_drv_get_port_stats(struct pcap_drv *drv, size_t drv_port_no) {
    pthread_rwlock_rdlock(drv->ports_rwlock);
    struct pcap_port *port = drv->ports[drv_port_no];
    pthread_rwlock_unlock(drv->ports_rwlock);

    if (port != NULL) {
        pthread_mutex_lock(port->stats_mutex);
        struct ofl_port_stats *ret = memcpy(malloc(sizeof(struct ofl_port_stats)), port->of_stats, sizeof(struct ofl_port_stats));
        pthread_mutex_unlock(port->stats_mutex);
        return ret;
    } else {
        return NULL;
    }
}
Ejemplo n.º 26
0
void summary_key_set_fwrite(summary_key_set_type * set, const char * filename) {
    pthread_rwlock_rdlock( &set->rw_lock );
    {
        FILE * stream = util_mkdir_fopen(filename , "w");
        if (stream) {
            stringlist_type * keys = hash_alloc_stringlist(set->key_set);
            stringlist_fwrite(keys, stream);
            stringlist_free(keys);
            fclose( stream );
        } else {
            util_abort("%s: failed to open: %s for writing \n", __func__, filename);
        }
    }
    pthread_rwlock_unlock( &set->rw_lock );
}
Ejemplo n.º 27
0
/* Extrae_OMPT_tf_task_id_set_running
   Annotates whether a ompt task is running or not. */
void Extrae_OMPT_tf_task_id_set_running (ompt_task_id_t ompt_tid, int b)
{
	unsigned u;

	pthread_rwlock_rdlock (&mutex_tid_tf);

	for (u = 0; u < n_allocated_ompt_tids_tf; u++)
		if (ompt_tids_tf[u].tid == ompt_tid)
		{
			ompt_tids_tf[u].is_running = b;
			break;
		}

	pthread_rwlock_unlock (&mutex_tid_tf);
}
Ejemplo n.º 28
0
static void state_map_select_matching__( state_map_type * map , bool_vector_type * select_target , int select_mask , bool select) {
  pthread_rwlock_rdlock( &map->rw_lock );
  {
    {
      const int * map_ptr = int_vector_get_ptr( map->state );

      for (int i=0; i < int_vector_size( map->state ); i++) {
        int state_value = map_ptr[i];
        if (state_value & select_mask) 
          bool_vector_iset( select_target , i , select);
      }
    }
    pthread_rwlock_unlock( &map->rw_lock );
  }
}
Ejemplo n.º 29
0
Archivo: main.c Proyecto: A-L-E-X/RIOT
static void *reader(void *arg)
{
    (void) arg;
    /* PRINTF("%s", "start"); */
    for (int i = 0; i < NUM_ITERATIONS; ++i) {
        pthread_rwlock_rdlock(&rwlock);
        unsigned cur = counter;
        do_sleep(1); /* simulate time that it takes to read the value */
        PRINTF("%i: read  <- %2u (correct = %u)", i, cur, cur == counter);
        pthread_rwlock_unlock(&rwlock);
        do_sleep(1);
    }
    /* PRINTF("%s", "done"); */
    return NULL;
}
Ejemplo n.º 30
0
void* AdConHash::LookUp(char * pKey)
{
	struct node_s* pNode;
	if (m_pHandle == NULL) 
	{
		return NULL;
	}
	pthread_rwlock_rdlock(&m_pLock);
	pNode = const_cast<struct node_s *>(conhash_lookup(m_pHandle, pKey));
	pthread_rwlock_unlock(&m_pLock);

	if(pNode == NULL)
		return NULL;
	return pNode->value;
}