Пример #1
0
/**
 * @brief	Initialize a pthread read/write lock attributes object with default values.
 * @see		pthread_rwlockattr_init()
 * @param	attr	a pointer to the read/write lock attributes object to be initialized.
 * @return	0 on success or an error number on failure.
 */
int rwlock_attr_init(pthread_rwlockattr_t *attr) {
#ifdef MAGMA_PEDANTIC
	int result = pthread_rwlockattr_init(attr);
	if (result) log_pedantic("Could not initialize the read/write lock attribute object. {pthread_rwlockattr_init = %i}", result);
	return result;
#else
	return pthread_rwlockattr_init(attr);
#endif
}
Пример #2
0
/** \brief Initialize the rwlock taking the rwlock attributes into account
 *
 * \b os_rwlockInit calls \b pthread_rwlock_init to intialize the posix
 * \b rwlock.
 *
 * In case the scope attribute is \b OS_SCOPE_SHARED, the posix
 * \b rwlock "pshared" attribute is set to \b PTHREAD_PROCESS_SHARED
 * otherwise it is set to \b PTHREAD_PROCESS_PRIVATE.
 */
os_result
os_rwlockInit (
    os_rwlock *rwlock, 
    const os_rwlockAttr *rwlockAttr)
{
    pthread_rwlockattr_t rwattr;
    int result;
    os_result rv;

    assert (rwlock != NULL);
    assert (rwlockAttr != NULL);
    pthread_rwlockattr_init (&rwattr);
    if (rwlockAttr->scopeAttr == OS_SCOPE_SHARED) {
        result = pthread_rwlockattr_setpshared (&rwattr, PTHREAD_PROCESS_SHARED);
    } else {
        result = pthread_rwlockattr_setpshared (&rwattr, PTHREAD_PROCESS_PRIVATE);
    }
    if (result == 0) {
        pthread_rwlock_init (rwlock, &rwattr);
    }
    pthread_rwlockattr_destroy (&rwattr);
    if (result == 0) {
	rv = os_resultSuccess;
    } else {
        rv = os_resultFail;
    }
    return rv;
}
Пример #3
0
Файл: heap.c Проект: kcyeu/xcb
/* The 'offset' parameter is optional, but must be provided to be able to use heap_remove().
 * If heap_remove() will not be used, then a negative value can be provided.
 */
heap_t heap_new(int height, ssize_t offset, int cmp(const void *x, const void *y)) {
	heap_t heap;
	pthread_mutexattr_t attr;
	pthread_rwlockattr_t rwattr;

	if (height <= 0)
		height = 8;
	if (cmp == NULL)
		return NULL;
	if (NEW(heap) == NULL)
		return NULL;
	heap->avail  = (height << 1) - 1;
	heap->curr   = 0;
	if ((heap->h = CALLOC(1, heap->avail * sizeof *heap->h)) == NULL) {
		FREE(heap);
		return NULL;
	}
	heap->offset = offset;
	heap->cmp    = cmp;
	pthread_mutexattr_init(&attr);
	pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP);
	pthread_mutex_init(&heap->lock, &attr);
	pthread_mutexattr_destroy(&attr);
	pthread_rwlockattr_init(&rwattr);
	pthread_rwlockattr_setkind_np(&rwattr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
	pthread_rwlock_init(&heap->rwlock, &rwattr);
	pthread_rwlockattr_destroy(&rwattr);
	return heap;
}
Пример #4
0
vp_lock_t *vp_lock_new(pthread_mutexattr_t *ma, pthread_condattr_t *ca)
{
  vp_lock_t *l;
#ifdef HAVE_PTHREAD_RWLOCKS
  pthread_rwlockattr_t attr;
#endif

  l = calloc(1,sizeof(*l));
  AN(l);
  l->magic = VP_LOCK_MAGIC;
#ifndef HAVE_PTHREAD_RWLOCKS
  l->r = l->w = l->nw = 0;
  l->t = vp_lock_unset;
  AZ(pthread_mutex_init(&l->m,ma));
  AZ(pthread_cond_init(&l->c,ca));
#else
  AZ(pthread_rwlockattr_init(&attr));
#ifdef HAVE_PTHREAD_RWLOCKATTR_SETPSHARED
  AZ(pthread_rwlockattr_setpshared(&attr,PTHREAD_PROCESS_PRIVATE));
#endif
  AZ(pthread_rwlock_init(&l->l,&attr));
  AZ(pthread_rwlockattr_destroy(&attr));
#endif
  return l;
}
Пример #5
0
celix_status_t producer_create(char* name, producer_pt* producer)
{
    celix_status_t status = CELIX_SUCCESS;

    producer_pt lclProducer = calloc(1, sizeof(*lclProducer));

    if (lclProducer != NULL) {

        lclProducer->name = strdup(name);
        lclProducer->utilizationStatsName = calloc(1, strlen(name) + strlen(THROUGHPUT_NAME_POSTFIX) + 1);

        if (lclProducer->name != NULL && lclProducer->utilizationStatsName != NULL) {
            pthread_rwlockattr_t queueLockAttr;

            sprintf(lclProducer->utilizationStatsName, "%s%s", lclProducer->name, (char*) THROUGHPUT_NAME_POSTFIX);

            pthread_rwlockattr_init(&queueLockAttr);
            pthread_rwlockattr_setkind_np(&queueLockAttr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
            pthread_rwlock_init(&lclProducer->queueLock, &queueLockAttr);

            lclProducer->queueServices = hashMap_create(utils_stringHash, NULL, utils_stringEquals, NULL);

            (*producer) = lclProducer;
        }
        else {
            status = CELIX_ENOMEM;
        }
    } else {
        status = CELIX_ENOMEM;
    }

    return status;
}
static uint32_t diameter_create_lock(diameter_lock_handle *lock)
{
	pthread_rwlockattr_t rwlock_attr;
	pthread_rwlock_t *rwlock;
	int ret;


	ret = pthread_rwlockattr_init(&rwlock_attr);
	if (ret)
		return DIAMETER_CB_ERROR;

	ret = pthread_rwlockattr_setkind_np(&rwlock_attr,
			PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
	if (ret)
		return DIAMETER_CB_ERROR;

	rwlock = nkn_malloc_type(sizeof(pthread_rwlock_t), mod_diameter_t);
	if (rwlock == NULL)
		return DIAMETER_CB_ERROR;

	ret = pthread_rwlock_init(rwlock, &rwlock_attr);
	if (ret) {
		free(rwlock);
		return DIAMETER_CB_ERROR;
	}

	*lock = rwlock;

	return DIAMETER_CB_OK;
}
Пример #7
0
int main()
{
	pthread_rwlockattr_t rwla;
	int pshared;
	int rc = 0;

	#ifndef PTHREAD_PROCESS_SHARED
	  printf("process-shared attribute is not available for testing\n");
	  return PTS_UNSUPPORTED;
	#endif

	/* Initialize a rwlock attributes object */
	if (pthread_rwlockattr_init(&rwla) != 0)
	{
		printf("Error at pthread_rwlockattr_init()\n");
		return PTS_UNRESOLVED;
	}

	 /* The default 'pshared' attribute should be PTHREAD_PROCESS_PRIVATE  */
	rc = pthread_rwlockattr_getpshared(&rwla, &pshared);
	if (rc != 0)
	{
		printf("Test FAILED: Error at pthread_rwlockattr_getpshared(): %d\n", rc);
		return PTS_FAIL;
	}

	if (pshared != PTHREAD_PROCESS_PRIVATE)
	{
		printf("Test FAILED: Incorrect default pshared value: %d\n", pshared);
		return PTS_FAIL;
	}

	printf("Test PASSED\n");
	return PTS_PASS;
}
Пример #8
0
Файл: 1-1.c Проект: kraj/ltp
int main(void)
{
	pthread_rwlockattr_t rwla;

	int rc;

	/* Initialize a rwlock attributes object */
	rc = pthread_rwlockattr_init(&rwla);
	if (rc != 0) {
		printf("Error at pthread_rwlockattr_init(), error code: %d\n",
		       rc);
		return PTS_UNRESOLVED;
	}

	/* Destroy the rwlock attributes object */
	rc = pthread_rwlockattr_destroy(&rwla);
	if (rc != 0) {
		printf
		    ("Error at pthread_rwlockattr_destroy(), error code: %d\n",
		     rc);
		printf("Test FAILED\n");
		return PTS_FAIL;
	}

	printf("Test PASSED\n");
	return PTS_PASS;
}
Пример #9
0
void
rw_init_flags(struct rwlock *rw, const char *name, int opts)
{
	pthread_rwlockattr_t attr;
	int flags;

	MPASS((opts & ~(RW_DUPOK | RW_NOPROFILE | RW_NOWITNESS | RW_QUIET |
	    RW_RECURSE)) == 0);
	ASSERT_ATOMIC_LOAD_PTR(rw->rw_lock,
	    ("%s: rw_lock not aligned for %s: %p", __func__, name,
	    &rw->rw_lock));

	flags = LO_UPGRADABLE;
	if (opts & RW_DUPOK)
		flags |= LO_DUPOK;
	if (opts & RW_NOPROFILE)
		flags |= LO_NOPROFILE;
	if (!(opts & RW_NOWITNESS))
		flags |= LO_WITNESS;
	if (opts & RW_RECURSE)
		flags |= LO_RECURSABLE;
	if (opts & RW_QUIET)
		flags |= LO_QUIET;

	lock_init(&rw->lock_object, &lock_class_rw, name, NULL, flags);
	pthread_rwlockattr_init(&attr);
	pthread_rwlock_init(&rw->rw_lock, &attr);
}
Пример #10
0
int __ast_rwlock_init(int tracking, const char *filename, int lineno, const char *func, const char *rwlock_name, ast_rwlock_t *t)
{
	int res;
	pthread_rwlockattr_t attr;

#ifdef DEBUG_THREADS

#if defined(AST_MUTEX_INIT_W_CONSTRUCTORS) && defined(CAN_COMPARE_MUTEX_TO_INIT_VALUE)
	int canlog = strcmp(filename, "logger.c") & t->tracking;

	if (t->lock != ((pthread_rwlock_t) __AST_RWLOCK_INIT_VALUE)) {
		__ast_mutex_logger("%s line %d (%s): Warning: rwlock '%s' is already initialized.\n",
				filename, lineno, func, rwlock_name);
		return 0;
	}
#endif /* AST_MUTEX_INIT_W_CONSTRUCTORS */

	if ((t->tracking = tracking)) {
		ast_reentrancy_init(&t->track);
	}
#endif /* DEBUG_THREADS */

	pthread_rwlockattr_init(&attr);

#ifdef HAVE_PTHREAD_RWLOCK_PREFER_WRITER_NP
	pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NP);
#endif

	res = pthread_rwlock_init(&t->lock, &attr);
	pthread_rwlockattr_destroy(&attr);
	return res;
}
Пример #11
0
int main()
{
	pthread_rwlockattr_t rwa;
	pthread_rwlock_t rwl1, rwl2;
	int rc;

	/* Initialize a read-write lock attributes object */
	rc = pthread_rwlockattr_init(&rwa);
	if (rc != 0)
	{
		printf("Error at pthread_rwlockattr_init(), returns %d\n", rc);
		return PTS_UNRESOLVED;
	}

	if ((rc = pthread_rwlock_init(&rwl1, &rwa)) != 0)
	{
		printf("Error at 1st pthread_rwlock_init()\n");
		return PTS_UNRESOLVED;
	}

	if ((rc = pthread_rwlock_init(&rwl2, &rwa)) != 0)
	{
		printf("Error at 2nd pthread_rwlock_init()\n");
		return PTS_UNRESOLVED;
	}

	if ((rc = pthread_rwlock_rdlock(&rwl1)) != 0)
	{
		printf("Error at pthread_rwlock_rdlock(): rwl1\n");
		return PTS_UNRESOLVED;
	}

	if ((rc = pthread_rwlockattr_destroy(&rwa)) != 0)
	{
		printf("Error at pthread_rwlockattr_destory()\n");
		return PTS_UNRESOLVED;
	}
	if ((rc = pthread_rwlock_unlock(&rwl1)) != 0)
	{
		printf("Error at pthread_rwlock_unlock: rwl1\n");
		printf("Test Failed.\n");
		return PTS_FAIL;
	}

	if ((rc = pthread_rwlock_rdlock(&rwl2)) != 0)
	{
		printf("Error at pthread_rwlock_rdlock():rwl2\n");
		printf("Test Failed.\n");
		return PTS_FAIL;
	}
	if ((rc = pthread_rwlock_unlock(&rwl2)) != 0)
	{
		printf("Error at pthread_rwlock_unlock:rwl2\n");
		printf("Test Failed.\n");
		return PTS_FAIL;
	}

	printf("Test PASSED\n");
	return PTS_PASS;
}
Пример #12
0
cRwLock::cRwLock(bool PreferWriter)
{
    pthread_rwlockattr_t attr;
    pthread_rwlockattr_init(&attr);
    pthread_rwlockattr_setkind_np(&attr, PreferWriter ? PTHREAD_RWLOCK_PREFER_WRITER_NP : PTHREAD_RWLOCK_PREFER_READER_NP);
    pthread_rwlock_init(&rwlock, &attr);
}
Пример #13
0
static void
noit_log_init_rwlock(noit_log_stream_t ls) {
  pthread_rwlockattr_t attr;
  pthread_rwlockattr_init(&attr);
  pthread_rwlockattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
  pthread_rwlock_init(ls->lock, &attr);
}
Пример #14
0
int get_thread_attr()
{
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    print_thread_attr(&attr);
    pthread_attr_destroy(&attr);
    printf("\n");

    pthread_mutexattr_t mutex_attr;
    pthread_mutexattr_init(&mutex_attr);
    print_mutexattr(&mutex_attr);
    pthread_mutexattr_destroy(&mutex_attr);
    printf("\n");

    pthread_rwlockattr_t rwlock_attr;
    pthread_rwlockattr_init(&rwlock_attr);
    print_rwlockattr(&rwlock_attr);
    pthread_rwlockattr_destroy(&rwlock_attr);
    printf("\n");

    pthread_condattr_t cond_attr;
    pthread_condattr_init(&cond_attr);
    print_condattr(&cond_attr);
    pthread_condattr_destroy(&cond_attr);
    printf("\n");

    return THREAD_ATTR_OK;
}
Пример #15
0
struct uwsgi_lock_item *uwsgi_rwlock_fast_init(char *id) {

#ifdef OBSOLETE_LINUX_KERNEL
	return uwsgi_lock_fast_init(id);
#else

	pthread_rwlockattr_t attr;

	struct uwsgi_lock_item *uli = uwsgi_register_lock(id, 1);

	if (pthread_rwlockattr_init(&attr)) {
		uwsgi_log("unable to allocate rwlock structure\n");
		exit(1);
	}

	if (pthread_rwlockattr_setpshared(&attr, PTHREAD_PROCESS_SHARED)) {
		uwsgi_log("unable to share rwlock\n");
		exit(1);
	}

	if (pthread_rwlock_init((pthread_rwlock_t *) uli->lock_ptr, &attr)) {
		uwsgi_log("unable to initialize rwlock\n");
		exit(1);
	}

	pthread_rwlockattr_destroy(&attr);

	uli->can_deadlock = 1;

	return uli;
#endif



}
Пример #16
0
CRWLock::CRWLock()
{
    pthread_rwlockattr_t rwlock_attr;
    int res;

    if((res = pthread_rwlockattr_init(&rwlock_attr)) != 0)
    {
        printf("file: "__FILE__", line: %d, " \
               "call pthread_rwlockattr_init failed, err: %s\n", \
               __LINE__, strerror(res));
        throw res;
    }

    if((res = pthread_rwlockattr_setpshared(&rwlock_attr, \
                                            PTHREAD_PROCESS_SHARED)) != 0)
    {
        printf("file: "__FILE__", line: %d, " \
               "set rwlock attr to PTHREAD_PROCESS_SHARED failed, err: %s\n", \
               __LINE__, strerror(res));
        throw res;
    }

    if((res = pthread_rwlock_init(&m_rwlock, &rwlock_attr)) != 0)
    {
        printf("file: "__FILE__", line: %d, " \
               "call pthread_rwlock_init failed, err: %s\n", \
               __LINE__, strerror(res));
        throw res;
    }
    pthread_rwlockattr_destroy(&rwlock_attr);
}
Пример #17
0
void test_rwlock_init(void) {
  int rc;
  pthread_rwlockattr_t attrs;
  rc = pthread_rwlockattr_init(&attrs);
  ASSERT_EQ(rc, 0);
  int shared = -1;
  rc = pthread_rwlockattr_getpshared(&attrs, &shared);
  ASSERT_EQ(rc, 0);
  ASSERT_EQ(shared, PTHREAD_PROCESS_PRIVATE);
  rc = pthread_rwlockattr_setpshared(&attrs, PTHREAD_PROCESS_SHARED);
#ifndef __GLIBC__
  /*
   * glibc's pthread_rwlockattr_setpshared doesn't currently fail
   * with PTHREAD_PROCESS_SHARED. TODO(sbc): remove this once
   * we fix the glibc bug:
   * https://code.google.com/p/nativeclient/issues/detail?id=4142
   */
  ASSERT_EQ(rc, EINVAL);
#endif
  rc = pthread_rwlockattr_setpshared(&attrs, PTHREAD_PROCESS_PRIVATE);
  ASSERT_EQ(rc, 0);
  rc = pthread_rwlock_init(&g_rwlock, &attrs);
  ASSERT_EQ(rc, 0);
  rc = pthread_rwlockattr_destroy(&attrs);
  ASSERT_EQ(rc, 0);
}
Пример #18
0
int main() {
    {
        pthread_mutexattr_t attr;
        if (pthread_mutexattr_init(&attr) != 0) {
            abort();
        }
        if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL) != 0) {
            abort();
        }
        if (pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE) != 0) {
            abort();
        }
    }

    {
        pthread_rwlockattr_t attr;
        if (pthread_rwlockattr_init(&attr) != 0) {
            abort();
        }
        if (pthread_rwlockattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE) != 0) {
            abort();
        }
    }

    // TODO(hamaji): PTHREAD_MUTEX_INITIALIZER has a different implementation...
#if 0
    if (pthread_mutex_lock(&g_mutex) != 0) {
        abort();
    }
#endif
}
/**
 * @brief Initialize the package.
 */
void cih_pkginit(void)
{
	pthread_rwlockattr_t rwlock_attr;
	cih_partition_t *cp;
	uint32_t npart;
	uint32_t cache_sz = 32767; /* XXX */
	int ix;

	/* avoid writer starvation */
	pthread_rwlockattr_init(&rwlock_attr);
#ifdef GLIBC
	pthread_rwlockattr_setkind_np(
	&rwlock_attr,
        PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
#endif
	npart = nfs_param.cache_param.nparts;
	cih_fhcache.npart = npart;
	cih_fhcache.partition = gsh_calloc(npart, sizeof(cih_partition_t));
	for (ix = 0; ix < npart; ++ix) {
		cp = &cih_fhcache.partition[ix];
                cp->part_ix = ix;
		pthread_rwlock_init(&cp->lock, &rwlock_attr);
		avltree_init(&cp->t, cih_fh_cmpf, 0 /* must be 0 */);
		cih_fhcache.cache_sz = cache_sz;
		cp->cache = gsh_calloc(cache_sz, sizeof(struct avltree_node *));
        }
	initialized = true;
}
Пример #20
0
Файл: 3-1.c Проект: 8l/rose
int main()
{
	int cnt = 0;
	pthread_rwlockattr_t rwlockattr;	

	if(pthread_rwlockattr_init(&rwlockattr) != 0)
	{
		printf("Error at pthread_rwlockattr_init()\n");
		return PTS_UNRESOLVED;
	}
	
	if(pthread_rwlock_init(&rwlock, &rwlockattr) != 0)
	{
		printf("Test FAILED: Error in pthread_rwlock_init()\n");
		return PTS_FAIL;
	}
	
	while(cnt++ < COUNT)
	{
		if(pthread_rwlock_rdlock(&rwlock) != 0)
		{
			printf("Test FAILED: cannot get read lock on %dth loop\n", cnt);
			return PTS_FAIL;
		}
		
		if(pthread_rwlock_unlock(&rwlock) != 0)
		{
			printf("Test FAILED: cannot release read lock on %dth loop\n", cnt);
			return PTS_FAIL;
		}
		
		if(pthread_rwlock_wrlock(&rwlock) != 0)
		{
			printf("Test FAILED: cannot get write lock on %dth loop\n", cnt);
			return PTS_FAIL;
		}
		
		if(pthread_rwlock_unlock(&rwlock) != 0)
		{
			printf("Test FAILED: cannot release write lock on %dth loop\n", cnt);
			return PTS_FAIL;
		}
	}	
	
	if(pthread_rwlock_destroy(&rwlock) != 0)
	{
		printf("Error at pthread_rwlockattr_destroy()\n");
		return PTS_UNRESOLVED;
	}	

	if(pthread_rwlockattr_destroy(&rwlockattr) != 0)
	{
		printf("Error at pthread_rwlockattr_destroy()\n");
		return PTS_UNRESOLVED;		
	}
	
	printf("Test PASSED\n");
	return PTS_PASS;
}
Пример #21
0
void InitTransactionLock(void)
{
	pthread_rwlockattr_t attr;
	pthread_rwlockattr_init(&attr);
	pthread_rwlockattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
	pthread_rwlock_init(&CommitProcArrayLock, &attr);
	pthread_rwlock_init(&ProcCommitLock, &attr);
}
Пример #22
0
static pthread_rwlock_t* hybris_alloc_init_rwlock(void)
{
    pthread_rwlock_t *realrwlock = malloc(sizeof(pthread_rwlock_t));
    pthread_rwlockattr_t attr;
    pthread_rwlockattr_init(&attr);
    pthread_rwlock_init(realrwlock, &attr);
    return realrwlock;
}
Пример #23
0
RWLock::RWLock(string name_): rwname(name_)
{
	pthread_rwlockattr_t attr;

	pthread_rwlockattr_init(&attr);
	pthread_rwlock_init(&rwlock, &attr);
	pthread_rwlockattr_destroy(&attr);
}
Пример #24
0
static int my_pthread_rwlockattr_init(pthread_rwlockattr_t *__attr)
{
    pthread_rwlockattr_t *realattr;

    realattr = malloc(sizeof(pthread_rwlockattr_t));
    *((unsigned int *)__attr) = (unsigned int) realattr;

    return pthread_rwlockattr_init(realattr);
}
Пример #25
0
CRWMutex::CRWMutex()
{
#ifndef ANDROID
   pthread_rwlockattr_init(&mAttr);
   pthread_rwlock_init(&mMutex, &mAttr);
#else
   rwlock_init(&mMutex);
#endif //ANDROID
}
Пример #26
0
CRWLock::CRWLock()
{
    pthread_rwlockattr_t attr;
    pthread_rwlockattr_init( &attr );
    //写锁优先,一旦有写锁去请求,后续所有的读锁请求都会阻塞,即使已经有线程获取了读锁也如此
    pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
    pthread_rwlock_init(&_rwlock,&attr);
    pthread_rwlockattr_destroy(&attr);
}
Пример #27
0
Файл: init.c Проект: Fyleo/rtems
void *POSIX_Init(
  void *argument
)
{
  int                   status;
  pthread_t             threadId;
  benchmark_timer_t end_time;
  pthread_rwlockattr_t  attr;

  TEST_BEGIN();

  status = pthread_create( &threadId, NULL, Blocker, NULL );
  rtems_test_assert( status == 0 );
  /*
   * Deliberately create the rwlock after the threads.  This way if the
   * threads do run before we intend, they will get an error.
   */

  /* creating rwlock */
    status = pthread_rwlockattr_init( &attr );
  rtems_test_assert( status == 0 );
  status = pthread_rwlock_init( &rwlock, &attr );
  rtems_test_assert( status == 0 );

  /*
   * Ensure the rwlock is unavailable so the other threads block.
   */
  /* lock rwlock to ensure thread blocks */
  status = pthread_rwlock_wrlock(&rwlock);
  rtems_test_assert( status == 0 );

  /*
   * Let the other thread start so the thread startup overhead,
   * is accounted for.  When we return, we can start the benchmark.
   */
  sched_yield();
    /* let other thread run */

  benchmark_timer_initialize();
    status = pthread_rwlock_unlock(&rwlock); /*  unlock the rwlock */
  end_time = benchmark_timer_read();
  rtems_test_assert( status == 0 );

  put_time(
    "pthread_rwlock_unlock: thread waiting no preempt",
    end_time,
    1,
    0,
    0
  );

  TEST_END();
  rtems_test_exit( 0 );

  return NULL;
}
Пример #28
0
ReadWriteLock::ReadWriteLock()
{
    pthread_rwlockattr_t myAttributes;
    pthread_rwlockattr_init(&myAttributes);
    pthread_rwlockattr_setkind_np(&myAttributes,PTHREAD_RWLOCK_PREFER_READER_NP);
    int myStatus = pthread_rwlock_init(&_myLock,&myAttributes);
    if (myStatus != 0) {
        throw LockInitFailed(strerror(myStatus),PLUS_FILE_LINE);
    }
}
Пример #29
0
guac_client* guac_client_alloc() {

    int i;
    pthread_rwlockattr_t lock_attributes;

    /* Allocate new client */
    guac_client* client = malloc(sizeof(guac_client));
    if (client == NULL) {
        guac_error = GUAC_STATUS_NO_MEMORY;
        guac_error_message = "Could not allocate memory for client";
        return NULL;
    }

    /* Init new client */
    memset(client, 0, sizeof(guac_client));

    client->args = __GUAC_CLIENT_NO_ARGS;
    client->state = GUAC_CLIENT_RUNNING;
    client->last_sent_timestamp = guac_timestamp_current();

    /* Generate ID */
    client->connection_id = guac_generate_id(GUAC_CLIENT_ID_PREFIX);
    if (client->connection_id == NULL) {
        free(client);
        return NULL;
    }

    /* Allocate buffer and layer pools */
    client->__buffer_pool = guac_pool_alloc(GUAC_BUFFER_POOL_INITIAL_SIZE);
    client->__layer_pool = guac_pool_alloc(GUAC_BUFFER_POOL_INITIAL_SIZE);

    /* Allocate stream pool */
    client->__stream_pool = guac_pool_alloc(0);

    /* Initialize streams */
    client->__output_streams = malloc(sizeof(guac_stream) * GUAC_CLIENT_MAX_STREAMS);

    for (i=0; i<GUAC_CLIENT_MAX_STREAMS; i++) {
        client->__output_streams[i].index = GUAC_CLIENT_CLOSED_STREAM_INDEX;
    }


    /* Init locks */
    pthread_rwlockattr_init(&lock_attributes);
    pthread_rwlockattr_setpshared(&lock_attributes, PTHREAD_PROCESS_SHARED);

    pthread_rwlock_init(&(client->__users_lock), &lock_attributes);

    /* Set up socket to broadcast to all users */
    client->socket = guac_socket_broadcast(client);

    return client;

}
Пример #30
0
rw_mutex::impl::impl()
  :valid(false)
{
  pthread_rwlockattr_t attr;
  pthread_rwlockattr_init(&attr);
#ifdef PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP
  // switch to writer-preferred lock on linux
  pthread_rwlockattr_setkind_np(&attr,
                                PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
#endif
  int res=pthread_rwlock_init(&lk,&attr);
  if (res==0) valid=true;
}