Static variables

191 views
Skip to first unread message

Кирилл Люлин

unread,
Mar 18, 2022, 1:33:02 PM3/18/22
to Chromium-dev
Hello, I can't solve one problem.
How to make a static variable in chromium project so that I can change and read it from any place in the code and in any chromium process (there are several of them, one per tab).
If this is not possible, are there any other ways to pass data between processes and tabs?

Lyulin Kirill Andreevich

Daniel Cheng

unread,
Mar 18, 2022, 1:43:53 PM3/18/22
to aleck...@gmail.com, Chromium-dev
One way to pass data between multiple processes is to use Mojo IPC. The specifics of what that involves depends on the particular use case though. For example, data that's associated with a Blink "frame" can be passed over the blink.mojom.LocalFrame / blink.mojom.LocalFrameHost Mojo interfaces--but often, it's passed over an interface registered with the BrowserInterfaceBroker instead.

Shared memory is another possibility; however, the shared memory region itself still needs to be passed over IPC, and shared memory itself has sharp edges, since it requires some way of synchronizing (with locks and/or not-quite-standards-compliant use of atomics), careful consideration of time-of-check time-of-use races, potential info leaks with struct padding, et cetera.

Daniel

--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google Groups "Chromium-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/2747e0f6-bfbd-412d-82ac-859c08229f3bn%40chromium.org.

Chris Hamilton

unread,
Mar 18, 2022, 1:46:31 PM3/18/22
to aleck...@gmail.com, Chromium-dev
You can't simply use a static variable because its storage ends up inside the Chrome binary, and the writable portions of that get their own memory backing for each process in the process tree.

There are many ways to pass data between processes, which in general is referred to as IPC (inter-process communication). In Chrome the preferred mechanism is to use message passing via Mojo, which is our custom message-passing IPC layer. Under the hood it uses different mechanisms on each platform (each platform typically has many different mechanisms available). Another way which maybe fits closer to the way you want things to work (simply read and write to a variable in memory) is to use shared memory. Chrome's shared memory primitives (which abstract away per-platform differences as much as possible) are here.

When using SharedMemory you'll still need a way for the various processes to "rendezvous" and find the same shared memory region... on some platforms that can be via "named" shared memory segments (rendezvous happens because the processes all know the "name" of the segment ahead of time and can ask the OS for it), or via an anonymous shared memory segment and some type of message passing. Chrome tends to use the latter mechanism. You would create the shared memory segment in the browser process (the root of the process tree), and pass it on to child processes either via a Mojo message (which directly supports shared memory segments) or even via raw handles as integers over the command-line. Using shared memory has its own set of problems because you'll need to carefully consider synchronization to make sure you don't have races between the different processes, etc. 

--

Кирилл Люлин

unread,
Mar 18, 2022, 1:47:29 PM3/18/22
to Chromium-dev, Daniel Cheng, Chromium-dev, Кирилл Люлин
Ohh, this is very difficult for me. I don't know much about C++ and chromium. As I understand there is no easy way? I don't need thread-safe code, therefore no locks 

пятница, 18 марта 2022 г. в 20:43:53 UTC+3, Daniel Cheng:

Кирилл Люлин

unread,
Mar 18, 2022, 1:50:20 PM3/18/22
to Chromium-dev, Chris Hamilton, Chromium-dev, Кирилл Люлин
Yes, it's probably better to use a simpler approach with SharedMemory, could you tell me how to do that?

пятница, 18 марта 2022 г. в 20:46:31 UTC+3, Chris Hamilton:

Chris Hamilton

unread,
Mar 18, 2022, 1:54:58 PM3/18/22
to aleck...@gmail.com, Chromium-dev, Daniel Cheng
On Fri, Mar 18, 2022 at 1:47 PM Кирилл Люлин <aleck...@gmail.com> wrote:
Ohh, this is very difficult for me. I don't know much about C++ and chromium. As I understand there is no easy way? I don't need thread-safe code, therefore no locks 

If you plan on reading the variables in some processes and writing them from others (hence, doing inter-process communication) then by definition you need to worry about thread-safety as each process is running entirely separate threads. Locks are one way to achieve this (but you'll need to use cross-process lock primitives), but so are atomics, as pointed out by Daniel. There are special cases: on specific hardware, writes and reads of specific sizes with specific alignments are guaranteed to be atomic by the hardware, but you can't rely on these sorts of things in a multi-OS multi-hardware product like Chrome.
 

Кирилл Люлин

unread,
Mar 18, 2022, 1:56:42 PM3/18/22
to Chromium-dev, Chris Hamilton, Chromium-dev, Daniel Cheng, Кирилл Люлин
No, still in my case, synchronization is not needed, I will write the data at the start of the browser, and then I will only read them. Maybe for this kind of use, you can think of something else besides SharedMemory

пятница, 18 марта 2022 г. в 20:54:58 UTC+3, Chris Hamilton:

Кирилл Люлин

unread,
Mar 18, 2022, 1:58:00 PM3/18/22
to Chromium-dev, Chris Hamilton, Chromium-dev, Daniel Cheng, Кирилл Люлин
If we talk about the general problem I want to solve is this:
1. Read data from a file on the disk, write it to RAM (at the start of the program)
2. Further from any place in the program to access the read data 

пятница, 18 марта 2022 г. в 20:54:58 UTC+3, Chris Hamilton:
On Fri, Mar 18, 2022 at 1:47 PM Кирилл Люлин <aleck...@gmail.com> wrote:

K. Moon

unread,
Mar 18, 2022, 2:14:59 PM3/18/22
to aleck...@gmail.com, Chromium-dev, Chris Hamilton, Daniel Cheng
You might want to read and understand Chromium's architecture first:

Chromium isn't a simple project, and what you want to do is hard by design.

Кирилл Люлин

unread,
Mar 18, 2022, 2:16:42 PM3/18/22
to Chromium-dev, km...@chromium.org, Chromium-dev, Chris Hamilton, Daniel Cheng, Кирилл Люлин
Okay, thank you very much. I'll try to think of something else.

пятница, 18 марта 2022 г. в 21:14:59 UTC+3, km...@chromium.org:
Reply all
Reply to author
Forward
0 new messages