Tk Source Code

Artifact [b4e33523]
Login

Artifact b4e33523f1835deec025a4d8ff161de49015ee02:

Attachment "gif-thread-safe.diff" to ticket [1080290f] added by dkf 2004-12-07 05:26:00.
? gif-thread-safe.diff
Index: ChangeLog
===================================================================
RCS file: /cvsroot/tktoolkit/tk/ChangeLog,v
retrieving revision 1.1193
diff -u -r1.1193 ChangeLog
--- ChangeLog	5 Dec 2004 22:10:46 -0000	1.1193
+++ ChangeLog	6 Dec 2004 22:19:34 -0000
@@ -1,3 +1,10 @@
+2004-12-06  Donal K. Fellows  <[email protected]>
+
+	* generic/tkImgGIF.c (DoExtension, GetCode): Put static buffers
+	into thread-local storage.
+	(FileWriteGIF): Wrap the GIF writing code in a mutex to make the
+	code thread-safe on a basic level.
+
 2004-12-05  Jeff Hobbs  <[email protected]>
 
 	* changes: updated for 8.4.9 release
Index: generic/tkImgGIF.c
===================================================================
RCS file: /cvsroot/tktoolkit/tk/generic/tkImgGIF.c,v
retrieving revision 1.26
diff -u -r1.26 tkImgGIF.c
--- generic/tkImgGIF.c	27 Jul 2004 21:19:32 -0000	1.26
+++ generic/tkImgGIF.c	6 Dec 2004 22:12:28 -0000
@@ -91,6 +91,14 @@
 
 typedef struct ThreadSpecificData {
     int fromData;
+    unsigned char workingBuffer[280];
+    struct {
+	int bytes;
+	int done;
+	unsigned int window;
+	int bitsInWindow;
+	unsigned char *c;
+    } reader;
 } ThreadSpecificData;
 static Tcl_ThreadDataKey dataKey;
 
@@ -146,9 +154,9 @@
  */
 
 static int		DoExtension _ANSI_ARGS_((Tcl_Channel chan, int label,
-			    int *transparent));
+			    unsigned char *buffer, int *transparent));
 static int		GetCode _ANSI_ARGS_((Tcl_Channel chan, int code_size,
-			    int flag));
+			    int flag, ThreadSpecificData *tsdPtr));
 static int		GetDataBlock _ANSI_ARGS_((Tcl_Channel chan,
 			    unsigned char *buf));
 static int		ReadColorMap _ANSI_ARGS_((Tcl_Channel chan, int number,
@@ -254,6 +262,8 @@
     static CONST char *optionStrings[] = {
 	"-index",	NULL
     };
+    ThreadSpecificData *tsdPtr = (ThreadSpecificData *) 
+	    Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
 
     if (format && Tcl_ListObjGetElements(interp, format,
 	    &argc, &objv) != TCL_OK) {
@@ -340,7 +350,7 @@
 	     * GIF terminator.
 	     */
 
-	    Tcl_AppendResult(interp,"no image data for this index",
+	    Tcl_AppendResult(interp, "no image data for this index",
 		    (char *) NULL);
 	    goto error;
 	}
@@ -356,7 +366,8 @@
 			TCL_STATIC);
 		goto error;
 	    }
-	    if (DoExtension(chan, buf[0], &transparent) < 0) {
+	    if (DoExtension(chan, buf[0], tsdPtr->workingBuffer,
+		    &transparent) < 0) {
 		Tcl_SetResult(interp, "error reading extension in GIF image",
 			TCL_STATIC);
 		goto error;
@@ -378,8 +389,8 @@
 	    goto error;
 	}
 
-	fileWidth = LM_to_uint(buf[4],buf[5]);
-	fileHeight = LM_to_uint(buf[6],buf[7]);
+	fileWidth = LM_to_uint(buf[4], buf[5]);
+	fileHeight = LM_to_uint(buf[6], buf[7]);
 
 	bitPixel = 1<<((buf[8]&0x07)+1);
 
@@ -436,7 +447,7 @@
 	    }
 	}
 
-	index = LM_to_uint(buf[0],buf[1]);
+	index = LM_to_uint(buf[0], buf[1]);
 	srcX -= index;
 	if (srcX<0) {
 	    destX -= srcX; width += srcX;
@@ -447,7 +458,7 @@
 	    width = fileWidth;
 	}
 
-	index = LM_to_uint(buf[2],buf[3]);
+	index = LM_to_uint(buf[2], buf[3]);
 	srcY -= index;
 	if (index > srcY) {
 	    destY -= srcY; height += srcY;
@@ -567,8 +578,8 @@
     } else {
 	memcpy((VOID *) header, (VOID *) data, 10);
     }
-    *widthPtr = LM_to_uint(header[6],header[7]);
-    *heightPtr = LM_to_uint(header[8],header[9]);
+    *widthPtr = LM_to_uint(header[6], header[7]);
+    *heightPtr = LM_to_uint(header[8], header[9]);
     return 1;
 }
 
@@ -668,8 +679,8 @@
 	return 0;
     }
 
-    *widthPtr = LM_to_uint(buf[0],buf[1]);
-    *heightPtr = LM_to_uint(buf[2],buf[3]);
+    *widthPtr = LM_to_uint(buf[0], buf[1]);
+    *heightPtr = LM_to_uint(buf[2], buf[3]);
     return 1;
 }
 
@@ -695,24 +706,22 @@
 	}
 
 	if (buffer) {
-	    buffer[i][CM_RED] = rgb[0] ;
-	    buffer[i][CM_GREEN] = rgb[1] ;
-	    buffer[i][CM_BLUE] = rgb[2] ;
-	    buffer[i][CM_ALPHA] = 255 ;
+	    buffer[i][CM_RED] = rgb[0];
+	    buffer[i][CM_GREEN] = rgb[1];
+	    buffer[i][CM_BLUE] = rgb[2];
+	    buffer[i][CM_ALPHA] = 255;
 	}
     }
     return 1;
 }
 
-
-
 static int
-DoExtension(chan, label, transparent)
+DoExtension(chan, label, buf, transparent)
     Tcl_Channel chan;
     int label;
+    unsigned char *buf;
     int *transparent;
 {
-    static unsigned char buf[256];
     int count;
 
     switch (label) {
@@ -756,7 +765,7 @@
 {
     unsigned char count;
 
-    if (! ReadOK(chan, &count,1)) {
+    if (!ReadOK(chan, &count, 1)) {
 	return -1;
     }
 
@@ -766,8 +775,6 @@
 
     return count;
 }
-
-
 
 /*
  *----------------------------------------------------------------------
@@ -821,6 +828,8 @@
     register unsigned char *top;
     int codeSize, clearCode, inCode, endCode, oldCode, maxCode;
     int code, firstCode;
+    ThreadSpecificData *tsdPtr = (ThreadSpecificData *) 
+	    Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
 
     /*
      *  Initialize the decoder
@@ -862,7 +871,7 @@
     }
     top = stack;
 
-    GetCode(chan, 0, 1);
+    GetCode(chan, 0, 1, tsdPtr);
 
     /*
      * Read until we finish the image
@@ -874,7 +883,7 @@
 		/*
 		 * Bummer -- our stack is empty.  Now we have to work!
 		 */
-		code = GetCode(chan, codeSize, 0);
+		code = GetCode(chan, codeSize, 0, tsdPtr);
 		if (code < 0) {
 		    return TCL_OK;
 		}
@@ -1045,69 +1054,64 @@
  */
 
 static int
-GetCode(chan, code_size, flag)
+GetCode(chan, code_size, flag, tsdPtr)
     Tcl_Channel chan;
     int code_size;
     int flag;
+    ThreadSpecificData *tsdPtr;
 {
-    static unsigned char buf[280];
-    static int bytes = 0, done;
-    static unsigned char *c;
-
-    static unsigned int window;
-    static int bitsInWindow = 0;
     int ret;
     
     if (flag) {
 	/*
 	 * Initialize the decoder.
 	 */
-	bitsInWindow = 0;
-	bytes = 0;
-	window = 0;
-	done = 0;
-	c = NULL;
+	tsdPtr->reader.bitsInWindow = 0;
+	tsdPtr->reader.bytes = 0;
+	tsdPtr->reader.window = 0;
+	tsdPtr->reader.done = 0;
+	tsdPtr->reader.c = NULL;
 	return 0;
     }
 
-    while (bitsInWindow < code_size) {
+    while (tsdPtr->reader.bitsInWindow < code_size) {
 	/*
 	 * Not enough bits in our window to cover the request.
 	 */
-	if (done) {
+	if (tsdPtr->reader.done) {
 	    return -1;
 	}
-	if (bytes == 0) {
+	if (tsdPtr->reader.bytes == 0) {
 	    /*
 	     * Not enough bytes in our buffer to add to the window.
 	     */
-	    bytes = GetDataBlock(chan, buf);
-	    c = buf;
-	    if (bytes <= 0) {
-		done = 1;
+	    tsdPtr->reader.bytes = GetDataBlock(chan, tsdPtr->workingBuffer);
+	    tsdPtr->reader.c = tsdPtr->reader.workingBuffer;
+	    if (tsdPtr->reader.bytes <= 0) {
+		tsdPtr->reader.done = 1;
 		break;
 	    }
 	}
 	/*
 	 * Tack another byte onto the window, see if that's enough.
 	 */
-	window += (*c) << bitsInWindow;
-	c++;
-	bitsInWindow += 8;
-	bytes--;
+	tsdPtr->reader.window +=
+		(*tsdPtr->reader.c) << tsdPtr->reader.bitsInWindow;
+	tsdPtr->reader.c++;
+	tsdPtr->reader.bitsInWindow += 8;
+	tsdPtr->reader.bytes--;
     }
 
-
     /*
      * The next code will always be the last code_size bits of the window.
      */
-    ret = window & ((1 << code_size) - 1);
+    ret = tsdPtr->reader.window & ((1 << code_size) - 1);
     
     /*
      * Shift data in the window to put the next code at the end.
      */
-    window >>= code_size;
-    bitsInWindow -= code_size;
+    tsdPtr->reader.window >>= code_size;
+    tsdPtr->reader.bitsInWindow -= code_size;
     return ret;
 }
 
@@ -1308,7 +1312,7 @@
 static int
 Fread(dst, hunk, count, chan)
     unsigned char *dst;		/* where to put the result */
-    size_t hunk,count;		/* how many */
+    size_t hunk, count;		/* how many */
     Tcl_Channel chan;
 {
     ThreadSpecificData *tsdPtr = (ThreadSpecificData *) 
@@ -1368,6 +1372,11 @@
 #define GIFBITS 12
 #define HSIZE  5003		/* 80% occupancy */
 
+/*
+ * Mutex protecting the static variables below.
+ */
+TCL_DECLARE_MUTEX(gifWriteMutex);
+
 static int ssize;
 static int csize;
 static int rsize;
@@ -1384,7 +1393,7 @@
  *	Definition of new functions to write GIFs
  */
 
-static int color _ANSI_ARGS_((int red,int green, int blue,
+static int color _ANSI_ARGS_((int red, int green, int blue,
 		unsigned char mapa[MAXCOLORMAPSIZE][3]));
 static void compress _ANSI_ARGS_((int init_bits, Tcl_Channel handle,
 		ifunptr readValue));
@@ -1413,14 +1422,25 @@
 	return TCL_ERROR;
     }
 
+    /*
+     * Wrap the actual GIF writing code in a mutex because it uses a
+     * *lot* of shared state. This is acceptable because it only
+     * causes contention problems when you have multiple threads
+     * writing GIFs at once; it could happen, but it is probably not
+     * all that likely.
+     */
+
+    Tcl_MutexLock(&gifWriteMutex);
     result = CommonWriteGIF(interp, chan, format, blockPtr);
+    Tcl_MutexUnlock(&gifWriteMutex);
+
     if (Tcl_Close(interp, chan) == TCL_ERROR) {
 	return TCL_ERROR;
     }
     return result;
 }
 
-#define Mputc(c,handle) Tcl_Write(handle,(char *) &c,1)
+#define Mputc(c,handle) Tcl_Write(handle, (char *) &c, 1)
 
 static int
 CommonWriteGIF(interp, handle, format, blockPtr)
@@ -1431,9 +1451,9 @@
 {
     int  resolution;
 
-    long  width,height,x;
+    long  width, height, x;
     unsigned char c;
-    unsigned int top,left;
+    unsigned int top, left;
 
     top = 0;
     left = 0;
@@ -1464,7 +1484,7 @@
     height = blockPtr->height;
     pixelo = blockPtr->pixelPtr + blockPtr->offset[0];
     pixelPitch = blockPtr->pitch;
-    savemap(blockPtr,mapa);
+    savemap(blockPtr, mapa);
     if (num >= MAXCOLORMAPSIZE) {
 	Tcl_AppendResult(interp, "too many colors", (char *) NULL);
 	return TCL_ERROR;
@@ -1473,20 +1493,20 @@
 	num = 2;
     }
     c = LSB(width);
-    Mputc(c,handle);
+    Mputc(c, handle);
     c = MSB(width);
-    Mputc(c,handle);
+    Mputc(c, handle);
     c = LSB(height);
-    Mputc(c,handle);
+    Mputc(c, handle);
     c = MSB(height);
-    Mputc(c,handle);
+    Mputc(c, handle);
 
     resolution = 0;
     while (num >> resolution) {
 	resolution++;
     }
     c = 111 + resolution * 17;
-    Mputc(c,handle);
+    Mputc(c, handle);
 
     num = 1 << resolution;
 
@@ -1495,21 +1515,21 @@
      */
 
     c = 0;
-    Mputc(c,handle);
+    Mputc(c, handle);
 
     /*
      * zero for future expansion.
      */
 
-    Mputc(c,handle);
+    Mputc(c, handle);
 
     for (x=0 ; x<num ; x++) {
 	c = mapa[x][CM_RED];
-	Mputc(c,handle);
+	Mputc(c, handle);
 	c = mapa[x][CM_GREEN];
-	Mputc(c,handle);
+	Mputc(c, handle);
 	c = mapa[x][CM_BLUE];
-	Mputc(c,handle);
+	Mputc(c, handle);
     }
 
     /*
@@ -1523,39 +1543,39 @@
     }
 
     c = GIF_START;
-    Mputc(c,handle);
+    Mputc(c, handle);
     c = LSB(top);
-    Mputc(c,handle);
+    Mputc(c, handle);
     c = MSB(top);
-    Mputc(c,handle);
+    Mputc(c, handle);
     c = LSB(left);
-    Mputc(c,handle);
+    Mputc(c, handle);
     c = MSB(left);
-    Mputc(c,handle);
+    Mputc(c, handle);
 
     c = LSB(width);
-    Mputc(c,handle);
+    Mputc(c, handle);
     c = MSB(width);
-    Mputc(c,handle);
+    Mputc(c, handle);
 
     c = LSB(height);
-    Mputc(c,handle);
+    Mputc(c, handle);
     c = MSB(height);
-    Mputc(c,handle);
+    Mputc(c, handle);
 
     c = 0;
-    Mputc(c,handle);
+    Mputc(c, handle);
     c = resolution;
-    Mputc(c,handle);
+    Mputc(c, handle);
 
     ssize = rsize = blockPtr->width;
     csize = blockPtr->height;
     compress(resolution+1, handle, ReadValue);
 
     c = 0; 
-    Mputc(c,handle);
+    Mputc(c, handle);
     c = GIF_TERMINATOR;
-    Mputc(c,handle);
+    Mputc(c, handle);
 
     return TCL_OK;	
 }
@@ -1580,7 +1600,7 @@
 
 static int
 nuevo(red, green, blue, mapa)
-    int red,green,blue;
+    int red, green, blue;
     unsigned char mapa[MAXCOLORMAPSIZE][3];
 {
     int x = (alphaOffset != 0);
@@ -1594,13 +1614,13 @@
 }
 
 static void
-savemap(blockPtr,mapa)
+savemap(blockPtr, mapa)
     Tk_PhotoImageBlock *blockPtr;
     unsigned char mapa[MAXCOLORMAPSIZE][3];
 {
     unsigned char *colores;
-    int x,y;
-    unsigned char red,green,blue;
+    int x, y;
+    unsigned char red, green, blue;
 
     if (alphaOffset) {
 	num = 0;
@@ -1619,7 +1639,7 @@
 		red = colores[0];
 		green = colores[greenOffset];
 		blue = colores[blueOffset];
-		if (nuevo(red,green,blue,mapa)) {
+		if (nuevo(red, green, blue, mapa)) {
 		    num++;
 		    if (num >= MAXCOLORMAPSIZE) {
 			return;
@@ -1738,6 +1758,11 @@
 
 #ifdef MIGIF_DEBUGGING_ENVARS
 
+/*
+ * This debugging code is absolutely not thread-safe. It's also not
+ * normally enabled either.
+ */
+
 static int verbose_set = 0;
 static int verbose;
 #define MIGIF_VERBOSE (verbose_set?verbose:set_verbose())