MATLAB: How to prevent a deadlock state in a SimEvents model using SimEvents R2013b

SimEvents

Two entities are being submitted to two servers, i.e. one entity per server. If server #1 is occupied, it should pause (and block) server #2 until server #1 becomes unoccupied. The same operation occurs if server #2 becomes occupied. At no point in time should both servers be blocked. However, an issue arises if the servers receive the entities at the "same" time, both servers become blocked and thus the system becomes locked. 
The attached model, "simpleExample_eventPrior.slx", demonstrates this behavior.
How can I prevent a deadlock state in a SimEvents model?

Best Answer

A method that will provide control to select which block has priority in this case would be to use a MATLAB Function block to break this "deadlock" state. Effectively when both the 'so' outputs from N-Server and N-Server1 are equally 1, preference should be given to one server. The attached example, "simpleExample_eventPrior_TS.slx", demonstrates a possible method to resolve this "deadlock" state.  
Additionally, since these blocks are mutually driving each other, priorities for the ServiceCompletion and the ControlEvent states are executed at different times. For additional information on the priorities and execution order, the SimEvents engine looks to be performing the ServiceCompletion for both N-Servers first in order of their priorities. Then afterwards, when both servers are full, the ControlRequests are evaluated. To further inspect the order of a SimEvents simulation, you can enter debug mode. This can be performed via the following command in the MATLAB command line:
 
>> sedebug('simpleExample_eventPrior')
 
Once in the debugger, the "runtoend" command can be used to generate a log for this particular example. At the end of the log, you can see that the ControlRequests for the servers are evaluated after both servers are already full. Hence, this model will alway end in the deadlock state.
Related Question