Implement native_synchronization within dcli/dcli/lib/src/process/process#1
Conversation
tsavo-at-pieces
commented
Mar 20, 2024
- Added native_synchronization package
- Deprecated internal mailboxes.dart
- Updated everything related to Mailboxes in the process directory within the internal dcli package
- Tested ./dcli/test/src/process/process/synchronous_test.dart on MacOS (Intel & Apple Silicon), Windows, and Linux (Ubuntu)
tsavo-at-pieces
left a comment
There was a problem hiding this comment.
Added permalink references to the relevant native_synchronization code
| import 'package:dcli_core/dcli_core.dart'; | ||
|
|
||
| import 'process/process_sync.dart'; | ||
| // import 'process/process_sync.dart'; |
There was a problem hiding this comment.
Seemed like an used import so I commented it out for now.
| final List<List<int>> stderrLines = <List<int>>[]; | ||
| int get sendAddress => send.rawAddress; | ||
| int get responseAddress => response.rawAddress; | ||
| // int get sendAddress => send.rawAddress; |
There was a problem hiding this comment.
These are no longer necessary.
| /// check the data has been sent to the spawned process | ||
| /// before we return | ||
| final response = send.takeOneMessage(); | ||
| final response = send.take(); |
There was a problem hiding this comment.
| /// Starts an isolate that spawns the command. | ||
| void _startIsolate(ProcessSettings processSettings, ProcessChannel channel) { | ||
| unawaited(Isolate.spawn((mailboxAddrs) async { | ||
| unawaited(Isolate.spawn<List<Sendable<Mailbox>>>((mailboxes) async { |
There was a problem hiding this comment.
Strong type to accept list of Sendable i.e. send and response.
Reference here: https://github.com/dart-lang/native_synchronization/blob/fc4ee3f3b5cdd439ed4c18f2003468ceb24e6458/lib/mailbox.dart#L130
| /// This code runs in the isolate. | ||
| final sendMailbox = Mailbox.fromAddress(mailboxAddrs[0]); | ||
| final responseMailbox = Mailbox.fromAddress(mailboxAddrs[1]); | ||
| final sendMailbox = mailboxes.first.materialize(); |
There was a problem hiding this comment.
Re-hydrate the mailboxes within the spawned isolate which was passed as a Sendable
| /// The tell the sender that we got their data and | ||
| /// sent it to stdin | ||
| sendMailbox.respond(Uint8List.fromList([ProcessChannel.RECEIVED])); | ||
| sendMailbox.put(Uint8List.fromList([ProcessChannel.RECEIVED])); |
There was a problem hiding this comment.
Send message back to main thread with 'put(..)'
Reference Here:
https://github.com/dart-lang/native_synchronization/blob/fc4ee3f3b5cdd439ed4c18f2003468ceb24e6458/lib/mailbox.dart#L71
| channel.responseAddress, | ||
| ])); | ||
| List<Sendable<Mailbox>>.from([ | ||
| channel.send.asSendable, |
There was a problem hiding this comment.
Pass along channel mailboxes 'asSendables'
Reference here: https://github.com/dart-lang/native_synchronization/blob/fc4ee3f3b5cdd439ed4c18f2003468ceb24e6458/lib/mailbox.dart#L130
| channel.send.asSendable, | ||
| channel.response.asSendable, | ||
| ]), | ||
| debugName: 'ProcessInIsolate')); |
There was a problem hiding this comment.
Can likely remove.
| void listenStdout(void Function(List<int>) callback) { | ||
| _channel.listenStdout((data) { | ||
| print('processSync recieved data from channel'); | ||
| // print('processSync recieved data from channel'); |
There was a problem hiding this comment.
Can comment back in if needed.