Skip to content

Commit

Permalink
Remove PhonyWorkQueue from #15557 since WebCrypto can now use real Wo…
Browse files Browse the repository at this point in the history
…rkQueue

We might revert this commit before merging. It's unclear if this is
the right approach or if we should keep PhonyWorkQueue. This way does
require manual event loop refs/unrefs in a bunch of places where
previously PhonyWorkQueue was handling it.
  • Loading branch information
190n committed Feb 12, 2025
1 parent c8b7d6e commit 5347f30
Show file tree
Hide file tree
Showing 13 changed files with 27 additions and 154 deletions.
15 changes: 0 additions & 15 deletions src/bun.js/bindings/EventLoopTaskNoContext.cpp

This file was deleted.

35 changes: 0 additions & 35 deletions src/bun.js/bindings/EventLoopTaskNoContext.h

This file was deleted.

4 changes: 3 additions & 1 deletion src/bun.js/bindings/webcrypto/CryptoAlgorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ ExceptionOr<size_t> CryptoAlgorithm::getKeyLength(const CryptoAlgorithmParameter
template<typename ResultCallbackType, typename OperationType>
static void dispatchAlgorithmOperation(WorkQueue& workQueue, ScriptExecutionContext& context, ResultCallbackType&& callback, CryptoAlgorithm::ExceptionCallback&& exceptionCallback, OperationType&& operation)
{
workQueue.dispatch(context.globalObject(),
context.refEventLoop();
workQueue.dispatch(
[operation = WTFMove(operation), callback = WTFMove(callback), exceptionCallback = WTFMove(exceptionCallback), contextIdentifier = context.identifier()]() mutable {
auto result = operation();
ScriptExecutionContext::postTaskTo(contextIdentifier, [result = crossThreadCopy(WTFMove(result)), callback = WTFMove(callback), exceptionCallback = WTFMove(exceptionCallback)](auto& context) mutable {
Expand All @@ -104,6 +105,7 @@ static void dispatchAlgorithmOperation(WorkQueue& workQueue, ScriptExecutionCont
return;
}
callback(result.releaseReturnValue());
context.unrefEventLoop();
});
});
}
Expand Down
6 changes: 4 additions & 2 deletions src/bun.js/bindings/webcrypto/CryptoAlgorithmECDH.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,13 @@ void CryptoAlgorithmECDH::deriveBits(const CryptoAlgorithmParameters& parameters

// This is a special case that can't use dispatchOperation() because it bundles
// the result validation and callback dispatch into unifiedCallback.
workQueue.dispatch(context.globalObject(),
context.refEventLoop();
workQueue.dispatch(
[baseKey = WTFMove(baseKey), publicKey = ecParameters.publicKey, length, unifiedCallback = WTFMove(unifiedCallback), contextIdentifier = context.identifier()]() mutable {
auto derivedKey = platformDeriveBits(downcast<CryptoKeyEC>(baseKey.get()), downcast<CryptoKeyEC>(*publicKey));
ScriptExecutionContext::postTaskTo(contextIdentifier, [derivedKey = WTFMove(derivedKey), length, unifiedCallback = WTFMove(unifiedCallback)](auto&) mutable {
ScriptExecutionContext::postTaskTo(contextIdentifier, [derivedKey = WTFMove(derivedKey), length, unifiedCallback = WTFMove(unifiedCallback)](auto& context) mutable {
unifiedCallback(WTFMove(derivedKey), length);
context.unrefEventLoop();
});
});
}
Expand Down
6 changes: 4 additions & 2 deletions src/bun.js/bindings/webcrypto/CryptoAlgorithmSHA1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ void CryptoAlgorithmSHA1::digest(Vector<uint8_t>&& message, VectorCallback&& cal
return;
}

workQueue.dispatch(context.globalObject(), [digest = WTFMove(digest), message = WTFMove(message), callback = WTFMove(callback), contextIdentifier = context.identifier()]() mutable {
context.refEventLoop();
workQueue.dispatch([digest = WTFMove(digest), message = WTFMove(message), callback = WTFMove(callback), contextIdentifier = context.identifier()]() mutable {
digest->addBytes(message.data(), message.size());
auto result = digest->computeHash();
ScriptExecutionContext::postTaskTo(contextIdentifier, [callback = WTFMove(callback), result = WTFMove(result)](auto&) {
ScriptExecutionContext::postTaskTo(contextIdentifier, [callback = WTFMove(callback), result = WTFMove(result)](auto& context) {
callback(result);
context.unrefEventLoop();
});
});
}
Expand Down
6 changes: 4 additions & 2 deletions src/bun.js/bindings/webcrypto/CryptoAlgorithmSHA224.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ void CryptoAlgorithmSHA224::digest(Vector<uint8_t>&& message, VectorCallback&& c
return;
}

workQueue.dispatch(context.globalObject(), [digest = WTFMove(digest), message = WTFMove(message), callback = WTFMove(callback), contextIdentifier = context.identifier()]() mutable {
context.refEventLoop();
workQueue.dispatch([digest = WTFMove(digest), message = WTFMove(message), callback = WTFMove(callback), contextIdentifier = context.identifier()]() mutable {
digest->addBytes(message.data(), message.size());
auto result = digest->computeHash();
ScriptExecutionContext::postTaskTo(contextIdentifier, [callback = WTFMove(callback), result = WTFMove(result)](auto&) {
ScriptExecutionContext::postTaskTo(contextIdentifier, [callback = WTFMove(callback), result = WTFMove(result)](auto& context) {
callback(result);
context.unrefEventLoop();
});
});
}
Expand Down
6 changes: 4 additions & 2 deletions src/bun.js/bindings/webcrypto/CryptoAlgorithmSHA256.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ void CryptoAlgorithmSHA256::digest(Vector<uint8_t>&& message, VectorCallback&& c
return;
}

workQueue.dispatch(context.globalObject(), [digest = WTFMove(digest), message = WTFMove(message), callback = WTFMove(callback), contextIdentifier = context.identifier()]() mutable {
context.refEventLoop();
workQueue.dispatch([digest = WTFMove(digest), message = WTFMove(message), callback = WTFMove(callback), contextIdentifier = context.identifier()]() mutable {
digest->addBytes(message.data(), message.size());
auto result = digest->computeHash();
ScriptExecutionContext::postTaskTo(contextIdentifier, [callback = WTFMove(callback), result = WTFMove(result)](auto&) {
ScriptExecutionContext::postTaskTo(contextIdentifier, [callback = WTFMove(callback), result = WTFMove(result)](auto& context) {
callback(result);
context.unrefEventLoop();
});
});
}
Expand Down
6 changes: 4 additions & 2 deletions src/bun.js/bindings/webcrypto/CryptoAlgorithmSHA384.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ void CryptoAlgorithmSHA384::digest(Vector<uint8_t>&& message, VectorCallback&& c
return;
}

workQueue.dispatch(context.globalObject(), [digest = WTFMove(digest), message = WTFMove(message), callback = WTFMove(callback), contextIdentifier = context.identifier()]() mutable {
context.refEventLoop();
workQueue.dispatch([digest = WTFMove(digest), message = WTFMove(message), callback = WTFMove(callback), contextIdentifier = context.identifier()]() mutable {
digest->addBytes(message.data(), message.size());
auto result = digest->computeHash();
ScriptExecutionContext::postTaskTo(contextIdentifier, [callback = WTFMove(callback), result = WTFMove(result)](auto&) {
ScriptExecutionContext::postTaskTo(contextIdentifier, [callback = WTFMove(callback), result = WTFMove(result)](auto& context) {
callback(result);
context.unrefEventLoop();
});
});
}
Expand Down
6 changes: 4 additions & 2 deletions src/bun.js/bindings/webcrypto/CryptoAlgorithmSHA512.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ void CryptoAlgorithmSHA512::digest(Vector<uint8_t>&& message, VectorCallback&& c
return;
}

workQueue.dispatch(context.globalObject(), [digest = WTFMove(digest), message = WTFMove(message), callback = WTFMove(callback), contextIdentifier = context.identifier()]() mutable {
context.refEventLoop();
workQueue.dispatch([digest = WTFMove(digest), message = WTFMove(message), callback = WTFMove(callback), contextIdentifier = context.identifier()]() mutable {
digest->addBytes(message.data(), message.size());
auto result = digest->computeHash();
ScriptExecutionContext::postTaskTo(contextIdentifier, [callback = WTFMove(callback), result = WTFMove(result)](auto&) {
ScriptExecutionContext::postTaskTo(contextIdentifier, [callback = WTFMove(callback), result = WTFMove(result)](auto& context) {
callback(result);
context.unrefEventLoop();
});
});
}
Expand Down
21 changes: 0 additions & 21 deletions src/bun.js/bindings/webcrypto/PhonyWorkQueue.cpp

This file was deleted.

19 changes: 0 additions & 19 deletions src/bun.js/bindings/webcrypto/PhonyWorkQueue.h

This file was deleted.

3 changes: 0 additions & 3 deletions src/bun.js/bindings/webcrypto/SubtleCrypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#include <wtf/Ref.h>
#include <wtf/RefCounted.h>
#include <wtf/WeakPtr.h>
#include "PhonyWorkQueue.h"

namespace JSC {
class ArrayBufferView;
Expand All @@ -44,8 +43,6 @@ class CallFrame;

namespace WebCore {

using WorkQueue = Bun::PhonyWorkQueue;

struct JsonWebKey;

class BufferSource;
Expand Down
48 changes: 0 additions & 48 deletions src/bun.js/event_loop.zig
Original file line number Diff line number Diff line change
Expand Up @@ -301,54 +301,6 @@ pub const CppTask = opaque {
}
};

pub const ConcurrentCppTask = struct {
cpp_task: *EventLoopTaskNoContext,
workpool_task: JSC.WorkPoolTask = .{ .callback = &runFromWorkpool },

const EventLoopTaskNoContext = opaque {
extern fn Bun__EventLoopTaskNoContext__performTask(task: *EventLoopTaskNoContext) void;
extern fn Bun__EventLoopTaskNoContext__createdInBunVm(task: *const EventLoopTaskNoContext) ?*VirtualMachine;

/// Deallocates `this`
pub fn run(this: *EventLoopTaskNoContext) void {
Bun__EventLoopTaskNoContext__performTask(this);
}

/// Get the VM that created this task
pub fn getVM(this: *const EventLoopTaskNoContext) ?*VirtualMachine {
return Bun__EventLoopTaskNoContext__createdInBunVm(this);
}
};

pub fn runFromWorkpool(task: *JSC.WorkPoolTask) void {
var this: *ConcurrentCppTask = @fieldParentPtr("workpool_task", task);
// Extract all the info we need from `this` and `cpp_task` before we call functions that
// free them
const cpp_task = this.cpp_task;
const maybe_vm = cpp_task.getVM();
this.destroy();
cpp_task.run();
if (maybe_vm) |vm| {
vm.event_loop.unrefConcurrently();
}
}

pub usingnamespace bun.New(@This());

pub export fn ConcurrentCppTask__createAndRun(cpp_task: *EventLoopTaskNoContext) void {
JSC.markBinding(@src());
if (cpp_task.getVM()) |vm| {
vm.event_loop.refConcurrently();
}
const cpp = ConcurrentCppTask.new(.{ .cpp_task = cpp_task });
JSC.WorkPool.schedule(&cpp.workpool_task);
}
};

comptime {
_ = ConcurrentCppTask.ConcurrentCppTask__createAndRun;
}

pub const JSCScheduler = struct {
pub const JSCDeferredWorkTask = opaque {
extern fn Bun__runDeferredWork(task: *JSCScheduler.JSCDeferredWorkTask) void;
Expand Down

0 comments on commit 5347f30

Please sign in to comment.