Skip to content

Commit

Permalink
refactor: use NativeFinalizer (#406)
Browse files Browse the repository at this point in the history
  • Loading branch information
blaugold committed Aug 25, 2022
1 parent dcd522e commit e7a259e
Show file tree
Hide file tree
Showing 48 changed files with 496 additions and 909 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/ci.yaml
Expand Up @@ -113,6 +113,13 @@ jobs:
- name: Setup Melos
run: dart pub global activate melos ${{ env.melos-version }}

- name: Setup CMake and Ninja
uses: lukka/get-cmake@latest

- name: Build native libraries
shell: bash
run: ./tools/ci-steps.sh buildNativeLibraries

- name: Bootstrap Dart package
run: ./tools/ci-steps.sh bootstrapPackage

Expand Down
39 changes: 7 additions & 32 deletions native/couchbase-lite-dart/include/CBL+Dart.h
Expand Up @@ -36,8 +36,11 @@ bool CBLDart_Initialize(void *dartInitializeDlData, void *cblInitContext,
typedef struct _CBLDart_AsyncCallback *CBLDart_AsyncCallback;

CBLDART_EXPORT
CBLDart_AsyncCallback CBLDart_AsyncCallback_New(uint32_t id, Dart_Handle object,
Dart_Port sendPort, bool debug);
CBLDart_AsyncCallback CBLDart_AsyncCallback_New(uint32_t id, Dart_Port sendPort,
bool debug);

CBLDART_EXPORT
void CBLDart_AsyncCallback_Delete(CBLDart_AsyncCallback callback);

CBLDART_EXPORT
void CBLDart_AsyncCallback_Close(CBLDart_AsyncCallback callback);
Expand All @@ -48,27 +51,6 @@ void CBLDart_AsyncCallback_CallForTest(CBLDart_AsyncCallback callback,

// === Couchbase Lite =========================================================

// === Base

/**
* Binds a CBLRefCounted to a Dart objects lifetime.
*
* If \p retain is true the ref counted object will be retained. Otherwise
* it will only be released once the Dart object is garbage collected.
*/
CBLDART_EXPORT
void CBLDart_BindCBLRefCountedToDartObject(Dart_Handle object,
CBLRefCounted *refCounted,
bool retain, char *debugName);

/**
* Sets whether information to debug CBLRefCounted is printed.
*
* This features is only functional in debug builds.
*/
CBLDART_EXPORT
void CBLDart_SetDebugRefCounted(bool enabled);

// === Log

CBLDART_EXPORT
Expand All @@ -95,8 +77,7 @@ CBLDatabase *CBLDart_CBLDatabase_Open(FLString name,
CBLError *errorOut);

CBLDART_EXPORT
void CBLDart_BindDatabaseToDartObject(Dart_Handle object, CBLDatabase *database,
char *debugName);
void CBLDart_CBLDatabase_Release(CBLDatabase *database);

CBLDART_EXPORT
bool CBLDart_CBLDatabase_Close(CBLDatabase *database, bool andDelete,
Expand Down Expand Up @@ -137,10 +118,6 @@ CBLListenerToken *CBLDart_CBLQuery_AddChangeListener(

// === Blob

CBLDART_EXPORT
void CBLDart_BindBlobReadStreamToDartObject(Dart_Handle object,
CBLBlobReadStream *stream);

CBLDART_EXPORT
FLSliceResult CBLDart_CBLBlobReader_Read(CBLBlobReadStream *stream,
uint64_t bufferSize,
Expand Down Expand Up @@ -174,9 +151,7 @@ CBLReplicator *CBLDart_CBLReplicator_Create(
CBLDart_ReplicatorConfiguration *config, CBLError *errorOut);

CBLDART_EXPORT
void CBLDart_BindReplicatorToDartObject(Dart_Handle object,
CBLReplicator *replicator,
char *debugName);
void CBLDart_CBLReplicator_Release(CBLReplicator *replicator);

CBLDART_EXPORT
void CBLDart_CBLReplicator_AddChangeListener(CBLReplicator *replicator,
Expand Down
47 changes: 15 additions & 32 deletions native/couchbase-lite-dart/include/Fleece+Dart.h
Expand Up @@ -11,32 +11,10 @@
// === Slice ==================================================================

CBLDART_EXPORT
void CBLDart_FLSliceResult_BindToDartObject(Dart_Handle object,
FLSliceResult slice, bool retain);
void CBLDart_FLSliceResult_RetainByBuf(void *buf);

CBLDART_EXPORT
void CBLDart_FLSliceResult_Retain(FLSliceResult slice);

CBLDART_EXPORT
void CBLDart_FLSliceResult_Release(FLSliceResult slice);

// === SharedKeys =============================================================

CBLDART_EXPORT
void CBLDart_FLSharedKeys_BindToDartObject(Dart_Handle object,
FLSharedKeys sharedKeys,
bool retain);

// === Doc ====================================================================

CBLDART_EXPORT
void CBLDart_FLDoc_BindToDartObject(Dart_Handle object, FLDoc doc);

// === Value ==================================================================

CBLDART_EXPORT
void CBLDart_FLValue_BindToDartObject(Dart_Handle object, FLValue value,
bool retain);
void CBLDart_FLSliceResult_ReleaseByBuf(void *buf);

// === Decoder ================================================================

Expand All @@ -45,7 +23,10 @@ void CBLDart_FLValue_BindToDartObject(Dart_Handle object, FLValue value,
struct KnownSharedKeys;

CBLDART_EXPORT
KnownSharedKeys *CBLDart_KnownSharedKeys_New(Dart_Handle object);
KnownSharedKeys *CBLDart_KnownSharedKeys_New();

CBLDART_EXPORT
void CBLDart_KnownSharedKeys_Delete(KnownSharedKeys *keys);

struct CBLDart_LoadedDictKey {
bool isKnownSharedKey; // Whether the key has been seen before. For shared
Expand Down Expand Up @@ -90,9 +71,12 @@ struct CBLDart_FLDictIterator;

CBLDART_EXPORT
CBLDart_FLDictIterator *CBLDart_FLDictIterator_Begin(
Dart_Handle object, FLDict dict, KnownSharedKeys *knownSharedKeys,
FLDict dict, KnownSharedKeys *knownSharedKeys,
CBLDart_LoadedDictKey *keyOut, CBLDart_LoadedFLValue *valueOut,
bool finalize, bool preLoad);
bool deleteOnDone, bool preLoad);

CBLDART_EXPORT
void CBLDart_FLDictIterator_Delete(CBLDart_FLDictIterator *iterator);

CBLDART_EXPORT
bool CBLDart_FLDictIterator_Next(CBLDart_FLDictIterator *iterator);
Expand All @@ -101,17 +85,16 @@ struct CBLDart_FLArrayIterator;

CBLDART_EXPORT
CBLDart_FLArrayIterator *CBLDart_FLArrayIterator_Begin(
Dart_Handle object, FLArray array, CBLDart_LoadedFLValue *valueOut,
bool finalize);
FLArray array, CBLDart_LoadedFLValue *valueOut, bool deleteOnDone);

CBLDART_EXPORT
void CBLDart_FLArrayIterator_Delete(CBLDart_FLArrayIterator *iterator);

CBLDART_EXPORT
bool CBLDart_FLArrayIterator_Next(CBLDart_FLArrayIterator *iterator);

// === Encoder ================================================================

CBLDART_EXPORT
void CBLDart_FLEncoder_BindToDartObject(Dart_Handle object, FLEncoder encoder);

CBLDART_EXPORT
bool CBLDart_FLEncoder_WriteArrayValue(FLEncoder encoder, FLArray array,
uint32_t index);
37 changes: 13 additions & 24 deletions native/couchbase-lite-dart/src/AsyncCallback.cpp
Expand Up @@ -50,16 +50,16 @@ AsyncCallbackRegistry::AsyncCallbackRegistry() {}

// === AsyncCallback ==========================================================

AsyncCallback::AsyncCallback(uint32_t id, Dart_Handle dartCallback,
Dart_Port sendport, bool debug)
: id_(id), debug_(debug), sendPort_(sendport) {
dartCallbackHandle_ = Dart_NewWeakPersistentHandle_DL(
dartCallback, this, sizeof(AsyncCallback), AsyncCallback::dartFinalizer);
assert(dartCallbackHandle_ != nullptr);

AsyncCallback::AsyncCallback(uint32_t id, Dart_Port sendPort, bool debug)
: id_(id), debug_(debug), sendPort_(sendPort) {
AsyncCallbackRegistry::instance.registerCallback(*this);
}

AsyncCallback::~AsyncCallback() {
close();
debugLog("deleted");
}

void AsyncCallback::setFinalizer(void *context, CallbackFinalizer finalizer) {
debugLog("setFinalizer");
std::scoped_lock lock(mutex_);
Expand All @@ -71,7 +71,9 @@ void AsyncCallback::setFinalizer(void *context, CallbackFinalizer finalizer) {
void AsyncCallback::close() {
{
std::scoped_lock lock(mutex_);
assert(!closed_);
if (closed_) {
return;
}

// After this point no new calls can be registered.
closed_ = true;
Expand All @@ -83,11 +85,6 @@ void AsyncCallback::close() {
finalizerContext_ = nullptr;
}

if (dartCallbackHandle_) {
Dart_DeleteWeakPersistentHandle_DL(dartCallbackHandle_);
dartCallbackHandle_ = nullptr;
}

{
std::scoped_lock lock(mutex_);
// Close calls which are executing or could be executed.
Expand All @@ -105,14 +102,6 @@ void AsyncCallback::close() {
debugLog("closed");
}

void AsyncCallback::dartFinalizer(void *dart_callback_data, void *peer) {
auto callback = reinterpret_cast<AsyncCallback *>(peer);
callback->debugLog("closing from Dart finalizer");
callback->dartCallbackHandle_ = nullptr;
callback->close();
delete callback;
}

void AsyncCallback::registerCall(AsyncCallbackCall &call) {
assert(AsyncCallbackRegistry::instance.callbackExists(*this));

Expand All @@ -134,8 +123,8 @@ void AsyncCallback::unregisterCall(AsyncCallbackCall &call) {
}

bool AsyncCallback::sendRequest(Dart_CObject *request) {
// If the send port and therefore the callack is closed before the request can
// be sent, this call retruns false. This allows us to avoid calling this
// If the send port and therefore the callback is closed before the request
// can be sent, this call returns false. This allows us to avoid calling this
// function under a lock.
return Dart_PostCObject_DL(sendPort_, request);
}
Expand Down Expand Up @@ -284,7 +273,7 @@ void AsyncCallbackCall::close() {
auto didTakeCall = AsyncCallbackRegistry::instance.takeBlockingCall(*this);
if (!didTakeCall) {
// If at this point we are not able to take the blocking call,
// `complete` already did and is just wainting for us to release
// `complete` already did and is just waiting for us to release
// the lock on this call.
debugLog("not completing call which will be completed with result");
return;
Expand Down
8 changes: 3 additions & 5 deletions native/couchbase-lite-dart/src/AsyncCallback.h
Expand Up @@ -42,8 +42,9 @@ typedef void (*CallbackFinalizer)(void *context);

class AsyncCallback {
public:
AsyncCallback(uint32_t id, Dart_Handle dartCallback, Dart_Port sendport,
bool debug);
AsyncCallback(uint32_t id, Dart_Port sendPort, bool debug);

~AsyncCallback();

uint32_t id() { return id_; };

Expand All @@ -53,8 +54,6 @@ class AsyncCallback {
private:
friend class AsyncCallbackCall;

void static dartFinalizer(void *dart_callback_data, void *peer);

void registerCall(AsyncCallbackCall &call);
void unregisterCall(AsyncCallbackCall &call);
bool sendRequest(Dart_CObject *request);
Expand All @@ -68,7 +67,6 @@ class AsyncCallback {
Dart_Port sendPort_ = ILLEGAL_PORT;
void *finalizerContext_ = nullptr;
CallbackFinalizer finalizer_ = nullptr;
Dart_WeakPersistentHandle dartCallbackHandle_ = nullptr;
std::vector<AsyncCallbackCall *> activeCalls_;
};

Expand Down

0 comments on commit e7a259e

Please sign in to comment.