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

ugly DO_JAVA_THREADS macro should be replaced

XMLWordPrintable

    • b09
    • generic
    • generic

      A code review comment from Stefan K about the "ugly macro":

      5) This macro and the jesting is pretty bad. I complained about it to Erik, and then he confessed that he wrote it :D

      +// Possibly the ugliest for loop the world has seen. C++ does not allow
      +// multiple types in the declaration section of the for loop. In this case
      +// we are only dealing with pointers and hence can cast them. It looks ugly
      +// but macros are ugly and therefore it's fine to make things absurdly ugly.
      +#define DO_JAVA_THREADS(LIST, X) \
      + for (JavaThread *MACRO_scan_interval = (JavaThread*)(uintptr_t)PrefetchScanIntervalInBytes, \
      + *MACRO_list = (JavaThread*)(LIST), \
      + **MACRO_end = ((JavaThread**)((ThreadsList*)MACRO_list)->threads()) + ((ThreadsList*)MACRO_list)->length(), \
      + **MACRO_current_p = (JavaThread**)((ThreadsList*)MACRO_list)->threads(), \
      + *X = (JavaThread*)prefetch_and_load_ptr((void**)MACRO_current_p, (intx)MACRO_scan_interval); \
      + MACRO_current_p != MACRO_end; \
      + MACRO_current_p++, \
      + X = (JavaThread*)prefetch_and_load_ptr((void**)MACRO_current_p, (intx)MACRO_scan_interval))
      +


      This can be rewritten without all these cast, and minimal usage of the macros expansions:

      struct JavaThreadPrefetchedIterator {
          ThreadList* _list;
          JavaThread** _end;
          JavaThread** _current;

          JavaThreadIteror(ThreadList* list) :
            _list(list), _end(list->threads() + list->length()), _current(list->threads()) {}

          JavaThread* current() {
            return context._current != context._end
              ? prefetch_and_load_ptr(context._current, PrefetchScanIntervalInBytes)
              : NULL) // ^ prefetch would be rewritten to return JavaThread* and not void*
          }

          void next() {
            _current++;
        };

      #define DO_JAVA_THREADS(LIST, X) \
        for (JavaThreadPrefetchedIterator iter(LIST); JavaThread* X = iter.current(); iter.next())

      (I did some changes to the code above and haven't compiled this exact version)

            dcubed Daniel Daugherty
            dcubed Daniel Daugherty
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: