public inbox for java-prs@sourceware.org
help / color / mirror / Atom feed
* [Bug libgcj/34574]  New: wait() call hangs in _Jv_CondWait taking the monitor with it causing the application to hang
@ 2007-12-24 18:24 rajathf at techmahindra dot com
  2007-12-24 18:27 ` [Bug libgcj/34574] " rajathf at techmahindra dot com
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: rajathf at techmahindra dot com @ 2007-12-24 18:24 UTC (permalink / raw)
  To: java-prs

This seems to be a critical issue causing a thread pool implementaion to go
into dead lock and normally happens if code inside run method does requires
little time and large number runnable objects are assigned to thread pool...

I have identified this issue in sun solaris 2.9

Please find below the code for each of the files required to reproduce scenario
and corresponding compilation options at the end...

-----------------------------------env_test.java-------------------------------------------
public class env_test implements Runnable {

        static private int count = 0;
    private int taskNumber;
    protected Done done;


        public static native void foo();

        public env_test()
        {

                count++;
        taskNumber = count;

        }

        public void  run()
        {
                foo();  

        }
}

---------------------------------TestJVSynchronise19thDec.cc--------------------------------------------

// This file was created by `gcjh -stubs'. -*- c++ -*-
//
// This file is intended to give you a head start on implementing native
// methods using CNI.
// Be aware: running `gcjh -stubs ' once more for this class may
// overwrite any edits you have made to this file.

#include <iostream.h>
#include <gcj/cni.h>
#include <gnu/gcj/RawData.h>
#include <env_test.h>
#include <ThreadPool.h>
#include <java/lang/Thread.h>

using namespace std;

using namespace java::lang;

Object *vl_pLock;

void env_test::foo()
{

        {
                JvSynchronize sync (vl_pLock);

                //std::cout <<
"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++ "  << std::endl;

                long  vl_CurrThreadID = Thread::currentThread()->getId(); 

                //std::cout << "()Thread ID : " << vl_CurrThreadID <<
std::endl;

                for (int i=0;i<30;i++)

                {
                        //cout << "Hello World" << endl;
                }

                //std::cout <<
"-------------------------------------------------------- "  << std::endl;

        }

}

int main(int argc, char *argv)
{

    JvCreateJavaVM(NULL);
    JvAttachCurrentThread(NULL,NULL);

        ThreadPool *pool = new ThreadPool(20);

    vl_pLock = new Object();

        for (int i=0;i<100000;i++)
        {
                pool->assign((gnu::gcj::RawData *) new env_test());
        }

        pool->complete();

        JvDetachCurrentThread();

}       

-------------------------------------ThreadPool.java---------------------------------------

import java.util.*;
//import gnu.gcj.RawData;

/**
 * Java Thread Pool
 * 
 * This is a thread pool that for Java, it is
 * simple to use and gets the job done. This program and
 * all supporting files are distributed under the Limited
 * GNU Public License (LGPL, http://www.gnu.org).
 * 
 * This is the main class for the thread pool. You should
 * create an instance of this class and assign tasks to it.
 * 
 * For more information visit http://www.jeffheaton.com.
 * 
 * @author Jeff Heaton (http://www.jeffheaton.com)
 * @version 1.0
 */
public class ThreadPool {
 /**
  * The threads in the pool.
  */
 protected Thread threads[] = null;
 /**
  * The backlog of assignments, which are waiting
  * for the thread pool.
  */
 Collection assignments = new ArrayList(3);
 //Vector assignments = new Vector();
 /**
  * A Done object that is used to track when the
  * thread pool is done, that is has no more work
  * to perform.
  */
 protected Done done = new Done();

 /**
  * The constructor.
  * 
  * @param size  How many threads in the thread pool.
  */
 public ThreadPool(int size)
 {
  threads = new WorkerThread[size];
  for (int i=0;i<threads.length;i++) {
   threads[i] = new WorkerThread(this);
   threads[i].start();
  }
 }

 /**
  * Add a task to the thread pool. Any class
  * which implements the Runnable interface
  * may be assienged. When this task runs, its
  * run method will be called.
  * 
  * @param r   An object that implements the Runnable interface
  */
 //public synchronized void assign(Runnable r)
 public synchronized void assign(Object r)
 //public void assign(Object r)
 {
        done.workerBegin();
        assignments.add(r);
        notify();
 }

 /**
  * Get a new work assignment.
  * 
  * @return A new assignment
  */
 public synchronized Runnable getAssignment()
 {
  try {
   while ( !assignments.iterator().hasNext() )
    wait();

   Runnable r = (Runnable)assignments.iterator().next();
   assignments.remove(r);
   return r;
  } catch (InterruptedException e) {
   done.workerEnd();
   return null;
  }
 }

 /**
  * Called to block the current thread until
  * the thread pool has no more work.
  */
 public void complete()
 {
  done.waitBegin();
  done.waitDone();
 }


 protected void finalize()  
 {
  done.reset();
  for (int i=0;i<threads.length;i++) {
   threads[i].interrupt();
   done.workerBegin();
   threads[i].destroy();
  }
  done.waitDone();
 }
}

/**
 * The worker threads that make up the thread pool.
 * 
 * @author Jeff Heaton
 * @version 1.0
 */
class WorkerThread extends Thread {
 /**
  * True if this thread is currently processing.
  */
 public boolean busy;
 /**
  * The thread pool that this object belongs to.
  */
 public ThreadPool owner;

 /**
  * The constructor.
  * 
  * @param o the thread pool 
  */
 WorkerThread(ThreadPool o)
 {
  owner = o;
 }

 /**
  * Scan for and execute tasks.
  */
 public void run()
 {
  Runnable target = null;

  do {
   target = owner.getAssignment();
   if (target!=null) {
    target.run();      
    owner.done.workerEnd();
   }
  } while (target!=null);
 }
}

/**
 * 
 * This is a thread pool for Java, it is
 * simple to use and gets the job done. This program and
 * all supporting files are distributed under the Limited
 * GNU Public License (LGPL, http://www.gnu.org).
 * 
 * This is a very simple object that
 * allows the TheadPool to determine when 
 * it is done. This object implements
 * a simple lock that the ThreadPool class
 * can wait on to determine completion.
 * Done is defined as the ThreadPool having
 * no more work to complete.
 * 
 * Copyright 2001 by Jeff Heaton
 *
 * @author Jeff Heaton (http://www.jeffheaton.com)
 * @version 1.0
 */
class Done {

 /**
  * The number of Worker object
  * threads that are currently working
  * on something.
  */
 private int _activeThreads = 0;

 /**
  * This boolean keeps track of if
  * the very first thread has started
  * or not. This prevents this object
  * from falsely reporting that the ThreadPool 
  * is done, just because the first thread
  * has not yet started.
  */
 private boolean _started = false;
 /**
  * This method can be called to block
  * the current thread until the ThreadPool
  * is done.
  */

 synchronized public void waitDone()
 {
  try {
   while ( _activeThreads>0 ) {
    wait();
   }
  } catch ( InterruptedException e ) {
  }
 }
 /**
  * Called to wait for the first thread to 
  * start. Once this method returns the
  * process has begun.
  */

 synchronized public void waitBegin()
 {
  try {
   while ( !_started ) {
    wait();
   }
  } catch ( InterruptedException e ) {
  }
 }


 /**
  * Called by a Worker object
  * to indicate that it has begun 
  * working on a workload.
  */
 synchronized public void workerBegin()
 {
  _activeThreads++;
  _started = true;
  notify();
 }

 /**
  * Called by a Worker object to 
  * indicate that it has completed a 
  * workload.
  */
 synchronized public void workerEnd()
 {
  _activeThreads--;
  notify();
 }

 /**
  * Called to reset this object to
  * its initial state.
  */
 synchronized public void reset()
 {
  _activeThreads = 0;
 }

}
--------------------------------------------------------------------------

compilation options used...


gcj -d . -C ThreadPool.java            /* Generate Class File */
gcj -c -d . ThreadPool.java           /* Compile Java File  */
gcjh ThreadPool


gcj -d . -C env_test.java           /* Generate Class File */
gcj -c -d . env_test.java          /* Compile Java File  */
gcjh env_test


g++ -m32 -Wno-deprecated -fPIC 
-I/appl/flstr/d2/fernanr2/MutexLock/TestJVSynchronise -c
TestJVSynchronise19thDec.cc

gcj -o TestJVSynchronise19thDec.exe TestJVSynchronise19thDec.o ThreadPool.o 
env_test.o  -lstdc++


-- 
           Summary: wait() call hangs in _Jv_CondWait taking the monitor
                    with it causing the application to hang
           Product: gcc
           Version: 4.2.2
            Status: UNCONFIRMED
          Severity: critical
          Priority: P3
         Component: libgcj
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: rajathf at techmahindra dot com
 GCC build triplet: Configured with: ./configure --
                    prefix=/software/gcc/4.2.2 --enab
  GCC host triplet: Thread model: posix
GCC target triplet: sparc-sun-solaris2.9


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34574


^ permalink raw reply	[flat|nested] 7+ messages in thread
[parent not found: <bug-34574-8172@http.gcc.gnu.org/bugzilla/>]

end of thread, other threads:[~2015-12-08 17:45 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-12-24 18:24 [Bug libgcj/34574] New: wait() call hangs in _Jv_CondWait taking the monitor with it causing the application to hang rajathf at techmahindra dot com
2007-12-24 18:27 ` [Bug libgcj/34574] " rajathf at techmahindra dot com
2007-12-24 18:58 ` rajathf at techmahindra dot com
2007-12-24 22:12 ` pinskia at gcc dot gnu dot org
     [not found] <bug-34574-8172@http.gcc.gnu.org/bugzilla/>
2013-06-15 16:13 ` pashev.igor at gmail dot com
2015-01-11 16:02 ` pashev.igor at gmail dot com
2015-12-08 17:45 ` ebotcazou at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).