All Downloads are FREE. Search and download functionalities are using the official Maven repository.

Alachisoft.NCache.Common.Util.ProcessorAffinity Maven / Gradle / Ivy

package Alachisoft.NCache.Common.Util;

/**
 * Gets and sets the processor affinity of the current thread.
 */
public final class ProcessorAffinity {
////C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
////#if !MONO
//
//    private static class Win32Native
//    {
//        //GetCurrentThread() returns only a pseudo handle. No need for a SafeHandle here.
//
//        public static native IntPtr GetCurrentThread();
//
//        static
//        {
//            System.loadLibrary("kernel32.dll");
//        }
//
//        public static native UIntPtr SetThreadAffinityMask(IntPtr handle, UIntPtr mask);
//    }
////#endif
//
////C# TO JAVA CONVERTER WARNING: Java does not allow user-defined value types. The behavior of this class will differ from the original:
////ORIGINAL LINE: public struct ProcessorAffinityHelper : IDisposable
//    public final static class ProcessorAffinityHelper implements IDisposable
//    {
//
//        private UIntPtr lastaffinity = new UIntPtr();
//
//        public ProcessorAffinityHelper()
//        {
//        }
//
//        public ProcessorAffinityHelper(UIntPtr lastaffinity)
//        {
//            this.lastaffinity = lastaffinity;
//        }
//
////C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
//        ///#region IDisposable Members
//        public void dispose()
//        {
////C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
////#if !MONO
//            if (lastaffinity != UIntPtr.Zero)
//            {
//                Win32Native.SetThreadAffinityMask(Win32Native.GetCurrentThread(), lastaffinity);
//                lastaffinity = UIntPtr.Zero;
//            }
////#else
//            //@UH
////#endif
//        }
//
////C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
//        ///#endregion
//        public ProcessorAffinityHelper clone()
//        {
//            ProcessorAffinityHelper varCopy = new ProcessorAffinityHelper();
//
//            varCopy.lastaffinity = this.lastaffinity;
//
//            return varCopy;
//        }
//    }
//
//    private static long maskfromids(Integer... ids)
//    {
////C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
////ORIGINAL LINE: ulong mask = 0;
//        long mask = 0;
//        for (int id : ids)
//        {
//            if (id < 0 || id >= Environment.ProcessorCount)
//            {
//                throw new IllegalArgumentException("CPUId", (new Integer(id)).toString());
//            }
//            mask |= 1 << id;
//        }
//        return mask;
//    }
//
//    /**
//     * Sets a processor affinity mask for the current thread.
//     *
//     * @param mask A thread affinity mask where each bit set to 1 specifies a logical processor on which this thread is allowed to run. Note: a thread cannot specify a broader set
//     * of CPUs than those specified in the process affinity mask.      *
//     * @return The previous affinity mask for the current thread.
//     */
//    public static UIntPtr SetThreadAffinityMask(UIntPtr mask)
//    {
////C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
////#if !MONO
//        UIntPtr lastaffinity = Win32Native.SetThreadAffinityMask(Win32Native.GetCurrentThread(), mask);
//        if (lastaffinity == UIntPtr.Zero)
//        {
//            throw new Win32Exception(Marshal.GetLastWin32Error());
//        }
//        return lastaffinity;
////#else
//        //@UH
//        return UIntPtr.Zero;
////#endif
//    }
//
//    /**
//     * Sets the logical CPUs that the current thread is allowed to execute on.
//     *
//     * @param CPUIds One or more logical processor identifier(s) the current thread is allowed to run on.Note: numbering starts from 0.
//     * @return The previous affinity mask for the current thread.
//     */
//    public static UIntPtr SetThreadAffinity(Integer... CPUIds)
//    {
//        return SetThreadAffinityMask(((UIntPtr) maskfromids(CPUIds)));
//    }
//
//    /**
//     * Restrict a code block to run on the specified logical CPUs in conjuction with the
//     * using statement.
//     *
//     * @param CPUIds One or more logical processor identifier(s) the current thread is allowed to run on.Note: numbering starts from 0.
//     * @return A helper structure that will reset the affinity when its Dispose() method is called at the end of the using block.
//     */
//    public static ProcessorAffinityHelper BeginAffinity(Integer... CPUIds)
//    {
//        return new ProcessorAffinityHelper(SetThreadAffinityMask(((UIntPtr) maskfromids(CPUIds))));
//    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy