OSDN Git Service

2007-04-16 Andrew Haley <aph@redhat.com>
[pf3gnuchains/gcc-fork.git] / libjava / gnu / gcj / runtime / natFinalizerThread.cc
1 // natFinalizerThread.cc - Implementation of FinalizerThread native methods.
2
3 /* Copyright (C) 2001, 2004  Free Software Foundation
4
5    This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
9 details.  */
10
11 #include <config.h>
12
13 #include <gcj/cni.h>
14 #include <jvm.h>
15
16 #include <gnu/gcj/runtime/FinalizerThread.h>
17
18 #include <java-threads.h>
19
20 static _Jv_Mutex_t mutex;
21 static _Jv_ConditionVariable_t condition;
22
23 // Initialize lock & condition variable.
24 void
25 gnu::gcj::runtime::FinalizerThread::init ()
26 {
27   _Jv_MutexInit (&mutex);
28   _Jv_CondInit (&condition);
29 }
30
31 // This is called by the GC when a finalizer is ready to be
32 // run.  It sets a flag and wakes up the finalizer thread. Note
33 // that this MUST NOT aquire any Java lock, as this could result in 
34 // the hash synchronization code being re-entered: the synchronization
35 // code itself might need to allocate. See PR 16478.
36 void
37 gnu::gcj::runtime::FinalizerThread::finalizerReady ()
38 {
39 #ifdef __JV_NO_THREADS__
40   _Jv_RunFinalizers ();
41 #else
42   _Jv_MutexLock (&mutex);
43   finalizer_ready = true;
44   _Jv_CondNotify (&condition, &mutex);
45   _Jv_MutexUnlock (&mutex);
46 #endif
47 }
48
49 // Main loop for the finalizer thread. 
50 void
51 gnu::gcj::runtime::FinalizerThread::run ()
52 {
53   while (true)
54     {
55       _Jv_MutexLock (&mutex);
56       if (! finalizer_ready)
57         _Jv_CondWait (&condition, &mutex, 0, 0);
58       finalizer_ready = false;
59       _Jv_MutexUnlock (&mutex);
60       _Jv_RunFinalizers ();
61     }
62 }