|
| 1 | +/* |
| 2 | + * Copyright 2011 Peter Lawrey |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package vanilla.java.busywaiting.impl; |
| 18 | + |
| 19 | +import vanilla.java.busywaiting.IBusyWaiter; |
| 20 | + |
| 21 | +/** |
| 22 | + * @author peter.lawrey |
| 23 | + */ |
| 24 | +public enum JNIBusyWaiting implements IBusyWaiter { |
| 25 | + INSTANCE; |
| 26 | + |
| 27 | + public static final boolean LOADED; |
| 28 | + |
| 29 | + static { |
| 30 | + boolean loaded; |
| 31 | + try { |
| 32 | + System.loadLibrary("affinity"); |
| 33 | + loaded = true; |
| 34 | + } catch (UnsatisfiedLinkError ule) { |
| 35 | + loaded = false; |
| 36 | + } |
| 37 | + LOADED = loaded; |
| 38 | + } |
| 39 | + |
| 40 | + public static native void pause0(); |
| 41 | + |
| 42 | + public static native long whileEqual0(long address, int iterations, long value); |
| 43 | + |
| 44 | + public static native long whileLessThan0(long address, int iterations, long value); |
| 45 | + |
| 46 | + @Override |
| 47 | + public void pause() { |
| 48 | + pause0(); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public long whileEqual(Object obj, long address, int iterations, long value) { |
| 53 | + if (obj == null) |
| 54 | + return whileEqual0(address, iterations, value); |
| 55 | + |
| 56 | + return whileEqual0(obj, address, iterations, value); |
| 57 | + } |
| 58 | + |
| 59 | + private static long whileEqual0(Object obj, long address, int iterations, long value) { |
| 60 | + long value2 = JavaBusyWaiting.UNSAFE.getLongVolatile(null, address); |
| 61 | + while (value2 == value && iterations-- > 0) { |
| 62 | + pause0(); |
| 63 | + value2 = JavaBusyWaiting.UNSAFE.getLongVolatile(obj, address); |
| 64 | + } |
| 65 | + return value2; |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public long whileLessThan(Object obj, long address, int iterations, long value) { |
| 70 | + if (obj == null) |
| 71 | + return whileLessThan0(address, iterations, value); |
| 72 | + |
| 73 | + return whileLessThan0(obj, address, iterations, value); |
| 74 | + } |
| 75 | + |
| 76 | + private static long whileLessThan0(Object obj, long address, int iterations, long value) { |
| 77 | + long value2 = JavaBusyWaiting.UNSAFE.getLongVolatile(null, address); |
| 78 | + while (value2 < value && iterations-- > 0) { |
| 79 | + pause0(); |
| 80 | + value2 = JavaBusyWaiting.UNSAFE.getLongVolatile(obj, address); |
| 81 | + } |
| 82 | + return value2; |
| 83 | + } |
| 84 | +} |
0 commit comments