// src/python_cpu_policy.hh - 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 2 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, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

#ifndef PYTHON_CPU_POLICY_HH
#define PYTHON_CPU_POLICY_HH 1

#include "config.h"

#include <Python.h>
#include <glibmm/thread.h>
#include <glibmm/ustring.h>

#include <iostream>

#include <sgpemv2/cpu_policy.hh>
#include <sgpemv2/user_interrupt_exception.hh>
#include <sgpemv2/malformed_policy_exception.hh>

namespace sgpem
{
  class PythonCPUPolicy;
  class PythonCPUPolicyManager;

  /** \brief A specialization of abstract class Policy

   This class represents a policy written in Python. Its methods interact with Python interpreter.
   See the documentation of class Policy for more detailed informations.
  */
  class SG_DLLEXPORT PythonCPUPolicy : public CPUPolicy
  {
  public:
    PythonCPUPolicy(const char* name) throw(MalformedPolicyException);
    virtual ~PythonCPUPolicy();

    /**
     Calls the method \c async_configure
    */
    void   configure() throw(UserInterruptException, MalformedPolicyException);

    /**
     Calls the method \c async_sort_queue
    */
    void    sort_queue() const throw(UserInterruptException, MalformedPolicyException);

    /**
     \returns A textual description of this policy.
    */
    Glib::ustring          get_description() const;

    Glib::ustring          get_name() const;

    /**
     \returns \c TRUE if the policy is preemptive.
     \returns \c FALSE if the policy is not preemptive.
    */
    bool    is_pre_emptive() const throw(UserInterruptException, MalformedPolicyException);

    /**
     \returns The integer value of its time-slice.
    */
    int    get_time_slice() const throw(UserInterruptException, MalformedPolicyException);

    void activate() throw(UserInterruptException, MalformedPolicyException);

    void deactivate();

  private:
    PythonCPUPolicy(const PythonCPUPolicy&);
    PythonCPUPolicy& operator=(const PythonCPUPolicy&);

    void wait_unlock() const throw(UserInterruptException, MalformedPolicyException);
    static std::string get_exception_information();

    static Glib::StaticRecMutex _mtx;
    
    PyObject* _upolicy_dict;
    PyObject* _adapter;

    Glib::ustring _name;
    Glib::ustring _description;
  };

}

#endif

