Ejemplo n.º 1
0
static void
AppLock(XtAppContext app)
{
    LockPtr app_lock = app->lock_info;
#ifdef _XMUTEX_NESTS
    xmutex_lock(app_lock->mutex);
    app_lock->level++;
#else
    xthread_t self = xthread_self();
    xmutex_lock(app_lock->mutex);
    if (!xthread_have_id(app_lock->holder)) {
	app_lock->holder = self;
    	assert(xthread_equal(app_lock->holder, self));
	xmutex_unlock(app_lock->mutex);
	return;
    }
    if (xthread_equal(app_lock->holder, self)) {
	app_lock->level++;
	xmutex_unlock(app_lock->mutex);
	return;
    }
    while(xthread_have_id(app_lock->holder)) {
	xcondition_wait(app_lock->cond, app_lock->mutex);
    }
    app_lock->holder = self;
    assert(xthread_equal(app_lock->holder, self));
    xmutex_unlock(app_lock->mutex);
#endif
}
Ejemplo n.º 2
0
static void
ProcessLock(void)
{
#ifdef _XMUTEX_NESTS
    xmutex_lock(process_lock->mutex);
    process_lock->level++;
#else
    xthread_t this_thread = xthread_self();

    xmutex_lock(process_lock->mutex);

    if (!xthread_have_id(process_lock->holder)) {
	process_lock->holder = this_thread;
	xmutex_unlock(process_lock->mutex);
	return;
    }

    if (xthread_equal(process_lock->holder,this_thread)) {
	process_lock->level++;
	xmutex_unlock(process_lock->mutex);
	return;
    }

    while(xthread_have_id(process_lock->holder))
	xcondition_wait(process_lock->cond, process_lock->mutex);

    process_lock->holder = this_thread;
    assert(xthread_equal(process_lock->holder, this_thread));
    xmutex_unlock(process_lock->mutex);
#endif
}
Ejemplo n.º 3
0
static void
YieldAppLock(
    XtAppContext app,
    Boolean* push_thread,
    Boolean* pushed_thread,
    int* level)
{
    LockPtr app_lock = app->lock_info;
    xthread_t self = xthread_self();
#ifndef _XMUTEX_NESTS
    xmutex_lock(app_lock->mutex);
    assert(xthread_equal(app_lock->holder, self));
#endif
    *level = app_lock->level;
    if (*push_thread) {
	*push_thread = FALSE;
	*pushed_thread = TRUE;

	if(app_lock->stack.sp == (int)app_lock->stack.size - 1) {
	    unsigned ii;
	    app_lock->stack.st = (struct _Tstack *)
		XtRealloc ((char *)app_lock->stack.st,
		(app_lock->stack.size + STACK_INCR) * sizeof (struct _Tstack));
	    ii = app_lock->stack.size;
	    app_lock->stack.size += STACK_INCR;
	    for ( ; ii < app_lock->stack.size; ii++) {
		app_lock->stack.st[ii].c = xcondition_malloc();
		xcondition_init(app_lock->stack.st[ii].c);
	    }
	}
	app_lock->stack.st[++(app_lock->stack.sp)].t = self;
    }
#ifdef _XMUTEX_NESTS
    while (app_lock->level > 0) {
	app_lock->level--;
	xmutex_unlock(app_lock->mutex);
    }
#else
    xcondition_signal(app_lock->cond);
    app_lock->level = 0;
    xthread_clear_id(app_lock->holder);
    xmutex_unlock(app_lock->mutex);
#endif
}
Ejemplo n.º 4
0
static void
ProcessUnlock(void)
{
#ifdef _XMUTEX_NESTS
    process_lock->level--;
    xmutex_unlock(process_lock->mutex);
#else
    xmutex_lock(process_lock->mutex);
    assert(xthread_equal(process_lock->holder, xthread_self()));
    if (process_lock->level != 0) {
	process_lock->level--;
	xmutex_unlock(process_lock->mutex);
	return;
    }

    xthread_clear_id(process_lock->holder);
    xcondition_signal(process_lock->cond);

    xmutex_unlock(process_lock->mutex);
#endif
}
Ejemplo n.º 5
0
static void
RestoreAppLock(
    XtAppContext app,
    int level,
    Boolean* pushed_thread)
{
    LockPtr app_lock = app->lock_info;
    xthread_t self = xthread_self();
    xmutex_lock(app_lock->mutex);
#ifdef _XMUTEX_NESTS
    app_lock->level++;
#else
    while(xthread_have_id(app_lock->holder)) {
	xcondition_wait(app_lock->cond, app_lock->mutex);
    }
#endif
    if (!xthread_equal(app_lock->stack.st[app_lock->stack.sp].t, self)) {
	int ii;
	for (ii = app_lock->stack.sp - 1; ii >= 0; ii--) {
	    if (xthread_equal(app_lock->stack.st[ii].t, self)) {
		xcondition_wait(app_lock->stack.st[ii].c, app_lock->mutex);
		break;
	    }
	}
#ifndef _XMUTEX_NESTS
	while(xthread_have_id(app_lock->holder)) {
	    xcondition_wait(app_lock->cond, app_lock->mutex);
	}
#endif
    }
#ifdef _XMUTEX_NESTS
    while (app_lock->level < level) {
	xmutex_lock(app_lock->mutex);
	app_lock->level++;
    }
#else
    app_lock->holder = self;
    app_lock->level = level;
    assert(xthread_equal(app_lock->holder, self));
#endif
    if (*pushed_thread) {
	*pushed_thread = FALSE;
	(app_lock->stack.sp)--;
	if (app_lock->stack.sp >= 0) {
	    xcondition_signal (app_lock->stack.st[app_lock->stack.sp].c);
	}
    }
#ifndef _XMUTEX_NESTS
    xmutex_unlock(app_lock->mutex);
#endif
}
Ejemplo n.º 6
0
static void
AppUnlock(XtAppContext app)
{
    LockPtr app_lock = app->lock_info;
#ifdef _XMUTEX_NESTS
    app_lock->level--;
    xmutex_unlock(app_lock->mutex);
#else
    xthread_t self;

    self = xthread_self();
    xmutex_lock(app_lock->mutex);
    assert(xthread_equal(app_lock->holder, self));
    if (app_lock->level != 0) {
	app_lock->level--;
	xmutex_unlock(app_lock->mutex);
	return;
    }
    xthread_clear_id(app_lock->holder);
    xcondition_signal(app_lock->cond);
    xmutex_unlock(app_lock->mutex);
#endif
}
Ejemplo n.º 7
0
void		display(t_philo *philo, char *action, int nb_fork)
{
  int		i;

  xmutex_lock(&philo->table->lock);
  printf("\033[%d;0H\033[2K", philo->pos + 1);
  printf("[%2d] [\033[0;36m%5s\033[0;0m] [Eated %d time(s)]",
	 philo->pos + 1,
	 action,
	 philo->nb_eat);
  i = 0;
  while (i < nb_fork)
    printf(" [chopstick %d]", philo->fork[i++] + 1);
  printf("\n");
  xmutex_unlock(&philo->table->lock);
}