From b1f8a8ad40a454674604d431af315421bdf99c56 Mon Sep 17 00:00:00 2001 From: coderJeff Date: Sun, 14 May 2023 08:50:21 -0400 Subject: [PATCH] fbdocs: wiki snapshot 2023.05.14 - update moved/renamed examples - update removed examples --- ...onfaq10-1.bas => criticalsectionfaq11.bas} | 2 +- .../multithreading/criticalsectionfaq14.bas | 40 ---- .../proguide/multithreading/emulatetls1.bas | 171 ++++++++++++++++++ ...riticalsectionfaq13.bas => emulatetp1.bas} | 2 +- ...ticalsectionfaq13-2.bas => emulatetp2.bas} | 2 +- ...ticalsectionfaq13-3.bas => emulatetp3.bas} | 2 +- ...ticalsectionfaq13-4.bas => emulatetp4.bas} | 2 +- ...ticalsectionfaq13-5.bas => emulatetp5.bas} | 2 +- 8 files changed, 177 insertions(+), 46 deletions(-) rename examples/manual/proguide/multithreading/{criticalsectionfaq10-1.bas => criticalsectionfaq11.bas} (98%) delete mode 100644 examples/manual/proguide/multithreading/criticalsectionfaq14.bas create mode 100644 examples/manual/proguide/multithreading/emulatetls1.bas rename examples/manual/proguide/multithreading/{criticalsectionfaq13.bas => emulatetp1.bas} (98%) rename examples/manual/proguide/multithreading/{criticalsectionfaq13-2.bas => emulatetp2.bas} (98%) rename examples/manual/proguide/multithreading/{criticalsectionfaq13-3.bas => emulatetp3.bas} (99%) rename examples/manual/proguide/multithreading/{criticalsectionfaq13-4.bas => emulatetp4.bas} (99%) rename examples/manual/proguide/multithreading/{criticalsectionfaq13-5.bas => emulatetp5.bas} (99%) diff --git a/examples/manual/proguide/multithreading/criticalsectionfaq10-1.bas b/examples/manual/proguide/multithreading/criticalsectionfaq11.bas similarity index 98% rename from examples/manual/proguide/multithreading/criticalsectionfaq10-1.bas rename to examples/manual/proguide/multithreading/criticalsectionfaq11.bas index 7e7c37abed..02c32f6471 100644 --- a/examples/manual/proguide/multithreading/criticalsectionfaq10-1.bas +++ b/examples/manual/proguide/multithreading/criticalsectionfaq11.bas @@ -1,4 +1,4 @@ -'' examples/manual/proguide/multithreading/criticalsectionfaq10-1.bas +'' examples/manual/proguide/multithreading/criticalsectionfaq11.bas '' '' Example extracted from the FreeBASIC Manual '' from topic 'Critical Sections FAQ' diff --git a/examples/manual/proguide/multithreading/criticalsectionfaq14.bas b/examples/manual/proguide/multithreading/criticalsectionfaq14.bas deleted file mode 100644 index e78435d50d..0000000000 --- a/examples/manual/proguide/multithreading/criticalsectionfaq14.bas +++ /dev/null @@ -1,40 +0,0 @@ -'' examples/manual/proguide/multithreading/criticalsectionfaq14.bas -'' -'' Example extracted from the FreeBASIC Manual -'' from topic 'Critical Sections FAQ' -'' -'' See Also: https://www.freebasic.net/wiki/wikka.php?wakka=ProPgMtCriticalSectionsFAQ -'' -------- - -Dim As Any Ptr ptid -Dim As Double t0 -Dim As Any Ptr p0 = @t0 -Dim As Double t1 -Dim As Double count -Dim As Single tmean -Dim As Single tmin = 10 '' start value -Dim As Single tmax = -10 '' start value - -Sub myThread (ByVal p As Any Ptr) - *Cast(Double Ptr, p) = Timer '' similar code line as in main code -End Sub - -Print "Tmin/Tmean/Tmax between begin of thread code and return from ThreadCreate() :" -Do - count += 1 - ptid = ThreadCreate(@myThread, @t1) - *Cast(Double Ptr, p0) = Timer '' similar code line as in thread code - - ThreadWait(ptid) - - tmean = (tmean * (count - 1) + (t1 - t0)) / count - If t1 - t0 < tmin Or t1 - t0 > tmax Then - If t1 - t0 < tmin Then - tmin = t1 - t0 - End If - If t1 - t0 > tmax Then - tmax = t1 - t0 - End If - Print Time; Using " Tmin=+###.###### ms Tmean=+###.###### ms Tmax=+###.###### ms"; tmin * 1000; tmean * 1000; tmax * 1000 - End If -Loop Until Inkey <> "" diff --git a/examples/manual/proguide/multithreading/emulatetls1.bas b/examples/manual/proguide/multithreading/emulatetls1.bas new file mode 100644 index 0000000000..cc53c0f157 --- /dev/null +++ b/examples/manual/proguide/multithreading/emulatetls1.bas @@ -0,0 +1,171 @@ +'' examples/manual/proguide/multithreading/emulatetls1.bas +'' +'' Example extracted from the FreeBASIC Manual +'' from topic 'Emulate a TLS (Thread Local Storage) and a TP (Thread Pooling) feature' +'' +'' See Also: https://www.freebasic.net/wiki/wikka.php?wakka=ProPgEmulateTlsTp +'' -------- + +#include Once "crt/string.bi" + +#if __FB_VERSION__ < "1.08" + ' Emulation of the function Threadself() of FreeBASIC + ' Before each use, the thread must refresh this function value with its own thread handle, + ' and all of this (refreshing + use) protected by a mutex. + Function ThreadSelf () ByRef As Any Ptr + Static As Any Ptr handle + Return handle + End Function +#else + #include Once "fbthread.bi" +#endif + +#macro CreateTLSdatatypeVariableFunction (variable_function_name, variable_datatype) +' Creation of a "variable_function_name" function to emulate a static datatype variable (not an array), +' with a value depending on the thread using it. + Namespace TLS + Function variable_function_name (ByVal cd As Boolean = True) ByRef As variable_datatype + ' Function emulating (creation/access/destruction) a static datatype variable with value depending on thread using it: + ' If calling without parameter (or with 'True') parameter, this allows to [create and] access the static datatype variable. + ' If calling with the 'False' parameter, this allows to destroy the static datatype variable. + Dim As Integer bound = 0 + Static As Any Ptr TLSindex(bound) + Static As variable_datatype TLSdata(bound) + Dim As Any Ptr Threadhandle = ThreadSelf() + Dim As Integer index = 0 + For I As Integer = 1 To UBound(TLSindex) ' search existing TLS variable (existing array element) for the running thread + If TLSindex(I) = Threadhandle Then + index = I + Exit For + End If + Next I + If index = 0 And cd = True Then ' create a new TLS variable (new array element) for a new thread + index = UBound(TLSindex) + 1 + ReDim Preserve TLSindex(index) + TLSindex(index) = Threadhandle + ReDim Preserve TLSdata(index) + ElseIf index > 0 And cd = False Then ' destroy a TLS variable (array element) and compact the array + If index < UBound(TLSindex) Then ' reorder the array elements + memmove(@TLSindex(index), @TLSindex(index + 1), (UBound(TLSindex) - index) * SizeOf(Any Ptr)) + Dim As variable_datatype Ptr p = Allocate(SizeOf(variable_datatype)) ' for compatibility to object with destructor + memmove(p, @TLSdata(index), SizeOf(variable_datatype)) ' for compatibility to object with destructor + memmove(@TLSdata(index), @TLSdata(index + 1), (UBound(TLSdata) - index) * SizeOf(variable_datatype)) + memmove(@TLSdata(UBound(TLSdata)), p, SizeOf(variable_datatype)) ' for compatibility to object with destructor + Deallocate(p) ' for compatibility to object with destructor + End If + ReDim Preserve TLSindex(UBound(TLSindex) - 1) + ReDim Preserve TLSdata(UBound(TLSdata) - 1) + index = 0 + End If + Return TLSdata(index) + End Function + End Namespace +#endmacro + +'------------------------------------------------------------------------------ + +Type threadData + Dim As Any Ptr handle + Dim As String prefix + Dim As String suffix + Dim As Double tempo + #if __FB_VERSION__ < "1.08" + Static As Any Ptr mutex + #endif +End Type +#if __FB_VERSION__ < "1.08" + Dim As Any Ptr threadData.mutex +#endif + +CreateTLSdatatypeVariableFunction (count, Integer) ' create a TLS static integer function + +Function counter() As Integer ' definition of a generic counter with counting depending on thread calling it + TLS.count() += 1 ' increment the TLS static integer + Return TLS.count() ' return the TLS static integer +End Function + +Sub Thread(ByVal p As Any Ptr) + Dim As threadData Ptr ptd = p + Dim As UInteger c + Do + #if __FB_VERSION__ < "1.08" + MutexLock(threadData.mutex) + ThreadSelf() = ptd->handle + #endif + c = counter() + #if __FB_VERSION__ < "1.08" + MutexUnlock(threadData.mutex) + #endif + Print ptd->prefix & c & ptd->suffix & " "; ' single print with concatenated string avoids using a mutex + Sleep ptd->tempo, 1 + Loop Until c = 12 + #if __FB_VERSION__ < "1.08" + MutexLock(threadData.mutex) + ThreadSelf() = ptd->handle + #endif + TLS.count(False) ' destroy the TLS static integer + #if __FB_VERSION__ < "1.08" + MutexUnlock(threadData.mutex) + #endif +End Sub + +'------------------------------------------------------------------------------ + +Print "|x| : counting from thread a" +Print "(x) : counting from thread b" +Print "[x] : counting from thread c" +Print + +#if __FB_VERSION__ < "1.08" + threadData.mutex = MutexCreate() +#endif + +Dim As threadData mtlsa +mtlsa.prefix = "|" +mtlsa.suffix = "|" +mtlsa.tempo = 100 +#if __FB_VERSION__ < "1.08" + MutexLock(threadData.mutex) +#endif +mtlsa.handle = ThreadCreate(@Thread, @mtlsa) +#if __FB_VERSION__ < "1.08" + MutexUnlock(threadData.mutex) +#endif + +Dim As threadData mtlsb +mtlsb.prefix = "(" +mtlsb.suffix = ")" +mtlsb.tempo = 150 +#if __FB_VERSION__ < "1.08" + MutexLock(threadData.mutex) +#endif +mtlsb.handle = ThreadCreate(@Thread, @mtlsb) +#if __FB_VERSION__ < "1.08" + MutexUnlock(threadData.mutex) +#endif + +Dim As threadData mtlsc +mtlsc.prefix = "[" +mtlsc.suffix = "]" +mtlsc.tempo = 250 +#if __FB_VERSION__ < "1.08" + MutexLock(threadData.mutex) +#endif +mtlsc.handle = ThreadCreate(@Thread, @mtlsc) +#if __FB_VERSION__ < "1.08" + MutexUnlock(threadData.mutex) +#endif + +ThreadWait(mtlsa.handle) +ThreadWait(mtlsb.handle) +ThreadWait(mtlsc.handle) +#if __FB_VERSION__ < "1.08" + MutexDestroy(threadData.mutex) +#endif + +Print +Print +Print "end of threads" + +Sleep + diff --git a/examples/manual/proguide/multithreading/criticalsectionfaq13.bas b/examples/manual/proguide/multithreading/emulatetp1.bas similarity index 98% rename from examples/manual/proguide/multithreading/criticalsectionfaq13.bas rename to examples/manual/proguide/multithreading/emulatetp1.bas index f543f3d113..a654886a11 100644 --- a/examples/manual/proguide/multithreading/criticalsectionfaq13.bas +++ b/examples/manual/proguide/multithreading/emulatetp1.bas @@ -1,4 +1,4 @@ -'' examples/manual/proguide/multithreading/criticalsectionfaq13.bas +'' examples/manual/proguide/multithreading/emulatetp1.bas '' '' Example extracted from the FreeBASIC Manual '' from topic 'Emulate a TLS (Thread Local Storage) and a TP (Thread Pooling) feature' diff --git a/examples/manual/proguide/multithreading/criticalsectionfaq13-2.bas b/examples/manual/proguide/multithreading/emulatetp2.bas similarity index 98% rename from examples/manual/proguide/multithreading/criticalsectionfaq13-2.bas rename to examples/manual/proguide/multithreading/emulatetp2.bas index 6c01fd0a13..32fe8983ae 100644 --- a/examples/manual/proguide/multithreading/criticalsectionfaq13-2.bas +++ b/examples/manual/proguide/multithreading/emulatetp2.bas @@ -1,4 +1,4 @@ -'' examples/manual/proguide/multithreading/criticalsectionfaq13-2.bas +'' examples/manual/proguide/multithreading/emulatetp2.bas '' '' Example extracted from the FreeBASIC Manual '' from topic 'Emulate a TLS (Thread Local Storage) and a TP (Thread Pooling) feature' diff --git a/examples/manual/proguide/multithreading/criticalsectionfaq13-3.bas b/examples/manual/proguide/multithreading/emulatetp3.bas similarity index 99% rename from examples/manual/proguide/multithreading/criticalsectionfaq13-3.bas rename to examples/manual/proguide/multithreading/emulatetp3.bas index 33323e315c..646c783323 100644 --- a/examples/manual/proguide/multithreading/criticalsectionfaq13-3.bas +++ b/examples/manual/proguide/multithreading/emulatetp3.bas @@ -1,4 +1,4 @@ -'' examples/manual/proguide/multithreading/criticalsectionfaq13-3.bas +'' examples/manual/proguide/multithreading/emulatetp3.bas '' '' Example extracted from the FreeBASIC Manual '' from topic 'Emulate a TLS (Thread Local Storage) and a TP (Thread Pooling) feature' diff --git a/examples/manual/proguide/multithreading/criticalsectionfaq13-4.bas b/examples/manual/proguide/multithreading/emulatetp4.bas similarity index 99% rename from examples/manual/proguide/multithreading/criticalsectionfaq13-4.bas rename to examples/manual/proguide/multithreading/emulatetp4.bas index bda59d0956..d824cdf706 100644 --- a/examples/manual/proguide/multithreading/criticalsectionfaq13-4.bas +++ b/examples/manual/proguide/multithreading/emulatetp4.bas @@ -1,4 +1,4 @@ -'' examples/manual/proguide/multithreading/criticalsectionfaq13-4.bas +'' examples/manual/proguide/multithreading/emulatetp4.bas '' '' Example extracted from the FreeBASIC Manual '' from topic 'Emulate a TLS (Thread Local Storage) and a TP (Thread Pooling) feature' diff --git a/examples/manual/proguide/multithreading/criticalsectionfaq13-5.bas b/examples/manual/proguide/multithreading/emulatetp5.bas similarity index 99% rename from examples/manual/proguide/multithreading/criticalsectionfaq13-5.bas rename to examples/manual/proguide/multithreading/emulatetp5.bas index 663be64bb4..8740d3cc7f 100644 --- a/examples/manual/proguide/multithreading/criticalsectionfaq13-5.bas +++ b/examples/manual/proguide/multithreading/emulatetp5.bas @@ -1,4 +1,4 @@ -'' examples/manual/proguide/multithreading/criticalsectionfaq13-5.bas +'' examples/manual/proguide/multithreading/emulatetp5.bas '' '' Example extracted from the FreeBASIC Manual '' from topic 'Emulate a TLS (Thread Local Storage) and a TP (Thread Pooling) feature'