/* Network-Integrated Multimedia Middleware (NMM)
 * http://www.motama.com/nmm.html
 *  
 * Copyright (C) 2006-2007
 * Motama GmbH, Saarbruecken, Germany
 * http://www.motama.com
 *  
 * This file is part of NMM. 
 * 
 * NMM 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; only version 2 of the License.      
 * 
 * NMM 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 NMM; if not, write to the Free Software Foundation,    
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */


#ifndef NMM_THREAD_HPP
#define NMM_THREAD_HPP NMM_THREAD_HPP

#include "nmm/nmm_prefix.hpp"

#include "nmm/Result.hpp"
#include "visibility.hpp"

#include <pthread.h>
#include <stdexcept>

namespace NMM{

  /** This class is a wrapper for a pthread-threads.
   */
  class DLLEXPORT Thread {
  public:
    typedef void* (*ThrFunction) (void*);
    enum Scheduling_Policy {
      FIFO,             // priority scheduling
      ROUND_ROBIN,      // priority + timer scheduling
      OTHER             // default scheduling (kernel)
    };

    /** Constructor
     */
    Thread();

    /** Destructor
     */
    ~Thread();

    /** Creates a new thread.
      *  The new thread applies the function start_routine passing it arg as first
      * argument.  The new thread terminates either explicitly, *
      * \param start_routine : pointer to the start_routine with type void* method(void*)
      * \param args : argument for the start_routine
      * \param attr : pthread attributes(see pthread manual)
      */
    void start(ThrFunction start_routine,	 void *arg, pthread_attr_t* attr = 0 )
      throw (const std::runtime_error&);

    /** Kills the thread.
     */
    void kill();

    /** Blocks until the thread terminates
     */
    Result join();

    /** Terminates the execution of the thread
     */
    inline void exit() { pthread_exit(&thread); }

    /** returns the ThreadID
      */
    inline pthread_t getID() { return thread; }

    /** enables pthread priority scheduling (requires root privileges)
      */
    void enableCustomScheduling(Scheduling_Policy);

    /** creates a default pthread attribute (EXPLICIT FIFO SCHEDULING)
      */
    void createDefaultAttribute();

    void setMinPriority();
    void setMidPriority();
    void setMaxPriority();
    int getSchedPriotity();
    /**
      * This function can suspend a thread (or process) for a given
      * number of milliseconds. The time granularity may be more or
      * less than one millisecond, and this call is neither guaranteed
      * not to return before the given time, nor not to return after.
      * This function simply gives a "best effort" delay.
      * \param millis The number of milliseconds to sleep.
      */
	static  void suspendThread(long millis);

  private:
    pthread_t thread;
    pthread_attr_t custom_sched_attr;        // The thread's pthread attribute
    struct sched_param fifo_param,rr_param;  // pthread scheduling parameters
    bool fifo_scheduling_enabled;
    bool rr_scheduling_enabled;
  };

}//end namespace NMM

#include "nmm/nmm_suffix.hpp"

#endif

