|
| 1 | +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:ffi'; |
| 6 | +import 'dart:typed_data'; |
| 7 | + |
| 8 | +import 'package:ffi/ffi.dart'; |
| 9 | + |
| 10 | +import 'package:native_synchronization/primitives.dart'; |
| 11 | +import 'package:native_synchronization/sendable.dart'; |
| 12 | + |
| 13 | +final class _MailboxRepr extends Struct { |
| 14 | + external Pointer<Uint8> buffer; |
| 15 | + |
| 16 | + @Int32() |
| 17 | + external int bufferLength; |
| 18 | + |
| 19 | + @Int32() |
| 20 | + external int state; |
| 21 | +} |
| 22 | + |
| 23 | +class _SendableMailbox { |
| 24 | + final int address; |
| 25 | + final Sendable<Mutex> mutex; |
| 26 | + final Sendable<ConditionVariable> condVar; |
| 27 | + |
| 28 | + _SendableMailbox( |
| 29 | + {required this.address, required this.mutex, required this.condVar}); |
| 30 | +} |
| 31 | + |
| 32 | +/// Mailbox communication primitive. |
| 33 | +/// |
| 34 | +/// This synchronization primitive allows a single producer to send messages |
| 35 | +/// to one or more consumers. Producer uses [put] to place a message into |
| 36 | +/// a mailbox which consumers can then [take] out. |
| 37 | +/// |
| 38 | +/// [Mailbox] object can not be directly sent to other isolates via a |
| 39 | +/// `SendPort`, but it can be converted to a `Sendable<Mailbox>` via |
| 40 | +/// `asSendable` getter. |
| 41 | +/// |
| 42 | +/// [Mailbox] object is owned by an isolate which created them. |
| 43 | +class Mailbox { |
| 44 | + final Pointer<_MailboxRepr> _mailbox; |
| 45 | + final Mutex _mutex; |
| 46 | + final ConditionVariable _condVar; |
| 47 | + |
| 48 | + static const _stateEmpty = 0; |
| 49 | + static const _stateFull = 1; |
| 50 | + static const _stateClosed = 2; |
| 51 | + |
| 52 | + static final finalizer = Finalizer((Pointer<_MailboxRepr> mailbox) { |
| 53 | + calloc.free(mailbox.ref.buffer); |
| 54 | + calloc.free(mailbox); |
| 55 | + }); |
| 56 | + |
| 57 | + Mailbox() |
| 58 | + : _mailbox = calloc.allocate(sizeOf<_MailboxRepr>()), |
| 59 | + _mutex = Mutex(), |
| 60 | + _condVar = ConditionVariable() { |
| 61 | + finalizer.attach(this, _mailbox); |
| 62 | + } |
| 63 | + |
| 64 | + Mailbox._fromSendable(_SendableMailbox sendable) |
| 65 | + : _mailbox = Pointer.fromAddress(sendable.address), |
| 66 | + _mutex = sendable.mutex.materialize(), |
| 67 | + _condVar = sendable.condVar.materialize(); |
| 68 | + |
| 69 | + /// Place a message into the mailbox if has space for it. |
| 70 | + /// |
| 71 | + /// If mailbox already contains a message or mailbox is closed then [put] will |
| 72 | + /// throw [StateError]. |
| 73 | + void put(Uint8List message) { |
| 74 | + final buffer = message.isEmpty ? nullptr : _toBuffer(message); |
| 75 | + _mutex.runLocked(() { |
| 76 | + if (_mailbox.ref.state != _stateEmpty) { |
| 77 | + throw StateError('Mailbox is closed or full'); |
| 78 | + } |
| 79 | + |
| 80 | + _mailbox.ref.state = _stateFull; |
| 81 | + _mailbox.ref.buffer = buffer; |
| 82 | + _mailbox.ref.bufferLength = message.length; |
| 83 | + |
| 84 | + _condVar.notify(); |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + /// Close a mailbox. |
| 89 | + /// |
| 90 | + /// If mailbox already contains a message then [close] will drop the message. |
| 91 | + void close() => _mutex.runLocked(() { |
| 92 | + if (_mailbox.ref.state == _stateFull && _mailbox.ref.bufferLength > 0) { |
| 93 | + malloc.free(_mailbox.ref.buffer); |
| 94 | + } |
| 95 | + |
| 96 | + _mailbox.ref.state = _stateClosed; |
| 97 | + _mailbox.ref.buffer = nullptr; |
| 98 | + _mailbox.ref.bufferLength = 0; |
| 99 | + |
| 100 | + _condVar.notify(); |
| 101 | + }); |
| 102 | + |
| 103 | + /// Take a message from the mailbox. |
| 104 | + /// |
| 105 | + /// If mailbox is empty then [take] will synchronously block until message |
| 106 | + /// is available or mailbox is closed. If mailbox is closed then [take] will |
| 107 | + /// throw [StateError]. |
| 108 | + Uint8List take() => _mutex.runLocked(() { |
| 109 | + while (_mailbox.ref.state == _stateEmpty) { |
| 110 | + _condVar.wait(_mutex); |
| 111 | + } |
| 112 | + |
| 113 | + if (_mailbox.ref.state == _stateClosed) { |
| 114 | + throw StateError('Mailbox is closed'); |
| 115 | + } |
| 116 | + |
| 117 | + final result = _toList(_mailbox.ref.buffer, _mailbox.ref.bufferLength); |
| 118 | + |
| 119 | + _mailbox.ref.state = _stateEmpty; |
| 120 | + _mailbox.ref.buffer = nullptr; |
| 121 | + _mailbox.ref.bufferLength = 0; |
| 122 | + return result; |
| 123 | + }); |
| 124 | + |
| 125 | + static final _emptyResponse = Uint8List(0); |
| 126 | + |
| 127 | + static Uint8List _toList(Pointer<Uint8> buffer, int length) { |
| 128 | + if (length == 0) { |
| 129 | + return _emptyResponse; |
| 130 | + } |
| 131 | + |
| 132 | + // TODO: remove feature detection once 3.1 becomes stable. |
| 133 | + // ignore: omit_local_variable_types |
| 134 | + final Uint8List Function(int) asTypedList = buffer.asTypedList; |
| 135 | + if (asTypedList is Uint8List Function(int, |
| 136 | + {Pointer<NativeFinalizerFunction> finalizer})) { |
| 137 | + return asTypedList(length, finalizer: malloc.nativeFree); |
| 138 | + } |
| 139 | + |
| 140 | + final result = Uint8List(length); |
| 141 | + result.setRange(0, length, buffer.asTypedList(length)); |
| 142 | + malloc.free(buffer); |
| 143 | + return result; |
| 144 | + } |
| 145 | + |
| 146 | + static Pointer<Uint8> _toBuffer(Uint8List list) { |
| 147 | + final buffer = malloc.allocate<Uint8>(list.length); |
| 148 | + buffer.asTypedList(list.length).setRange(0, list.length, list); |
| 149 | + return buffer; |
| 150 | + } |
| 151 | + |
| 152 | + Sendable<Mailbox> get asSendable => Sendable.wrap( |
| 153 | + Mailbox._fromSendable, |
| 154 | + _SendableMailbox( |
| 155 | + address: _mailbox.address, |
| 156 | + mutex: _mutex.asSendable, |
| 157 | + condVar: _condVar.asSendable)); |
| 158 | +} |
0 commit comments