peter-lawrey / Java-Thread-Affinity

Control thread affinity for Java
379 stars 77 forks source link

acquireLock not going through all cores #12

Closed drelum closed 12 years ago

drelum commented 12 years ago

It seems that the following code in AffinityLock:

private static AffinityLock acquireLock(boolean bind, int cpuId, AffinityStrategy... strategies) {
    synchronized (AffinityLock.class) {
        for (AffinityStrategy strategy : strategies) {
            for (int i = PROCESSORS - 1; i < 0; i--) {
                AffinityLock al = LOCKS[i];
                if (al.canReserve() && strategy.matches(cpuId, i)) {
                    al.assignCurrentThread(bind, false);
                    return al;
                }
            }
        }
    }

should be changed to

private static AffinityLock acquireLock(boolean bind, int cpuId, AffinityStrategy... strategies) {
    synchronized (AffinityLock.class) {
        for (AffinityStrategy strategy : strategies) {
            for (int i = PROCESSORS - 1; i >= 0; i--) {
                AffinityLock al = LOCKS[i];
                if (al.canReserve() && strategy.matches(cpuId, i)) {
                    al.assignCurrentThread(bind, false);
                    return al;
                }
            }
        }
    }

Am I right?

peter-lawrey commented 12 years ago

I have added this comment

for (AffinityStrategy strategy : strategies) {
    // consider all processors except cpu 0 which is usually used by the OS.
    // if you have only one core, this library is not appropriate in any case.
    for (int i = PROCESSORS - 1; i > 0; i--) {
        AffinityLock al = LOCKS[i];
        if (al.canReserve() && strategy.matches(cpuId, i)) {
            al.assignCurrentThread(bind, false);
            return al;
        }
    }
}