Skip to content

Move static allocation for fetch queue from JS to C++ code #12049

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 1 commit into from
Aug 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 1 addition & 7 deletions src/library_fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@ var LibraryFetch = {
#else
$Fetch__postset: 'Fetch.staticInit();',
#endif
$fetchWorkQueue: '0',
$Fetch: Fetch,
_emscripten_get_fetch_work_queue__deps: ['$fetchWorkQueue'],
_emscripten_get_fetch_work_queue: function() {
if (!fetchWorkQueue) fetchWorkQueue = _malloc(12);
return fetchWorkQueue;
},
_emscripten_fetch_get_response_headers_length: _fetch_get_response_headers_length,
_emscripten_fetch_get_response_headers: _fetch_get_response_headers,
_emscripten_fetch_free: _fetch_free,
Expand All @@ -35,7 +29,7 @@ var LibraryFetch = {
#if FETCH_SUPPORT_INDEXEDDB
'$__emscripten_fetch_cache_data', '$__emscripten_fetch_load_cached_data', '$__emscripten_fetch_delete_cached_data',
#endif
'_emscripten_get_fetch_work_queue', 'emscripten_is_main_browser_thread']
'emscripten_is_main_browser_thread']
};

mergeInto(LibraryManager.library, LibraryFetch);
38 changes: 18 additions & 20 deletions system/lib/fetch/emscripten_fetch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,35 @@ extern "C" {
// Uncomment the following and clear the cache with emcc --clear-cache to rebuild this file to
// enable internal debugging. #define FETCH_DEBUG

struct __emscripten_fetch_queue {
static void fetch_free(emscripten_fetch_t* fetch);

// APIs defined in JS
void emscripten_start_fetch(emscripten_fetch_t* fetch);
int32_t _emscripten_fetch_get_response_headers_length(int32_t fetchID);
int32_t _emscripten_fetch_get_response_headers(int32_t fetchID, int32_t dst, int32_t dstSizeBytes);
void _emscripten_fetch_free(unsigned int);

struct emscripten_fetch_queue {
emscripten_fetch_t** queuedOperations;
int numQueuedItems;
int queueSize;
};

static void fetch_free(emscripten_fetch_t* fetch);
emscripten_fetch_queue* _emscripten_get_fetch_queue() {
static thread_local emscripten_fetch_queue g_queue;

extern "C" {
void emscripten_start_fetch(emscripten_fetch_t* fetch);
__emscripten_fetch_queue* _emscripten_get_fetch_work_queue();

__emscripten_fetch_queue* _emscripten_get_fetch_queue() {
__emscripten_fetch_queue* queue = _emscripten_get_fetch_work_queue();
if (!queue->queuedOperations) {
queue->queueSize = 64;
queue->numQueuedItems = 0;
queue->queuedOperations =
(emscripten_fetch_t**)malloc(sizeof(emscripten_fetch_t*) * queue->queueSize);
if (!g_queue.queuedOperations) {
g_queue.queueSize = 64;
g_queue.numQueuedItems = 0;
g_queue.queuedOperations =
(emscripten_fetch_t**)malloc(sizeof(emscripten_fetch_t*) * g_queue.queueSize);
}
return queue;
}

int32_t _emscripten_fetch_get_response_headers_length(int32_t fetchID);
int32_t _emscripten_fetch_get_response_headers(int32_t fetchID, int32_t dst, int32_t dstSizeBytes);
void _emscripten_fetch_free(unsigned int);
return &g_queue;
}

void emscripten_proxy_fetch(emscripten_fetch_t* fetch) {
// TODO: mutex lock
__emscripten_fetch_queue* queue = _emscripten_get_fetch_queue();
emscripten_fetch_queue* queue = _emscripten_get_fetch_queue();
// TODO handle case when queue->numQueuedItems >= queue->queueSize
queue->queuedOperations[queue->numQueuedItems++] = fetch;
#ifdef FETCH_DEBUG
Expand Down