Skip to content

Commit 438d633

Browse files
Refactor request cleanup to be handled entirely by worker thread
1 parent 9ba47f2 commit 438d633

3 files changed

Lines changed: 34 additions & 48 deletions

File tree

ffrunner.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ extern UINT ioMsg;
126126
void register_get_request(const char *url, bool doNotify, void *notifyData);
127127
void register_post_request(const char *url, bool doNotify, void *notifyData, uint32_t postDataLen, const char *postData);
128128
void register_temp_request(const char *url, HANDLE outFile, HANDLE onDone);
129-
bool handle_io_progress(Request *req);
129+
void handle_io_progress(Request *req);
130130
void submit_request(Request *req);
131131
void complete_request();
132132
void init_network(char *mainSrcUrl);

graphics.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ window_proc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1010
PAINTSTRUCT ps;
1111
HDC hdc;
1212
Request *req;
13-
bool done;
1413

1514
switch (uMsg) {
1615
case WM_CLOSE:
@@ -61,11 +60,8 @@ window_proc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
6160

6261
if (uMsg == ioMsg) {
6362
req = (Request*)lParam;
64-
done = handle_io_progress(req);
63+
handle_io_progress(req);
6564
SetEvent(req->readyEvent);
66-
if (done) {
67-
free(req);
68-
}
6965
return 0;
7066
}
7167

requests.c

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ cancel_request(Request *req)
112112
req->failed = true;
113113
}
114114

115-
bool
115+
void
116116
handle_io_progress(Request *req)
117117
{
118118
NPError err;
@@ -169,36 +169,16 @@ handle_io_progress(Request *req)
169169
logmsg("destroystream error %d\n", err);
170170
}
171171
free(req->stream);
172+
req->stream = NULL;
172173
}
173174

174175
if (req->doNotify) {
175176
logmsg("> NPP_UrlNotify %s %d %p\n", req->originalUrl, req->doneReason, req->notifyData);
176177
pluginFuncs.urlnotify(&npp, req->originalUrl, req->doneReason, req->notifyData);
177178
}
178179

179-
if ((req->source == REQ_SRC_FILE || req->source == REQ_SRC_CACHE) && req->handles.hFile != INVALID_HANDLE_VALUE) {
180-
CloseHandle(req->handles.hFile);
181-
}
182-
183-
if (req->source == REQ_SRC_CACHE) {
184-
UnlockUrlCacheEntryFileA(req->originalUrl, 0);
185-
}
186-
187-
if (req->source == REQ_SRC_HTTP) {
188-
if (req->handles.http.hReq != NULL) {
189-
InternetCloseHandle(req->handles.http.hReq);
190-
}
191-
if (req->handles.http.hConn != NULL) {
192-
InternetCloseHandle(req->handles.http.hConn);
193-
}
194-
}
195-
196180
complete_request();
197-
198-
return true;
199181
}
200-
201-
return false;
202182
}
203183

204184
void
@@ -570,28 +550,16 @@ VOID CALLBACK
570550
handle_request(PTP_CALLBACK_INSTANCE inst, void *reqArg, PTP_WORK work)
571551
{
572552
Request *req;
573-
HANDLE doneEvent;
574-
HANDLE readyEvent;
575-
bool done;
576553

577554
req = (Request *)reqArg;
578-
doneEvent = req->doneEvent;
579-
readyEvent = req->readyEvent;
580-
done = false;
581-
while (!done) {
555+
while (true) {
582556
if (req->source == REQ_SRC_UNSET) {
583557
init_request(req);
584558
}
585559
if (!req->failed) {
586560
progress_request(req);
587561
}
588562

589-
/*
590-
* Capture done state before posting to the main thread,
591-
* since the main thread may free req after processing.
592-
*/
593-
done = req->done || req->failed;
594-
595563
if (req->hOutFile != INVALID_HANDLE_VALUE) {
596564
/* write directly to output file */
597565
DWORD written;
@@ -600,18 +568,40 @@ handle_request(PTP_CALLBACK_INSTANCE inst, void *reqArg, PTP_WORK work)
600568
}
601569
req->writePtr = req->writeSize;
602570
} else {
603-
/* notify main thread to pipe progress to the plugin */
571+
/* main thread has to pipe progress to the plugin */
604572
PostMessageW(hwnd, ioMsg, (WPARAM)NULL, (LPARAM)req);
605-
WaitForSingleObject(readyEvent, INFINITE);
573+
WaitForSingleObject(req->readyEvent, INFINITE);
574+
}
575+
576+
if (req->done || req->failed) {
577+
break;
578+
}
579+
}
580+
581+
/* cleanup I/O handles */
582+
if ((req->source == REQ_SRC_FILE || req->source == REQ_SRC_CACHE)
583+
&& req->handles.hFile != INVALID_HANDLE_VALUE) {
584+
CloseHandle(req->handles.hFile);
585+
}
586+
if (req->source == REQ_SRC_CACHE) {
587+
UnlockUrlCacheEntryFileA(req->originalUrl, 0);
588+
}
589+
if (req->source == REQ_SRC_HTTP) {
590+
if (req->handles.http.hReq != NULL) {
591+
InternetCloseHandle(req->handles.http.hReq);
592+
}
593+
if (req->handles.http.hConn != NULL) {
594+
InternetCloseHandle(req->handles.http.hConn);
606595
}
607596
}
608597

609-
/* Close handles here, because req has been freed at this point */
610-
if (doneEvent != INVALID_HANDLE_VALUE) {
611-
SetEvent(doneEvent);
612-
CloseHandle(doneEvent);
598+
/* cleanup signals */
599+
if (req->doneEvent != INVALID_HANDLE_VALUE) {
600+
SetEvent(req->doneEvent);
601+
CloseHandle(req->doneEvent);
613602
}
614-
CloseHandle(readyEvent);
603+
CloseHandle(req->readyEvent);
604+
free(req);
615605
}
616606

617607
static void

0 commit comments

Comments
 (0)