unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* strerror thread safety
@ 2004-07-27 23:02 Kevin Ryde
  0 siblings, 0 replies; only message in thread
From: Kevin Ryde @ 2004-07-27 23:02 UTC (permalink / raw)


[-- Attachment #1: Type: text/plain, Size: 868 bytes --]

        * error.c (scm_strerror): Use scm_i_misc_mutex around strerror since
        it's not thread safe.
        (scm_syserror): Use scm_strerror rather than SCM_I_STRERROR, to take
        advantage of this.
        * fports.c (scm_open_file): Use scm_strerror likewise.
        * filesys.c (scm_stat, scm_lstat): Ditto.

A program provoking the problem (in glibc at least),


(use-modules (ice-9 threads))

(define e1 9999)
(define s1 (strerror e1))

(define e2 7777)
(define s2 (strerror e2))

(begin-thread
 (while #t
   (display "-")
   (let ((s (strerror e1)))
     (if (not (string=? s1 s))
	 (begin
	   (format #t "oops, wrong s1, got ~s want ~s\n" s s1)
	   (primitive-exit 1))))))

(while #t
  (display ".")
  (let ((s (strerror e2)))
    (if (not (string=? s2 s))
	(begin
	  (format #t "oops, wrong s2, got ~s want ~s\n" s s2)
	  (primitive-exit 1)))))



[-- Attachment #2: error.c.strerror-mutex.diff --]
[-- Type: text/plain, Size: 2588 bytes --]

Index: error.c
===================================================================
RCS file: /cvsroot/guile/guile/guile-core/libguile/error.c,v
retrieving revision 1.75
diff -u -u -r1.75 error.c
--- error.c	23 Jul 2004 15:43:01 -0000	1.75
+++ error.c	27 Jul 2004 23:01:30 -0000
@@ -1,4 +1,4 @@
-/* Copyright (C) 1995,1996,1997,1998,2000,2001 Free Software Foundation, Inc.
+/* Copyright (C) 1995,1996,1997,1998,2000,2001, 2004 Free Software Foundation, Inc.
  * 
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -26,6 +26,7 @@
 #include <errno.h>
 
 #include "libguile/_scm.h"
+#include "libguile/dynwind.h"
 #include "libguile/pairs.h"
 #include "libguile/strings.h"
 #include "libguile/throw.h"
@@ -142,13 +143,34 @@
 # define SCM_I_ERRNO() errno
 #endif /* __MINGW32__ */
 
+/* strerror may not be thread safe, for instance in glibc (version 2.3.2) an
+   error number not among the known values results in a string like "Unknown
+   error 9999" formed in a static buffer, which will be overwritten by a
+   similar call in another thread.  A test program running two threads with
+   different unknown error numbers can trip this problem fairly quickly.
+
+   Some systems don't do what glibc does, instead just giving a single
+   "Unknown error" for unrecognised numbers.  It doesn't seem worth trying
+   to tell if that's the case, a mutex is reasonably fast, and in any case
+   strerror isn't needed very often.  */
+
 SCM_DEFINE (scm_strerror, "strerror", 1, 0, 0, 
             (SCM err),
 	    "Return the Unix error message corresponding to @var{err}, which\n"
 	    "must be an integer value.")
 #define FUNC_NAME s_scm_strerror
 {
-  return scm_makfrom0str (SCM_I_STRERROR (scm_to_int (err)));
+  SCM ret;
+  scm_frame_begin (0);
+  scm_frame_unwind_handler ((void(*)(void*)) scm_mutex_unlock,
+                            &scm_i_misc_mutex,
+                            SCM_F_WIND_EXPLICITLY);
+  scm_mutex_lock (&scm_i_misc_mutex);
+
+  ret = scm_makfrom0str (SCM_I_STRERROR (scm_to_int (err)));
+
+  scm_frame_end ();
+  return ret;
 }
 #undef FUNC_NAME
 
@@ -156,13 +178,12 @@
 void
 scm_syserror (const char *subr)
 {
-  int save_errno = SCM_I_ERRNO ();
-  
+  SCM err = scm_from_int (SCM_I_ERRNO ());
   scm_error (scm_system_error_key,
 	     subr,
 	     "~A",
-	     scm_cons (scm_makfrom0str (SCM_I_STRERROR (save_errno)), SCM_EOL),
-	     scm_cons (scm_from_int (save_errno), SCM_EOL));
+	     scm_cons (scm_strerror (err), SCM_EOL),
+	     scm_cons (err, SCM_EOL));
 }
 
 void

[-- Attachment #3: Type: text/plain, Size: 143 bytes --]

_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-devel

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2004-07-27 23:02 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-07-27 23:02 strerror thread safety Kevin Ryde

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).