All Downloads are FREE. Search and download functionalities are using the official Maven repository.

Alachisoft.NCache.Common.Threading.Latch Maven / Gradle / Ivy

There is a newer version: 5.3.3
Show newest version
package Alachisoft.NCache.Common.Threading;

import Alachisoft.NCache.Common.BitSet;

/**
 *
 */
public class Latch {
    /**
     * A watchdog that prevents pre-init use of cache.
     */
    private Promise _initWatch = new Promise();
    /**
     * The runtime status of this node.
     */
    private BitSet _status = new BitSet();

    public Latch() {
    }

    public Latch(byte initialStatus) {
        _status.setData(initialStatus);
    }

    public final BitSet getStatus() {
        return _status;
    }


    /**
     * Check is aall of the bits given in the bitset is set.
     *
     * @param status
     * @return
     */
    public final boolean AreAllBitsSet(byte status) {
        return _status.IsBitSet(status);
    }

    /**
     * Check is any of the bits given in the bitset is set.
     *
     * @param status
     * @return
     */
    public final boolean IsAnyBitsSet(byte status) {
        return _status.IsAnyBitSet(status);
    }

    /**
     * The runtime status of this node.
     */
    public final void Clear() {
        synchronized (this) {
            _status.setData((byte) 0);
            _initWatch.SetResult(_status);
        }
    }

    /**
     * The runtime status of this node.
     */
    public final void SetStatusBit(byte bitsToSet, byte bitsToUnset) {
        synchronized (this) {
            _status.Set(bitsToSet, bitsToUnset);
            _initWatch.SetResult(_status);
        }
    }

    /**
     * Blocks the thread until any of the two statii is reached.
     *
     * @param status
     */
    public final void WaitForAny(byte status) {
        while (!IsAnyBitsSet(status)) {
            Object result = _initWatch.WaitResult(Long.MAX_VALUE);
            if (result == null) {
                return;
            }
        }
    }


    public final void WaitForAny(byte status, long timeout) {
        if (timeout > 0) {
            while (!IsAnyBitsSet(status)) {
                Object result = _initWatch.WaitResult(timeout);
                /** Result of a reset on the watch dog, done from dispose
                 */
                if (result == null) {
                    return;
                }
            }
        }
    }

    public void waitForAnyUpdated(byte status) {
        while (!IsAnyBitsSet(status)) {
            Object result = _initWatch.WaitResult(Long.MAX_VALUE);
        }
    }

    /**
     * Blocks the thread until any of the two statii is reached.
     *
     * @param status
     */
    public final void WaitForAll(byte status) {
        while (!AreAllBitsSet(status)) {
            Object result = _initWatch.WaitResult(Long.MAX_VALUE);
            if (result == null) {
                return;
            }
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy