Semaphores are back
Drawing thread is now always running waiting for semaphore signal
This commit is contained in:
170
osm.pb
170
osm.pb
@@ -58,7 +58,11 @@ Module OSM
|
|||||||
OSMZoom.i
|
OSMZoom.i
|
||||||
DeltaX.i
|
DeltaX.i
|
||||||
DeltaY.i
|
DeltaY.i
|
||||||
PassNb.i
|
Mutex.i
|
||||||
|
Semaphore.i
|
||||||
|
Dirty.i
|
||||||
|
PassNB.i
|
||||||
|
End.i
|
||||||
EndStructure
|
EndStructure
|
||||||
|
|
||||||
Structure TileThread
|
Structure TileThread
|
||||||
@@ -91,7 +95,7 @@ Module OSM
|
|||||||
|
|
||||||
TargetLocation.Location ; Latitude and Longitude from focus point
|
TargetLocation.Location ; Latitude and Longitude from focus point
|
||||||
TargetTile.Tile ; Focus tile coords
|
TargetTile.Tile ; Focus tile coords
|
||||||
*Drawing.DrawingParameters ; Drawing parameters based on focus point
|
Drawing.DrawingParameters ; Drawing parameters based on focus point
|
||||||
|
|
||||||
CallBackLocation.i ; @Procedure(latitude.d,lontitude.d)
|
CallBackLocation.i ; @Procedure(latitude.d,lontitude.d)
|
||||||
|
|
||||||
@@ -108,8 +112,7 @@ Module OSM
|
|||||||
MemCache.TileMemCach ; Image in memory cache
|
MemCache.TileMemCach ; Image in memory cache
|
||||||
List MapImageIndex.ImgMemCach() ; Index from MemCache\Image() to construct map
|
List MapImageIndex.ImgMemCach() ; Index from MemCache\Image() to construct map
|
||||||
|
|
||||||
DrawingThreadMutex.i ;Only one main drawing thread
|
Moving.i
|
||||||
EmergencyQuit.i
|
|
||||||
Dirty.i ;To signal that drawing need a refresh
|
Dirty.i ;To signal that drawing need a refresh
|
||||||
LoadingMutex.i
|
LoadingMutex.i
|
||||||
DrawingMutex.i
|
DrawingMutex.i
|
||||||
@@ -238,9 +241,10 @@ Module OSM
|
|||||||
OSM\LoadingMutex = CreateMutex()
|
OSM\LoadingMutex = CreateMutex()
|
||||||
OSM\DrawingMutex = CreateMutex()
|
OSM\DrawingMutex = CreateMutex()
|
||||||
;OSM\CurlMutex = CreateMutex()
|
;OSM\CurlMutex = CreateMutex()
|
||||||
OSM\DrawingThreadMutex = CreateMutex()
|
OSM\Moving = #False
|
||||||
OSM\EmergencyQuit = #False
|
|
||||||
OSM\Dirty = #False
|
OSM\Dirty = #False
|
||||||
|
OSM\Drawing\Semaphore = CreateSemaphore()
|
||||||
|
OSM\Drawing\Mutex = CreateMutex()
|
||||||
|
|
||||||
;-*** PROXY
|
;-*** PROXY
|
||||||
|
|
||||||
@@ -271,6 +275,7 @@ Module OSM
|
|||||||
EndIf
|
EndIf
|
||||||
|
|
||||||
curl_global_init(#CURL_GLOBAL_ALL);
|
curl_global_init(#CURL_GLOBAL_ALL);
|
||||||
|
CreateThread(@DrawingThread(), @OSM\Drawing)
|
||||||
|
|
||||||
EndProcedure
|
EndProcedure
|
||||||
;- ***
|
;- ***
|
||||||
@@ -458,7 +463,7 @@ Module OSM
|
|||||||
|
|
||||||
LockMutex(OSM\LoadingMutex)
|
LockMutex(OSM\LoadingMutex)
|
||||||
|
|
||||||
If OSM\EmergencyQuit = #False
|
If OSM\Moving = #False
|
||||||
LockMutex(OSM\MemCache\Mutex)
|
LockMutex(OSM\MemCache\Mutex)
|
||||||
*CacheImagePtr = AddElement(OSM\MemCache\Image())
|
*CacheImagePtr = AddElement(OSM\MemCache\Image())
|
||||||
Debug " CacheImagePtr : " + Str(*CacheImagePtr)
|
Debug " CacheImagePtr : " + Str(*CacheImagePtr)
|
||||||
@@ -468,11 +473,11 @@ Module OSM
|
|||||||
OSM\MemCache\Image()\nImage = -1 ;By now, this tile is in "loading" state, for thread synchro
|
OSM\MemCache\Image()\nImage = -1 ;By now, this tile is in "loading" state, for thread synchro
|
||||||
UnlockMutex(OSM\MemCache\Mutex)
|
UnlockMutex(OSM\MemCache\Mutex)
|
||||||
nImage = GetTileFromHDD(*Tile\OSMZoom, *Tile\OSMTileX, *Tile\OSMTileY)
|
nImage = GetTileFromHDD(*Tile\OSMZoom, *Tile\OSMTileX, *Tile\OSMTileY)
|
||||||
If nImage = -1 And OSM\EmergencyQuit = #False
|
If nImage = -1 And OSM\Moving = #False
|
||||||
nImage = GetTileFromWeb(*Tile\OSMZoom, *Tile\OSMTileX, *Tile\OSMTileY)
|
nImage = GetTileFromWeb(*Tile\OSMZoom, *Tile\OSMTileX, *Tile\OSMTileY)
|
||||||
EndIf
|
EndIf
|
||||||
LockMutex(OSM\MemCache\Mutex)
|
LockMutex(OSM\MemCache\Mutex)
|
||||||
If nImage <> -1 And OSM\EmergencyQuit = #False
|
If nImage <> -1 And OSM\Moving = #False
|
||||||
Debug "Adding tile " + Str(nImage) + " to mem cache"
|
Debug "Adding tile " + Str(nImage) + " to mem cache"
|
||||||
;AddTileToMemCache(Zoom, XTile, YTile, nImage)
|
;AddTileToMemCache(Zoom, XTile, YTile, nImage)
|
||||||
OSM\MemCache\Image()\nImage = nImage
|
OSM\MemCache\Image()\nImage = nImage
|
||||||
@@ -496,10 +501,9 @@ Module OSM
|
|||||||
|
|
||||||
Debug " Drawing tile nb " + " X : " + Str(*Tile\OSMTileX) + " Y : " + Str(*Tile\OSMTileX)
|
Debug " Drawing tile nb " + " X : " + Str(*Tile\OSMTileX) + " Y : " + Str(*Tile\OSMTileX)
|
||||||
Debug " at coords " + Str(x) + "," + Str(y)
|
Debug " at coords " + Str(x) + "," + Str(y)
|
||||||
|
|
||||||
|
|
||||||
LockMutex(OSM\DrawingMutex)
|
LockMutex(OSM\DrawingMutex)
|
||||||
If OSM\EmergencyQuit = #False ;Quit before drawing
|
If OSM\Moving = #False ;Quit before drawing
|
||||||
StartVectorDrawing(CanvasVectorOutput(OSM\Gadget))
|
StartVectorDrawing(CanvasVectorOutput(OSM\Gadget))
|
||||||
If IsImage(*Tile\nImage)
|
If IsImage(*Tile\nImage)
|
||||||
MovePathCursor(x, y)
|
MovePathCursor(x, y)
|
||||||
@@ -508,7 +512,7 @@ Module OSM
|
|||||||
DrawVectorText(Str(x) + ", " + Str(y))
|
DrawVectorText(Str(x) + ", " + Str(y))
|
||||||
Else
|
Else
|
||||||
Debug "Image missing"
|
Debug "Image missing"
|
||||||
OSM\Dirty = #True ;Signal that this image is missing so we should have to redraw
|
OSM\Drawing\Dirty = #True ;Signal that this image is missing so we should have to redraw
|
||||||
EndIf
|
EndIf
|
||||||
StopVectorDrawing()
|
StopVectorDrawing()
|
||||||
EndIf
|
EndIf
|
||||||
@@ -534,7 +538,7 @@ Module OSM
|
|||||||
For y = - ny To ny
|
For y = - ny To ny
|
||||||
For x = - nx To nx
|
For x = - nx To nx
|
||||||
|
|
||||||
If OSM\EmergencyQuit
|
If OSM\Moving
|
||||||
Break 2
|
Break 2
|
||||||
EndIf
|
EndIf
|
||||||
|
|
||||||
@@ -622,70 +626,69 @@ Module OSM
|
|||||||
|
|
||||||
Procedure DrawingThread(*Drawing.DrawingParameters)
|
Procedure DrawingThread(*Drawing.DrawingParameters)
|
||||||
|
|
||||||
Debug "--------- Main drawing thread ------------"
|
Repeat
|
||||||
OSM\Dirty = #False
|
|
||||||
|
WaitSemaphore(OSM\Drawing\Semaphore)
|
||||||
LockMutex(OSM\DrawingThreadMutex) ; Only one main drawing thread at once
|
|
||||||
|
Debug "--------- Main drawing thread ------------"
|
||||||
Protected CenterX = GadgetWidth(OSM\Gadget) / 2
|
|
||||||
Protected CenterY = GadgetHeight(OSM\Gadget) / 2
|
LockMutex(OSM\Drawing\Mutex) ; Only one main drawing thread at once
|
||||||
|
|
||||||
DrawTiles(*Drawing)
|
OSM\Drawing\Dirty = #False
|
||||||
|
|
||||||
LockMutex(OSM\DrawingMutex)
|
Protected CenterX = GadgetWidth(OSM\Gadget) / 2
|
||||||
StartVectorDrawing(CanvasVectorOutput(OSM\Gadget))
|
Protected CenterY = GadgetHeight(OSM\Gadget) / 2
|
||||||
DrawTrack(*Drawing)
|
|
||||||
Pointer(CenterX, CenterY, #Red)
|
DrawTiles(*Drawing)
|
||||||
StopVectorDrawing()
|
|
||||||
UnlockMutex(OSM\DrawingMutex)
|
LockMutex(OSM\DrawingMutex)
|
||||||
|
StartVectorDrawing(CanvasVectorOutput(OSM\Gadget))
|
||||||
UnlockMutex(OSM\DrawingThreadMutex)
|
DrawTrack(*Drawing)
|
||||||
|
Pointer(CenterX, CenterY, #Red)
|
||||||
;- Redraw
|
StopVectorDrawing()
|
||||||
;If something was not correctly drawn, redraw after a while
|
UnlockMutex(OSM\DrawingMutex)
|
||||||
If OSM\Dirty And OSM\EmergencyQuit = #False
|
|
||||||
Debug "Something was dirty ! We try again to redraw"
|
;- Redraw
|
||||||
;Delay(250)
|
;If something was not correctly drawn, redraw after a while
|
||||||
*Drawing\PassNb + 1
|
If OSM\Drawing\Dirty And OSM\Moving = #False
|
||||||
CreateThread(@DrawingThread(), *Drawing)
|
Debug "Something was dirty ! We try again to redraw"
|
||||||
EndIf
|
;Delay(250)
|
||||||
|
OSM\Drawing\PassNb + 1
|
||||||
*Drawing\PassNb - 1
|
SignalSemaphore(OSM\Drawing\Semaphore)
|
||||||
If *Drawing\PassNb = 0
|
EndIf
|
||||||
FreeMemory(*Drawing)
|
|
||||||
EndIf
|
UnlockMutex(OSM\Drawing\Mutex)
|
||||||
|
|
||||||
|
Until OSM\Drawing\End
|
||||||
|
|
||||||
EndProcedure
|
EndProcedure
|
||||||
|
|
||||||
Procedure SetLocation(latitude.d, longitude.d, zoom = 15)
|
Procedure SetLocation(latitude.d, longitude.d, zoom = 15)
|
||||||
|
|
||||||
Protected *Drawing.DrawingParameters
|
|
||||||
|
|
||||||
If zoom > OSM\ZoomMax : zoom = OSM\ZoomMax : EndIf
|
If zoom > OSM\ZoomMax : zoom = OSM\ZoomMax : EndIf
|
||||||
If zoom < OSM\ZoomMin : zoom = OSM\ZoomMin : EndIf
|
If zoom < OSM\ZoomMin : zoom = OSM\ZoomMin : EndIf
|
||||||
OSM\Zoom = zoom
|
OSM\Zoom = zoom
|
||||||
OSM\TargetLocation\Latitude = latitude
|
OSM\TargetLocation\Latitude = latitude
|
||||||
OSM\TargetLocation\Longitude = longitude
|
OSM\TargetLocation\Longitude = longitude
|
||||||
LatLon2XY(@OSM\TargetLocation, @OSM\TargetTile)
|
LatLon2XY(@OSM\TargetLocation, @OSM\TargetTile)
|
||||||
;*** Creates a drawing thread and fill parameters
|
|
||||||
*Drawing = AllocateMemory(SizeOf(DrawingParameters))
|
|
||||||
;Convert X, Y in tile.decimal into real pixels
|
;Convert X, Y in tile.decimal into real pixels
|
||||||
*Drawing\x = OSM\TargetTile\x
|
OSM\Position\X = OSM\TargetTile\X * OSM\TileSize
|
||||||
*Drawing\y = OSM\TargetTile\y
|
OSM\Position\Y = OSM\TargetTile\Y * OSM\TileSize
|
||||||
|
;*** Creates a drawing thread and fill parameters
|
||||||
|
LockMutex(OSM\Drawing\Mutex)
|
||||||
|
OSM\Drawing\x = OSM\TargetTile\x
|
||||||
|
OSM\Drawing\y = OSM\TargetTile\y
|
||||||
;Position in the tile
|
;Position in the tile
|
||||||
*Drawing\DeltaX = *Drawing\x * OSM\TileSize - (Int(*Drawing\x) * OSM\TileSize)
|
OSM\Drawing\DeltaX = OSM\Drawing\x * OSM\TileSize - (Int(OSM\Drawing\x) * OSM\TileSize)
|
||||||
*Drawing\DeltaY = *Drawing\y * OSM\TileSize - (Int(*Drawing\y) * OSM\TileSize)
|
OSM\Drawing\DeltaY = OSM\Drawing\y * OSM\TileSize - (Int(OSM\Drawing\y) * OSM\TileSize)
|
||||||
OSM\EmergencyQuit = #False
|
UnlockMutex(OSM\Drawing\Mutex)
|
||||||
*Drawing\PassNb = 1
|
SignalSemaphore(OSM\Drawing\Semaphore)
|
||||||
CreateThread(@DrawingThread(), *Drawing)
|
|
||||||
;***
|
;***
|
||||||
|
|
||||||
EndProcedure
|
EndProcedure
|
||||||
|
|
||||||
Procedure SetZoom(Zoom.i, mode.i = #PB_Relative)
|
Procedure SetZoom(Zoom.i, mode.i = #PB_Relative)
|
||||||
|
|
||||||
Protected *Drawing.DrawingParameters
|
|
||||||
|
|
||||||
Select mode
|
Select mode
|
||||||
Case #PB_Relative
|
Case #PB_Relative
|
||||||
OSM\Zoom = OSM\Zoom + zoom
|
OSM\Zoom = OSM\Zoom + zoom
|
||||||
@@ -696,17 +699,17 @@ Module OSM
|
|||||||
If OSM\Zoom > OSM\ZoomMax : OSM\Zoom = OSM\ZoomMax : EndIf
|
If OSM\Zoom > OSM\ZoomMax : OSM\Zoom = OSM\ZoomMax : EndIf
|
||||||
If OSM\Zoom < OSM\ZoomMin : OSM\Zoom = OSM\ZoomMin : EndIf
|
If OSM\Zoom < OSM\ZoomMin : OSM\Zoom = OSM\ZoomMin : EndIf
|
||||||
LatLon2XY(@OSM\TargetLocation, @OSM\TargetTile)
|
LatLon2XY(@OSM\TargetLocation, @OSM\TargetTile)
|
||||||
|
OSM\Position\X = OSM\TargetTile\X * OSM\TileSize
|
||||||
|
OSM\Position\Y = OSM\TargetTile\Y * OSM\TileSize
|
||||||
;*** Creates a drawing thread and fill parameters
|
;*** Creates a drawing thread and fill parameters
|
||||||
*Drawing = AllocateMemory(SizeOf(DrawingParameters))
|
LockMutex(OSM\Drawing\Mutex)
|
||||||
;Convert X, Y in tile.decimal into real pixels
|
OSM\Drawing\x = OSM\TargetTile\x
|
||||||
*Drawing\x = OSM\TargetTile\x
|
OSM\Drawing\y = OSM\TargetTile\y
|
||||||
*Drawing\y = OSM\TargetTile\y
|
|
||||||
;Position in the tile
|
;Position in the tile
|
||||||
*Drawing\DeltaX = *Drawing\x * OSM\TileSize - Int(*Drawing\x) * OSM\TileSize
|
OSM\Drawing\DeltaX = OSM\Drawing\x * OSM\TileSize - (Int(OSM\Drawing\x) * OSM\TileSize)
|
||||||
*Drawing\DeltaY = *Drawing\y * OSM\TileSize - Int(*Drawing\y) * OSM\TileSize
|
OSM\Drawing\DeltaY = OSM\Drawing\y * OSM\TileSize - (Int(OSM\Drawing\y) * OSM\TileSize)
|
||||||
OSM\EmergencyQuit = #False
|
UnlockMutex(OSM\Drawing\Mutex)
|
||||||
*Drawing\PassNb = 1
|
SignalSemaphore(OSM\Drawing\Semaphore)
|
||||||
CreateThread(@DrawingThread(), *Drawing)
|
|
||||||
;***
|
;***
|
||||||
|
|
||||||
EndProcedure
|
EndProcedure
|
||||||
@@ -736,7 +739,7 @@ Module OSM
|
|||||||
Case #PB_EventType_MouseMove
|
Case #PB_EventType_MouseMove
|
||||||
If OSM\MoveStartingPoint\x <> - 1
|
If OSM\MoveStartingPoint\x <> - 1
|
||||||
;Need a refresh
|
;Need a refresh
|
||||||
OSM\EmergencyQuit = #True
|
OSM\Moving = #True
|
||||||
MouseX = GetGadgetAttribute(OSM\Gadget, #PB_Canvas_MouseX) - OSM\MoveStartingPoint\x
|
MouseX = GetGadgetAttribute(OSM\Gadget, #PB_Canvas_MouseX) - OSM\MoveStartingPoint\x
|
||||||
MouseY = GetGadgetAttribute(OSM\Gadget, #PB_Canvas_MouseY) - OSM\MoveStartingPoint\y
|
MouseY = GetGadgetAttribute(OSM\Gadget, #PB_Canvas_MouseY) - OSM\MoveStartingPoint\y
|
||||||
;Old move values
|
;Old move values
|
||||||
@@ -745,26 +748,26 @@ Module OSM
|
|||||||
;New move values
|
;New move values
|
||||||
OSM\Position\x - MouseX
|
OSM\Position\x - MouseX
|
||||||
OSM\Position\y - MouseY
|
OSM\Position\y - MouseY
|
||||||
;-*** Creates a drawing thread and fill parameters
|
;-*** Sill parameters and signal the drawing thread
|
||||||
*Drawing = AllocateMemory(SizeOf(DrawingParameters))
|
LockMutex(OSM\Drawing\Mutex)
|
||||||
;OSM tile position in tile.decimal
|
;OSM tile position in tile.decimal
|
||||||
*Drawing\x = OSM\Position\x / OSM\TileSize
|
OSM\Drawing\x = OSM\Position\x / OSM\TileSize
|
||||||
*Drawing\y = OSM\Position\y / OSM\TileSize
|
OSM\Drawing\y = OSM\Position\y / OSM\TileSize
|
||||||
;Pixel shift
|
;Pixel shift
|
||||||
*Drawing\DeltaX = OSM\Position\x - Int(*Drawing\x) * OSM\TileSize
|
OSM\Drawing\DeltaX = OSM\Position\x - Int(OSM\Drawing\x) * OSM\TileSize
|
||||||
*Drawing\DeltaY = OSM\Position\y - Int(*Drawing\y) * OSM\TileSize
|
OSM\Drawing\DeltaY = OSM\Position\y - Int(OSM\Drawing\y) * OSM\TileSize
|
||||||
;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))
|
||||||
;Debug "--- New tile"
|
;Debug "--- New tile"
|
||||||
;*Drawing\x = TileX
|
;*Drawing\x = TileX
|
||||||
;*Drawing\y = TileY
|
;*Drawing\y = TileY
|
||||||
Debug "OSM\Position\x " + Str(OSM\Position\x) + " ; OSM\Position\y " + Str(OSM\Position\y)
|
Debug "OSM\Position\x " + Str(OSM\Position\x) + " ; OSM\Position\y " + Str(OSM\Position\y)
|
||||||
XY2LatLon(*Drawing, @OSM\TargetLocation)
|
XY2LatLon(@OSM\Drawing, @OSM\TargetLocation)
|
||||||
Debug "OSM\TargetTile\x " + StrD(*Drawing\x) + " ; OSM\TargetTile\y " + StrD(*Drawing\y)
|
Debug "OSM\TargetTile\x " + StrD(OSM\Drawing\x) + " ; OSM\TargetTile\y " + StrD(OSM\Drawing\y)
|
||||||
;EndIf
|
;EndIf
|
||||||
OSM\EmergencyQuit = #False
|
OSM\Drawing\PassNb = 1
|
||||||
*Drawing\PassNb = 1
|
UnlockMutex(OSM\Drawing\Mutex)
|
||||||
CreateThread(@DrawingThread(), *Drawing)
|
SignalSemaphore(OSM\Drawing\Semaphore)
|
||||||
;- ***
|
;- ***
|
||||||
OSM\MoveStartingPoint\x = GetGadgetAttribute(OSM\Gadget, #PB_Canvas_MouseX)
|
OSM\MoveStartingPoint\x = GetGadgetAttribute(OSM\Gadget, #PB_Canvas_MouseX)
|
||||||
OSM\MoveStartingPoint\y = GetGadgetAttribute(OSM\Gadget, #PB_Canvas_MouseY)
|
OSM\MoveStartingPoint\y = GetGadgetAttribute(OSM\Gadget, #PB_Canvas_MouseY)
|
||||||
@@ -774,6 +777,7 @@ Module OSM
|
|||||||
EndIf
|
EndIf
|
||||||
EndIf
|
EndIf
|
||||||
Case #PB_EventType_LeftButtonUp
|
Case #PB_EventType_LeftButtonUp
|
||||||
|
OSM\Moving = #False
|
||||||
OSM\MoveStartingPoint\x = - 1
|
OSM\MoveStartingPoint\x = - 1
|
||||||
OSM\TargetTile\x = OSM\Position\x / OSM\TileSize
|
OSM\TargetTile\x = OSM\Position\x / OSM\TileSize
|
||||||
OSM\TargetTile\y = OSM\Position\y / OSM\TileSize
|
OSM\TargetTile\y = OSM\Position\y / OSM\TileSize
|
||||||
@@ -877,8 +881,8 @@ CompilerIf #PB_Compiler_IsMainFile
|
|||||||
CompilerEndIf
|
CompilerEndIf
|
||||||
|
|
||||||
; IDE Options = PureBasic 5.42 LTS (Windows - x64)
|
; IDE Options = PureBasic 5.42 LTS (Windows - x64)
|
||||||
; CursorPosition = 754
|
; CursorPosition = 659
|
||||||
; FirstLine = 726
|
; FirstLine = 626
|
||||||
; Folding = -----
|
; Folding = -----
|
||||||
; EnableUnicode
|
; EnableUnicode
|
||||||
; EnableThread
|
; EnableThread
|
||||||
|
Reference in New Issue
Block a user