The branch, master has been updated
       via  422b2fa s3: Run pthreadpooltest in make test
       via  39375de s3: Add pthreadpooltest to main build
       via  f4a0f85 s3: pthreadpool_sig_fd->pthreadpool_signal_fd
       via  3c405f5 s3: Tiny doc for pthreadpool
       via  62689d8 s3: Many pthreadpool fixes
       via  23a6af4 s3: Add a 10-second timeout for the 445 or netbios 
connection to a DC
      from  f7bc844 s3:rpc_client: map fault codes to NTSTATUS with 
dcerpc_fault_to_nt_status()

http://gitweb.samba.org/?p=samba.git;a=shortlog;h=master


- Log -----------------------------------------------------------------
commit 422b2fa0d064f2afeeec400223bb8a47deecc4a5
Author: Volker Lendecke <v...@samba.org>
Date:   Sun Apr 24 10:36:56 2011 +0200

    s3: Run pthreadpooltest in make test
    
    Autobuild-User: Volker Lendecke <vlen...@samba.org>
    Autobuild-Date: Mon Apr 25 10:39:12 CEST 2011 on sn-devel-104

commit 39375de8354b676904e1fea097a68178acd987e4
Author: Volker Lendecke <v...@samba.org>
Date:   Sat Apr 23 16:49:04 2011 +0200

    s3: Add pthreadpooltest to main build

commit f4a0f856f31291dd316a937f8060d2f205e8d4d6
Author: Volker Lendecke <v...@samba.org>
Date:   Sun Apr 24 10:09:45 2011 +0200

    s3: pthreadpool_sig_fd->pthreadpool_signal_fd

commit 3c405f5e1d81d33a01ab822aeba93634338d5b25
Author: Volker Lendecke <v...@samba.org>
Date:   Sat Apr 23 22:25:36 2011 +0200

    s3: Tiny doc for pthreadpool

commit 62689d8166b8e070f855e6910470796dd7e1b2c8
Author: Volker Lendecke <v...@samba.org>
Date:   Fri Apr 22 11:47:11 2011 +0200

    s3: Many pthreadpool fixes
    
    In particular, this makes it fork-safe

commit 23a6af46c84cd9b738af403d80c5187d858eac03
Author: Volker Lendecke <v...@samba.org>
Date:   Sun Apr 17 20:16:07 2011 +0200

    s3: Add a 10-second timeout for the 445 or netbios connection to a DC

-----------------------------------------------------------------------

Summary of changes:
 source3/Makefile.in                         |   12 +-
 source3/configure.in                        |    4 +-
 source3/include/proto.h                     |    3 +-
 source3/include/pthreadpool.h               |   42 ---
 source3/lib/fncall.c                        |    4 +-
 source3/lib/pthreadpool/Makefile            |    9 +
 source3/lib/{ => pthreadpool}/pthreadpool.c |  259 +++++++++++++-------
 source3/lib/pthreadpool/pthreadpool.h       |   94 +++++++
 source3/lib/pthreadpool/tests.c             |  362 +++++++++++++++++++++++++++
 source3/libsmb/smbsock_connect.c            |   13 +-
 source3/script/tests/test_pthreadpool.sh    |   17 ++
 source3/selftest/tests.py                   |    3 +
 source3/torture/test_smbsock_any_connect.c  |    2 +-
 source3/winbindd/winbindd_cm.c              |    4 +-
 14 files changed, 691 insertions(+), 137 deletions(-)
 delete mode 100644 source3/include/pthreadpool.h
 create mode 100644 source3/lib/pthreadpool/Makefile
 rename source3/lib/{ => pthreadpool}/pthreadpool.c (70%)
 create mode 100644 source3/lib/pthreadpool/pthreadpool.h
 create mode 100644 source3/lib/pthreadpool/tests.c
 create mode 100755 source3/script/tests/test_pthreadpool.sh


Changeset truncated at 500 lines:

diff --git a/source3/Makefile.in b/source3/Makefile.in
index e503112..b3764ca 100644
--- a/source3/Makefile.in
+++ b/source3/Makefile.in
@@ -97,6 +97,7 @@ TDBTOOL = @TDBTOOL@
 TDBDUMP = @TDBDUMP@
 TDBRESTORE = @TDBRESTORE@
 TDBTORTURE = @TDBTORTURE@
+PTHREADPOOLTEST = @PTHREADPOOLTEST@
 
 INSTALLCMD=@INSTALL@
 INSTALLLIBCMD_SH=@INSTALLLIBCMD_SH@
@@ -230,7 +231,7 @@ TORTURE_PROGS = bin/smbtorture@EXEEXT@ bin/msgtest@EXEEXT@ \
        bin/masktest@EXEEXT@ bin/locktest@EXEEXT@ \
        bin/locktest2@EXEEXT@ bin/nsstest@EXEEXT@ bin/vfstest@EXEEXT@ \
        bin/pdbtest@EXEEXT@ $(TALLOCTORT) bin/replacetort@EXEEXT@ \
-       $(TDBTORTURE) \
+       $(TDBTORTURE) $(PTHREADPOOLTEST) \
        bin/smbconftort@EXEEXT@ bin/vlp@EXEEXT@
 
 BIN_PROGS = @EXTRA_BIN_PROGS@ \
@@ -1129,6 +1130,9 @@ SMBCONFTORT_OBJ = $(SMBCONFTORT_OBJ0) \
                  $(LIBSMB_ERR_OBJ) \
                  $(POPT_LIB_OBJ)
 
+PTHREADPOOLTEST_OBJ = lib/pthreadpool/pthreadpool.o \
+               lib/pthreadpool/tests.o
+
 LIBNET_OBJ = libnet/libnet_join.o \
             libnet/libnet_keytab.o \
             librpc/gen_ndr/ndr_libnet_join.o
@@ -1567,6 +1571,8 @@ replacetort : SHOWFLAGS bin/replacetort@EXEEXT@
 
 smbconftort : SHOWFLAGS bin/smbconftort@EXEEXT@
 
+pthreadpooltest : SHOWFLAGS bin/pthreadpooltest@EXEEXT@
+
 timelimit : SHOWFLAGS bin/timelimit@EXEEXT@
 
 nsswitch : SHOWFLAGS bin/winbindd@EXEEXT@ bin/wbinfo@EXEEXT@ @WINBIND_NSS@ \
@@ -1892,6 +1898,10 @@ bin/smbconftort@EXEEXT@: $(SMBCONFTORT_OBJ) @BUILD_POPT@ 
$(LIBTALLOC) $(LIBTDB)
                $(DYNEXP) $(LIBS) $(LDAP_LIBS) $(POPT_LIBS) \
                $(LIBTALLOC_LIBS) $(LIBTDB_LIBS)
 
+bin/pthreadpooltest@EXEEXT@: $(PTHREADPOOLTEST_OBJ)
+       @echo Linking $@
+       @$(CC) -o $@ $(PTHREADPOOLTEST_OBJ) $(LDFLAGS) -lpthread
+
 bin/masktest@EXEEXT@: $(BINARY_PREREQS) $(MASKTEST_OBJ) @BUILD_POPT@ 
$(LIBTALLOC) $(LIBTDB) $(LIBWBCLIENT)
        @echo Linking $@
        @$(CC) -o $@ $(MASKTEST_OBJ) $(LDFLAGS) $(DYNEXP) \
diff --git a/source3/configure.in b/source3/configure.in
index b2c1856..3a2f089 100644
--- a/source3/configure.in
+++ b/source3/configure.in
@@ -6647,7 +6647,9 @@ if test x"$enable_pthreadpool" = x"yes" -a 
x"$samba_cv_HAVE_PTHREAD" = x"yes"; t
     LIBS="$LIBS $PTHREAD_LDFLAGS"
     CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
     AC_DEFINE(WITH_PTHREADPOOL, 1, [Whether to include pthreadpool helpers])
-    AC_SUBST(PTHREADPOOL_OBJ, "lib/pthreadpool.o")
+    AC_SUBST(PTHREADPOOL_OBJ, "lib/pthreadpool/pthreadpool.o")
+    PTHREADPOOLTEST="bin/pthreadpooltest\$(EXEEXT)"
+    AC_SUBST(PTHREADPOOLTEST)
 fi
 
 #################################################
diff --git a/source3/include/proto.h b/source3/include/proto.h
index 36f0235..69672cf 100644
--- a/source3/include/proto.h
+++ b/source3/include/proto.h
@@ -2837,7 +2837,7 @@ NTSTATUS smbsock_connect_recv(struct tevent_req *req, int 
*sock,
 NTSTATUS smbsock_connect(const struct sockaddr_storage *addr, uint16_t port,
                         const char *called_name, int called_type,
                         const char *calling_name, int calling_type,
-                        int *pfd, uint16_t *ret_port);
+                        int *pfd, uint16_t *ret_port, int sec_timeout);
 
 struct tevent_req *smbsock_any_connect_send(TALLOC_CTX *mem_ctx,
                                            struct tevent_context *ev,
@@ -2856,6 +2856,7 @@ NTSTATUS smbsock_any_connect(const struct 
sockaddr_storage *addrs,
                             int *calling_types,
                             size_t num_addrs,
                             uint16_t port,
+                            int sec_timeout,
                             int *pfd, size_t *chosen_index,
                             uint16_t *chosen_port);
 
diff --git a/source3/include/pthreadpool.h b/source3/include/pthreadpool.h
deleted file mode 100644
index 7ef7ddf..0000000
--- a/source3/include/pthreadpool.h
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * Unix SMB/CIFS implementation.
- * threadpool implementation based on pthreads
- * Copyright (C) Volker Lendecke 2009
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-#ifndef __PTHREADPOOL_H__
-#define __PTHREADPOOL_H__
-
-struct pthreadpool;
-
-int pthreadpool_init(unsigned max_threads, struct pthreadpool **presult);
-int pthreadpool_destroy(struct pthreadpool *pool);
-
-/*
- * Add a job to a pthreadpool.
- */
-int pthreadpool_add_job(struct pthreadpool *pool, int job_id,
-                       void (*fn)(void *private_data), void *private_data);
-
-/*
- * Get the signalling fd out of a thread pool. This fd will become readable
- * when a job is finished. The job that finished can be retrieved via
- * pthreadpool_finished_job().
- */
-int pthreadpool_sig_fd(struct pthreadpool *pool);
-int pthreadpool_finished_job(struct pthreadpool *pool);
-
-#endif
diff --git a/source3/lib/fncall.c b/source3/lib/fncall.c
index e810b68..91377e0 100644
--- a/source3/lib/fncall.c
+++ b/source3/lib/fncall.c
@@ -21,7 +21,7 @@
 
 #if WITH_PTHREADPOOL
 
-#include "pthreadpool.h"
+#include "lib/pthreadpool/pthreadpool.h"
 
 struct fncall_state {
        struct fncall_context *ctx;
@@ -86,7 +86,7 @@ struct fncall_context *fncall_context_init(TALLOC_CTX 
*mem_ctx,
        }
        talloc_set_destructor(ctx, fncall_context_destructor);
 
-       ctx->sig_fd = pthreadpool_sig_fd(ctx->pool);
+       ctx->sig_fd = pthreadpool_signal_fd(ctx->pool);
        if (ctx->sig_fd == -1) {
                TALLOC_FREE(ctx);
                return NULL;
diff --git a/source3/lib/pthreadpool/Makefile b/source3/lib/pthreadpool/Makefile
new file mode 100644
index 0000000..48626bd
--- /dev/null
+++ b/source3/lib/pthreadpool/Makefile
@@ -0,0 +1,9 @@
+all: tests
+
+CFLAGS=-O3 -g -Wall
+
+pthreadpool.o: pthreadpool.c pthreadpool.h
+       gcc -c -O3 -o pthreadpool.o pthreadpool.c -I../../..
+
+tests: tests.o pthreadpool.o
+       gcc -o tests tests.o pthreadpool.o -lpthread
\ No newline at end of file
diff --git a/source3/lib/pthreadpool.c b/source3/lib/pthreadpool/pthreadpool.c
similarity index 70%
rename from source3/lib/pthreadpool.c
rename to source3/lib/pthreadpool/pthreadpool.c
index b62bab0..2a75a52 100644
--- a/source3/lib/pthreadpool.c
+++ b/source3/lib/pthreadpool/pthreadpool.c
@@ -26,8 +26,10 @@
 #include <signal.h>
 #include <assert.h>
 #include <fcntl.h>
+#include <sys/time.h>
 
 #include "pthreadpool.h"
+#include "lib/util/dlinklist.h"
 
 struct pthreadpool_job {
        struct pthreadpool_job *next;
@@ -38,6 +40,11 @@ struct pthreadpool_job {
 
 struct pthreadpool {
        /*
+        * List pthreadpools for fork safety
+        */
+       struct pthreadpool *prev, *next;
+
+       /*
         * Control access to this struct
         */
        pthread_mutex_t mutex;
@@ -85,6 +92,12 @@ struct pthreadpool {
        pthread_t               exited[1]; /* We alloc more */
 };
 
+static pthread_mutex_t pthreadpools_mutex = PTHREAD_MUTEX_INITIALIZER;
+static struct pthreadpool *pthreadpools = NULL;
+static pthread_once_t pthreadpool_atfork_initialized = PTHREAD_ONCE_INIT;
+
+static void pthreadpool_prep_atfork(void);
+
 /*
  * Initialize a thread pool
  */
@@ -95,13 +108,21 @@ int pthreadpool_init(unsigned max_threads, struct 
pthreadpool **presult)
        size_t size;
        int ret;
 
-       size = sizeof(struct pthreadpool) + max_threads * sizeof(pthread_t);
+       size = sizeof(struct pthreadpool)
+               + (max_threads-1) * sizeof(pthread_t);
 
        pool = (struct pthreadpool *)malloc(size);
        if (pool == NULL) {
                return ENOMEM;
        }
 
+       ret = pipe(pool->sig_pipe);
+       if (ret == -1) {
+               int err = errno;
+               free(pool);
+               return err;
+       }
+
        ret = pthread_mutex_init(&pool->mutex, NULL);
        if (ret != 0) {
                free(pool);
@@ -121,44 +142,117 @@ int pthreadpool_init(unsigned max_threads, struct 
pthreadpool **presult)
        pool->num_exited = 0;
        pool->max_threads = max_threads;
        pool->num_idle = 0;
-       pool->sig_pipe[0] = -1;
-       pool->sig_pipe[1] = -1;
+
+       ret = pthread_mutex_lock(&pthreadpools_mutex);
+       if (ret != 0) {
+               pthread_cond_destroy(&pool->condvar);
+               pthread_mutex_destroy(&pool->mutex);
+               free(pool);
+               return ret;
+       }
+       DLIST_ADD(pthreadpools, pool);
+
+       ret = pthread_mutex_unlock(&pthreadpools_mutex);
+       assert(ret == 0);
+
+       pthread_once(&pthreadpool_atfork_initialized, pthreadpool_prep_atfork);
 
        *presult = pool;
+
        return 0;
 }
 
-/*
- * Create and return a file descriptor which becomes readable when a job has
- * finished
- */
-
-int pthreadpool_sig_fd(struct pthreadpool *pool)
+static void pthreadpool_prepare(void)
 {
-       int result, ret;
+       int ret;
+       struct pthreadpool *pool;
 
-       ret = pthread_mutex_lock(&pool->mutex);
-       if (ret != 0) {
-               errno = ret;
-               return -1;
+       ret = pthread_mutex_lock(&pthreadpools_mutex);
+       assert(ret == 0);
+
+       pool = pthreadpools;
+
+       while (pool != NULL) {
+               ret = pthread_mutex_lock(&pool->mutex);
+               assert(ret == 0);
+               pool = pool->next;
        }
+}
+
+static void pthreadpool_parent(void)
+{
+       int ret;
+       struct pthreadpool *pool;
+
+       pool = DLIST_TAIL(pthreadpools);
+
+       while (1) {
+               ret = pthread_mutex_unlock(&pool->mutex);
+               assert(ret == 0);
 
-       if (pool->sig_pipe[0] != -1) {
-               result = pool->sig_pipe[0];
-               goto done;
+               if (pool == pthreadpools) {
+                       break;
+               }
+               pool = pool->prev;
        }
 
-       ret = pipe(pool->sig_pipe);
-       if (ret == -1) {
-               result = -1;
-               goto done;
+       ret = pthread_mutex_unlock(&pthreadpools_mutex);
+       assert(ret == 0);
+}
+
+static void pthreadpool_child(void)
+{
+       int ret;
+       struct pthreadpool *pool;
+
+       pool = DLIST_TAIL(pthreadpools);
+
+       while (1) {
+               close(pool->sig_pipe[0]);
+               close(pool->sig_pipe[1]);
+
+               ret = pipe(pool->sig_pipe);
+               assert(ret == 0);
+
+               pool->num_threads = 0;
+               pool->num_exited = 0;
+               pool->num_idle = 0;
+
+               while (pool->jobs != NULL) {
+                       struct pthreadpool_job *job;
+                       job = pool->jobs;
+                       pool->jobs = job->next;
+                       free(job);
+               }
+               pool->last_job = NULL;
+
+               ret = pthread_mutex_unlock(&pool->mutex);
+               assert(ret == 0);
+
+               if (pool == pthreadpools) {
+                       break;
+               }
+               pool = pool->prev;
        }
 
-       result = pool->sig_pipe[0];
-done:
-       ret = pthread_mutex_unlock(&pool->mutex);
+       ret = pthread_mutex_unlock(&pthreadpools_mutex);
        assert(ret == 0);
-       return result;
+}
+
+static void pthreadpool_prep_atfork(void)
+{
+       pthread_atfork(pthreadpool_prepare, pthreadpool_parent,
+                      pthreadpool_child);
+}
+
+/*
+ * Return the file descriptor which becomes readable when a job has
+ * finished
+ */
+
+int pthreadpool_signal_fd(struct pthreadpool *pool)
+{
+       return pool->sig_pipe[0];
 }
 
 /*
@@ -181,59 +275,21 @@ static void pthreadpool_join_children(struct pthreadpool 
*pool)
 
 int pthreadpool_finished_job(struct pthreadpool *pool)
 {
-       int result, ret, fd;
+       int result;
        ssize_t nread;
 
-       ret = pthread_mutex_lock(&pool->mutex);
-       if (ret != 0) {
-               errno = ret;
-               return -1;
-       }
-
-       /*
-        * Just some cleanup under the mutex
-        */
-       pthreadpool_join_children(pool);
-
-       fd = pool->sig_pipe[0];
-
-       ret = pthread_mutex_unlock(&pool->mutex);
-       assert(ret == 0);
-
-       if (fd == -1) {
-               errno = EINVAL;
-               return -1;
-       }
-
        nread = -1;
        errno = EINTR;
 
        while ((nread == -1) && (errno == EINTR)) {
-               nread = read(fd, &result, sizeof(int));
+               nread = read(pool->sig_pipe[0], &result, sizeof(int));
        }
-
-       /*
-        * TODO: handle nread > 0 && nread < sizeof(int)
-        */
-
-       /*
-        * Lock the mutex to provide a memory barrier for data from the worker
-        * thread to the main thread. The pipe access itself does not have to
-        * be locked, for sizeof(int) the write to a pipe is atomic, and only
-        * one thread reads from it. But we need to lock the mutex briefly
-        * even if we don't do anything under the lock, to make sure we can
-        * see all memory the helper thread has written.
-        */
-
-       ret = pthread_mutex_lock(&pool->mutex);
-       if (ret == -1) {
-               errno = ret;
-               return -1;
+       if (nread == -1) {
+               return errno;
+       }
+       if (nread != sizeof(int)) {
+               return EINVAL;
        }
-
-       ret = pthread_mutex_unlock(&pool->mutex);
-       assert(ret == 0);
-
        return result;
 }
 
@@ -250,6 +306,12 @@ int pthreadpool_destroy(struct pthreadpool *pool)
                return ret;
        }
 
+       if ((pool->jobs != NULL) || pool->shutdown) {
+               ret = pthread_mutex_unlock(&pool->mutex);
+               assert(ret == 0);
+               return EBUSY;
+       }
+
        if (pool->num_threads > 0) {
                /*
                 * We have active threads, tell them to finish, wait for that.
@@ -294,14 +356,30 @@ int pthreadpool_destroy(struct pthreadpool *pool)
        ret = pthread_mutex_destroy(&pool->mutex);
        ret1 = pthread_cond_destroy(&pool->condvar);
 
-       if ((ret == 0) && (ret1 == 0)) {
-               free(pool);
+       if (ret != 0) {
+               return ret;
+       }
+       if (ret1 != 0) {
+               return ret1;
        }
 
+       ret = pthread_mutex_lock(&pthreadpools_mutex);
        if (ret != 0) {
                return ret;
        }
-       return ret1;
+       DLIST_REMOVE(pthreadpools, pool);
+       ret = pthread_mutex_unlock(&pthreadpools_mutex);
+       assert(ret == 0);
+
+       close(pool->sig_pipe[0]);
+       pool->sig_pipe[0] = -1;
+
+       close(pool->sig_pipe[1]);
+       pool->sig_pipe[1] = -1;
+
+       free(pool);
+
+       return 0;
 }
 
 /*
@@ -325,7 +403,8 @@ static void *pthreadpool_server(void *arg)
        }
 
        while (1) {
-               struct timespec timeout;


-- 
Samba Shared Repository

Reply via email to