This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Add RuntimeCell and RuntimeRefCell
#11155
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| // This file is part of Substrate. | ||
|
|
||
| // Copyright (C) 2017-2022 Parity Technologies (UK) Ltd. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| //! Types used to sync access to shared data. All of these types rely on the assumption | ||
| //! that no data parallelism exists within the runtime. | ||
|
|
||
| use sp_std::{ | ||
| cell::{Cell, RefCell}, | ||
| ops::{Deref, DerefMut}, | ||
| }; | ||
|
|
||
| /// A [`Sync`] version of [`Cell`]. Safe to use within the runtime. | ||
| pub struct RuntimeCell<T>(Cell<T>); | ||
|
|
||
| impl<T> RuntimeCell<T> { | ||
| /// See [`Cell::new`] | ||
| pub const fn new(value: T) -> Self { | ||
| Self(Cell::new(value)) | ||
| } | ||
| } | ||
|
|
||
| unsafe impl<T> Sync for RuntimeCell<T> {} | ||
|
|
||
| impl<T> Deref for RuntimeCell<T> { | ||
| type Target = Cell<T>; | ||
|
|
||
| fn deref(&self) -> &Self::Target { | ||
| &self.0 | ||
| } | ||
| } | ||
|
|
||
| impl<T> DerefMut for RuntimeCell<T> { | ||
| fn deref_mut(&mut self) -> &mut Self::Target { | ||
| &mut self.0 | ||
| } | ||
| } | ||
|
|
||
| /// A [`Sync`] version of [`RefCell`]. Safe to use within the runtime. | ||
| pub struct RuntimeRefCell<T>(RefCell<T>); | ||
|
|
||
| impl<T> RuntimeRefCell<T> { | ||
| /// See [`RefCell::new`] | ||
| pub const fn new(value: T) -> Self { | ||
| Self(RefCell::new(value)) | ||
| } | ||
| } | ||
|
|
||
| unsafe impl<T> Sync for RuntimeRefCell<T> {} | ||
|
|
||
| impl<T> Deref for RuntimeRefCell<T> { | ||
| type Target = RefCell<T>; | ||
|
|
||
| fn deref(&self) -> &Self::Target { | ||
| &self.0 | ||
| } | ||
| } | ||
|
|
||
| impl<T> DerefMut for RuntimeRefCell<T> { | ||
| fn deref_mut(&mut self) -> &mut Self::Target { | ||
| &mut self.0 | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As long as we have the native runtime, this isn't safe. The native runtime can be executed from different threads.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really? Multiple threads operate on the same memory at the same time? Why would that be the case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Native runtime. This static is shared within the entire context of the binary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure but you still don't call into any runtime code concurrently with more than one thread? That would make no sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not the runtime itself, however you can call from different threads into the runtime. Imagine some thread importing a block and another validating an extrinsic. Both will call into the runtime
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume in the wasm case we spawn different instances for those calls which are then called in parallel.
To me it sounds like those calls need to be serialized to have the same behavior as with the wasm runtime.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do you want to serialize them? The point is, we don't use any global context and if we use it it needs to be thread local.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay I get it now. The native runtime recycles the memory from other calls while the wasm gets fresh memory. Yeah this makes this wrong, unfortunately.