summaryrefslogtreecommitdiff
path: root/NET/worlds/scape/SoundResource.java
blob: 19975f2d5715cd2e710dc6d89ac02e9b28550c7b (plain) (blame)
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
package NET.worlds.scape;

class SoundResource {
   private static SoundResource instance = null;
   public static final int RSX = 1;
   public static final int RA = 2;
   private int currentPlayer = 0;
   private int counter = 0;
   private boolean isShutdownTime = false;

   private SoundResource() {
   }

   private static void debugOut(int n, String s) {
      if (Sound.debugLevel > n) {
         System.out.println(s);
      }
   }

   public static SoundResource instance() {
      if (instance == null) {
         debugOut(6, "Instantiating a new SoundResource");
         instance = new SoundResource();
      }

      return instance;
   }

   public boolean syncLock(int type) {
      debugOut(6, "Calling syncLock with type " + type + " counter " + this.counter);
      if (this.isShutdownTime) {
         return false;
      } else {
         if (type != this.currentPlayer) {
            this.currentPlayer = type;
            if (this.counter > 0) {
               this.isShutdownTime = true;
            } else {
               this.currentPlayer = type;
               this.counter = 1;
            }
         } else {
            this.counter++;
         }

         return true;
      }
   }

   public synchronized void asyncLock() {
      debugOut(6, "SoundResource::asyncLock with " + this.isShutdownTime);
      boolean shutdown = false;

      while (this.isShutdownTime) {
         shutdown = true;
         Thread.yield();
         debugOut(6, "asyncLock yield");
      }

      if (shutdown) {
         debugOut(6, "asyncLock: adding artificial delay");

         try {
            Thread.sleep(500L);
         } catch (InterruptedException var3) {
         }
      }
   }

   public boolean syncUnlock() {
      debugOut(6, "SoundResource::syncUnlock with " + this.counter);
      boolean rv = false;
      this.counter--;
      if (this.counter <= 0) {
         rv = true;
      }

      if (this.counter <= 0 && this.isShutdownTime) {
         this.counter = 1;
         this.isShutdownTime = false;
      }

      return rv;
   }

   public boolean isOK() {
      return !this.isShutdownTime;
   }
}