--- visbjp_orig/VIS.H	2009-07-29 11:40:40.000000000 +0200
+++ visbjp/VIS.H	2010-01-12 14:36:30.865639900 +0100
@@ -119,12 +119,17 @@
 
 
 #ifndef __alpha
-extern	HANDLE	my_mutex;
-#define	LOCK	WaitForSingleObject( my_mutex, INFINITE )
-#define	UNLOCK	ReleaseMutex( my_mutex )
+extern	HANDLE	read_mutex;
+extern	HANDLE	save_mutex;
+#define	READ_LOCK	WaitForSingleObject( read_mutex, INFINITE )
+#define	READ_UNLOCK	ReleaseMutex( read_mutex )
+#define	SAVE_LOCK	WaitForSingleObject( save_mutex, INFINITE )
+#define	SAVE_UNLOCK	ReleaseMutex( save_mutex )
 #else
-#define	LOCK
-#define	UNLOCK
+#define	READ_LOCK
+#define	READ_UNLOCK
+#define	SAVE_LOCK
+#define	SAVE_UNLOCK
 #endif
 
 
--- visbjp_orig/VIS.C	2009-07-30 09:59:14.000000000 +0200
+++ visbjp/VIS.C	2010-01-18 12:03:03.759787600 +0100
@@ -19,7 +19,8 @@
 int			leafon;		// the next leaf to be given to a thread to process
 
 #ifndef __alpha
-HANDLE my_mutex;
+HANDLE read_mutex;
+HANDLE save_mutex;
 #endif
 
 byte			*vismap, *vismap_p, *vismap_end; // past visfile
@@ -47,6 +48,18 @@
 
 FILE			*logfile;
 
+BOOL WINAPI HandlerRoutine( DWORD dwCtrlType )
+{
+	if( dwCtrlType == CTRL_C_EVENT )
+	{
+		SAVE_LOCK;
+
+		exit( 0 );
+	}
+
+	return FALSE;
+};
+
 void logvprintf (char *fmt, va_list argptr)
 {
 	ShowPercent (NULL, 0, -1);
@@ -428,6 +441,8 @@
 	byte	      *ZData;
 	static double PrevTime;
 
+	SAVE_LOCK;
+
 	if (source != NULL)
 	{
 		// Init
@@ -451,20 +466,33 @@
 
 		PrevTime = I_FloatTime ();
 
+		SAVE_UNLOCK;
+
 		return;
 	}
 
+	if (!Flush)
+	{
+		State.CurrPortal = CurrPortal + 1;
+	}
+
 	if (NoSave)
+	{
+		SAVE_UNLOCK;
+
 		return;
+	}
 
 	Time = I_FloatTime ();
 
 	if (!Flush)
 	{
-		State.CurrPortal = CurrPortal + 1;
-
 		if (Time < PrevTime + SaveTime)
+		{
+			SAVE_UNLOCK;
+
 			return;
+		}
 	}
 
 	PrevTime = Time;
@@ -509,7 +537,7 @@
 
 	for (i = 0, p = portals; i < 2 * numportals; ++i, ++p)
 	{
-		ps.status = p->status;
+		ps.status = (p->status == stat_working) ? stat_none : p->status;
 		ps.visbits = p->visbits != NULL;
 		ps.mightsee = p->mightsee != NULL;
 
@@ -536,6 +564,8 @@
 	SavingTime = I_FloatTime () - Time;
 	State.StateTime += SavingTime;
 //logprintf ("State save: %.2lf\n", SavingTime);
+
+	SAVE_UNLOCK;
 }
 
 /*
@@ -636,7 +666,7 @@
 		else
 			SafeRead (StateFile, &ps, sizeof(portals_t));
 
-		p->status = ps.status;
+		p->status = (ps.status == stat_working) ? stat_none : ps.status;
 		p->nummightsee = ps.nummightsee;
 		p->numcansee = ps.numcansee;
 
@@ -676,7 +706,7 @@
 	portal_t	*p, *tp;
 	int		min;
 
-	LOCK;
+	READ_LOCK;
 
 	min = 99999;
 	p = NULL;
@@ -694,7 +724,7 @@
 	if (p)
 		p->status = stat_working;
 
-	UNLOCK;
+	READ_UNLOCK;
 
 	return p;
 }
@@ -723,7 +753,7 @@
 			if (!p)
 				break;
 
-			ShowPercent (NULL, State.CurrPortal + 1, numportals * 2 );
+			ShowPercent (NULL, State.CurrPortal + 1, numportals * 2);
 
 			if (verbose)
 				logprintf ("portal:%6i  might see:%5i  ", (int)(p - portals), p->nummightsee);
@@ -914,7 +944,8 @@
 	HANDLE	work_threads[MAX_THREADS];
 	int		i;
 
-	my_mutex = CreateMutex( NULL, FALSE, NULL );
+	read_mutex = CreateMutex( NULL, FALSE, NULL );
+	save_mutex = CreateMutex( NULL, FALSE, NULL );
 
 	ShowPercent ("Full", 0, 0);
 
@@ -927,7 +958,8 @@
 
 	ShowPercent (NULL, 100, 100);
 
-	ReleaseMutex( my_mutex );
+	CloseHandle( read_mutex );
+	CloseHandle( save_mutex );
 }
 #else
 	LeafThread (0);
@@ -1193,6 +1225,8 @@
 
 #endif
 
+	SetConsoleCtrlHandler( HandlerRoutine, TRUE );
+
     logfile = fopen (LOGFILENAME, "w");
 	logprintf ("---- WVis 2.31 ----\n");
 	logprintf ("  Modified by Bengt Jardrup\n");
