File: PrioritySemaphore.java

package info (click to toggle)
concurrent-dfsg 1.3.4-4
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, squeeze, stretch, wheezy
  • size: 976 kB
  • ctags: 2,018
  • sloc: java: 10,704; xml: 49; makefile: 12
file content (90 lines) | stat: -rw-r--r-- 2,741 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
  File: PrioritySemaphore.java

  Originally written by Doug Lea and released into the public domain.
  This may be used for any purposes whatsoever without acknowledgment.
  Thanks for the assistance and support of Sun Microsystems Labs,
  and everyone contributing, testing, and using this code.

  History:
  Date       Who                What
  11Jun1998  dl               Create public version
*/


package EDU.oswego.cs.dl.util.concurrent;

/**
 * A Semaphore that grants requests to threads with higher
 * Thread priority rather than lower priority when there is
 * contention. Ordering of requests with the same priority
 * is approximately FIFO.
 * Priorities are based on Thread.getPriority.
 * Changing the priority of an already-waiting thread does NOT 
 * change its ordering. This class also does not specially deal with priority
 * inversion --  when a new high-priority thread enters
 * while a low-priority thread is currently running, their
 * priorities are <em>not</em> artificially manipulated.
 * <p>[<a href="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>]

**/

public class PrioritySemaphore extends QueuedSemaphore {

  /** 
   * Create a Semaphore with the given initial number of permits.
   * Using a seed of one makes the semaphore act as a mutual exclusion lock.
   * Negative seeds are also allowed, in which case no acquires will proceed
   * until the number of releases has pushed the number of permits past 0.
  **/


  public PrioritySemaphore(long initialPermits) { 
    super(new PriorityWaitQueue(), initialPermits);
  }

  protected static class PriorityWaitQueue extends WaitQueue {


    /** An array of wait queues, one per priority **/
    protected final FIFOSemaphore.FIFOWaitQueue[] cells_ = 
      new FIFOSemaphore.FIFOWaitQueue[Thread.MAX_PRIORITY -
                                     Thread.MIN_PRIORITY + 1];

    /**
     * The index of the highest priority cell that may need to be signalled,
     * or -1 if none. Used to minimize array traversal.
    **/

    protected int maxIndex_ = -1;

    protected PriorityWaitQueue() { 
      for (int i = 0; i < cells_.length; ++i) 
        cells_[i] = new FIFOSemaphore.FIFOWaitQueue();
    }

    protected void insert(WaitNode w) {
      int idx = Thread.currentThread().getPriority() - Thread.MIN_PRIORITY;
      cells_[idx].insert(w); 
      if (idx > maxIndex_) maxIndex_ = idx;
    }

    protected WaitNode extract() {
      for (;;) {
        int idx = maxIndex_;
        if (idx < 0) 
          return null;
        WaitNode w = cells_[idx].extract();
        if (w != null) 
          return w;
        else
          --maxIndex_;
      }
    }
  }





}