Ejemplo n.º 1
0
Thread *waitSetSignalNext(Monitor *mon) {
    Thread *thread = mon->wait_set;

    if(thread != NULL) {
        waitSetUnlinkThread(mon, thread);
        pthread_cond_signal(&thread->wait_cv);

        thread->notify_id = mon->wait_count;
    }

    return thread;
}
Ejemplo n.º 2
0
int monitorWait0(Monitor *mon, Thread *self, long long ms, int ns,
                 int blocked, int interruptible) {
    char timed = (ms != 0) || (ns != 0);
    char interrupted = FALSE;
    char timeout = FALSE;
    struct timespec ts;
    int old_count;

    /* Check we own the monitor */
    if(mon->owner != self)
        return FALSE;

    disableSuspend(self);

    /* Unlock the monitor.  As it could be recursively
       locked remember the recursion count */
    old_count = mon->count;
    mon->owner = NULL;
    mon->count = 0;

    /* Counter used in thin-lock deflation */
    mon->in_wait++;

    self->wait_mon = mon;

    if(timed) {
       getTimeoutRelative(&ts, ms, ns);
       self->state = TIMED_WAITING;
    } else
       self->state = blocked ? BLOCKED : WAITING;

    if(interruptible && self->interrupted)
        interrupted = TRUE;
    else {
        if(blocked) {
            self->blocked_mon = mon;
            self->blocked_count++;
        } else
            self->waited_count++;

        self->interrupting = FALSE;

        /* Add the thread onto the end of the wait set */
        waitSetAppend(mon, self);

        while(self->wait_next != NULL && !self->interrupting && !timeout)
            if(timed) {
                timeout = pthread_cond_timedwait(&self->wait_cv,
                              &mon->lock, &ts) == ETIMEDOUT;

                /* On Linux/i386 systems using LinuxThreads,
                   pthread_cond_timedwait is implemented using
                   sigjmp/longjmp.  This resets the fpu control
                   word back to 64-bit precision.  The macro is
                   empty for sane platforms. */ 

                FPU_HACK;
            } else
                pthread_cond_wait(&self->wait_cv, &mon->lock);
    }

    /* If we've been interrupted or timed-out, we will not have been
       removed from the wait set.  If we have, we must have been
       notified afterwards.  In this case, the notify has been lost,
       and we must signal another thread */

    if(self->interrupting || timeout) {
        /* An interrupt after a timeout remains pending */
        interrupted = interruptible && !timeout;

        if(self->wait_next != NULL)
            waitSetUnlinkThread(mon, self);
        else {
            /* Notify lost.  Signal another thread only if it
               was on the wait set at the time of the notify */
            if(mon->wait_set != NULL && mon->wait_set->wait_id <
                                                self->notify_id) {
                Thread *thread = waitSetSignalNext(mon);
                thread->notify_id = self->notify_id;
            }
        }
    }

    self->state = RUNNING;
    self->wait_mon = NULL;

    if(blocked)
        self->blocked_mon = NULL;

   /* Restore the monitor owner and recursion count */
    mon->count = old_count;
    mon->owner = self;
    mon->in_wait--;

    enableSuspend(self);

    if(interrupted) {
        self->interrupted = FALSE;
        signalException(java_lang_InterruptedException, NULL);
    }

    return TRUE;
}
Ejemplo n.º 3
0
Archivo: lock.c Proyecto: webos21/xi
int monitorWait0(Monitor *mon, Thread *self, long long ms, int ns, int blocked,
		int interruptible) {
	char timed = (ms != 0) || (ns != 0);
	char interrupted = FALSE;
	char timeout = FALSE;
	int ts = 0;
	int old_count = 0;

	// Check we own the monitor
	if (mon->owner != self)
		return FALSE;

	disableSuspend(self);

	// Unlock the monitor.  As it could be recursively
	// locked remember the recursion count
	old_count = mon->count;
	mon->owner = NULL;
	mon->count = 0;

	// Counter used in thin-lock deflation
	mon->in_wait++;

	self->wait_mon = mon;

	if (timed) {
		ts = getTimeoutRelative(ms, ns);
		self->state = TIMED_WAITING;
	} else {
		self->state = blocked ? BLOCKED : WAITING;
	}

	if (interruptible && self->interrupted) {
		interrupted = TRUE;
	} else {
		if (blocked) {
			self->blocked_mon = mon;
			self->blocked_count++;
		} else {
			self->waited_count++;
		}

		self->interrupting = FALSE;

		// Add the thread onto the end of the wait set
		waitSetAppend(mon, self);

		while (self->wait_next != NULL && !self->interrupting && !timeout)
			if (timed) {
				timeout = xi_thread_cond_timedwait(&self->wait_cv, &mon->lock,
						ts) == XI_COND_RV_ERR_TIMEOUT;

				// On Linux/i386 systems using LinuxThreads,
				// pthread_cond_timedwait is implemented using
				// sigjmp/longjmp.  This resets the fpu control
				// word back to 64-bit precision.  The macro is
				// empty for sane platforms.
				FPU_HACK;
			} else {
				xi_thread_cond_wait(&self->wait_cv, &mon->lock);
			}
	}

	// If we've been interrupted or timed-out, we will not have been
	// removed from the wait set.  If we have, we must have been
	// notified afterwards.  In this case, the notify has been lost,
	// and we must signal another thread

	if (self->interrupting || timeout) {
		// An interrupt after a timeout remains pending
		interrupted = interruptible && !timeout;

		if (self->wait_next != NULL) {
			waitSetUnlinkThread(mon, self);
		} else {
			// Notify lost.  Signal another thread only if it
			// was on the wait set at the time of the notify
			if (mon->wait_set != NULL && mon->wait_set->wait_id
					< self->notify_id) {
				Thread *thread = waitSetSignalNext(mon);
				thread->notify_id = self->notify_id;
			}
		}
	}

	self->state = RUNNING;
	self->wait_mon = NULL;

	if (blocked) {
		self->blocked_mon = NULL;
	}

	// Restore the monitor owner and recursion count
	mon->count = old_count;
	mon->owner = self;
	mon->in_wait--;

	enableSuspend(self);

	if (interrupted) {
		self->interrupted = FALSE;
		signalException(java_lang_InterruptedException, NULL);
	}

	return TRUE;
}