Package co.paralleluniverse.strands

Examples of co.paralleluniverse.strands.Strand


             *    the more typical non-reentrant case.
             * 3. If step 2 fails either because strand
             *    apparently not eligible or CAS fails or count
             *    saturated, chain to version with full retry loop.
             */
            Strand current = Strand.currentStrand();
            int c = getState();
            if (exclusiveCount(c) != 0 &&
                getExclusiveOwnerStrand() != current)
                return -1;
            int r = sharedCount(c);
            if (!readerShouldBlock() &&
                r < MAX_COUNT &&
                compareAndSetState(c, c + SHARED_UNIT)) {
                if (r == 0) {
                    firstReader = current;
                    firstReaderHoldCount = 1;
                } else if (firstReader == current) {
                    firstReaderHoldCount++;
                } else {
                    HoldCounter rh = cachedHoldCounter;
                    if (rh == null || rh.tid != current.getId())
                        cachedHoldCounter = rh = readHolds.get();
                    else if (rh.count == 0)
                        readHolds.set(rh);
                    rh.count++;
                }
View Full Code Here


         * Performs tryLock for write, enabling barging in both modes.
         * This is identical in effect to tryAcquire except for lack
         * of calls to writerShouldBlock.
         */
        final boolean tryWriteLock() {
            Strand current = Strand.currentStrand();
            int c = getState();
            if (c != 0) {
                int w = exclusiveCount(c);
                if (w == 0 || current != getExclusiveOwnerStrand())
                    return false;
View Full Code Here

         * Performs tryLock for read, enabling barging in both modes.
         * This is identical in effect to tryAcquireShared except for
         * lack of calls to readerShouldBlock.
         */
        final boolean tryReadLock() {
            Strand current = Strand.currentStrand();
            for (;;) {
                int c = getState();
                if (exclusiveCount(c) != 0 &&
                    getExclusiveOwnerStrand() != current)
                    return false;
                int r = sharedCount(c);
                if (r == MAX_COUNT)
                    throw new Error("Maximum lock count exceeded");
                if (compareAndSetState(c, c + SHARED_UNIT)) {
                    if (r == 0) {
                        firstReader = current;
                        firstReaderHoldCount = 1;
                    } else if (firstReader == current) {
                        firstReaderHoldCount++;
                    } else {
                        HoldCounter rh = cachedHoldCounter;
                        if (rh == null || rh.tid != current.getId())
                            cachedHoldCounter = rh = readHolds.get();
                        else if (rh.count == 0)
                            readHolds.set(rh);
                        rh.count++;
                    }
View Full Code Here

        final int getReadHoldCount() {
            if (getReadLockCount() == 0)
                return 0;

            Strand current = Strand.currentStrand();
            if (firstReader == current)
                return firstReaderHoldCount;

            HoldCounter rh = cachedHoldCounter;
            if (rh != null && rh.tid == current.getId())
                return rh.count;

            int count = readHolds.get().count;
            if (count == 0) readHolds.remove();
            return count;
View Full Code Here

         * followed by the {@linkplain Strand#getName name} of the owning strand.
         *
         * @return a string identifying this lock, as well as its lock state
         */
        public String toString() {
            Strand o = sync.getOwner();
            return super.toString() + ((o == null) ?
                                       "[Unlocked]" :
                                       "[Locked by strand " + o.getName() + "]");
        }
View Full Code Here

    @SuppressWarnings("LeakingThisInConstructor")
    public Fiber(String name, FiberScheduler scheduler, int stackSize, SuspendableCallable<V> target) {
        this.name = name;
        this.fid = nextFiberId();
        this.scheduler = scheduler;
        Strand parent = Strand.currentStrand(); // retaining the parent as a field is a huge, complex memory leak
        this.target = target;
        this.task = scheduler != null ? scheduler.newFiberTask(this) : new FiberForkJoinScheduler.FiberForkJoinTask(this);
        this.initialStackSize = stackSize;
        this.stack = new Stack(this, stackSize > 0 ? stackSize : DEFAULT_STACK_SIZE);
        this.state = State.NEW;
View Full Code Here

    private static Fiber getCurrentFiber() {
        final Thread currentThread = Thread.currentThread();
        if (FiberForkJoinScheduler.isFiberThread(currentThread))
            return FiberForkJoinScheduler.getTargetFiber(currentThread);
        else {
            Strand s = currentStrand.get();
            return s instanceof Fiber ? (Fiber) s : null;
        }
    }
View Full Code Here

    }

    @Override
    @Suspendable
    public final V get() throws InterruptedException, ExecutionException {
        final Strand s = strand;
        if (s == null)
            throw new IllegalStateException("Actor strand not set (not started?)");
        if (s instanceof Fiber)
            return ((Fiber<V>) s).get();
        else {
            s.join();
            return actor.getResult();
        }
    }
View Full Code Here

     * methods themselves provide extra safeguards to ensure liveness.
     */
    private void release(WNode h) {
        if (h != null) {
            WNode q;
            Strand w;
            U.compareAndSwapInt(h, WSTATUS, WAITING, 0);
            if ((q = h.next) == null || q.status == CANCELLED) {
                for (WNode t = wtail; t != null && t != h; t = t.prev)
                    if (t.status <= 0)
                        q = t;
            }
            if (q != null) {
                for (WNode r = q;;) {  // release co-waiters too
                    if ((w = r.strand) != null) {
                        r.strand = null;
                        w.unpark();
                    }
                    if ((r = q.cowait) == null)
                        break;
                    U.compareAndSwapObject(q, WCOWAIT, r, r.cowait);
                }
View Full Code Here

        for (int spins = SPINS;;) {
            WNode np, pp;
            int ps;
            long s, ns;
            Strand w;
            while ((np = node.prev) != p && np != null)
                (p = np).next = node;   // stale
            if (whead == p) {
                for (int k = spins;;) { // spin at head
                    if (((s = state) & ABITS) == 0L) {
View Full Code Here

TOP

Related Classes of co.paralleluniverse.strands.Strand

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.