Beispiel #1
0
/**
 * Notify all threads waiting on a monitor.
 *
 * A thread is considered to be waiting on the monitor if
 * it is currently blocked while executing hythread_monitor_wait on the monitor.
 *
 * If no threads are waiting, no action is taken.
 *
 *
 * @param[in] mon_ptr a monitor to be signaled
 * @return  0 once the monitor has been signaled<br>HYTHREAD_ILLEGAL_MONITOR_STATE if the current thread does not own the monitor
 *
 * @see hythread_monitor_notify, hythread_monitor_enter, hythread_monitor_wait
 */
IDATA VMCALL hythread_monitor_notify_all(hythread_monitor_t mon_ptr) {
    if (mon_ptr->owner != tm_self_tls) {
        return TM_ERROR_ILLEGAL_STATE;
    }
    mon_ptr->notify_count = mon_ptr->wait_count;
    return hycond_notify_all(&mon_ptr->condition);
}
Beispiel #2
0
IDATA VMCALL hythread_decrease_nondaemon_threads_count(hythread_t thread, IDATA threads_to_keep)
{
    hythread_library_t lib = thread->library;
    IDATA status = port_mutex_lock(&lib->TM_LOCK);
    if (status != TM_ERROR_NONE) {
        return status;
    }

    if (lib->nondaemon_thread_count <= 0) {
        status = port_mutex_unlock(&lib->TM_LOCK);
        if (status != TM_ERROR_NONE) {
            return status;
        }
        return TM_ERROR_ILLEGAL_STATE;
    }

    CTRACE(("TM: nondaemons decreased, thread: %p count: %d\n", thread,
           lib->nondaemon_thread_count));

    lib->nondaemon_thread_count--;
    if (lib->nondaemon_thread_count - threads_to_keep <= 0) {
        status = hycond_notify_all(&lib->nondaemon_thread_cond);
        CTRACE(("TM: nondaemons all dead, thread: %p count: %d\n", thread,
               lib->nondaemon_thread_count));
        if (status != TM_ERROR_NONE) {
            port_mutex_unlock(&lib->TM_LOCK);
            return status;
        }
    }

    status = port_mutex_unlock(&lib->TM_LOCK);
    return status;
} // hythread_countdown_nondaemon_threads
static IDATA HYTHREAD_PROC hythread_interrupter(void *args)
{
    IDATA status;
    hythread_monitor_t mon = (hythread_monitor_t)args;

    status = hythread_monitor_enter(mon);
    assert(status == TM_ERROR_NONE);
    status = hycond_notify_all(&mon->condition);
    assert(status == TM_ERROR_NONE);
    hythread_exit(mon);
    return 0;
}
/*
 * Exit locktable write section
 */
static void locktable_writer_exit() {
    IDATA status = port_mutex_lock(&lock_table->mutex);
    assert(status == TM_ERROR_NONE);

    if (lock_table->readers_reading > 0) {
        lock_table->readers_reading = lock_table->readers_waiting;
        lock_table->readers_waiting = 0;
        lock_table->state = HYTHREAD_LOCKTABLE_READING;
        hycond_notify_all(&lock_table->read);
    } else if (lock_table->writers_waiting > 0) {
        hycond_notify(&lock_table->write);
    } else {
        lock_table->state = HYTHREAD_LOCKTABLE_IDLE;
    }

    status = port_mutex_unlock(&lock_table->mutex);
    assert(status == TM_ERROR_NONE);
}
/** 
 * Interrupt a thread.
 * 
 * If the thread is currently blocked (i.e. waiting on a monitor_wait or sleeping)
 * resume the thread and cause it to return from the blocking function with
 * HYTHREAD_INTERRUPTED.
 * 
 * @param[in] thread a thread to be interrupted
 * @return none
 */
void VMCALL hythread_interrupt(hythread_t thread) {
    IDATA status;
    hythread_monitor_t mon;

    apr_atomic_set32(&thread->interrupted, TRUE);

    mon = thread->waited_monitor;
    if (mon) {
        // If thread was doing any kind of wait, notify it.
        if (hythread_monitor_try_enter(mon) == TM_ERROR_NONE) {
            status = hycond_notify_all(&mon->condition);
            assert(status == TM_ERROR_NONE);
            status = hythread_monitor_exit(mon);
            assert(status == TM_ERROR_NONE);
        } else {
            status = hythread_create(NULL, 0, 0, 0,
                hythread_interrupter, (void *)mon);
            assert (status == TM_ERROR_NONE);
        }
    }
} // hythread_interrupt
Beispiel #6
0
/**
 * Decreases the count for latch.
 *
 * If the count reaches zero, all threads awaiting on the latch are unblocked.
 * @param[in] latch the latch 
 * @sa java.util.concurrent.CountDownLatch.countDown()
 */
IDATA VMCALL hylatch_count_down(hylatch_t latch) {
    IDATA status;
    
    status = port_mutex_lock(&latch->mutex);
    if (status != TM_ERROR_NONE) return status;
    if (latch->count <= 0) {
        status = port_mutex_unlock(&latch->mutex);
        if (status != TM_ERROR_NONE) return status;
        return TM_ERROR_ILLEGAL_STATE;
    }
    latch->count--;
    if (latch->count == 0) {
        status = hycond_notify_all(&latch->condition); 
        if (status != TM_ERROR_NONE) {
            port_mutex_unlock(&latch->mutex);
            return status;
        }
    }
            
    status = port_mutex_unlock(&latch->mutex);
    if (status != TM_ERROR_NONE) return status;
        
    return TM_ERROR_NONE;       
}