JDK-8193559 : ugly DO_JAVA_THREADS macro should be replaced
  • Type: Enhancement
  • Component: hotspot
  • Sub-Component: runtime
  • Affected Version: 10
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2017-12-14
  • Updated: 2021-11-09
  • Resolved: 2021-08-02
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 18
18 b09Fixed
Related Reports
Blocks :  
Relates :  
Description
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) 
Comments
Changeset: 0a852363 Author: Daniel D. Daugherty <dcubed@openjdk.org> Date: 2021-08-02 16:01:27 +0000 URL: https://git.openjdk.java.net/jdk/commit/0a85236396c667c8d2c890e4384c623b39455075
02-08-2021