irqthread.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 232 行

JAVA
232
字号
/*
 * $Id: IRQThread.java,v 1.1 2003/11/25 11:41:17 epr Exp $
 */
package org.jnode.vm;

import org.jnode.system.IRQHandler;
import org.jnode.system.ResourceOwner;

/**
 * Thread for IRQ handler.
 * 
 * @author epr
 */
final class IRQThread extends Thread {

	private final IRQManager mgr;
	private final int irq;
	private boolean stop;
	private int irqCount;
	private int handledIrqCount;
	/** Is this a shared IRQ? */
	private final boolean shared;
	private IRQAction actions;

	public IRQThread(IRQManager mgr, int irq, ResourceOwner owner, IRQHandler handler, boolean shared) {
		super("IRQ-" + irq);
		this.mgr = mgr;
		this.irq = irq;
		this.setPriority(Thread.MAX_PRIORITY);
		this.stop = false;
		this.irqCount = 0;
		this.handledIrqCount = 0;
		this.shared = shared;
		this.actions = new IRQAction(owner, handler);
	}

	/**
	 * Add an IRQ handler
	 * 
	 * @param owner
	 * @param handler
	 */
	public synchronized void addHandler(ResourceOwner owner, IRQHandler handler) {
		final IRQAction a = new IRQAction(owner, handler);
		if (actions == null) {
			actions = a;
		} else {
			actions.add(a);
		}
	}

	/**
	 * Remove a given handler
	 * 
	 * @param handler
	 */
	public synchronized void remove(IRQHandler handler) {
		if (this.actions != null) {
			this.actions = this.actions.remove(handler);
		}
	}

	public boolean isEmpty() {
		return (this.actions == null);
	}

	/**
	 * Stop this IRQ thread.
	 *  
	 */
	public void stopThread() {
		this.stop = true;
		interrupt();
	}

	/**
	 * Continue to run until i'm stopped.
	 * 
	 * @see java.lang.Runnable#run()
	 */
	public void run() {
		while (!stop) {
			doHandle();
		}
	}

	/**
	 * Wait to be notified or then handle exactly 1 interrupt.
	 */
	private synchronized void doHandle() {

		if (irqCount == handledIrqCount) {
			try {
				wait();
			} catch (InterruptedException ex) {
				// Ignore
			}
		} else {
			try {
				try {
					/*
					 * if (irq == 10) { Screen.debug(" <handle IRQ 10/> ");
					 */
					IRQAction action = this.actions;
					while (action != null) {
						try {
							if (action.isEnabled()) {
								action.getHandler().handleInterrupt(irq);
							}
						} catch (Throwable ex) {
							action.incErrorCount();
							ex.printStackTrace();
						}
						action = action.getNext();
					}
					handledIrqCount++;
				} finally {
					mgr.eoi(irq);
				}
			} catch (Throwable ex) {
				ex.printStackTrace();
			}
		}
	}

	/**
	 * Tell this thread how many IRQ's have been received, so this thread
	 * can start the IRQ handler if needed.
	 * @param count
	 * @param current
	 * @throws PragmaUninterruptible
	 */
	final void signalIRQ(int count, VmThread current)
	throws PragmaUninterruptible {
		this.irqCount = count;
		if (irqCount != handledIrqCount) {
			if (this.getVmThread() != current) {
				interrupt();
			}
		}
	}

	/**
	 * @return int
	 */
	public final int getIrq() {
		return irq;
	}

	/**
	 * Convert to a String representation.
	 * 
	 * @see java.lang.Object#toString()
	 * @return String
	 */
	public String toString() {
		final StringBuffer b = new StringBuffer();
		if (shared) {
			b.append("shared ");
		}
		IRQAction a = this.actions;
		boolean first = true;
		while (a != null) {
			if (!first) {
				b.append(", ");
			}
			first = false;
			b.append(a);
			a = a.getNext();
		}
		return b.toString();
	}

	static class IRQAction {
		private final ResourceOwner owner;
		private final IRQHandler handler;
		private IRQAction next;
		private int errorCount;

		public IRQAction(ResourceOwner owner, IRQHandler handler) {
			this.owner = owner;
			this.handler = handler;
		}

		public final IRQHandler getHandler() {
			return handler;
		}

		public final IRQAction getNext() {
			return next;
		}

		public final void add(IRQAction action) {
			IRQAction p = this;
			while (p.next != null) {
				p = p.next;
			}
			p.next = action;
		}

		public final IRQAction remove(IRQHandler handler) {
			if (this.handler == handler) {
				return this.next;
			} else if (this.next != null) {
				this.next = this.next.remove(handler);
				return this;
			} else {
				return this;
			}
		}

		public final void incErrorCount() {
			errorCount++;
		}
		
		public final boolean isEnabled() {
			return errorCount < 10;
		}

		public final String toString() {
			return owner.getShortDescription() + ((errorCount > 0) ? " errors " + errorCount : "");
		}
	}
	/**
	 * @return Returns the shared.
	 */
	public final boolean isShared() {
		return this.shared;
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?