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

as_Worker_thread() doesn't check what it intends

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P4
    • 17
    • 16
    • hotspot
    • None
    • gc
    • b25

    Description

      In the Thread class we have:

       virtual WorkerThread* as_Worker_thread() const { return NULL; }

      and then in WorkerThread we have:

        virtual WorkerThread* as_Worker_thread() const {
          assert(is_Worker_thread(), "Dubious cast to WorkerThread*?");
          return (WorkerThread*) this;
        }

      The intent is clear, but the use of virtual functions means this doesn't work as expected. The assertion can never fire because this is the implementation inside the WorkerThread class so you only call this method when you are guaranteed to have a WorkerThread instance. If you happen to call as_Worker_thread on a non-WorkerThread you will just get NULL and no assertion.

      If you wanted to use virtual functions then the correct way to implement this would be:

      In Thread class:

       virtual WorkerThread* as_Worker_thread() const {
          assert(false, "incorrect cast to WorkerThread");
          return NULL;
       }

      and in WorkerThread class simply:

       virtual WorkerThread* as_Worker_thread() const { return this; }

      Or without virtual functions you use the same pattern as as_Compiler_thread (relative to JavaThread) and simply have the following in the Thread class:

        WorkerThread* as_Worker_thread() const {
          assert(is_Worker_thread(), "Dubious cast to WorkerThread*?");
          return (WorkerThread*) this;
        }

      Attachments

        Issue Links

          Activity

            People

              ayang Albert Yang
              dholmes David Holmes
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: