Skip to content

Commit 1ae143e

Browse files
committed
Added getThreadId() support for MAC OSX
1 parent c4e53f3 commit 1ae143e

2 files changed

Lines changed: 79 additions & 1 deletion

File tree

‎affinity/src/main/java/net/openhft/affinity/AffinitySupport.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package net.openhft.affinity;
1818

1919
import net.openhft.affinity.impl.NullAffinity;
20+
import net.openhft.affinity.impl.OSXJNAAffinity;
2021
import net.openhft.affinity.impl.PosixJNAAffinity;
2122
import net.openhft.affinity.impl.WindowsJNAAffinity;
2223
import org.jetbrains.annotations.NotNull;
@@ -44,7 +45,11 @@ public enum AffinitySupport {
4445
} else if (osName.contains("x") && isJNAAvailable() && PosixJNAAffinity.LOADED) {
4546
LOGGER.fine("Using Posix JNA-based affinity control implementation");
4647
AFFINITY_IMPL = PosixJNAAffinity.INSTANCE;
47-
} else {
48+
}else if (osName.contains("Mac") && isJNAAvailable()) {
49+
LOGGER.fine("Using MAC OSX JNA-based thread id implementation");
50+
AFFINITY_IMPL = OSXJNAAffinity.INSTANCE;
51+
}
52+
else {
4853
LOGGER.info("Using dummy affinity control implementation");
4954
AFFINITY_IMPL = NullAffinity.INSTANCE;
5055
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2013 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 net.openhft.affinity.impl;
18+
19+
20+
import com.sun.jna.LastErrorException;
21+
import com.sun.jna.Library;
22+
import com.sun.jna.Native;
23+
import net.openhft.affinity.IAffinity;
24+
25+
import java.lang.management.ManagementFactory;
26+
import java.util.logging.Level;
27+
import java.util.logging.Logger;
28+
29+
/**
30+
* This is essentially the same as the NullAffinity implementation but with concrete
31+
* support for getThreadId().
32+
* @author daniel.shaya
33+
*/
34+
public enum OSXJNAAffinity implements IAffinity {
35+
INSTANCE;
36+
private static final Logger LOGGER = Logger.getLogger(OSXJNAAffinity.class.getName());
37+
38+
@Override
39+
public long getAffinity() {
40+
return -1;
41+
}
42+
43+
@Override
44+
public void setAffinity(final long affinity) {
45+
if (LOGGER.isLoggable(Level.FINE))
46+
LOGGER.fine("unable to set mask to " + Long.toHexString(affinity) + " as the JNIa nd JNA libraries and not loaded");
47+
}
48+
49+
@Override
50+
public int getCpu() {
51+
return -1;
52+
}
53+
54+
@Override
55+
public int getProcessId() {
56+
final String name = ManagementFactory.getRuntimeMXBean().getName();
57+
return Integer.parseInt(name.split("@")[0]);
58+
}
59+
60+
@Override
61+
public int getThreadId() {
62+
final CLibrary lib = CLibrary.INSTANCE;
63+
return CLibrary.INSTANCE.pthread_self();
64+
}
65+
66+
interface CLibrary extends Library {
67+
CLibrary INSTANCE = (CLibrary)
68+
Native.loadLibrary("libpthread.dylib", CLibrary.class);
69+
70+
int pthread_self() throws LastErrorException;
71+
72+
}
73+
}

0 commit comments

Comments
 (0)