Skip to content

Commit

Permalink
close #21
Browse files Browse the repository at this point in the history
  • Loading branch information
Enchufa2 committed Apr 25, 2016
1 parent 687a365 commit b1502ae
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
12 changes: 9 additions & 3 deletions src/resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
#include "simulator.h"

void Resource::set_capacity(int value) {
int last = capacity;
capacity = value;
// serve another
while (queue_count)
if (!try_serve_from_queue(sim->verbose, sim->now())) break;
if (capacity > last) {
// serve another
while (queue_count)
if (!try_serve_from_queue(sim->verbose, sim->now())) break;
} else if (capacity < last) {
while (server_count > capacity)
if (!try_free_server(sim->verbose, sim->now())) break;
}
if (is_monitored()) observe(sim->now());
}

Expand Down
32 changes: 19 additions & 13 deletions src/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ class Resource: public Entity {
return server_count + amount <= capacity;
}

virtual bool try_free_server(bool verbose, double time) { return false; }

virtual void insert_in_server(bool verbose, double time, Arrival* arrival, int amount,
int priority, int preemptible, bool restart) {
if (verbose) verbose_print(time, arrival->name, "SERVE");
Expand Down Expand Up @@ -226,21 +228,25 @@ class PreemptiveResource: public Resource {
return false;
}

virtual bool try_free_server(bool verbose, double time) {
typename T::iterator first = server.begin();
first->arrival->deactivate(first->restart);
if (verbose) verbose_print(time, first->arrival->name, "PREEMPT");
if (first->arrival->is_monitored()) {
double last = first->arrival->get_activity(this->name);
first->arrival->set_activity(this->name, time - last);
}
preempted.insert((*first));
queue_count += first->amount;
server_count -= first->amount;
server.erase(first);
return true;
}

virtual void insert_in_server(bool verbose, double time, Arrival* arrival, int amount,
int priority, int preemptible, bool restart) {
if (capacity > 0) while (server_count + amount > capacity) {
typename T::iterator first = server.begin();
first->arrival->deactivate(first->restart);
if (verbose) verbose_print(time, first->arrival->name, "PREEMPT");
if (first->arrival->is_monitored()) {
double last = first->arrival->get_activity(this->name);
first->arrival->set_activity(this->name, time - last);
}
preempted.insert((*first));
queue_count += first->amount;
server_count -= first->amount;
server.erase(first);
}
if (capacity > 0) while (server_count + amount > capacity)
try_free_server(verbose, time);
if (verbose) verbose_print(time, arrival->name, "SERVE");
server_count += amount;
server.emplace(time, arrival, amount, priority, preemptible, restart);
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-simmer-resource-schedule.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ test_that("arrivals 1) are dequeued when resource's capacity increases and 2) re

inf_sch <- schedule(c(0, 1, 2), c(1, 3, 1), Inf)

arrivals <- simmer(verbose=T) %>%
arrivals <- simmer() %>%
add_resource("dummy", inf_sch) %>%
add_generator("asdf", t, at(0, 0, 0)) %>%
run() %>%
Expand All @@ -86,7 +86,7 @@ test_that("arrivals are preempted when resource's capacity decreases", {

inf_sch <- schedule(c(0, 1, 2), c(1, 3, 1), Inf)

arrivals <- simmer(verbose=T) %>%
arrivals <- simmer() %>%
add_resource("dummy", inf_sch, preemptive=TRUE) %>%
add_generator("asdf", t, at(0, 0, 0)) %>%
run() %>%
Expand Down

0 comments on commit b1502ae

Please sign in to comment.