Ejemplo n.º 1
0
int
gp_monitor_enter(gp_monitor * mona)
{
    pthread_mutex_t * const mon = (pthread_mutex_t *)mona;
    int scode;

#ifdef GS_RECURSIVE_MUTEXATTR
    scode = pthread_mutex_lock(mon);
#else
    assert(((gp_pthread_recursive_t *)mona)->lcount >= 0);

    if ((scode = pthread_mutex_trylock(mon)) == 0) {
        ((gp_pthread_recursive_t *)mona)->self_id = pthread_self();
        ((gp_pthread_recursive_t *)mona)->lcount++;
    } else {
        if (pthread_equal(pthread_self(),((gp_pthread_recursive_t *)mona)->self_id)) {
            ((gp_pthread_recursive_t *)mona)->lcount++;
            scode = 0;
        }
        else {
            /* we were not the owner, wait */
            scode = pthread_mutex_lock(mon);
            ((gp_pthread_recursive_t *)mona)->self_id = pthread_self();
            ((gp_pthread_recursive_t *)mona)->lcount++;
        }
    }
#endif
    return SEM_ERROR_CODE(scode);
}
Ejemplo n.º 2
0
int
gp_monitor_open(gp_monitor * mona)
{
    pthread_mutex_t *mon;
    int scode;
    pthread_mutexattr_t attr;
    pthread_mutexattr_t *attrp = NULL;

    if (!mona)
        return -1;		/* monitors are not movable */


#ifdef GS_RECURSIVE_MUTEXATTR
    attrp = &attr;
    scode = pthread_mutexattr_init(attrp);
    if (scode < 0) goto done;

    scode = pthread_mutexattr_settype(attrp, GS_RECURSIVE_MUTEXATTR);
    if (scode < 0) {
        goto done;
    }
#else     
    ((gp_pthread_recursive_t *)mona)->self_id = 0;	/* Not valid unless mutex is locked */
    ((gp_pthread_recursive_t *)mona)->lcount = 0;
#endif

    mon = &((gp_pthread_recursive_t *)mona)->mutex;
    scode = pthread_mutex_init(mon, attrp);
    if (attrp)
        (void)pthread_mutexattr_destroy(attrp);
done:
    return SEM_ERROR_CODE(scode);
}
Ejemplo n.º 3
0
int
gp_semaphore_signal(gp_semaphore * sema)
{
    pt_semaphore_t * const sem = (pt_semaphore_t *)sema;
    int scode, scode2;

    scode = pthread_mutex_lock(&sem->mutex);
    if (scode != 0)
        return SEM_ERROR_CODE(scode);
    if (sem->count++ == 0)
        scode = pthread_cond_signal(&sem->cond);
    scode2 = pthread_mutex_unlock(&sem->mutex);
    if (scode == 0)
        scode = scode2;
    return SEM_ERROR_CODE(scode);
}
Ejemplo n.º 4
0
int
gp_monitor_close(gp_monitor * mona)
{
    pthread_mutex_t * const mon = &((gp_pthread_recursive_t *)mona)->mutex;
    int scode;

    scode = pthread_mutex_destroy(mon);
    return SEM_ERROR_CODE(scode);
}
Ejemplo n.º 5
0
int
gp_monitor_leave(gp_monitor * mona)
{
    pthread_mutex_t * const mon = (pthread_mutex_t *)mona;
    int scode;

    scode = pthread_mutex_unlock(mon);
    ((gp_pthread_recursive_t *)mona)->self_id = 0;	/* Not valid unless mutex is locked */
    return SEM_ERROR_CODE(scode);
}
Ejemplo n.º 6
0
int
gp_monitor_enter(gp_monitor * mona)
{
    pthread_mutex_t * const mon = (pthread_mutex_t *)mona;
    int scode;

    if ((scode = pthread_mutex_trylock(mon)) == 0) {
	((gp_pthread_recursive_t *)mona)->self_id = pthread_self();
	return SEM_ERROR_CODE(scode);
    } else {
	if (pthread_equal(pthread_self(),((gp_pthread_recursive_t *)mona)->self_id))
	    return 0;
	else {
	    /* we were not the owner, wait */
	    scode = pthread_mutex_lock(mon);
	    ((gp_pthread_recursive_t *)mona)->self_id = pthread_self();
	    return SEM_ERROR_CODE(scode);
	}
    }
}
Ejemplo n.º 7
0
int
gp_semaphore_wait(gp_semaphore * sema)
{
    pt_semaphore_t * const sem = (pt_semaphore_t *)sema;
    int scode, scode2;

    scode = pthread_mutex_lock(&sem->mutex);
    if (scode != 0)
	return SEM_ERROR_CODE(scode);
    while (sem->count == 0) {
        scode = pthread_cond_wait(&sem->cond, &sem->mutex);
        if (scode != 0)
	    break;
    }
    if (scode == 0)
	--sem->count;
    scode2 = pthread_mutex_unlock(&sem->mutex);
    if (scode == 0)
	scode = scode2;
    return SEM_ERROR_CODE(scode);
}
Ejemplo n.º 8
0
int
gp_semaphore_close(gp_semaphore * sema)
{
    pt_semaphore_t * const sem = (pt_semaphore_t *)sema;
    int scode, scode2;

    scode = pthread_cond_destroy(&sem->cond);
    scode2 = pthread_mutex_destroy(&sem->mutex);
    if (scode == 0)
	scode = scode2;
    return SEM_ERROR_CODE(scode);
}
Ejemplo n.º 9
0
int
gp_monitor_open(gp_monitor * mona)
{
    pthread_mutex_t * const mon = &((gp_pthread_recursive_t *)mona)->mutex;
    int scode;

    if (!mona)
	return -1;		/* monitors are not movable */
    ((gp_pthread_recursive_t *)mona)->self_id = 0;	/* Not valid unless mutex is locked */
    scode = pthread_mutex_init(mon, NULL);
    return SEM_ERROR_CODE(scode);
}
Ejemplo n.º 10
0
int
gp_semaphore_open(gp_semaphore * sema)
{
    pt_semaphore_t * const sem = (pt_semaphore_t *)sema;
    int scode;

    if (!sema)
	return -1;		/* semaphores are not movable */
    sem->count = 0;
    scode = pthread_mutex_init(&sem->mutex, NULL);
    if (scode == 0)
	scode = pthread_cond_init(&sem->cond, NULL);
    return SEM_ERROR_CODE(scode);
}
Ejemplo n.º 11
0
int
gp_monitor_leave(gp_monitor * mona)
{
    pthread_mutex_t * const mon = (pthread_mutex_t *)mona;
    int scode = 0;

#ifdef GS_RECURSIVE_MUTEXATTR
    scode = pthread_mutex_unlock(mon);
#else
    assert(((gp_pthread_recursive_t *)mona)->lcount > 0 && ((gp_pthread_recursive_t *)mona)->self_id != 0);

    if (pthread_equal(pthread_self(),((gp_pthread_recursive_t *)mona)->self_id)) {
      if ((--((gp_pthread_recursive_t *)mona)->lcount) == 0) {
          ((gp_pthread_recursive_t *)mona)->self_id = 0;	/* Not valid unless mutex is locked */
          scode = pthread_mutex_unlock(mon);

      }
    }
    else {
        scode = -1 /* should be EPERM */;
    }
#endif
    return SEM_ERROR_CODE(scode);
}