public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH][gdbsupport] Add sequential_for_each
@ 2022-06-25 13:20 Tom de Vries
  2022-07-14 15:02 ` [comitted][gdbsupport] " Tom de Vries
  2022-07-15 16:38 ` [PATCH][gdbsupport] " Tom Tromey
  0 siblings, 2 replies; 4+ messages in thread
From: Tom de Vries @ 2022-06-25 13:20 UTC (permalink / raw)
  To: gdb-patches

Hi,

Add a sequential_for_each alongside the parallel_for_each, which can be used
as a drop-in replacement.

This can be useful when debugging multi-threading behaviour, and you want to
limit multi-threading in a fine-grained way.

Tested on x86_64-linux, by using it instead of the parallel_for_each in
dwarf2_build_psymtabs_hard.

Any comments?

Thanks,
- Tom

[gdbsupport] Add sequential_for_each

---
 gdbsupport/parallel-for.h | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/gdbsupport/parallel-for.h b/gdbsupport/parallel-for.h
index 7b6891a0dcb..16bca4fe3cf 100644
--- a/gdbsupport/parallel-for.h
+++ b/gdbsupport/parallel-for.h
@@ -172,6 +172,25 @@ parallel_for_each (unsigned n, RandomIt first, RandomIt last,
     });
 }
 
+template<class RandomIt, class RangeFunction>
+typename gdb::detail::par_for_accumulator<
+    typename std::result_of<RangeFunction (RandomIt, RandomIt)>::type
+  >::result_type
+sequential_for_each (unsigned n, RandomIt first, RandomIt last,
+		   RangeFunction callback)
+{
+  using result_type
+    = typename std::result_of<RangeFunction (RandomIt, RandomIt)>::type;
+
+  gdb::detail::par_for_accumulator<result_type> results (0);
+
+  /* Process all the remaining elements in the main thread.  */
+  return results.finish ([=] ()
+    {
+      return callback (first, last);
+    });
+}
+
 }
 
 #endif /* GDBSUPPORT_PARALLEL_FOR_H */

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [comitted][gdbsupport] Add sequential_for_each
  2022-06-25 13:20 [PATCH][gdbsupport] Add sequential_for_each Tom de Vries
@ 2022-07-14 15:02 ` Tom de Vries
  2022-07-15 16:38 ` [PATCH][gdbsupport] " Tom Tromey
  1 sibling, 0 replies; 4+ messages in thread
From: Tom de Vries @ 2022-07-14 15:02 UTC (permalink / raw)
  To: gdb-patches

[-- Attachment #1: Type: text/plain, Size: 470 bytes --]

On 6/25/22 15:20, Tom de Vries wrote:
> Hi,
> 
> Add a sequential_for_each alongside the parallel_for_each, which can be used
> as a drop-in replacement.
> 
> This can be useful when debugging multi-threading behaviour, and you want to
> limit multi-threading in a fine-grained way.
> 
> Tested on x86_64-linux, by using it instead of the parallel_for_each in
> dwarf2_build_psymtabs_hard.
> 
> Any comments?

Added function header comment, and committed.

Thanks,
- Tom

[-- Attachment #2: 0001-gdbsupport-Add-sequential_for_each.patch --]
[-- Type: text/x-patch, Size: 1564 bytes --]

[gdbsupport] Add sequential_for_each

Add a sequential_for_each alongside the parallel_for_each, which can be used
as a drop-in replacement.

This can be useful when debugging multi-threading behaviour, and you want to
limit multi-threading in a fine-grained way.

Tested on x86_64-linux, by using it instead of the parallel_for_each in
dwarf2_build_psymtabs_hard.

---
 gdbsupport/parallel-for.h | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/gdbsupport/parallel-for.h b/gdbsupport/parallel-for.h
index 7b6891a0dcb..a614fc35766 100644
--- a/gdbsupport/parallel-for.h
+++ b/gdbsupport/parallel-for.h
@@ -172,6 +172,29 @@ parallel_for_each (unsigned n, RandomIt first, RandomIt last,
     });
 }
 
+/* A sequential drop-in replacement of parallel_for_each.  This can be useful
+   when debugging multi-threading behaviour, and you want to limit
+   multi-threading in a fine-grained way.  */
+
+template<class RandomIt, class RangeFunction>
+typename gdb::detail::par_for_accumulator<
+    typename std::result_of<RangeFunction (RandomIt, RandomIt)>::type
+  >::result_type
+sequential_for_each (unsigned n, RandomIt first, RandomIt last,
+		   RangeFunction callback)
+{
+  using result_type
+    = typename std::result_of<RangeFunction (RandomIt, RandomIt)>::type;
+
+  gdb::detail::par_for_accumulator<result_type> results (0);
+
+  /* Process all the remaining elements in the main thread.  */
+  return results.finish ([=] ()
+    {
+      return callback (first, last);
+    });
+}
+
 }
 
 #endif /* GDBSUPPORT_PARALLEL_FOR_H */

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH][gdbsupport] Add sequential_for_each
  2022-06-25 13:20 [PATCH][gdbsupport] Add sequential_for_each Tom de Vries
  2022-07-14 15:02 ` [comitted][gdbsupport] " Tom de Vries
@ 2022-07-15 16:38 ` Tom Tromey
  2022-07-15 16:44   ` Tom de Vries
  1 sibling, 1 reply; 4+ messages in thread
From: Tom Tromey @ 2022-07-15 16:38 UTC (permalink / raw)
  To: Tom de Vries via Gdb-patches

Tom> Add a sequential_for_each alongside the parallel_for_each, which can be used
Tom> as a drop-in replacement.

Tom> This can be useful when debugging multi-threading behaviour, and you want to
Tom> limit multi-threading in a fine-grained way.

It's not fine-grained but I typically just set the number of worker
threads to 0.

Tom

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH][gdbsupport] Add sequential_for_each
  2022-07-15 16:38 ` [PATCH][gdbsupport] " Tom Tromey
@ 2022-07-15 16:44   ` Tom de Vries
  0 siblings, 0 replies; 4+ messages in thread
From: Tom de Vries @ 2022-07-15 16:44 UTC (permalink / raw)
  To: Tom Tromey, Tom de Vries via Gdb-patches

On 7/15/22 18:38, Tom Tromey wrote:
> Tom> Add a sequential_for_each alongside the parallel_for_each, which can be used
> Tom> as a drop-in replacement.
> 
> Tom> This can be useful when debugging multi-threading behaviour, and you want to
> Tom> limit multi-threading in a fine-grained way.
> 
> It's not fine-grained but I typically just set the number of worker
> threads to 0.

FWIW, the use case I'm using it for atm is developing some code that 
introduces a new parallel_for_each, and then switching some existing 
parallel_for_each back to sequential to limit the race conditions I'm 
going to run into to a certain scope.

Thanks,
- Tom

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-07-15 16:44 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-25 13:20 [PATCH][gdbsupport] Add sequential_for_each Tom de Vries
2022-07-14 15:02 ` [comitted][gdbsupport] " Tom de Vries
2022-07-15 16:38 ` [PATCH][gdbsupport] " Tom Tromey
2022-07-15 16:44   ` Tom de Vries

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).