/*
 * Jakelib2 - General purpose C++ library
 * Copyright (C) 2001 Florian Wolff  (florian@donuz.de)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * $Id: Monitorable.h,v 1.1 2002/12/02 10:35:59 florian Exp $
 */

#ifndef INCLUDE_JAKELIB2_LANG_MONITORABLE_H
# define INCLUDE_JAKELIB2_LANG_MONITORABLE_H
# include "Thread.h"

#ifdef HAVE_SEMAPHORE_H
# include <semaphore.h>
#endif

//#define JAKELIB_ASSERT(cond, msg) if (!(cond)) System::out.println(msg)
//#define JAKEKIB_SYNCHRONIZED(mon) Sentry __jakelib_sentry(mon)


namespace jakelib {
  namespace lang {

    /**
     * @brief Not yet implemented!!!!
     *
     * @class Monitorable
     * @version $Id: Monitorable.h,v 1.1 2002/12/02 10:35:59 florian Exp $
     * @author Florian 'Overflo' Wolff (florian@donuz.de)
     */
    class Monitorable
      {
      public:
	Monitorable() {
          pthread_mutex_init(&mutex, null);
          count = 0;
          thread = null;
          //printf("Monitorable - condWaitNotify = %i\n",
          //       pthread_cond_init(&condWaitNotify, null));
          //printf("Monitorable - cond = %i\n",
          //       pthread_cond_init(&cond, null));
        }

	~Monitorable() {
          pthread_mutex_destroy(&mutex);
          pthread_cond_destroy(&cond);
          pthread_cond_destroy(&condWaitNotify);
        }

        void acquire() {
          printf("   acquire... %x\n", Thread::currentThread());
          for (;;) {
            pthread_mutex_lock(&mutex);
            //printf("   acquire 1\n");
            if (count == 0) {
              //printf("   acquire 2\n");
              thread = Thread::currentThread();
              //printf("   acquire 3\n");
              count = 1;
              //printf("   acquire: %i\n", count);
              pthread_mutex_unlock(&mutex);
              return;
            }
            else if (thread == Thread::currentThread()) {
              // FIXME: pthread_equal
              count ++;
              //printf("   acquire: %i\n", count);
              pthread_mutex_unlock(&mutex);
              return;
            }
            //printf("  cond_wait()\n");
            pthread_cond_wait(&cond, &mutex);
            pthread_mutex_unlock(&mutex);
            //Thread::yield();
          }
        }

        void release() {
          pthread_mutex_lock(&mutex);
          //assert(thread == Thread::currentThread());
          //assert(count > 0);
          count --;
          //printf("   release: %i\n", count);
          if (count == 0) {
            thread = null;
            pthread_mutex_unlock(&mutex);
            pthread_cond_signal(&cond);
            Thread::yield();
          }
          else {
            pthread_mutex_unlock(&mutex);
          }
        }

        void wait() {
          printf("wait() %x / %i\n", thread, count);
          pthread_mutex_lock(&mutex);
          // FIXME: pthread_equal
          if (thread == Thread::currentThread()) {
            Thread* oldThread = thread;
            int oldCount = count;

            count = 0;
            thread = null;

            printf(" -> pthread_cond_wait()\n");
            pthread_cond_wait(&condWaitNotify, &mutex);

            count = oldCount;
            thread = oldThread;
            printf("/wait() %x / %i\n", thread, count);
          }
          else {
            pthread_mutex_unlock(&mutex);
            throw IllegalMonitorStateException();
          }
        }

        void notify() {
          pthread_cond_signal(&condWaitNotify);
          Thread::yield();
        }

        void notifyAll() {
          pthread_cond_broadcast(&condWaitNotify);
          Thread::yield();
        }

      private:
	pthread_mutex_t mutex;
        Thread* thread;
        int count;
        pthread_cond_t cond, condWaitNotify;
      };

    class Sentry {
    public:

      Sentry(Monitorable* mon) {
        printf("  Sentry() start\n");
        this->mon = mon;
        mon->acquire();
        //printf("  Sentry() end\n");
      }
      ~Sentry() {
        mon->release();
        printf("  ~Sentry()\n");
      }

    private:
      Monitorable* mon;
    };
  }
}
#endif

