|
| 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