コード例 #1
0
        public static bool CpuidTx(uint index, uint ecxValue,
                                   out uint eax, out uint ebx, out uint ecx, out uint edx,
                                   GroupAffinity affinity)
        {
            var previousAffinity = ThreadAffinity.Set(affinity);

            if (previousAffinity == GroupAffinity.Undefined)
            {
                eax = ebx = ecx = edx = 0;
                return(false);
            }

            Cpuid(index, ecxValue, out eax, out ebx, out ecx, out edx);

            ThreadAffinity.Set(previousAffinity);
            return(true);
        }
コード例 #2
0
        public static bool IsValid(GroupAffinity affinity)
        {
            if (OperatingSystem.IsUnix)
            {
                if (affinity.Group > 0)
                {
                    return(false);
                }
            }

            try {
                var previous = Set(affinity);
                if (previous == GroupAffinity.Undefined)
                {
                    return(false);
                }
                Set(previous);
                return(true);
            } catch {
                return(false);
            }
        }
コード例 #3
0
        /// <summary>
        /// Sets the processor group affinity for the current thread.
        /// </summary>
        /// <param name="affinity">The processor group affinity.</param>
        /// <returns>The previous processor group affinity.</returns>
        public static GroupAffinity Set(GroupAffinity affinity)
        {
            if (affinity == GroupAffinity.Undefined)
            {
                return(GroupAffinity.Undefined);
            }

            if (OperatingSystem.IsUnix)
            {
                if (affinity.Group > 0)
                {
                    throw new ArgumentOutOfRangeException("affinity.Group");
                }

                ulong result = 0;
                if (NativeMethods.sched_getaffinity(0, (IntPtr)8, ref result) != 0)
                {
                    return(GroupAffinity.Undefined);
                }

                ulong mask = affinity.Mask;
                if (NativeMethods.sched_setaffinity(0, (IntPtr)8, ref mask) != 0)
                {
                    return(GroupAffinity.Undefined);
                }

                return(new GroupAffinity(0, result));
            }
            else
            {
                UIntPtr uIntPtrMask;
                try {
                    uIntPtrMask = (UIntPtr)affinity.Mask;
                } catch (OverflowException) {
                    throw new ArgumentOutOfRangeException("affinity.Mask");
                }

                var groupAffinity = new NativeMethods.GROUP_AFFINITY {
                    Group = affinity.Group,
                    Mask  = uIntPtrMask
                };

                var currentThread = NativeMethods.GetCurrentThread();

                try {
                    if (NativeMethods.SetThreadGroupAffinity(currentThread,
                                                             ref groupAffinity, out var previousGroupAffinity))
                    {
                        return(new GroupAffinity(previousGroupAffinity.Group,
                                                 (ulong)previousGroupAffinity.Mask));
                    }
                    else
                    {
                        return(GroupAffinity.Undefined);
                    }
                } catch (EntryPointNotFoundException) {
                    if (affinity.Group > 0)
                    {
                        throw new ArgumentOutOfRangeException("affinity.Group");
                    }

                    var previous = (ulong)NativeMethods.SetThreadAffinityMask(
                        currentThread, uIntPtrMask);

                    return(new GroupAffinity(0, previous));
                }
            }
        }