Download slots bugfix

This commit is contained in:
djes
2017-06-12 12:32:51 +02:00
parent 1b903ffdc6
commit bb405afca9

View File

@@ -1095,7 +1095,7 @@ Module PBMap
If LifeTime > MaxLifeTime If LifeTime > MaxLifeTime
MyDebug(" Deleting too old (" + StrU(LifeTime) + " secs) " + CacheFile, 3) MyDebug(" Deleting too old (" + StrU(LifeTime) + " secs) " + CacheFile, 3)
DeleteFile(CacheFile) DeleteFile(CacheFile)
ProcedureReturn 0 ProcedureReturn #False
EndIf EndIf
EndIf EndIf
; Everything is OK, loads the file ; Everything is OK, loads the file
@@ -1155,6 +1155,7 @@ Module PBMap
PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_TILE_CLEANUP, *Tile) ; To free memory outside the thread PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_TILE_CLEANUP, *Tile) ; To free memory outside the thread
ProcedureReturn #False ProcedureReturn #False
EndIf EndIf
MyDebug(" Thread for image " + *Tile\CacheFile + " waiting a download slot", 5)
Delay(500) Delay(500)
LockMutex(PBMap\DownloadSlotsMutex) LockMutex(PBMap\DownloadSlotsMutex)
Wend Wend
@@ -1169,16 +1170,25 @@ Module PBMap
Case #PB_Http_Success Case #PB_Http_Success
Size = FinishHTTP(*Tile\Download) Size = FinishHTTP(*Tile\Download)
MyDebug(" Thread for image " + *Tile\CacheFile + " finished. Size : " + Str(Size), 5) MyDebug(" Thread for image " + *Tile\CacheFile + " finished. Size : " + Str(Size), 5)
LockMutex(PBMap\DownloadSlotsMutex)
PBMap\DownloadSlots - 1
UnlockMutex(PBMap\DownloadSlotsMutex)
PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_TILE_CLEANUP, *Tile) ; To free memory outside the thread PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_TILE_CLEANUP, *Tile) ; To free memory outside the thread
ProcedureReturn #True ProcedureReturn #True
Case #PB_Http_Failed Case #PB_Http_Failed
FinishHTTP(*Tile\Download) FinishHTTP(*Tile\Download)
MyDebug(" Thread for image " + *Tile\CacheFile + " failed.", 5) MyDebug(" Thread for image " + *Tile\CacheFile + " failed.", 5)
LockMutex(PBMap\DownloadSlotsMutex)
PBMap\DownloadSlots - 1
UnlockMutex(PBMap\DownloadSlotsMutex)
PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_TILE_CLEANUP, *Tile) ; To free memory outside the thread PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_TILE_CLEANUP, *Tile) ; To free memory outside the thread
ProcedureReturn #False ProcedureReturn #False
Case #PB_Http_Aborted Case #PB_Http_Aborted
FinishHTTP(*Tile\Download) FinishHTTP(*Tile\Download)
MyDebug(" Thread for image " + *Tile\CacheFile + " aborted.", 5) MyDebug(" Thread for image " + *Tile\CacheFile + " aborted.", 5)
LockMutex(PBMap\DownloadSlotsMutex)
PBMap\DownloadSlots - 1
UnlockMutex(PBMap\DownloadSlotsMutex)
PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_TILE_CLEANUP, *Tile) ; To free memory outside the thread PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_TILE_CLEANUP, *Tile) ; To free memory outside the thread
ProcedureReturn #False ProcedureReturn #False
Default Default
@@ -1200,9 +1210,9 @@ Module PBMap
; HDD, or launch a web loading thread, and try again on the next drawing loop. ; HDD, or launch a web loading thread, and try again on the next drawing loop.
Protected *timg.ImgMemCach = FindMapElement(PBMap\MemCache\Images(), key) Protected *timg.ImgMemCach = FindMapElement(PBMap\MemCache\Images(), key)
If *timg If *timg
MyDebug("Key : " + key + " found in memory cache", 5) MyDebug("Key : " + key + " found in memory cache", 4)
If *timg\nImage If *timg\nImage
MyDebug(" as image " + *timg\nImage, 5) MyDebug(" as image " + *timg\nImage, 4)
; *** Cache management ; *** Cache management
; Retrieves the image in the time stack, push it to the end (to say it's the lastly used) ; Retrieves the image in the time stack, push it to the end (to say it's the lastly used)
ChangeCurrentElement(PBMap\MemCache\ImagesTimeStack(), *timg\TimeStackPtr) ChangeCurrentElement(PBMap\MemCache\ImagesTimeStack(), *timg\TimeStackPtr)
@@ -1211,13 +1221,13 @@ Module PBMap
; *** ; ***
ProcedureReturn *timg ProcedureReturn *timg
Else Else
MyDebug(" but not the image.", 5) MyDebug(" but not the image.", 4)
EndIf EndIf
Else Else
; Creates a new cache element ; Creates a new cache element
*timg = AddMapElement(PBMap\MemCache\Images(), key) *timg = AddMapElement(PBMap\MemCache\Images(), key)
If *timg = 0 If *timg = 0
MyDebug(" Can't add a new cache element.", 5) MyDebug(" Can't add a new cache element.", 4)
ProcedureReturn 0 ProcedureReturn 0
EndIf EndIf
; add a new time stack element at the End ; add a new time stack element at the End
@@ -1225,13 +1235,13 @@ Module PBMap
; Stores the time stack ptr ; Stores the time stack ptr
*timg\TimeStackPtr = AddElement(PBMap\MemCache\ImagesTimeStack()) *timg\TimeStackPtr = AddElement(PBMap\MemCache\ImagesTimeStack())
If *timg\TimeStackPtr = 0 If *timg\TimeStackPtr = 0
MyDebug(" Can't add a new time stack element.", 5) MyDebug(" Can't add a new time stack element.", 4)
DeleteMapElement(PBMap\MemCache\Images()) DeleteMapElement(PBMap\MemCache\Images())
ProcedureReturn 0 ProcedureReturn 0
EndIf EndIf
; Associates the time stack element to the cache element ; Associates the time stack element to the cache element
PBMap\MemCache\ImagesTimeStack()\MapKey = MapKey(PBMap\MemCache\Images()) PBMap\MemCache\ImagesTimeStack()\MapKey = MapKey(PBMap\MemCache\Images())
MyDebug("Key : " + key + " added in memory cache", 5) MyDebug("Key : " + key + " added in memory cache", 4)
; *** ; ***
EndIf EndIf
If *timg\Tile = 0 ; Checks if a loading thread is not already running If *timg\Tile = 0 ; Checks if a loading thread is not already running
@@ -2439,9 +2449,6 @@ Module PBMap
; After a Web tile loading thread, clean the tile structure memory and set the image nb in the cache ; After a Web tile loading thread, clean the tile structure memory and set the image nb in the cache
; avoid to have threads accessing vars (and avoid some mutex), see GetImageThread() ; avoid to have threads accessing vars (and avoid some mutex), see GetImageThread()
*Tile\Download = 0 *Tile\Download = 0
LockMutex(PBMap\DownloadSlotsMutex)
PBMap\DownloadSlots - 1
UnlockMutex(PBMap\DownloadSlotsMutex)
Protected timg = PBMap\MemCache\Images(key)\Tile\nImage ; Get this new tile image nb Protected timg = PBMap\MemCache\Images(key)\Tile\nImage ; Get this new tile image nb
PBMap\MemCache\Images(key)\nImage = timg ; Stores it in the cache using the key PBMap\MemCache\Images(key)\nImage = timg ; Stores it in the cache using the key
FreeMemory(PBMap\MemCache\Images(key)\Tile) ; Frees the data needed for the thread FreeMemory(PBMap\MemCache\Images(key)\Tile) ; Frees the data needed for the thread
@@ -2837,9 +2844,10 @@ CompilerIf #PB_Compiler_IsMainFile
CompilerEndIf CompilerEndIf
; IDE Options = PureBasic 5.60 (Windows - x64) ; IDE Options = PureBasic 5.60 (Windows - x64)
; CursorPosition = 1259 ; CursorPosition = 1107
; FirstLine = 1229 ; FirstLine = 1104
; Folding = ------------------- ; Folding = -------------------
; EnableThread ; EnableThread
; EnableXP ; EnableXP
; CompileSourceDirectory ; CompileSourceDirectory
; Watchlist = PBMap::PBMap\DownloadSlots