--- old/src/hotspot/share/runtime/threadSMR.hpp Wed Nov 8 08:13:25 2017 +++ new/src/hotspot/share/runtime/threadSMR.hpp Wed Nov 8 08:13:24 2017 @@ -133,8 +133,13 @@ }; // This stack allocated JavaThreadIterator is used to walk the -// specified ThreadsList. +// specified ThreadsList using the following style: // +// JavaThreadIterator jti(t_list); +// for (JavaThread *jt = jti.first(); jt != NULL; jt = jti.next()) { +// ... +// } +// class JavaThreadIterator : public StackObj { ThreadsList * _list; uint _index; @@ -164,5 +169,40 @@ return _list->thread_at(_index); } }; + +// This stack allocated ThreadsListHandle and JavaThreadIterator combo +// is used to walk the ThreadsList in the included ThreadsListHandle +// using the following style: +// +// for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) { +// ... +// } +// +class JavaThreadIteratorWithHandle : public StackObj { + ThreadsListHandle _tlh; + uint _index; + +public: + JavaThreadIteratorWithHandle() : _tlh(), _index(0) {} + + uint length() const { + return _tlh.length(); + } + + ThreadsList *list() const { + return _tlh.list(); + } + + JavaThread *next() { + if (_index >= length()) { + return NULL; + } + return _tlh.list()->thread_at(_index++); + } + + void rewind() { + _index = 0; + } +}; #endif // SHARE_VM_RUNTIME_THREADSMR_HPP