Skip to content

Commit

Permalink
Make SubtleCrypto use Bun WorkPool instead of WTF::WorkQueue
Browse files Browse the repository at this point in the history
  • Loading branch information
190n committed Dec 3, 2024
1 parent 62a26a6 commit 6af5b25
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/bun.js/bindings/webcrypto/CryptoAlgorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#include <wtf/Function.h>
#include <wtf/ThreadSafeRefCounted.h>
#include <wtf/Vector.h>
#include <wtf/WorkQueue.h>
#include "SubtleCrypto.h"

#if ENABLE(WEB_CRYPTO)

Expand Down
23 changes: 23 additions & 0 deletions src/bun.js/bindings/webcrypto/PhonyWorkQueue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "PhonyWorkQueue.h"

#include <wtf/text/ASCIILiteral.h>
#include "EventLoopTask.h"

using WebCore::EventLoopTask;

namespace Bun {

Ref<PhonyWorkQueue> PhonyWorkQueue::create(WTF::ASCIILiteral name)
{
(void)name;
return adoptRef(*new PhonyWorkQueue);
}

extern "C" void ConcurrentCppTask__createAndRun(EventLoopTask*);

void PhonyWorkQueue::dispatch(WTF::Function<void()>&& function)
{
ConcurrentCppTask__createAndRun(new EventLoopTask(WTFMove(function)));
}

} // namespace Bun
20 changes: 20 additions & 0 deletions src/bun.js/bindings/webcrypto/PhonyWorkQueue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include "root.h"

#include <wtf/RefCounted.h>
#include <wtf/Ref.h>

namespace Bun {

// Work queue which really uses CppTask.Concurrent in Bun's event loop (which enqueues into a WorkPool).
// Maintained so that SubtleCrypto functions can pretend they're using a WorkQueue, even though
// WTF::WorkQueue doesn't work and we need to use Bun's equivalent.
class PhonyWorkQueue : public WTF::RefCounted<PhonyWorkQueue> {
public:
static Ref<PhonyWorkQueue> create(WTF::ASCIILiteral name);

void dispatch(Function<void()>&&);
};

}; // namespace Bun
4 changes: 3 additions & 1 deletion src/bun.js/bindings/webcrypto/SubtleCrypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include <wtf/Ref.h>
#include <wtf/RefCounted.h>
#include <wtf/WeakPtr.h>
#include <wtf/WorkQueue.h>
#include "PhonyWorkQueue.h"

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

namespace WebCore {

using WorkQueue = Bun::PhonyWorkQueue;

struct JsonWebKey;

class BufferSource;
Expand Down
20 changes: 20 additions & 0 deletions src/bun.js/event_loop.zig
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,26 @@ pub const CppTask = opaque {
JSC.markBinding(@src());
Bun__performTask(global, this);
}

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

pub fn runFromWorkpool(task: *JSC.WorkPoolTask) void {
var this: *Concurrent = @fieldParentPtr("workpool_task", task);
const cpp_task = this.cpp_task;
this.destroy();
// TODO figure out what this should be
cpp_task.run(undefined);
}

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

pub export fn ConcurrentCppTask__createAndRun(cpp_task: *CppTask) void {
const cpp = Concurrent.new(.{ .cpp_task = cpp_task });
JSC.WorkPool.schedule(&cpp.workpool_task);
}
};
pub const JSCScheduler = struct {
pub const JSCDeferredWorkTask = opaque {
Expand Down

0 comments on commit 6af5b25

Please sign in to comment.