Coder Social home page Coder Social logo

dart-lang / native_synchronization Goto Github PK

View Code? Open in Web Editor NEW
26.0 24.0 0.0 34 KB

Low-level synchronization primitives built using dart:ffi.

Home Page: https://pub.dev/packages/native_synchronization

License: BSD 3-Clause "New" or "Revised" License

Dart 100.00%

native_synchronization's Introduction

Dart pub package package 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 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.

native_synchronization's People

Contributors

dependabot[bot] avatar devoncarew avatar mraleph avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

native_synchronization's Issues

put only allows a single message but no way to determine if mailbox is full

I'm trying to use mailbox to implement a replacement for waitFor when running process.

The problem I'm encountering is that a mailbox only allows a single message at time.

Ignoring the performance concerns for a moment my immediate problem is that when I call put it throws an error saying the mailbox is full (the second time put is called). It looks like the api provides no way to determine if the mailbox is full which means I have to call put and then capture an exception which, whilst it works, really doesn't feel like the correct approach.

So in the end I guess to questions:

  1. should the api have a method to determine if the mailbox is full?
  2. should the mailbox allow more than a single message at a time?

Allowing multiple messages would seem to be more performant.

Async lock + waitFor

Hello!
Thank you for the simple and great library. I was considering using it as a way to synchronize writes to a SQLite database from different isolates, since SQLite only supports one writer at a time. Context: simolus3/drift#2760

The problem is that the SQLite library is async, but runLocked doesn't work with async callbacks as stated in the docs.

I managed to "solve" it (at least for my simple demo) by creating an async runLocked + waitFor. The problem is that waitFor is being removed in Dart 3.3.
How could this be solved in an alternative manner without the waitFor?

Thank you!

Future<R> runLockedAsync<R>(Future<R> Function() action) async {
    _lock();
    try {
      return waitFor(action());
    } finally {
      _unlock();
    }
  }

Demo:

import 'dart:isolate';
import 'dart:math';

import 'package:native_synchronization/primitives.dart';

void main() async {
  final mutex = Mutex();
  const numIsolates = 20;

  final fs = <Future>[];
  for (var i = 0; i < numIsolates; i++) {
    fs.add(runInIsolate(mutex, i));
  }

  await Future.wait(fs);
}

Future<void> runInIsolate(Mutex mutex, int id) async {
  final sendableMutex = mutex.asSendable;
  await Isolate.run(debugName: 'isolate_$id', () async {
    final r = Random();
    final mutex = sendableMutex.materialize();
    for (var i = 0; i < 100; i++) {
      await mutex.runLockedAsync(() async {
        print('ASYNC isolate $id inside $i');
        final ms = r.nextInt(50);
        await Future.delayed(Duration(milliseconds: ms));
      });

      await Future.delayed(Duration(milliseconds: 100));
    }
  });
}

repo created

@mraleph @mkustermann - here's the repo for package:native_synchronization. It likely still needs some initial setup wrt the docs and description.

timing issue establishing a mailbox

DCli is attempting to use a mailbox to spawn process synchronously.

My primary isolate spawns a secondary isolate which runs a process.

The primary isolate needs to call take() to receive the secondary isolate's send port.

The issue is that we have a timing problem.

If I call take() in the primary isolate before the secondary isolate has a chance to spawn then take() essentially blocks the secondary isolate from spawning.

I can't use an await when spawning the secondary isolate as the core reason for using a mailbox is to spawn the process synchronously.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.