Skip to content

Commit 88056d1

Browse files
committed
Testing on Java 6, 7 and 8. Mostly Javadoc changes. :P
1 parent 485578d commit 88056d1

File tree

5 files changed

+43
-24
lines changed

5 files changed

+43
-24
lines changed

affinity/src/main/java/net/openhft/affinity/AffinityLock.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public class AffinityLock {
7777

7878
/**
7979
* Set the CPU layout for this machine. CPUs which are not mentioned will be ignored.
80-
* <p/>
80+
* <p></p>
8181
* Changing the layout will have no impact on thread which have already been assigned.
8282
* It only affects subsequent assignments.
8383
*
@@ -110,7 +110,7 @@ public static void cpuLayout(@NotNull CpuLayout cpuLayout) {
110110

111111
/**
112112
* Translate a layout id into a logical cpu id.
113-
* <p/>
113+
* <p></p>
114114
* This translation is perform so that regardless of how
115115
*
116116
* @param id
@@ -153,7 +153,7 @@ public static AffinityLock acquireLock() {
153153

154154
/**
155155
* Assign any free core to this thread.
156-
* <p/>
156+
* <p></p>
157157
* In reality, only one cpu is assigned, the rest of the threads for that core are reservable so they are not used.
158158
*
159159
* @return A handle for the current AffinityLock.
@@ -164,7 +164,7 @@ public static AffinityLock acquireCore() {
164164

165165
/**
166166
* Assign a cpu which can be bound to the current thread or another thread.
167-
* <p/>
167+
* <p></p>
168168
* This can be used for defining your thread layout centrally and passing the handle via dependency injection.
169169
*
170170
* @param bind if true, bind the current thread, if false, reserve a cpu which can be bound later.
@@ -176,7 +176,7 @@ public static AffinityLock acquireLock(boolean bind) {
176176

177177
/**
178178
* Assign a core(and all its cpus) which can be bound to the current thread or another thread.
179-
* <p/>
179+
* <p></p>
180180
* This can be used for defining your thread layout centrally and passing the handle via dependency injection.
181181
*
182182
* @param bind if true, bind the current thread, if false, reserve a cpu which can be bound later.
@@ -336,7 +336,7 @@ private boolean canReserve() {
336336

337337
/**
338338
* Give another affinity lock relative to this one based on a list of strategies.
339-
* <p/>
339+
* <p></p>
340340
* The strategies are evaluated in order to (like a search path) to find the next appropriate thread.
341341
* If ANY is not the last strategy, a warning is logged and no cpu is assigned (leaving the OS to choose)
342342
*

affinity/src/main/java/net/openhft/affinity/AffinityThreadFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
/**
2525
* This is a ThreadFactory which assigns threads based the strategies provided.
26-
* <p/>
26+
* <p></p>
2727
* If no strategies are provided AffinityStrategies.ANY is used.
2828
*
2929
* @author peter.lawrey

affinity/src/main/java/net/openhft/affinity/impl/PosixJNAAffinity.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* Implementation of {@link IAffinity} based on JNA call of
3030
* sched_setaffinity(3)/sched_getaffinity(3) from 'c' library. Applicable for most
3131
* linux/unix platforms
32-
* <p/>
32+
* <p></p>
3333
* TODO Support assignment to core 64 and above
3434
*
3535
* @author peter.lawrey
@@ -60,14 +60,17 @@ public long getAffinity() {
6060
final int ret = lib.sched_getaffinity(0, Integer.SIZE / 8, cpuset32);
6161
if (ret < 0)
6262
throw new IllegalStateException("sched_getaffinity((" + Integer.SIZE / 8 + ") , &(" + cpuset32 + ") ) return " + ret);
63-
return cpuset32.getValue();
63+
return cpuset32.getValue() & 0xFFFFFFFFL;
6464
} catch (LastErrorException e) {
6565
throw new IllegalStateException("sched_getaffinity((" + Integer.SIZE / 8 + ") , &(" + cpuset32 + ") ) errorNo=" + e.getErrorCode(), e);
6666
}
6767
}
6868

6969
@Override
7070
public void setAffinity(final long affinity) {
71+
int procs = Runtime.getRuntime().availableProcessors();
72+
if (procs < 64 && (affinity & ((1L << procs) - 1)) == 0)
73+
throw new IllegalArgumentException("Cannot set zero affinity");
7174
final CLibrary lib = CLibrary.INSTANCE;
7275
try {
7376
//fixme: where are systems with more then 64 cores...
@@ -76,7 +79,19 @@ public void setAffinity(final long affinity) {
7679
throw new IllegalStateException("sched_setaffinity((" + Long.SIZE / 8 + ") , &(" + affinity + ") ) return " + ret);
7780
}
7881
} catch (LastErrorException e) {
79-
throw new IllegalStateException("sched_getaffinity((" + Long.SIZE / 8 + ") , &(" + affinity + ") ) errorNo=" + e.getErrorCode(), e);
82+
if (e.getErrorCode() != 22 || (affinity & 0xFFFFFFFFL) != affinity)
83+
throw new IllegalStateException("sched_setaffinity((" + Long.SIZE / 8 + ") , &(" + affinity + ") ) errorNo=" + e.getErrorCode(), e);
84+
}
85+
if (procs < 32 && (affinity & ((1L << procs) - 1)) == 0)
86+
throw new IllegalArgumentException("Cannot set zero affinity for 32-bit set affinity");
87+
final IntByReference cpuset32 = new IntByReference(0);
88+
cpuset32.setValue((int) affinity);
89+
try {
90+
final int ret = lib.sched_setaffinity(0, Integer.SIZE / 8, cpuset32);
91+
if (ret < 0)
92+
throw new IllegalStateException("sched_setaffinity((" + Integer.SIZE / 8 + ") , &(" + Integer.toHexString(cpuset32.getValue()) + ") ) return " + ret);
93+
} catch (LastErrorException e) {
94+
throw new IllegalStateException("sched_setaffinity((" + Integer.SIZE / 8 + ") , &(" + Integer.toHexString(cpuset32.getValue()) + ") ) errorNo=" + e.getErrorCode(), e);
8095
}
8196
}
8297

affinity/src/test/java/net/openhft/affinity/impl/AbstractAffinityImplTest.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void getAffinityReturnsValidValue() throws Exception {
4747
"Affinity mask " + affinity + " must be >0",
4848
affinity > 0
4949
);
50-
final int allCoresMask = (1 << CORES) - 1;
50+
final long allCoresMask = (1L << CORES) - 1;
5151
assertTrue(
5252
"Affinity mask " + affinity + " must be <=(2^" + CORES + "-1 = " + allCoresMask + ")",
5353
affinity <= allCoresMask
@@ -64,7 +64,7 @@ public void getAffinityReturnsValuePreviouslySet() throws Exception {
6464
final IAffinity impl = getImpl();
6565
final int cores = CORES;
6666
for (int core = 0; core < cores; core++) {
67-
final long mask = (1 << core);
67+
final long mask = (1L << core);
6868
getAffinityReturnsValuePreviouslySet(impl, mask);
6969
}
7070
}
@@ -79,7 +79,11 @@ private void getAffinityReturnsValuePreviouslySet(final IAffinity impl,
7979

8080
@After
8181
public void tearDown() throws Exception {
82-
final int anyCore = (1 << CORES) - 1;
83-
getImpl().setAffinity(anyCore);
82+
final long anyCore = (1L << CORES) - 1;
83+
try {
84+
getImpl().setAffinity(anyCore);
85+
} catch (Exception e) {
86+
e.printStackTrace();
87+
}
8488
}
8589
}

affinity/src/test/java/org/junit/Assert.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@
2424

2525
/**
2626
* TODO replace with 4.12 when it is released with a simple bug fix.
27-
* <p/>
27+
* <p></p>
2828
* A set of assertion methods useful for writing tests. Only failed assertions
2929
* are recorded. These methods can be used directly:
3030
* <code>Assert.assertEquals(...)</code>, however, they read better if they
3131
* are referenced through static import:<br/>
32-
* <p/>
32+
* <p></p>
3333
* <pre>
3434
* import static org.junit.Assert.*;
3535
* ...
@@ -576,8 +576,8 @@ static public void assertEquals(String message, long expected, long actual) {
576576

577577
/**
578578
* @deprecated Use
579-
* <code>assertEquals(double expected, double actual, double delta)</code>
580-
* instead
579+
* <code>assertEquals(double expected, double actual, double delta)</code>
580+
* instead
581581
*/
582582
@Deprecated
583583
static public void assertEquals(double expected, double actual) {
@@ -586,8 +586,8 @@ static public void assertEquals(double expected, double actual) {
586586

587587
/**
588588
* @deprecated Use
589-
* <code>assertEquals(String message, double expected, double actual, double delta)</code>
590-
* instead
589+
* <code>assertEquals(String message, double expected, double actual, double delta)</code>
590+
* instead
591591
*/
592592
@Deprecated
593593
static public void assertEquals(String message, double expected,
@@ -825,15 +825,15 @@ public static void assertEquals(Object[] expecteds, Object[] actuals) {
825825
* Asserts that <code>actual</code> satisfies the condition specified by
826826
* <code>matcher</code>. If not, an {@link AssertionError} is thrown with
827827
* information about the matcher and failing value. Example:
828-
* <p/>
828+
* <p></p>
829829
* <pre>
830830
* assertThat(0, is(1)); // fails:
831831
* // failure message:
832832
* // expected: is &lt;1&gt;
833833
* // got value: &lt;0&gt;
834834
* assertThat(0, is(not(1))) // passes
835835
* </pre>
836-
* <p/>
836+
* <p></p>
837837
* <code>org.hamcrest.Matcher</code> does not currently document the meaning
838838
* of its type parameter <code>T</code>. This method assumes that a matcher
839839
* typed as <code>Matcher&lt;T&gt;</code> can be meaningfully applied only
@@ -855,7 +855,7 @@ public static <T> void assertThat(T actual, Matcher<? super T> matcher) {
855855
* Asserts that <code>actual</code> satisfies the condition specified by
856856
* <code>matcher</code>. If not, an {@link AssertionError} is thrown with
857857
* the reason and information about the matcher and failing value. Example:
858-
* <p/>
858+
* <p></p>
859859
* <pre>
860860
* assertThat(&quot;Help! Integers don't work&quot;, 0, is(1)); // fails:
861861
* // failure message:
@@ -864,7 +864,7 @@ public static <T> void assertThat(T actual, Matcher<? super T> matcher) {
864864
* // got value: &lt;0&gt;
865865
* assertThat(&quot;Zero is one&quot;, 0, is(not(1))) // passes
866866
* </pre>
867-
* <p/>
867+
* <p></p>
868868
* <code>org.hamcrest.Matcher</code> does not currently document the meaning
869869
* of its type parameter <code>T</code>. This method assumes that a matcher
870870
* typed as <code>Matcher&lt;T&gt;</code> can be meaningfully applied only

0 commit comments

Comments
 (0)