// -*-C++-*- 

/*  inc/platform/threads/PThread.hpp  */

/*
 * Author: Philogelos A. <Philogelos@yahoo.com>
 * Maintainer: Philogelos A.
 * Keywords: C++, library, containers
 *
 * Copyright (C) 1999 Philogelos A.
 *
 * This file is part of Quercus Robusta.
 *
 * Quercus Robusta is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Library General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This software 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 Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this software; see the file COPYING.LIB.  If not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 */



#ifndef __PTHREAD_HPP__
#define __PTHREAD_HPP__

/* $Id$ */

#define USE_PTHREADS

#if defined( WANT_USE_PTHREADS )
#define USE_PTHREADS
#elif defined( UNWANT_USE_PTHREADS )
#if defined( USE_PTHREADS )
#undef USE_PTHREADS
#endif
#endif


#if defined( USE_PTHREADS )

#include "ThreadFactory.hpp"
#include "ThreadsSystem.hpp"
#include "Thread.hpp"
#include "Object.hpp"
#include "defines.h"
#include "Mutex.hpp"
#include "CondVar.hpp"
#include "Semaphore.hpp"
#include "String.hpp"

#include <pthread.h>
#include <semaphore.h>

class PThreadFactory;
class PThread;
class PThreadsSystem;
class PCondVar;
class PMutex;
class PSemaphore;

class PThreadFactory : public virtual ThreadFactory,
					   public virtual Object
{
public: PThreadFactory();
public: virtual ~PThreadFactory();

public: virtual Thread *createFromFunc( void * ( *aStartFunc )( void *anArg ), void *anArg );
public: virtual ThreadsSystem *getThreadSystem();

protected: PThreadsSystem *pthreadsSystem;

public: static pthread_key_t backRefKey;

protected: static void initClass();
protected: static pthread_once_t initClassOnce;
};

class PThreadsSystem : public virtual ThreadsSystem,
					   public virtual Object
{
  friend PThread;

public: PThreadsSystem();
public: virtual ~PThreadsSystem();

public: virtual void onceFunc( void ( *aFunct )() );
public: virtual Mutex *mutex();
public: virtual Mutex *recMutex();
public: virtual CondVar *condition();
public: virtual Semaphore *semaphore( Index anInitialCount );

public: virtual Thread *getSelf();
public: virtual void cancel( Thread *aVictim );
public: virtual void sleep( Index aSecond, Index aNanoseconds );
public: virtual void exitCurrent( void *aStatus );

public: static int pthrCall( const char *callName, int retCode );
public: static Thread *getById( pthread_t anId );

protected: static PThread *mainThread;
};

class ThreadDeath;

/**

   @author Philogelos
   @created
*/
class PThread : public virtual Thread,
				public virtual Object
{
  friend PThreadFactory;

  /**
   */
public: PThread( pthread_t anId );

  /**
   */
public: virtual ~PThread();

public: virtual Index getId() const;
public: virtual boolean isSystemThread() const;
public: virtual void join();

public:  virtual String  toString() const;
public:  virtual String  getClassName() const;

public: virtual void setName( String aName );
public: virtual String getName() const;

protected: void setId( pthread_t anId );
protected: pthread_t id;
protected: ThreadDeath *death;

protected: String name;
  
#if defined( TESTING )
public:  virtual boolean tester( int ) const;
#endif
};

class PMutex : public virtual Mutex,
			   public virtual Object
{
  friend PCondVar;

public: PMutex();
public: PMutex( pthread_mutexattr_t *anAttribute );
public: virtual ~PMutex();

public: virtual void enter( const Top * = null );
public: virtual void leave( const Top * = null );
public: virtual boolean tryEnter( const Top * = null );

public:  virtual String  toString() const;

protected: pthread_mutex_t core;
};

typedef struct
{
  void *( *func )( void *anArg );
  void *arg;
  Thread **object;
  CondVar *startup;
  CondVar *condition;
  CondVar *back;
} CFuncCall;

class PCondVar : public virtual CondVar,
				 public virtual Object
{
public: PCondVar( PMutex *aMutex );
public: virtual ~PCondVar();

public: virtual Mutex *getMutex() const;
public: virtual void wait();
public: virtual void signal();
public: virtual void broadcast();

public:  virtual String  toString() const;

protected: PMutex *mutex;
protected: pthread_cond_t core;
};

class PSemaphore : public virtual Semaphore,
				   public virtual Object
{
public: PSemaphore( boolean aShared, Index anInitialCount );
public: virtual ~PSemaphore();

public: void inc();
public: void dec();
public: boolean tryDec();

protected: sem_t core;
};

/* USE_PTHREADS */
#endif

/* $Log$ */

/* __PTHREAD_HPP__ */
#endif
