Better loading with better mutex handling

This commit is contained in:
djes
2016-07-27 20:26:09 +02:00
parent b93f22adb1
commit 8b817c2525

28
osm.pb
View File

@@ -58,6 +58,7 @@ Module OSM
OSMTileX.i OSMTileX.i
OSMTileY.i OSMTileY.i
OSMZoom.i OSMZoom.i
Mutex.i
Semaphore.i Semaphore.i
Dirty.i Dirty.i
PassNB.i PassNB.i
@@ -241,6 +242,7 @@ Module OSM
OSM\MemCache\Mutex = CreateMutex() OSM\MemCache\Mutex = CreateMutex()
;OSM\CurlMutex = CreateMutex() ;OSM\CurlMutex = CreateMutex()
OSM\Dirty = #False OSM\Dirty = #False
OSM\Drawing\Mutex = CreateMutex()
OSM\Drawing\Semaphore = CreateSemaphore() OSM\Drawing\Semaphore = CreateSemaphore()
;-*** PROXY ;-*** PROXY
@@ -370,7 +372,7 @@ Module OSM
Debug "Check if we have this image in memory" Debug "Check if we have this image in memory"
LockMutex(OSM\MemCache\Mutex) ;TODO : use maps
ForEach OSM\MemCache\Image() ForEach OSM\MemCache\Image()
If Zoom = OSM\MemCache\Image()\Zoom And OSM\MemCache\Image()\xTile = XTile And OSM\MemCache\Image()\yTile = YTile If Zoom = OSM\MemCache\Image()\Zoom And OSM\MemCache\Image()\xTile = XTile And OSM\MemCache\Image()\yTile = YTile
nImage = OSM\MemCache\Image()\nImage nImage = OSM\MemCache\Image()\nImage
@@ -380,7 +382,6 @@ Module OSM
; DeleteElement(OSM\MemCache\Image()) ; DeleteElement(OSM\MemCache\Image())
EndIf EndIf
Next Next
UnlockMutex(OSM\MemCache\Mutex)
ProcedureReturn nImage ProcedureReturn nImage
@@ -394,6 +395,7 @@ Module OSM
Debug "Check if we have this image on HDD" Debug "Check if we have this image on HDD"
If FileSize(OSM\HDDCachePath + cacheFile) > 0 If FileSize(OSM\HDDCachePath + cacheFile) > 0
nImage = LoadImage(#PB_Any, OSM\HDDCachePath + CacheFile) nImage = LoadImage(#PB_Any, OSM\HDDCachePath + CacheFile)
If IsImage(nImage) If IsImage(nImage)
@@ -522,6 +524,8 @@ Module OSM
Debug "Drawing tiles" Debug "Drawing tiles"
;We're locking the cache to launch all drawings with existing tiles (the loading threads will be launched but delayed after the drawing)
LockMutex(OSM\MemCache\Mutex)
For y = - ny - 1 To ny + 1 For y = - ny - 1 To ny + 1
For x = - nx - 1 To nx + 1 For x = - nx - 1 To nx + 1
@@ -548,7 +552,7 @@ Module OSM
;Check if the image exists ;Check if the image exists
\nImage = GetTileFromMem(\OSMZoom, \OSMTileX, \OSMTileY) \nImage = GetTileFromMem(\OSMZoom, \OSMTileX, \OSMTileY)
If \nImage = -1 If \nImage = -1
;If not, load it in the background ;If not, load it in the background (but after the drawing thanks to the mutex)
\GetImageThread = CreateThread(@GetImageThread(), *NewTile) \GetImageThread = CreateThread(@GetImageThread(), *NewTile)
OSM\TilesThreads()\GetImageThread = \GetImageThread OSM\TilesThreads()\GetImageThread = \GetImageThread
Debug " Creating get image thread nb " + Str(\GetImageThread) Debug " Creating get image thread nb " + Str(\GetImageThread)
@@ -563,6 +567,7 @@ Module OSM
EndIf EndIf
Next Next
Next Next
UnlockMutex(OSM\MemCache\Mutex)
;Free tile memory when the loading thread has finished ;Free tile memory when the loading thread has finished
;TODO : exit this proc from drawtiles in a special "free ressources" task ;TODO : exit this proc from drawtiles in a special "free ressources" task
@@ -666,17 +671,18 @@ Module OSM
;- Redraw ;- Redraw
;If something was not correctly drawn, redraw after a while ;If something was not correctly drawn, redraw after a while
;Be sure that we're not modifying while moving
LockMutex(OSM\Drawing\Mutex)
If *Drawing\Dirty If *Drawing\Dirty
Debug "Something was dirty ! We try again to redraw" Debug "Something was dirty ! We try again to redraw"
;Delay(250) ;Delay(250)
*Drawing\PassNb + 1 *Drawing\PassNb + 1
SignalSemaphore(*Drawing\Semaphore) SignalSemaphore(*Drawing\Semaphore)
;TODO : Could be nice to avoid multiple redraws when not moving anymore Else
; Else ;Clean the semaphore to avoid multiple unuseful redraws
; ;Clean the semaphore Repeat : Until TrySemaphore(*Drawing\Semaphore) = 0
; Repeat
; Until TrySemaphore(*Drawing\Semaphore) = 0
EndIf EndIf
UnlockMutex(OSM\Drawing\Mutex)
Until *Drawing\End Until *Drawing\End
@@ -819,9 +825,11 @@ Module OSM
OSM\Position\y - MouseY OSM\Position\y - MouseY
;-*** Fill parameters and signal the drawing thread ;-*** Fill parameters and signal the drawing thread
;OSM tile position in tile.decimal ;OSM tile position in tile.decimal
LockMutex(OSM\Drawing\Mutex)
OSM\Drawing\x = OSM\Position\x / OSM\TileSize OSM\Drawing\x = OSM\Position\x / OSM\TileSize
OSM\Drawing\y = OSM\Position\y / OSM\TileSize OSM\Drawing\y = OSM\Position\y / OSM\TileSize
OSM\Drawing\PassNb = 1 OSM\Drawing\PassNb = 1
UnlockMutex(OSM\Drawing\Mutex)
;Moved to a new tile ? ;Moved to a new tile ?
;If (Int(OSM\Position\x / OSM\TileSize)) <> (Int(OldX / OSM\TileSize)) Or (Int(OSM\Position\y / OSM\TileSize)) <> (Int(OldY / OSM\TileSize)) ;If (Int(OSM\Position\x / OSM\TileSize)) <> (Int(OldX / OSM\TileSize)) Or (Int(OSM\Position\y / OSM\TileSize)) <> (Int(OldY / OSM\TileSize))
XY2LatLon(@OSM\Drawing, @OSM\TargetLocation) XY2LatLon(@OSM\Drawing, @OSM\TargetLocation)
@@ -974,8 +982,8 @@ CompilerIf #PB_Compiler_IsMainFile
CompilerEndIf CompilerEndIf
; IDE Options = PureBasic 5.42 LTS (Windows - x64) ; IDE Options = PureBasic 5.42 LTS (Windows - x64)
; CursorPosition = 672 ; CursorPosition = 374
; FirstLine = 646 ; FirstLine = 348
; Folding = ------ ; Folding = ------
; EnableUnicode ; EnableUnicode
; EnableThread ; EnableThread