Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.ExtLibs/HeuristicLab.SimSharp/3.0.7/SimSharp-3.0.7/Core/Resources/PriorityResource.cs @ 14186

Last change on this file since 14186 was 14186, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 3.6 KB
Line 
1#region License Information
2/* SimSharp - A .NET port of SimPy, discrete event simulation framework
3Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4
5This program is free software: you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation, either version 3 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
17#endregion
18
19using System;
20using System.Collections.Generic;
21using System.Linq;
22
23namespace SimSharp {
24  public class PriorityResource {
25
26    public int Capacity { get; protected set; }
27
28    public int InUse { get { return Users.Count; } }
29
30    public int Remaining { get { return Capacity - InUse; } }
31
32    protected Environment Environment { get; private set; }
33
34    protected SortedList<int, Queue<PriorityRequest>> RequestQueue { get; private set; }
35    protected Queue<Release> ReleaseQueue { get; private set; }
36    protected HashSet<Request> Users { get; private set; }
37
38    public PriorityResource(Environment environment, int capacity = 1) {
39      if (capacity <= 0) throw new ArgumentException("Capacity must be > 0.", "capacity");
40      Environment = environment;
41      Capacity = capacity;
42      RequestQueue = new SortedList<int, Queue<PriorityRequest>>();
43      ReleaseQueue = new Queue<Release>();
44      Users = new HashSet<Request>();
45    }
46
47    public virtual PriorityRequest Request(int priority = 1) {
48      var request = new PriorityRequest(Environment, TriggerRelease, DisposeCallback, priority);
49      if (!RequestQueue.ContainsKey(priority))
50        RequestQueue.Add(priority, new Queue<PriorityRequest>());
51      RequestQueue[priority].Enqueue(request);
52      TriggerRequest();
53      return request;
54    }
55
56    public virtual Release Release(PriorityRequest request) {
57      var release = new Release(Environment, request, TriggerRequest);
58      ReleaseQueue.Enqueue(release);
59      TriggerRelease();
60      return release;
61    }
62
63    protected void DisposeCallback(Event @event) {
64      var request = @event as PriorityRequest;
65      if (request != null) Release(request);
66    }
67
68    protected virtual void DoRequest(Request request) {
69      if (Users.Count < Capacity) {
70        Users.Add(request);
71        request.Succeed();
72      }
73    }
74
75    protected virtual void DoRelease(Release release) {
76      Users.Remove(release.Request);
77      release.Succeed();
78    }
79
80    protected virtual void TriggerRequest(Event @event = null) {
81      foreach (var entry in RequestQueue) {
82        var cascade = false;
83        var requests = entry.Value;
84        while (requests.Count > 0) {
85          var req = requests.Peek();
86          DoRequest(req);
87          if (req.IsTriggered) {
88            requests.Dequeue();
89          } else {
90            cascade = true;
91            break;
92          }
93        }
94        if (cascade) break;
95      }
96    }
97
98    protected virtual void TriggerRelease(Event @event = null) {
99      while (ReleaseQueue.Count > 0) {
100        var release = ReleaseQueue.Peek();
101        DoRelease(release);
102        if (release.IsTriggered) {
103          ReleaseQueue.Dequeue();
104        } else break;
105      }
106    }
107  }
108}
Note: See TracBrowser for help on using the repository browser.