]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
lib: tevent: make TEVENT_SIG_INCREMENT atomic.
authorJeremy Allison <jra@samba.org>
Wed, 30 Jul 2014 16:58:47 +0000 (09:58 -0700)
committerKarolin Seeger <kseeger@samba.org>
Thu, 21 Aug 2014 14:55:17 +0000 (16:55 +0200)
On arm platforms incrementing a variable is not
an atomic operation, so may be interrupted by
signal processing (if a signal interrupts another
signal handler).

Use compiler built-ins to make this atomic.
__sync_fetch_and_add() works on gcc, llvm,
IBM xlC on AIX, and Intel icc (10.1 and
above).

atomic_add_32() works on Oracle Solaris.

Based on an inital patch from kamei@osstech.co.jp.

Bug #10640 - smbd is not responding - tevent_common_signal_handler() increments non-atomic variables

https://bugzilla.samba.org/show_bug.cgi?id=10640

Back-ported from master 536c799f00d7bdd6a574b6bdbc0e9c742eeef8b5

Signed-off-by: Jeremy Allison <jra@samba.org>
Reviewed-by: Volker Lendecke <Volker.Lendecke@SerNet.DE>
lib/replace/replace.h
lib/replace/wscript
lib/tevent/tevent_signal.c

index 674a2b431c912ff497032d2b4ca0e9a014bd0c55..6dbb4f0e01ea925734b87dc2718c7e0f6a8923a8 100644 (file)
@@ -871,4 +871,9 @@ int usleep(useconds_t);
 void rep_setproctitle(const char *fmt, ...) PRINTF_ATTRIBUTE(1, 2);
 #endif
 
+/* Needed for Solaris atomic_add_XX functions. */
+#if defined(HAVE_SYS_ATOMIC_H)
+#include <sys/atomic.h>
+#endif
+
 #endif /* _LIBREPLACE_REPLACE_H */
index 61a25ecd3078acf48a52bf07d473c51da1f248b3..59aa2d16847fca2d5f1957b2ded0792f30dc4c82 100644 (file)
@@ -102,6 +102,7 @@ struct foo bar = { .y = 'X', .x = 1 };
     conf.CHECK_HEADERS('sys/extattr.h sys/ea.h sys/proplist.h sys/cdefs.h')
     conf.CHECK_HEADERS('utmp.h utmpx.h lastlog.h malloc.h')
     conf.CHECK_HEADERS('syscall.h sys/syscall.h inttypes.h')
+    conf.CHECK_HEADERS('sys/atomic.h')
 
     conf.CHECK_CODE('''
                     #include <unistd.h>
@@ -209,6 +210,30 @@ struct foo bar = { .y = 'X', .x = 1 };
                        msg="Checking whether we have ucontext_t",
                        headers='signal.h sys/ucontext.h')
 
+    # Check for atomic builtins. */
+    conf.CHECK_CODE('''
+                    int main(void) {
+                        int i;
+                        (void)__sync_fetch_and_add(&i, 1);
+                        return 0;
+                    }
+                    ''',
+                    'HAVE___SYNC_FETCH_AND_ADD',
+                    msg='Checking for __sync_fetch_and_add compiler builtin')
+
+    conf.CHECK_CODE('''
+                    #include <stdint.h>
+                    #include <sys/atomic.h>
+                    int main(void) {
+                        int32_t i;
+                        atomic_add_32(&i, 1);
+                        return 0;
+                    }
+                    ''',
+                    'HAVE_ATOMIC_ADD_32',
+                    headers='stdint.h sys/atomic.h',
+                    msg='Checking for atomic_add_32 compiler builtin')
+
     # these may be builtins, so we need the link=False strategy
     conf.CHECK_FUNCS('strdup memmem printf memset memcpy memmove strcpy strncpy bzero', link=False)
 
index b5a56ef03aa5ffe87e22c7b46d91b3d328e3ce73..6cddc770b4a9ca2d2577b169e9dbdd20b6370c10 100644 (file)
@@ -42,7 +42,13 @@ struct tevent_sigcounter {
        uint32_t seen;
 };
 
+#if defined(HAVE___SYNC_FETCH_AND_ADD)
+#define TEVENT_SIG_INCREMENT(s) __sync_fetch_and_add(&((s).count), 1)
+#elif defined(HAVE_ATOMIC_ADD_32)
+#define TEVENT_SIG_INCREMENT(s) atomic_add_32(&((s).count), 1)
+#else
 #define TEVENT_SIG_INCREMENT(s) (s).count++
+#endif
 #define TEVENT_SIG_SEEN(s, n) (s).seen += (n)
 #define TEVENT_SIG_PENDING(s) ((s).seen != (s).count)