Make Producer.CreateTraceWriter thread-safe

This CL allows the CreateTraceWriter() method of
TracingServiceImpl::ProducerEndpointImpl to be called on any thread.
When using an IPC transport, CreateTraceWriter() is implemented by the
IPC layer and ProducerEndpointImpl's CreateTraceWriter() is not used.
When the Producer lives in-process, instead,
TracingServiceImpl::ProducerEndpointImpl uses a local
SharedMemoryArbiter.

Bug: v8:935550
Change-Id: I31db12f1f2d112b9d88dec8450bb7da6dfb0b959
diff --git a/src/tracing/core/tracing_service_impl.h b/src/tracing/core/tracing_service_impl.h
index a7f06bc..cc9bee6 100644
--- a/src/tracing/core/tracing_service_impl.h
+++ b/src/tracing/core/tracing_service_impl.h
@@ -20,6 +20,7 @@
 #include <functional>
 #include <map>
 #include <memory>
+#include <mutex>
 #include <set>
 
 #include "perfetto/base/gtest_prod_util.h"
@@ -136,8 +137,12 @@
     // before use.
     std::map<WriterID, BufferID> writers_;
 
-    // This is used only in in-process configurations (mostly tests).
+    // This is used only in in-process configurations. The mutex protects
+    // concurrent construction of |inproc_shmem_arbiter_|.
+    // SharedMemoryArbiterImpl methods themselves are thread-safe.
+    std::mutex inproc_shmem_arbiter_mutex_;
     std::unique_ptr<SharedMemoryArbiterImpl> inproc_shmem_arbiter_;
+
     PERFETTO_THREAD_CHECKER(thread_checker_)
     base::WeakPtrFactory<ProducerEndpointImpl> weak_ptr_factory_;  // Keep last.
   };