1 /*
   2  * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * Common AWT definitions
  28  */
  29 
  30 #ifndef _AWT_
  31 #define _AWT_
  32 
  33 #include "jvm.h"
  34 #include "jni_util.h"
  35 #include "debug_util.h"
  36 
  37 #ifndef HEADLESS
  38 #include <X11/Intrinsic.h>
  39 #endif /* !HEADLESS */
  40 
  41 
  42 /* The JVM instance: defined in awt_MToolkit.c */
  43 extern JavaVM *jvm;
  44 
  45 extern jclass tkClass;
  46 extern jmethodID awtLockMID;
  47 extern jmethodID awtUnlockMID;
  48 extern jmethodID awtWaitMID;
  49 extern jmethodID awtNotifyMID;
  50 extern jmethodID awtNotifyAllMID;
  51 extern jboolean awtLockInited;
  52 
  53 /* Perform sanity and consistency checks on AWT locking */
  54 #ifdef DEBUG
  55 #define DEBUG_AWT_LOCK
  56 #endif
  57 
  58 /*
  59  * The following locking primitives should be defined
  60  *
  61 #define AWT_LOCK()
  62 #define AWT_NOFLUSH_UNLOCK()
  63 #define AWT_WAIT(tm)
  64 #define AWT_NOTIFY()
  65 #define AWT_NOTIFY_ALL()
  66  */
  67 
  68 /*
  69  * Convenience macros based on AWT_NOFLUSH_UNLOCK
  70  */
  71 extern void awt_output_flush();
  72 #define AWT_UNLOCK() AWT_FLUSH_UNLOCK()
  73 #define AWT_FLUSH_UNLOCK() do {                 \
  74     awt_output_flush();                         \
  75     AWT_NOFLUSH_UNLOCK();                       \
  76 } while (0)
  77 
  78 #define AWT_LOCK_IMPL() \
  79     (*env)->CallStaticVoidMethod(env, tkClass, awtLockMID)
  80 #define AWT_NOFLUSH_UNLOCK_IMPL() \
  81     (*env)->CallStaticVoidMethod(env, tkClass, awtUnlockMID)
  82 #define AWT_WAIT_IMPL(tm) \
  83     (*env)->CallStaticVoidMethod(env, tkClass, awtWaitMID, (jlong)(tm))
  84 #define AWT_NOTIFY_IMPL() \
  85     (*env)->CallStaticVoidMethod(env, tkClass, awtNotifyMID)
  86 #define AWT_NOTIFY_ALL_IMPL() \
  87     (*env)->CallStaticVoidMethod(env, tkClass, awtNotifyAllMID)
  88 
  89 /*
  90  * Unfortunately AWT_LOCK debugging does not work with XAWT due to mixed
  91  * Java/C use of AWT lock.
  92  */
  93 #if defined(DEBUG_AWT_LOCK) && !defined(XAWT)
  94 extern int awt_locked;
  95 extern char *lastF;
  96 extern int lastL;
  97 
  98 #define AWT_LOCK() do {                                                 \
  99     if (!awtLockInited) {                                               \
 100         jio_fprintf(stderr, "AWT lock error, awt_lock is null\n");      \
 101     }                                                                   \
 102     if (awt_locked < 0) {                                               \
 103         jio_fprintf(stderr,                                             \
 104                     "AWT lock error (%s,%d) (last held by %s,%d) %d\n", \
 105                     __FILE__, __LINE__, lastF, lastL, awt_locked);      \
 106     }                                                                   \
 107     lastF = __FILE__;                                                   \
 108     lastL = __LINE__;                                                   \
 109     AWT_LOCK_IMPL();                                                    \
 110     ++awt_locked;                                                       \
 111 } while (0)
 112 
 113 #define AWT_NOFLUSH_UNLOCK() do {                               \
 114     lastF = "";                                                 \
 115     lastL = -1;                                                 \
 116     if (awt_locked < 1) {                                       \
 117         jio_fprintf(stderr, "AWT unlock error (%s,%d,%d)\n",    \
 118                     __FILE__, __LINE__, awt_locked);            \
 119     }                                                           \
 120     --awt_locked;                                               \
 121     AWT_NOFLUSH_UNLOCK_IMPL();                                  \
 122 } while (0)
 123 
 124 #define AWT_WAIT(tm) do {                                       \
 125     int old_lockcount = awt_locked;                             \
 126     if (awt_locked < 1) {                                       \
 127         jio_fprintf(stderr, "AWT wait error (%s,%d,%d)\n",      \
 128                     __FILE__, __LINE__, awt_locked);            \
 129     }                                                           \
 130     awt_locked = 0;                                             \
 131     AWT_WAIT_IMPL(tm);                                          \
 132     awt_locked = old_lockcount;                                 \
 133 } while (0)
 134 
 135 #define AWT_NOTIFY() do {                                       \
 136     if (awt_locked < 1) {                                       \
 137         jio_fprintf(stderr, "AWT notify error (%s,%d,%d)\n",    \
 138                     __FILE__, __LINE__, awt_locked);            \
 139     }                                                           \
 140     AWT_NOTIFY_IMPL();                                          \
 141 } while(0)
 142 
 143 #define AWT_NOTIFY_ALL() do {                                           \
 144     if (awt_locked < 1) {                                               \
 145         jio_fprintf(stderr, "AWT notify all error (%s,%d,%d)\n",        \
 146                     __FILE__, __LINE__, awt_locked);                    \
 147     }                                                                   \
 148     AWT_NOTIFY_ALL_IMPL();                                              \
 149 } while (0)
 150 
 151 #else
 152 
 153 #define AWT_LOCK()           AWT_LOCK_IMPL()
 154 #define AWT_NOFLUSH_UNLOCK() AWT_NOFLUSH_UNLOCK_IMPL()
 155 #define AWT_WAIT(tm)         AWT_WAIT_IMPL(tm)
 156 #define AWT_NOTIFY()         AWT_NOTIFY_IMPL()
 157 #define AWT_NOTIFY_ALL()     AWT_NOTIFY_ALL_IMPL()
 158 
 159 #endif /* DEBUG_AWT_LOCK && !XAWT */
 160 
 161 #ifndef HEADLESS
 162 extern Display         *awt_display; /* awt_GraphicsEnv.c */
 163 extern Boolean          awt_ModLockIsShiftLock; /* XToolkit.c */
 164 #endif /* !HEADLESS */
 165 
 166 #endif /* ! _AWT_ */