private static void RemoveFromSortedWakeUpList(WakeUpCallRequest request) { if (Monitor.TryEnter(_wakeupCallList, 5000)) { try { LinkedListNode <WakeUpCallRequest> node; if (_requestDict.TryGetValue(request.RoomNo, out node)) { _wakeupCallList.Remove(node); _requestDict.Remove(request.RoomNo); } } finally { Monitor.Exit(_wakeupCallList); } } }
private static LinkedListNode <WakeUpCallRequest> InsertIntoSortedWakeUpList(WakeUpCallRequest request) { if (request.WakeUpTime == null) { return(null); } DateTime dateTimeToInsert = request.WakeUpTime.Value; LinkedListNode <WakeUpCallRequest> nodeToInsert = null; if (Monitor.TryEnter(_wakeupCallList, 5000)) { try { LinkedListNode <WakeUpCallRequest> node = _wakeupCallList.First; // when the list is empty if (node == null) { nodeToInsert = _wakeupCallList.AddFirst(request); } else // when the list is not empty { while (node != null) { if (node.Value.WakeUpTime.Value >= dateTimeToInsert) { break; } node = node.Next; } nodeToInsert = node == null?_wakeupCallList.AddLast(request) : _wakeupCallList.AddBefore(node, request); } _requestDict[request.RoomNo] = nodeToInsert; } finally { Monitor.Exit(_wakeupCallList); } } return(nodeToInsert); }
private static void Alarm() { if (Monitor.TryEnter(_wakeupCallList)) { try { if (_wakeupCallList.First == null) { return; } WakeUpCallRequest firstCallRequest = _wakeupCallList.First.Value; DateTime earliestCallTime = firstCallRequest.WakeUpTime.Value; if (DateTime.Today + MockAPI.getCurrenTime() >= earliestCallTime) { MockAPI.sendAlarmCall(firstCallRequest.RoomNo); RemoveFromSortedWakeUpList(firstCallRequest); } } finally { Monitor.Exit(_wakeupCallList); } } }