Skip to content

transfer package:native_synchronization #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
6a24a65
first commit
devoncarew Jul 11, 2023
5183e24
Update workflow to run tests on all OSes (dart-lang/native_synchroniz…
mraleph Aug 7, 2023
473f72a
Initial implementation of the package. (dart-lang/native_synchronizat…
mraleph Aug 9, 2023
ef45ca3
Prepare to publish v0.1.0 (dart-lang/native_synchronization#4)
mraleph Aug 9, 2023
8de76ba
Lower SDK lower bound to 3.0.0 (dart-lang/native_synchronization#5)
mraleph Aug 9, 2023
161490d
blast_repo fixes (dart-lang/native_synchronization#6)
devoncarew Aug 11, 2023
bc4ca39
add standard markdown badges (dart-lang/native_synchronization#7)
devoncarew Aug 11, 2023
d0f7ba0
Bump actions/checkout from 3.5.3 to 3.6.0 (dart-lang/native_synchroni…
dependabot[bot] Sep 1, 2023
1cc5a3f
Bump dart-lang/setup-dart from 1.5.0 to 1.5.1 (dart-lang/native_synch…
dependabot[bot] Oct 1, 2023
9e462b0
Bump actions/checkout from 3.6.0 to 4.1.0 (dart-lang/native_synchroni…
dependabot[bot] Oct 2, 2023
d7a6986
Bump actions/checkout from 4.1.0 to 4.1.1 (dart-lang/native_synchroni…
dependabot[bot] Nov 1, 2023
ebeb1bb
Bump dart-lang/setup-dart from 1.5.1 to 1.6.0 (dart-lang/native_synch…
dependabot[bot] Nov 1, 2023
1df8cc4
Bump dart-lang/setup-dart from 1.6.0 to 1.6.2 (dart-lang/native_synch…
dependabot[bot] Feb 1, 2024
044a655
Bump actions/checkout from 4.1.1 to 4.1.2 (dart-lang/native_synchroni…
dependabot[bot] Apr 1, 2024
44041f1
Bump actions/checkout from 4.1.2 to 4.1.4 (dart-lang/native_synchroni…
dependabot[bot] May 1, 2024
c2d1493
Bump dart-lang/setup-dart from 1.6.2 to 1.6.4 (dart-lang/native_synch…
dependabot[bot] May 2, 2024
2c6c4ce
blast_repo fixes (dart-lang/native_synchronization#21)
devoncarew May 9, 2024
dec7c2d
Bump actions/checkout from 4.1.4 to 4.1.5 in the github-actions group…
dependabot[bot] May 9, 2024
e096914
Bump actions/checkout from 4.1.5 to 4.1.6 in the github-actions group…
dependabot[bot] Jun 1, 2024
dc5c91d
Bump the github-actions group with 2 updates (dart-lang/native_synchr…
dependabot[bot] Jul 1, 2024
80894f7
Add closed state to Mailbox (dart-lang/native_synchronization#26)
ntkme Aug 1, 2024
f09bb07
Merge package:native_synchronization into shared tool repository
devoncarew Sep 23, 2024
393c076
update github workflow files
devoncarew Sep 23, 2024
44d4a91
update the status badge
devoncarew Sep 26, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions .github/workflows/native_synchronization.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: package:native_synchronization
permissions: read-all

on:
pull_request:
branches: [ main ]
paths:
- '.github/workflows/native_synchronization.yml'
- 'pkgs/native_synchronization/**'
push:
branches: [ main ]
paths:
- '.github/workflows/native_synchronization.yml'
- 'pkgs/native_synchronization/**'
schedule:
- cron: '0 0 * * 0' # weekly

jobs:
analyze:
runs-on: ${{ matrix.os }}
defaults:
run:
working-directory: pkgs/native_synchronization
strategy:
matrix:
os: [ubuntu-latest]
sdk: [dev, stable]

steps:
# These are the latest versions of the github actions; dependabot will
# send PRs to keep these up-to-date.
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332
- uses: dart-lang/setup-dart@0a8a0fc875eb934c15d08629302413c671d3f672
with:
sdk: ${{ matrix.sdk }}

- name: Install dependencies
run: dart pub get

- name: Verify formatting
run: dart format --output=none --set-exit-if-changed .

- name: Analyze project source
run: dart analyze --fatal-infos

test:
needs: analyze
runs-on: ${{ matrix.os }}
defaults:
run:
working-directory: pkgs/native_synchronization
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
sdk: [dev, stable]
steps:
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332
- uses: dart-lang/setup-dart@0a8a0fc875eb934c15d08629302413c671d3f672
with:
sdk: ${{ matrix.sdk }}

- name: Install dependencies
run: dart pub get

- name: Run tests
run: dart test
13 changes: 13 additions & 0 deletions pkgs/native_synchronization/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Files and directories created by pub.
.dart_tool/
.packages

# Conventional directory for build outputs.
build/

# Omit committing pubspec.lock for library packages; see
# https://dart.dev/guides/libraries/private-files#pubspeclock.
pubspec.lock

# VSCode configuration files
.vscode/
13 changes: 13 additions & 0 deletions pkgs/native_synchronization/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## 0.3.0

- Add a closed state to `Mailbox`.

## 0.2.0

- Lower SDK lower bound to 3.0.0.

## 0.1.0

- Initial version.
- Expose `Mutex` and `ConditionVariable`
- Implement `Mailbox`.
27 changes: 27 additions & 0 deletions pkgs/native_synchronization/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Copyright 2023, the Dart project authors.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of Google LLC nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 changes: 24 additions & 0 deletions pkgs/native_synchronization/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[![Dart](https://github.com/dart-lang/native_synchronization/actions/workflows/dart.yaml/badge.svg)](https://github.com/dart-lang/native_synchronization/actions/workflows/dart.yaml)
[![pub package](https://img.shields.io/pub/v/native_synchronization.svg)](https://pub.dev/packages/native_synchronization)
[![package publisher](https://img.shields.io/pub/publisher/native_synchronization.svg)](https://pub.dev/packages/native_synchronization/publisher)

This package exposes a portable interface for low-level thread
synchronization primitives like `Mutex` and `ConditionVariable`.

It also provides some slightly more high-level synchronization primitives
like `Mailbox` built on top of low-level primitives.

## Status: experimental

**NOTE**: This package is currently experimental and published under the
[labs.dart.dev](https://dart.dev/dart-team-packages) pub publisher in order to
solicit feedback.

For packages in the labs.dart.dev publisher we generally plan to either graduate
the package into a supported publisher (dart.dev, tools.dart.dev) after a period
of feedback and iteration, or discontinue the package. These packages have a
much higher expected rate of API and breaking changes.

Your feedback is valuable and will help us evolve this package. For general
feedback, suggestions, and comments, please file an issue in the
[bug tracker](https://github.com/dart-lang/native_synchronization/issues).
1 change: 1 addition & 0 deletions pkgs/native_synchronization/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: package:dart_flutter_team_lints/analysis_options.yaml
158 changes: 158 additions & 0 deletions pkgs/native_synchronization/lib/mailbox.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:ffi';
import 'dart:typed_data';

import 'package:ffi/ffi.dart';

import 'package:native_synchronization/primitives.dart';
import 'package:native_synchronization/sendable.dart';

final class _MailboxRepr extends Struct {
external Pointer<Uint8> buffer;

@Int32()
external int bufferLength;

@Int32()
external int state;
}

class _SendableMailbox {
final int address;
final Sendable<Mutex> mutex;
final Sendable<ConditionVariable> condVar;

_SendableMailbox(
{required this.address, required this.mutex, required this.condVar});
}

/// Mailbox communication primitive.
///
/// This synchronization primitive allows a single producer to send messages
/// to one or more consumers. Producer uses [put] to place a message into
/// a mailbox which consumers can then [take] out.
///
/// [Mailbox] object can not be directly sent to other isolates via a
/// `SendPort`, but it can be converted to a `Sendable<Mailbox>` via
/// `asSendable` getter.
///
/// [Mailbox] object is owned by an isolate which created them.
class Mailbox {
final Pointer<_MailboxRepr> _mailbox;
final Mutex _mutex;
final ConditionVariable _condVar;

static const _stateEmpty = 0;
static const _stateFull = 1;
static const _stateClosed = 2;

static final finalizer = Finalizer((Pointer<_MailboxRepr> mailbox) {
calloc.free(mailbox.ref.buffer);
calloc.free(mailbox);
});

Mailbox()
: _mailbox = calloc.allocate(sizeOf<_MailboxRepr>()),
_mutex = Mutex(),
_condVar = ConditionVariable() {
finalizer.attach(this, _mailbox);
}

Mailbox._fromSendable(_SendableMailbox sendable)
: _mailbox = Pointer.fromAddress(sendable.address),
_mutex = sendable.mutex.materialize(),
_condVar = sendable.condVar.materialize();

/// Place a message into the mailbox if has space for it.
///
/// If mailbox already contains a message or mailbox is closed then [put] will
/// throw [StateError].
void put(Uint8List message) {
final buffer = message.isEmpty ? nullptr : _toBuffer(message);
_mutex.runLocked(() {
if (_mailbox.ref.state != _stateEmpty) {
throw StateError('Mailbox is closed or full');
}

_mailbox.ref.state = _stateFull;
_mailbox.ref.buffer = buffer;
_mailbox.ref.bufferLength = message.length;

_condVar.notify();
});
}

/// Close a mailbox.
///
/// If mailbox already contains a message then [close] will drop the message.
void close() => _mutex.runLocked(() {
if (_mailbox.ref.state == _stateFull && _mailbox.ref.bufferLength > 0) {
malloc.free(_mailbox.ref.buffer);
}

_mailbox.ref.state = _stateClosed;
_mailbox.ref.buffer = nullptr;
_mailbox.ref.bufferLength = 0;

_condVar.notify();
});

/// Take a message from the mailbox.
///
/// If mailbox is empty then [take] will synchronously block until message
/// is available or mailbox is closed. If mailbox is closed then [take] will
/// throw [StateError].
Uint8List take() => _mutex.runLocked(() {
while (_mailbox.ref.state == _stateEmpty) {
_condVar.wait(_mutex);
}

if (_mailbox.ref.state == _stateClosed) {
throw StateError('Mailbox is closed');
}

final result = _toList(_mailbox.ref.buffer, _mailbox.ref.bufferLength);

_mailbox.ref.state = _stateEmpty;
_mailbox.ref.buffer = nullptr;
_mailbox.ref.bufferLength = 0;
return result;
});

static final _emptyResponse = Uint8List(0);

static Uint8List _toList(Pointer<Uint8> buffer, int length) {
if (length == 0) {
return _emptyResponse;
}

// TODO: remove feature detection once 3.1 becomes stable.
// ignore: omit_local_variable_types
final Uint8List Function(int) asTypedList = buffer.asTypedList;
if (asTypedList is Uint8List Function(int,
{Pointer<NativeFinalizerFunction> finalizer})) {
return asTypedList(length, finalizer: malloc.nativeFree);
}

final result = Uint8List(length);
result.setRange(0, length, buffer.asTypedList(length));
malloc.free(buffer);
return result;
}

static Pointer<Uint8> _toBuffer(Uint8List list) {
final buffer = malloc.allocate<Uint8>(list.length);
buffer.asTypedList(list.length).setRange(0, list.length, list);
return buffer;
}

Sendable<Mailbox> get asSendable => Sendable.wrap(
Mailbox._fromSendable,
_SendableMailbox(
address: _mailbox.address,
mutex: _mutex.asSendable,
condVar: _condVar.asSendable));
}
Loading