diff --git a/src/d_netfil.c b/src/d_netfil.c index 07401eb46..0a9edf097 100644 --- a/src/d_netfil.c +++ b/src/d_netfil.c @@ -111,7 +111,9 @@ static fileused_t transferFiles[UINT8_MAX + 1]; // Receiver structure INT32 fileneedednum; // Number of files needed to join the server fileneeded_t fileneeded[MAX_WADFILES]; // List of needed files +#ifdef HAVE_THREADS static I_mutex downloadmutex; +#endif char downloaddir[512] = "DOWNLOAD"; file_download_t filedownload; @@ -1357,14 +1359,18 @@ void CURLAbortFile(void) { filedownload.http_running = false; +#ifdef HAVE_THREADS // lock and unlock to wait for the download thread to exit I_lock_mutex(&downloadmutex); I_unlock_mutex(downloadmutex); +#endif } void CURLGetFile(void) { +#ifdef HAVE_THREADS I_lock_mutex(&downloadmutex); +#endif CURLMcode mc; /* return code used by curl_multi_wait() */ CURLcode easyres; /* Return from easy interface */ CURLMsg *m; /* for picking up messages with the transfer status */ @@ -1459,7 +1465,9 @@ void CURLGetFile(void) } filedownload.http_running = false; +#ifdef HAVE_THREADS I_unlock_mutex(downloadmutex); +#endif } HTTP_login * diff --git a/src/r_draw_span.c b/src/r_draw_span.c index 15ae7b1a2..d7a9aaa68 100644 --- a/src/r_draw_span.c +++ b/src/r_draw_span.c @@ -15,6 +15,7 @@ // SPANS // ========================================================================== +#ifdef HAVE_THREADS #ifdef _WIN32 #include #define local_for_thread static __thread @@ -22,6 +23,9 @@ #include #define local_for_thread thread_local static #endif +#else +#define local_for_thread static +#endif #define SPANSIZE 16 #define INVSPAN 0.0625f diff --git a/src/r_main.cpp b/src/r_main.cpp index 21d115abe..544eee445 100644 --- a/src/r_main.cpp +++ b/src/r_main.cpp @@ -39,8 +39,6 @@ #include "r_fps.h" // Frame interpolation/uncapped #include "tables.h" -#include "core/thread_pool.h" - #ifdef HWRENDER #include "hardware/hw_main.h" #endif diff --git a/src/r_plane.cpp b/src/r_plane.cpp index ce3ed6972..03e921f2b 100644 --- a/src/r_plane.cpp +++ b/src/r_plane.cpp @@ -32,7 +32,9 @@ #include "r_fps.h" #include "r_portal.h" +#ifdef HAVE_THREADS #include "core/thread_pool.h" +#endif // // opening @@ -248,6 +250,7 @@ static void R_MapPlane(drawspandata_t *ds, spandrawfunc_t *localspanfunc, INT32 pindex = distance >> LIGHTZSHIFT; if (pindex >= MAXLIGHTZ) pindex = MAXLIGHTZ - 1; + ds->colormap = ds->planezlight[pindex]; if (encoremap && !ds->currentplane->noencore) @@ -356,6 +359,7 @@ void R_ClearPlanes(void) static visplane_t *new_visplane(unsigned hash) { visplane_t *check = freetail; + if (!check) { check = static_cast(calloc(1, sizeof (*check))); @@ -372,8 +376,10 @@ static visplane_t *new_visplane(unsigned hash) if (!freetail) freehead = &freetail; } + check->next = visplanes[hash]; visplanes[hash] = check; + return check; } @@ -620,12 +626,14 @@ static void R_MakeSpans(void (*mapfunc)(drawspandata_t* ds, void(*spanfunc)(draw if (b2 >= vidheight) b2 = vidheight-1; if (x-1 >= vid.width) x = vid.width; + drawspandata_t dc_copy = *ds; + +#ifdef HAVE_THREADS // We want to draw N spans per subtask to ensure the work is // coarse enough to not be too slow due to task scheduling overhead. // To safely do this, we need to copy part of spanstart to a local. // This is essentially loop unrolling across threads. constexpr const int kSpanTaskGranularity = 8; - drawspandata_t dc_copy = *ds; while (t1 < t2 && t1 <= b1) { INT32 spanstartcopy[kSpanTaskGranularity] = {0}; @@ -684,6 +692,20 @@ static void R_MakeSpans(void (*mapfunc)(drawspandata_t* ds, void(*spanfunc)(draw } b1 -= taskspans; } +#else + (void)allow_parallel; + + while (t1 < t2 && t1 <= b1) + { + mapfunc(&dc_copy, localspanfunc, t1, spanstart[t1], x - 1, false); + t1++; + } + while (b1 > b2 && b1 >= t1) + { + mapfunc(&dc_copy, localspanfunc, b1, spanstart[b1], x - 1, false); + b1--; + } +#endif while (t2 < t1 && t2 <= b2) spanstart[t2++] = x; @@ -696,11 +718,15 @@ void R_DrawPlanes(void) visplane_t *pl; INT32 i; drawspandata_t ds = {}; +#ifdef HAVE_THREADS srb2::ThreadPool::Sema tp_sema; +#endif R_UpdatePlaneRipple(&ds); +#ifdef HAVE_THREADS srb2::g_main_threadpool->begin_sema(); +#endif for (i = 0; i < MAXVISPLANES; i++, pl++) { for (pl = visplanes[i]; pl; pl = pl->next) @@ -711,9 +737,11 @@ void R_DrawPlanes(void) R_DrawSinglePlane(&ds, pl, cv_parallelsoftware.value); } } +#ifdef HAVE_THREADS tp_sema = srb2::g_main_threadpool->end_sema(); srb2::g_main_threadpool->notify_sema(tp_sema); srb2::g_main_threadpool->wait_sema(tp_sema); +#endif } static void R_DrawSkyPlane(visplane_t *pl, void(*colfunc2)(drawcolumndata_t*), boolean allow_parallel); @@ -722,9 +750,11 @@ void R_DrawSkyPlanes(void) { visplane_t *pl; INT32 i; - srb2::ThreadPool::Sema tp_sema; +#ifdef HAVE_THREADS + srb2::ThreadPool::Sema tp_sema; srb2::g_main_threadpool->begin_sema(); +#endif for (i = 0; i < MAXVISPLANES; i++, pl++) { for (pl = visplanes[i]; pl; pl = pl->next) @@ -735,9 +765,11 @@ void R_DrawSkyPlanes(void) R_DrawSkyPlane(pl, colfunc, cv_parallelsoftware.value); } } +#ifdef HAVE_THREADS tp_sema = srb2::g_main_threadpool->end_sema(); srb2::g_main_threadpool->notify_sema(tp_sema); srb2::g_main_threadpool->wait_sema(tp_sema); +#endif } static void R_DrawSkyPlane(visplane_t *pl, void(*colfunc2)(drawcolumndata_t*), boolean allow_parallel) @@ -783,6 +815,7 @@ static void R_DrawSkyPlane(visplane_t *pl, void(*colfunc2)(drawcolumndata_t*), b R_GenerateTexture(texturetranslation[skytexture]); } +#ifdef HAVE_THREADS while (x <= pl->maxx) { // Tune concurrency granularity here to maximize throughput @@ -824,6 +857,31 @@ static void R_DrawSkyPlane(visplane_t *pl, void(*colfunc2)(drawcolumndata_t*), b x += kSkyPlaneMacroColumns; } +#else + (void)allow_parallel; + + for (x = pl->minx; x <= pl->maxx; x++) + { + dc.yl = pl->top[x]; + dc.yh = pl->bottom[x]; + + if (dc.yl > dc.yh) + { + continue; + } + + INT32 angle = (pl->viewangle + xtoviewangle[x])>>ANGLETOSKYSHIFT; + angle -= (skytextureoffset >> FRACBITS); + + dc.iscale = FixedMul(skyscale, FINECOSINE(xtoviewangle[x]>>ANGLETOFINESHIFT)); + dc.x = x; + dc.source = + R_GetColumn(texturetranslation[skytexture], + -angle); // get negative of angle for each column to display sky correct way round! --Monster Iestyn 27/01/18 + + colfunc2(&dc); + } +#endif } // Returns the height of the sloped plane at (x, y) as a 32.16 fixed_t diff --git a/src/r_segs.cpp b/src/r_segs.cpp index d21c8270e..44def4ba4 100644 --- a/src/r_segs.cpp +++ b/src/r_segs.cpp @@ -30,7 +30,6 @@ #include "screen.h" #include "core/memory.h" -#include "core/thread_pool.h" #define HEIGHTBITS 12 #define HEIGHTUNIT (1<prev; +#ifdef HAVE_THREADS srb2::ThreadPool::Sema tp_sema; srb2::g_main_threadpool->begin_sema(); +#endif R_DrawSinglePlane(&ds, r2->plane, cv_parallelsoftware.value); +#ifdef HAVE_THREADS tp_sema = srb2::g_main_threadpool->end_sema(); srb2::g_main_threadpool->notify_sema(tp_sema); srb2::g_main_threadpool->wait_sema(tp_sema); +#endif R_DoneWithNode(r2); r2 = next; } diff --git a/src/sdl/i_system.cpp b/src/sdl/i_system.cpp index 2b610cded..6609f5697 100644 --- a/src/sdl/i_system.cpp +++ b/src/sdl/i_system.cpp @@ -206,7 +206,6 @@ static char returnWadPath[256]; #include "../r_fps.h" #include "../s_sound.h" -#include "../core/thread_pool.h" #ifdef MAC_ALERT #include "macosx/mac_alert.h" @@ -220,7 +219,10 @@ static char returnWadPath[256]; #include "../byteptr.h" #endif +#ifdef HAVE_THREADS +#include "../core/thread_pool.h" static std::thread::id g_main_thread_id; +#endif INT32 numcontrollers = 0; @@ -517,6 +519,7 @@ static void I_ReportSignal(int num, int coredumped) #ifndef NEWSIGNALHANDLER FUNCNORETURN static ATTRNORETURN void signal_handler(INT32 num) { +#ifdef HAVE_THREADS if (g_main_thread_id != std::this_thread::get_id()) { // Do not attempt any sort of recovery if this signal triggers off the main thread @@ -524,6 +527,7 @@ FUNCNORETURN static ATTRNORETURN void signal_handler(INT32 num) raise(num); exit(-2); } +#endif g_in_exiting_signal_handler = true; @@ -887,7 +891,9 @@ static inline void I_ShutdownConsole(void){} // static void I_RegisterSignals (void) { +#ifdef HAVE_THREADS g_main_thread_id = std::this_thread::get_id(); +#endif #ifdef SIGINT signal(SIGINT , quit_handler); @@ -2049,11 +2055,13 @@ void I_Error(const char *error, ...) va_list argptr; char buffer[8192]; +#ifdef HAVE_THREADS if (std::this_thread::get_id() != g_main_thread_id) { // Do not attempt a graceful shutdown. Errors off the main thread are unresolvable. exit(-2); } +#endif // recursive error detecting if (shutdowning)