Commit aa2da210 authored by Guillaume Seguin's avatar Guillaume Seguin

Protect critical code paths in 3D viewer

I have yet to see a crash from such an issue, but it could clearly
happen, so let's make it safe
parent e41dc4f0
...@@ -20,6 +20,7 @@ import time ...@@ -20,6 +20,7 @@ import time
import numpy import numpy
import math import math
import logging import logging
import threading
from ctypes import sizeof from ctypes import sizeof
...@@ -221,10 +222,14 @@ class Model(object): ...@@ -221,10 +222,14 @@ class Model(object):
axis_letter_map = dict([(v, k) for k, v in letter_axis_map.items()]) axis_letter_map = dict([(v, k) for k, v in letter_axis_map.items()])
lock = None
def __init__(self, offset_x=0, offset_y=0): def __init__(self, offset_x=0, offset_y=0):
self.offset_x = offset_x self.offset_x = offset_x
self.offset_y = offset_y self.offset_y = offset_y
self.lock = threading.Lock()
self.init_model_attributes() self.init_model_attributes()
def init_model_attributes(self): def init_model_attributes(self):
...@@ -341,6 +346,7 @@ class GcodeModel(Model): ...@@ -341,6 +346,7 @@ class GcodeModel(Model):
self.only_current = False self.only_current = False
while layer_idx < len(model_data.all_layers): while layer_idx < len(model_data.all_layers):
with self.lock:
nlines = len(model_data) nlines = len(model_data)
if nlines * 12 != vertices.size: if nlines * 12 != vertices.size:
self.travels.resize(nlines * 6, refcheck = False) self.travels.resize(nlines * 6, refcheck = False)
...@@ -505,6 +511,7 @@ class GcodeModel(Model): ...@@ -505,6 +511,7 @@ class GcodeModel(Model):
(model_data.ymin, model_data.ymax, model_data.depth), (model_data.ymin, model_data.ymax, model_data.depth),
(model_data.zmin, model_data.zmax, model_data.height)) (model_data.zmin, model_data.zmax, model_data.height))
with self.lock:
self.travels.resize(travel_vertex_k, refcheck = False) self.travels.resize(travel_vertex_k, refcheck = False)
self.vertices.resize(vertex_k, refcheck = False) self.vertices.resize(vertex_k, refcheck = False)
self.colors.resize(color_k, refcheck = False) self.colors.resize(color_k, refcheck = False)
...@@ -552,6 +559,7 @@ class GcodeModel(Model): ...@@ -552,6 +559,7 @@ class GcodeModel(Model):
# ------------------------------------------------------------------------ # ------------------------------------------------------------------------
def init(self): def init(self):
with self.lock:
self.layers_loaded = self.max_layers self.layers_loaded = self.max_layers
self.initialized = True self.initialized = True
if self.buffers_created: if self.buffers_created:
...@@ -569,6 +577,7 @@ class GcodeModel(Model): ...@@ -569,6 +577,7 @@ class GcodeModel(Model):
self.buffers_created = True self.buffers_created = True
def display(self, mode_2d=False): def display(self, mode_2d=False):
with self.lock:
glPushMatrix() glPushMatrix()
glTranslatef(self.offset_x, self.offset_y, 0) glTranslatef(self.offset_x, self.offset_y, 0)
glEnableClientState(GL_VERTEX_ARRAY) glEnableClientState(GL_VERTEX_ARRAY)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment