Skip to content

Commit c0aa403

Browse files
authored
Cleanup formatting in emscripten_fetch.c. NFC (emscripten-core#22091)
1 parent 1b9d0f5 commit c0aa403

File tree

1 file changed

+46
-34
lines changed

1 file changed

+46
-34
lines changed

system/lib/fetch/emscripten_fetch.c

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,15 @@ emscripten_fetch_queue* _emscripten_get_fetch_queue() {
4545
if (!g_queue.queuedOperations) {
4646
g_queue.queueSize = 64;
4747
g_queue.numQueuedItems = 0;
48-
g_queue.queuedOperations =
49-
(emscripten_fetch_t**)malloc(sizeof(emscripten_fetch_t*) * g_queue.queueSize);
48+
g_queue.queuedOperations = malloc(sizeof(emscripten_fetch_t*) * g_queue.queueSize);
5049
}
5150
return &g_queue;
5251
}
5352

5453
void emscripten_proxy_fetch(emscripten_fetch_t* fetch) {
5554
// TODO: mutex lock
5655
emscripten_fetch_queue* queue = _emscripten_get_fetch_queue();
57-
// TODO handle case when queue->numQueuedItems >= queue->queueSize
56+
// TODO handle case when queue->numQueuedItems >= queue->queueSize
5857
queue->queuedOperations[queue->numQueuedItems++] = fetch;
5958
#ifdef FETCH_DEBUG
6059
emscripten_dbgf("Queued fetch to fetch-worker to process. There are "
@@ -118,8 +117,9 @@ emscripten_fetch_t* emscripten_fetch(emscripten_fetch_attr_t* fetch_attr, const
118117

119118
if (fetch_attr->requestHeaders) {
120119
size_t headersCount = 0;
121-
while (fetch_attr->requestHeaders[headersCount])
120+
while (fetch_attr->requestHeaders[headersCount]) {
122121
++headersCount;
122+
}
123123
const char** headers = (const char**)malloc((headersCount + 1) * sizeof(const char*));
124124
if (!headers) {
125125
fetch_free(fetch);
@@ -148,43 +148,50 @@ emscripten_fetch_t* emscripten_fetch(emscripten_fetch_attr_t* fetch_attr, const
148148

149149
EMSCRIPTEN_RESULT emscripten_fetch_wait(emscripten_fetch_t* fetch, double timeoutMsecs) {
150150
#if __EMSCRIPTEN_PTHREADS__
151-
if (!fetch)
151+
if (!fetch) {
152152
return EMSCRIPTEN_RESULT_INVALID_PARAM;
153+
}
153154
uint32_t proxyState = fetch->__proxyState;
154-
if (proxyState == 2)
155-
return EMSCRIPTEN_RESULT_SUCCESS; // already finished.
156-
if (proxyState != 1)
157-
return EMSCRIPTEN_RESULT_INVALID_PARAM; // the fetch should be ongoing?
155+
if (proxyState == 2) {
156+
// already finished.
157+
return EMSCRIPTEN_RESULT_SUCCESS;
158+
}
159+
if (proxyState != 1) {
160+
// the fetch should be ongoing?
161+
return EMSCRIPTEN_RESULT_INVALID_PARAM;
162+
}
158163
#ifdef FETCH_DEBUG
159164
emscripten_dbg("fetch: emscripten_fetch_wait..");
160165
#endif
161-
if (timeoutMsecs <= 0)
166+
if (timeoutMsecs <= 0) {
162167
return EMSCRIPTEN_RESULT_TIMED_OUT;
168+
}
163169
while (proxyState == 1 /*sent to proxy worker*/) {
164-
if (!emscripten_is_main_browser_thread()) {
165-
int ret = emscripten_futex_wait(&fetch->__proxyState, proxyState, timeoutMsecs);
166-
if (ret == -ETIMEDOUT)
167-
return EMSCRIPTEN_RESULT_TIMED_OUT;
168-
proxyState = fetch->__proxyState;
169-
} else {
170+
if (emscripten_is_main_browser_thread()) {
170171
emscripten_err("fetch: emscripten_fetch_wait failed: main thread cannot block to wait for long periods of time! Migrate the application to run in a worker to perform synchronous file IO, or switch to using asynchronous IO.");
171172
return EMSCRIPTEN_RESULT_FAILED;
172173
}
174+
int ret = emscripten_futex_wait(&fetch->__proxyState, proxyState, timeoutMsecs);
175+
if (ret == -ETIMEDOUT) {
176+
return EMSCRIPTEN_RESULT_TIMED_OUT;
177+
}
178+
proxyState = fetch->__proxyState;
173179
}
174180
#ifdef FETCH_DEBUG
175181
emscripten_dbg("fetch: emscripten_fetch_wait done..");
176182
#endif
177183

178-
if (proxyState == 2)
179-
return EMSCRIPTEN_RESULT_SUCCESS;
180-
else
184+
if (proxyState != 2) {
181185
return EMSCRIPTEN_RESULT_FAILED;
186+
}
187+
return EMSCRIPTEN_RESULT_SUCCESS;
182188
#else
183-
if (fetch->readyState >= STATE_DONE)
189+
if (fetch->readyState >= STATE_DONE) {
184190
return EMSCRIPTEN_RESULT_SUCCESS; // already finished.
185-
if (timeoutMsecs == 0)
191+
}
192+
if (timeoutMsecs == 0) {
186193
return EMSCRIPTEN_RESULT_TIMED_OUT /*Main thread testing completion with sleep=0msecs*/;
187-
else {
194+
} else {
188195
#ifdef FETCH_DEBUG
189196
emscripten_err("fetch: emscripten_fetch_wait() cannot stop to wait when building without pthreads!");
190197
#endif
@@ -194,17 +201,19 @@ EMSCRIPTEN_RESULT emscripten_fetch_wait(emscripten_fetch_t* fetch, double timeou
194201
}
195202

196203
EMSCRIPTEN_RESULT emscripten_fetch_close(emscripten_fetch_t* fetch) {
197-
if (!fetch)
204+
if (!fetch) {
198205
return EMSCRIPTEN_RESULT_SUCCESS; // Closing null pointer is ok, same as with free().
206+
}
199207

200208
#if __EMSCRIPTEN_PTHREADS__
201209
fetch->__proxyState = 0;
202210
#endif
203211
// This function frees the fetch pointer so that it is invalid to access it anymore.
204212
// Use a few key fields as an integrity check that we are being passed a good pointer to a valid
205213
// fetch structure, which has not been yet closed. (double close is an error)
206-
if (fetch->id == 0 || fetch->readyState > STATE_MAX)
214+
if (fetch->id == 0 || fetch->readyState > STATE_MAX) {
207215
return EMSCRIPTEN_RESULT_INVALID_PARAM;
216+
}
208217

209218
// This fetch is aborted. Call the error handler if the fetch was still in progress and was
210219
// canceled in flight.
@@ -219,22 +228,25 @@ EMSCRIPTEN_RESULT emscripten_fetch_close(emscripten_fetch_t* fetch) {
219228
}
220229

221230
size_t emscripten_fetch_get_response_headers_length(emscripten_fetch_t *fetch) {
222-
if (!fetch || fetch->readyState < STATE_HEADERS_RECEIVED) return 0;
231+
if (!fetch || fetch->readyState < STATE_HEADERS_RECEIVED) {
232+
return 0;
233+
}
223234

224235
return (size_t)_emscripten_fetch_get_response_headers_length(fetch->id);
225236
}
226237

227238
size_t emscripten_fetch_get_response_headers(emscripten_fetch_t *fetch, char *dst, size_t dstSizeBytes) {
228-
if (!fetch || fetch->readyState < STATE_HEADERS_RECEIVED) return 0;
239+
if (!fetch || fetch->readyState < STATE_HEADERS_RECEIVED) {
240+
return 0;
241+
}
229242

230243
return (size_t)_emscripten_fetch_get_response_headers(fetch->id, dst, dstSizeBytes);
231244
}
232245

233246
char **emscripten_fetch_unpack_response_headers(const char *headersString) {
234247
// Get size of output array and allocate.
235248
size_t numHeaders = 0;
236-
for (const char *pos = strchr(headersString, '\n'); pos; pos = strchr(pos + 1, '\n'))
237-
{
249+
for (const char *pos = strchr(headersString, '\n'); pos; pos = strchr(pos + 1, '\n')) {
238250
numHeaders++;
239251
}
240252
char **unpackedHeaders = (char**)malloc(sizeof(char*) * ((numHeaders * 2) + 1));
@@ -243,8 +255,7 @@ char **emscripten_fetch_unpack_response_headers(const char *headersString) {
243255
// Allocate each header.
244256
const char *rowStart = headersString;
245257
const char *rowEnd = strchr(rowStart, '\n');
246-
for (size_t headerNum = 0; rowEnd; headerNum += 2)
247-
{
258+
for (size_t headerNum = 0; rowEnd; headerNum += 2) {
248259
const char *split = strchr(rowStart, ':');
249260
size_t headerSize = (size_t)split - (size_t)rowStart;
250261
char* header = (char*)malloc(headerSize + 1);
@@ -267,10 +278,10 @@ char **emscripten_fetch_unpack_response_headers(const char *headersString) {
267278
}
268279

269280
void emscripten_fetch_free_unpacked_response_headers(char **unpackedHeaders) {
270-
if (unpackedHeaders)
271-
{
272-
for (size_t i = 0; unpackedHeaders[i]; ++i)
281+
if (unpackedHeaders) {
282+
for (size_t i = 0; unpackedHeaders[i]; ++i) {
273283
free((void*)unpackedHeaders[i]);
284+
}
274285
free((void*)unpackedHeaders);
275286
}
276287
}
@@ -288,8 +299,9 @@ static void fetch_free(emscripten_fetch_t* fetch) {
288299
free((void*)fetch->__attributes.userName);
289300
free((void*)fetch->__attributes.password);
290301
if (fetch->__attributes.requestHeaders) {
291-
for (size_t i = 0; fetch->__attributes.requestHeaders[i]; ++i)
302+
for (size_t i = 0; fetch->__attributes.requestHeaders[i]; ++i) {
292303
free((void*)fetch->__attributes.requestHeaders[i]);
304+
}
293305
free((void*)fetch->__attributes.requestHeaders);
294306
}
295307
free((void*)fetch->__attributes.overriddenMimeType);

0 commit comments

Comments
 (0)