/// <summary> /// This function releases a thread from the semaphore /// </summary> /// <param name="thread"> a reference to the thread to release</param> /// <returns>the number of free allocation slots available after releasing the thread</returns> public int Up(ref SimulatorThread thread) { if (insideThreads.Contains(thread)) { insideThreads.Remove(thread); thread.OwnerProcess.AllocatedResources.Remove(resource); thread.WaitingForSemaphore = false; thread.OwnsSemaphore = false; thread.PreviousState = thread.CurrentState; thread.CurrentState = EnumThreadState.UNKNOWN; slots++; } else if (waitingThreads.Contains(thread)) { List <SimulatorThread> temp = waitingThreads.ToList(); temp.Remove(thread); waitingThreads = new Queue <SimulatorThread>(temp); thread.OwnerProcess.AllocatedResources.Remove(resource); thread.WaitingForSemaphore = false; thread.OwnsSemaphore = false; thread.PreviousState = thread.CurrentState; thread.CurrentState = EnumThreadState.UNKNOWN; } else { MessageBox.Show("Thread must be captured by the semaphore before being released"); return(int.MinValue); } CaptureNextThread(); return(slots); }
/// <summary> /// This function captures a thread inside the semaphore /// </summary> /// <param name="thread"> a reference to the thread to capture</param> /// <returns> the number of free allocation slots available after capturing the thread</returns> public int Down(ref SimulatorThread thread) { if (!insideThreads.Contains(thread) && !waitingThreads.Contains(thread)) { if (slots > 0) { thread.OwnerProcess.AllocatedResources.Add(resource); insideThreads.Add(thread); thread.OwnsSemaphore = true; thread.WaitingForSemaphore = false; thread.PreviousState = thread.CurrentState; thread.CurrentState = EnumThreadState.IN_SEMAPHORE; slots--; } else { waitingThreads.Enqueue(thread); thread.OwnerProcess.RequestedResources.Add(resource); thread.OwnsSemaphore = false; thread.WaitingForSemaphore = true; thread.PreviousState = thread.CurrentState; thread.CurrentState = EnumThreadState.WAITING_SEMAPHORE; } } else { MessageBox.Show("Can not capture a thread that has already been captured by the semaphore"); return(int.MinValue); } return(slots); }
public SpinLock(SimulatorThread thread, int timeout = int.MaxValue) { this.locked = false; this.process = null; this.thread = thread; this.timeout = timeout; }
public SpinLock(SimulatorProcess process, int timeout = int.MaxValue) { this.locked = false; this.process = process; this.thread = null; this.timeout = timeout; }
/// <summary> /// Constructor for execution unit that starts executing from a specified location in the program /// </summary> /// <param name="thread"> the thread to execute </param> /// <param name="currentIndex"> the index to start executing from</param> /// <param name="clockSpeed"> the clock speed of the CPU </param> public ThreadExecutionUnit(SimulatorThread thread, int clockSpeed, int currentIndex) : base(thread.OwnerProcess, clockSpeed, currentIndex) { }
/// <summary> /// Constructor for execution unit that starts executing from the beginning of the program /// </summary> /// <param name="thread"> the thread to execute </param> /// <param name="clockSpeed"> the clock speed of the CPU </param> public ThreadExecutionUnit(SimulatorThread thread, int clockSpeed) : base(thread.OwnerProcess, clockSpeed) { }