From e4b83f269520f35a3ec0c49c54234e0b28523c9e Mon Sep 17 00:00:00 2001
From: Christophe Geuzaine <cgeuzaine@ulg.ac.be>
Date: Wed, 28 Aug 2002 07:14:55 +0000
Subject: [PATCH] try to fix some weird race conditions due to WID->check
 (called inside Msg) firing a 2nd draw() before the fir(st has finished...
 This is a general problem with our "pseudo-threaded" (i.e. putting many
 WID->check() calls) approach... I added a new lock in draw() (we already have
 one in Generate()), but this is pretty ugly.

---
 Fltk/Message.cpp       |  7 +++++--
 Fltk/Opengl_Window.cpp |  9 ++++++++-
 Graphics/Post.cpp      | 26 +++++++++++++++-----------
 3 files changed, 28 insertions(+), 14 deletions(-)

diff --git a/Fltk/Message.cpp b/Fltk/Message.cpp
index 5104126cdd..2aa391d5c5 100644
--- a/Fltk/Message.cpp
+++ b/Fltk/Message.cpp
@@ -1,4 +1,4 @@
-// $Id: Message.cpp,v 1.29 2002-06-15 21:25:27 geuzaine Exp $
+// $Id: Message.cpp,v 1.30 2002-08-28 07:14:55 geuzaine Exp $
 //
 // Copyright (C) 1997 - 2002 C. Geuzaine, J.-F. Remacle
 //
@@ -127,7 +127,10 @@ void Msg(int level, char *fmt, ...){
   if(!WID)
     window = -1;
   else 
-    WID->check();
+    WID->check(); 
+  // this is pretty costly, but permits to keep the app
+  // responsive... the downside is that it can cause race
+  // conditions
 
   if(CTX.verbosity >= verb){
     va_start (args, fmt);
diff --git a/Fltk/Opengl_Window.cpp b/Fltk/Opengl_Window.cpp
index c846860a9d..a2717e2956 100644
--- a/Fltk/Opengl_Window.cpp
+++ b/Fltk/Opengl_Window.cpp
@@ -1,4 +1,4 @@
-// $Id: Opengl_Window.cpp,v 1.27 2002-08-26 17:41:32 geuzaine Exp $
+// $Id: Opengl_Window.cpp,v 1.28 2002-08-28 07:14:55 geuzaine Exp $
 //
 // Copyright (C) 1997 - 2002 C. Geuzaine, J.-F. Remacle
 //
@@ -46,6 +46,12 @@ static int    ZOOM = 0 ;
 static double ZOOM_X0, ZOOM_Y0, ZOOM_X1, ZOOM_Y1;
 
 void Opengl_Window::draw() {
+  static int locked=0;
+  if(locked) 
+    return;
+  else
+    locked=1;
+  Msg(DEBUG, "Opengl_Window->draw()");
   if(!valid()){
     valid(1);
     CTX.viewport[0] = 0 ; CTX.viewport[1] = 0 ;
@@ -109,6 +115,7 @@ void Opengl_Window::draw() {
     glMatrixMode(GL_MODELVIEW);
     ZOOM = 0;
   }
+  locked=0;
 }
 
 void Opengl_Window::draw_overlay() {
diff --git a/Graphics/Post.cpp b/Graphics/Post.cpp
index 6eaabdf3e5..be9d764019 100644
--- a/Graphics/Post.cpp
+++ b/Graphics/Post.cpp
@@ -1,4 +1,4 @@
-// $Id: Post.cpp,v 1.38 2002-08-28 03:16:09 geuzaine Exp $
+// $Id: Post.cpp,v 1.39 2002-08-28 07:14:55 geuzaine Exp $
 //
 // Copyright (C) 1997 - 2002 C. Geuzaine, J.-F. Remacle
 //
@@ -138,7 +138,7 @@ void Get_Coords(double Explode, double *Offset, int nbnod,
   }
 }
 
-static double storedEye[3]={0,0,0};
+static double storedEye[3]={0.,0.,0.};
 
 int changedEye(){
   double zeye=100*CTX.lc, tmp[3];
@@ -151,6 +151,7 @@ int changedEye(){
     storedEye[0] = tmp[0];
     storedEye[1] = tmp[1];
     storedEye[2] = tmp[2];
+    Msg(DEBUG, "New eye = (%g %g %g)", tmp[0], tmp[1], tmp[2]);
     return 1;
   }
   return 0;
@@ -227,6 +228,18 @@ void Draw_Post (void) {
 
     if(v->Visible && !v->Dirty){ 
 
+      // sort the data % eye for transparency
+
+      if(CTX.alpha && 
+	 v->NbST && v->DrawTriangles && v->DrawScalars &&
+	 ColorTable_IsAlpha(&v->CT) && 
+	 changedEye()){
+	Msg(DEBUG, "Sorting triangles in view %d", v->Num);
+	nb = List_Nbr(v->ST) / v->NbST ;
+	qsort(v->ST->array,v->NbST,nb*sizeof(double),compareTriangleEye);
+	v->Changed = 1; // force displaylist regeneration
+      }
+
       if(CTX.display_lists && !v->Changed && v->DisplayListNum>0){
 
 	Msg(DEBUG, "Call display List %d", v->DisplayListNum);
@@ -386,15 +399,6 @@ void Draw_Post (void) {
 	
 	if(v->NbST && v->DrawTriangles && v->DrawScalars){
 	  nb = List_Nbr(v->ST) / v->NbST ;
-	  if(CTX.alpha){ // sort the triangles % eye
-	    if(ColorTable_IsAlpha(&v->CT) && changedEye()){
-	      Msg(INFO, "Resorting triangles (eye=%g %g %g)", 
-		  storedEye[0],storedEye[1],storedEye[2]);
-	      qsort(v->ST->array,v->NbST,nb*sizeof(double),compareTriangleEye);
-	      v->Changed = 1;
-	      Msg(INFO, "End sorting triangles");
-	    }
-	  }
 	  if(v->Light && v->SmoothNormals && v->Changed && v->IntervalsType != DRAW_POST_ISO){
 	    Msg(DEBUG, "Preprocessing of triangle normals in view %d", v->Num);
 	    for(i = 0 ; i < List_Nbr(v->ST) ; i+=nb){
-- 
GitLab