2222
2323#include < iostream>
2424#include < thread>
25+ #ifdef EMSCRIPTEN
26+ #define SAME_THREAD_UPLOAD
27+ #endif
2528
2629namespace duckdb {
2730
@@ -342,8 +345,10 @@ void S3FileSystem::NotifyUploadsInProgress(S3FileHandle &file_handle) {
342345 }
343346 // Note that there are 2 cv's because otherwise we might deadlock when the final flushing thread is notified while
344347 // another thread is still waiting for an upload thread
348+ #ifndef SAME_THREAD_UPLOAD
345349 file_handle.uploads_in_progress_cv .notify_one ();
346350 file_handle.final_flush_cv .notify_one ();
351+ #endif
347352}
348353
349354void S3FileSystem::UploadBuffer (S3FileHandle &file_handle, shared_ptr<S3WriteBuffer> write_buffer) {
@@ -420,18 +425,25 @@ void S3FileSystem::FlushBuffer(S3FileHandle &file_handle, shared_ptr<S3WriteBuff
420425
421426 {
422427 unique_lock<mutex> lck (file_handle.uploads_in_progress_lock );
423- // check if there are upload threads available
428+ // check if there are upload threads available
429+ #ifndef SAME_THREAD_UPLOAD
424430 if (file_handle.uploads_in_progress >= file_handle.config_params .max_upload_threads ) {
425431 // there are not - wait for one to become available
426432 file_handle.uploads_in_progress_cv .wait (lck, [&file_handle] {
427433 return file_handle.uploads_in_progress < file_handle.config_params .max_upload_threads ;
428434 });
429435 }
436+ #endif
430437 file_handle.uploads_in_progress ++;
431438 }
432439
433- thread upload_thread (UploadBuffer, std::ref (file_handle), write_buffer);
434- upload_thread.detach ();
440+ #ifdef SAME_THREAD_UPLOAD
441+ UploadBuffer (file_handle, write_buffer);
442+ return ;
443+ #endif
444+
445+ thread upload_thread (UploadBuffer, std::ref (file_handle), write_buffer);
446+ upload_thread.detach ();
435447}
436448
437449// Note that FlushAll currently does not allow to continue writing afterwards. Therefore, FinalizeMultipartUpload should
@@ -453,7 +465,9 @@ void S3FileSystem::FlushAllBuffers(S3FileHandle &file_handle) {
453465 }
454466 }
455467 unique_lock<mutex> lck (file_handle.uploads_in_progress_lock );
468+ #ifndef SAME_THREAD_UPLOAD
456469 file_handle.final_flush_cv .wait (lck, [&file_handle] { return file_handle.uploads_in_progress == 0 ; });
470+ #endif
457471
458472 file_handle.RethrowIOError ();
459473}
0 commit comments