/** * Bind the current thread to this reservable lock. * * @param wholeCore if true, also reserve the whole core. */ public void bind(boolean wholeCore) { if (bound && assignedThread != null && assignedThread.isAlive()) throw new IllegalStateException("cpu " + cpuId + " already bound to " + assignedThread); if (wholeCore) { int core = coreForId(cpuId); for (AffinityLock al : CORES.get(core)) { if (bound && al.assignedThread != null && al.assignedThread.isAlive()) { LOGGER.severe("cpu " + al.cpuId + " already bound to " + al.assignedThread); } else { al.bound = true; al.assignedThread = Thread.currentThread(); } } if (LOGGER.isLoggable(Level.INFO)) { StringBuilder sb = new StringBuilder().append("Assigning core ").append(core); String sep = ": cpus "; for (AffinityLock al : CORES.get(core)) { sb.append(sep).append(al.cpuId); sep = ", "; } sb.append(" to ").append(assignedThread); LOGGER.info(sb.toString()); } } else if (cpuId >= 0) { bound = true; assignedThread = Thread.currentThread(); if (LOGGER.isLoggable(Level.INFO)) LOGGER.info("Assigning cpu " + cpuId + " to " + assignedThread); } if (cpuId >= 0) AffinitySupport.setAffinity(1L << cpuId); }
/** * Bind the current thread to this reservable lock. * * @param wholeCore if true, also reserve the whole core. */ public void bind(boolean wholeCore) { if (bound && assignedThread != null && assignedThread.isAlive()) throw new IllegalStateException("cpu " + cpuId + " already bound to " + assignedThread); if (wholeCore) { lockInventory.bindWholeCore(cpuId); } else if (cpuId >= 0) { bound = true; assignedThread = Thread.currentThread(); LOGGER.info("Assigning cpu {} to {}", cpuId, assignedThread); } if (cpuId >= 0) AffinitySupport.setAffinity(1L << cpuId); }
/** Release the current AffinityLock which can be discarded. */ public void release() { Thread t = Thread.currentThread(); synchronized (AffinityLock.class) { for (AffinityLock al : LOCKS) { Thread at = al.assignedThread; if (at == t) { if (LOGGER.isLoggable(Level.INFO)) LOGGER.info("Releasing cpu " + al.cpuId + " from " + t); al.assignedThread = null; al.bound = false; } else if (at != null && !at.isAlive()) { LOGGER.warning("Releasing cpu " + al.cpuId + " from " + t + " as it is not alive."); al.assignedThread = null; al.bound = false; } } } AffinitySupport.setAffinity(BASE_AFFINITY); }