Index: trunk/src/slib.c
===================================================================
--- trunk/src/slib.c	(revision 317)
+++ trunk/src/slib.c	(revision 318)
@@ -1595,7 +1595,4 @@
 static char badfd_orig_mesg[128];
 
-SH_MUTEX_STATIC(mutex_ticket, PTHREAD_MUTEX_INITIALIZER);
-
-static unsigned int nonce_counter = TOFFSET;
 
 char * sl_check_stale()
@@ -1622,4 +1619,34 @@
 }
 
+typedef struct { volatile unsigned int atom; } atomic_t;
+static atomic_t nonce_counter = { TOFFSET };
+
+#if defined(__GNUC__) && (defined(__i486__) || defined(__x86_64__))
+/* from linux/include/asm-i386/atomic.h */
+static unsigned int atomic_add ( unsigned int i, atomic_t *var)
+{
+  unsigned int j = i;
+
+  __asm__ __volatile__ ("lock; xaddl %0, %1"
+			: "+r" (i), "+m" (var->atom)
+			: : "memory");
+  return j+i; 
+}
+#else
+SH_MUTEX_STATIC(mutex_ticket, PTHREAD_MUTEX_INITIALIZER);
+
+static unsigned int atomic_add ( unsigned int i, atomic_t *var)
+{
+  volatile unsigned int j;
+
+  SH_MUTEX_LOCK_UNSAFE(mutex_ticket);
+  var->atom += i;
+  j = var->atom;
+  SH_MUTEX_UNLOCK_UNSAFE(mutex_ticket);
+
+  return j;
+}
+#endif
+
 static
 SL_TICKET sl_create_ticket (unsigned int myindex) 
@@ -1628,4 +1655,5 @@
   unsigned int low;  /* nonce */
   SL_TICKET    retval = SL_EINTERNAL;
+  unsigned int nonce;/* nonce */
 
   SL_ENTER(_("sl_create_ticket"));
@@ -1648,28 +1676,28 @@
     }
 
-  SH_MUTEX_LOCK_UNSAFE(mutex_ticket);
-
-  low = nonce_counter & 0xffff;
-
-  /* Overflow -> nonce too big.
-   */
-  if ((low != nonce_counter++) || low == 0)
-    {
-      retval = SL_EINTERNAL03;
-      goto out_ticket;
-    }
- 
+  nonce = atomic_add(1, &nonce_counter);
+
   /* Wrap around the nonce counter.
    * This is a dirty trick.
    */
-  if (nonce_counter > 0x7fff)
-    nonce_counter = TOFFSET;
+  if (nonce > 0x7fff)
+    {
+      nonce_counter.atom = TOFFSET;
+      nonce = atomic_add(1, &nonce_counter);
+    }
+
+  low = nonce & 0xffff;
+
+  /* Overflow -> nonce too big.
+   */
+  if ((low != nonce) || low == 0)
+    {
+      retval = SL_EINTERNAL03;
+      goto out_ticket;
+    }
 
   retval = (SL_TICKET) ((high << 16) | low);
 
  out_ticket:
-  ;
-
-  SH_MUTEX_UNLOCK_UNSAFE(mutex_ticket);
   SL_RETURN (retval, _("sl_create_ticket")); 
 }
