Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8263712

Safely return the results of a TRAPS function call

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Won't Fix
    • Icon: P4 P4
    • tbd
    • 17
    • hotspot

      In JDK-8064811 "Use THREAD instead of CHECK_NULL in return statements", we have introduce this coding style

          int callee(TRAPS);
          int caller(TRAPS) {
            return callee(THREAD);
          }

      However, this is error prone when the caller is modified in the future, like:

          int caller(TRAPS) {
            callee(THREAD);
            do_more_stuff(); // <-- shouldn't come here if exception was thrown!
            return 0;
          }

      This style would be safer:

          int caller(TRAPS) {
            int n = callee(CHECK_0);
            // safe to add code here .....
            return n;
          }

      However, the CHECK_0 macro introduces an unnecessary check:

          int caller(Thread* __thread__) {
            int n = callee(__thread__);
            if (__thread__->_pending_exception != NULL) { return 0; }
            return n;
          }
       
      We should have a style that's both safe (guarantee that the caller returns immediately after the callee has returned) and efficient (no extra check).

      ======== PROPOSAL ===========
      $ cat test.cpp
      class Thread;

      #define TRAPS Thread* __thread__

      #define RETURN(x) \
        { int __in_return_macro__ = 0; \
           return x; \
        }

      inline Thread* must_be_in_return_macro(int dummy, Thread* t) {
        return t;
      }

      #define CHECK_RETURN \
        must_be_in_return_macro(__in_return_macro__, __thread__)

      int may_throw_a(TRAPS);
      int may_throw_b(TRAPS);

      int caller(int n, TRAPS) {
        if (n > 0) {
          RETURN(may_throw_a(CHECK_RETURN));
        } else {
          RETURN(may_throw_b(CHECK_RETURN));
        }
      }
      ========================

      In the future, if we need to add more code after the may_throw_a() call, we must change the CHECK_RETURN macro back to a CHECK_0, or else the code won't compile:

      void caller(int n, TRAPS) {
        if (n > 0) {
         // may_throw_a(CHECK_RETURN); <-- won't compile
         may_throw_a(CHECK_0);
         do_more_stuff();
         ...

            coleenp Coleen Phillimore
            iklam Ioi Lam
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: