// src/backend/resource_policy_priority_inheritance.cc - Copyright 2005, 2006, University
//               of Padova, dept. of Pure and Applied
//               Mathematics
//
// This file is part of SGPEMv2.
//
// This is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// SGPEMv2 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SGPEMv2. If not, see http://www.gnu.org/licenses/.


#include "resource_policy_priority_inheritance.hh"

#include <sgpemv2/thread.hh>

using namespace std;
using namespace sgpem;

ResourcePolicyPriorityInheritance::~ResourcePolicyPriorityInheritance()
{}

void
ResourcePolicyPriorityInheritance::configure()

{
}

void
ResourcePolicyPriorityInheritance::enforce(Environment& /*environment*/, Environment::SubRequestQueue& queue, SubRequest& sr)

{
  typedef Environment::SubRequestQueue SubRequestQueue;

  SubRequestQueue old_queue(queue);
  queue.clear();

  // the priority may only rise!  
  int pmax = sr.get_request().get_thread().get_current_priority();


  typedef SubRequestQueue::iterator It;
  for (It i = old_queue.begin(); i != old_queue.end(); i++)
  {
    // determine the max priority
    int pthat = (**i).get_request().get_thread().get_current_priority();
    if (pthat < pmax)
      pmax = pthat;
    // allocated ones remain allocated
    if((**i).get_state() == Request::state_allocated)
      queue.push_back(*i);
  }

  int pthis = sr.get_request().get_thread().get_current_priority();

  // assume they are ordered by priority.
  bool inserted = false;
  typedef SubRequestQueue::iterator It;
  for (It i = old_queue.begin(); i != old_queue.end(); i++)
  {
    // allocated ones remain allocated
    if((**i).get_state() != Request::state_allocated)
    {
      // non-allocated ones with lower priority go after the newly arrived
      int pthat = (**i).get_request().get_thread().get_current_priority();
      if(!inserted && pthis <= pthat)
      {
        queue.push_back(&sr);
        inserted = true;
      }
      if (*i != &sr)
        queue.push_back(*i);
    }
    else
    {
      // increase the priority of a the thread to the maximum priority of any process waiting
      int push = pmax - (**i).get_request().get_thread().get_current_priority();
      (**i).get_request().get_thread().set_priority_push(push);
    }
  }
}

Glib::ustring
ResourcePolicyPriorityInheritance::get_description() const
{
  return _("A resource policy which solves the priority inversion problem by raising the priority of a thread to the maximum relative to the queue.");
}

Glib::ustring
ResourcePolicyPriorityInheritance::get_name() const
{
  return _("Basic Priority Inheritance Protocol");
}


void
ResourcePolicyPriorityInheritance::activate()
{
}

void
ResourcePolicyPriorityInheritance::deactivate()
{
}
