aboutsummaryrefslogtreecommitdiffstats
path: root/tests/t-lock.c
diff options
context:
space:
mode:
authorWerner Koch <[email protected]>2014-01-09 18:14:09 +0000
committerWerner Koch <[email protected]>2014-01-16 08:58:22 +0000
commitff937c39febe63d52c55590d8e3bd3a460f26651 (patch)
tree5eb58c4075c5e8981c13fca80db99ed8f9679742 /tests/t-lock.c
parentImprove maintainability by rewriting the mkheader helper. (diff)
downloadlibgpg-error-ff937c39febe63d52c55590d8e3bd3a460f26651.tar.gz
libgpg-error-ff937c39febe63d52c55590d8e3bd3a460f26651.zip
Add gpgrt_lock_ functions.
* src/gpg-error.h.in (GPGRT_LOCK_DEFINE): New. (gpgrt_lock_init): New. (gpgrt_lock_lock): New. (gpgrt_lock_unlock): New. (gpgrt_lock_destroy): New. (gpgrt_yield): New. * src/gpg-error.def.in: Add new functions. * m4/lock.m4, m4/threadlib.m4: New. Taken from current gnulib. * configure.ac: Call gl_LOCK. Check size of pthread_mutex_t. Add LIBTHREAD to GPG_ERROR_CONFIG_LIBS. * src/err-codes.h.in (GPG_ERR_INV_LOCK_OBJ): New. * src/gen-posix-lock-obj.c: New. * src/gen-w32-lock-obj.c: New. * src/lock.h, src/thread.h: New. * src/posix-lock-obj.h, src/w32-lock-obj.h: New. * src/posix-lock.c, src/w32-lock.c: New. * src/posix-thread.c, src/w32-thread.c: * src/w32-lock-obj-pub.in: New. * src/mkheader.c (include_file): Support build time include files. (write_special): Add keyword "include:lock-obj". * src/Makefile.am: (posix-lock-obj-pub.in): New rule. (noinst_PROGRAMS): Add gen-*-lock-obj helpers. * tests/t-common.h: New. * tests/t-lock.c: New. * tests/Makefile.am (t_lock_LDADD): Add new test. -- This patch introduces the gpgrt_ functions which will be extended over time to provide a library of commonly used code in GnuPG and Libgcrypt. Having them in a library named libgpg-error is a misnomer but this way we can achieve a smooth upgrade path. In contrasts to other GnuPG libraries, the gpgrt_ functions return a simple gpg_err_code_t and not gpg_error_t. The rationale for this is that a source of error identifier does not make sense here; it is better to use the source of error identifier of the caller. This can easily be achieved in a component by wrapping these function in a gpg_error macro/inline. There is no cross-compiling support for Posix platforms; the gen-posix-lock-obj tool must be run on the target system. Note that the gen-w32-lock-obj tool is not needed at build time but was used to figure out ABI definitions for Windows. Signed-off-by: Werner Koch <[email protected]>
Diffstat (limited to 'tests/t-lock.c')
-rw-r--r--tests/t-lock.c318
1 files changed, 318 insertions, 0 deletions
diff --git a/tests/t-lock.c b/tests/t-lock.c
new file mode 100644
index 0000000..91923fb
--- /dev/null
+++ b/tests/t-lock.c
@@ -0,0 +1,318 @@
+/* t-lock.c - Check the lock functions
+ * Copyright (C) 2013 g10 Code GmbH
+ *
+ * This file is part of libgpg-error.
+ *
+ * libgpg-error is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * libgpg-error 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#ifdef _WIN32
+# include <windows.h>
+#else
+# include <pthread.h>
+#endif
+
+#define PGM "t-lock"
+
+#include "t-common.h"
+
+#ifdef _WIN32
+# define THREAD_RET_TYPE DWORD WINAPI
+# define THREAD_RET_VALUE 0
+#else
+# define THREAD_RET_TYPE void *
+# define THREAD_RET_VALUE NULL
+#endif
+
+
+/* Our tests works by having a a couple of accountant threads which do
+ random transactions between accounts and a revision threads which
+ checks that the balance of all accounts is invariant. The idea for
+ this check is due to Bruno Haible. */
+#define N_ACCOUNT 8
+#define ACCOUNT_VALUE 42
+static int account[N_ACCOUNT];
+GPGRT_LOCK_DEFINE (accounts_lock);
+
+/* Number of transactions done by each accountant. */
+#define N_TRANSACTIONS 1000
+
+/* Number of accountants to run. */
+#define N_ACCOUNTANTS 5
+
+/* Maximum transaction value. A quite low value is used so that we
+ would get an integer overflow. */
+#define MAX_TRANSACTION_VALUE 50
+
+/* Flag to tell the revision thread to finish. */
+static volatile int stop_revision_thread;
+
+
+/* Initialze all accounts. */
+static void
+init_accounts (void)
+{
+ int i;
+
+ for (i=0; i < N_ACCOUNT; i++)
+ account[i] = ACCOUNT_VALUE;
+}
+
+
+/* Check that the sum of all accounts matches the intial sum. */
+static void
+check_accounts (void)
+{
+ int i, sum;
+
+ sum = 0;
+ for (i = 0; i < N_ACCOUNT; i++)
+ sum += account[i];
+ if (sum != N_ACCOUNT * ACCOUNT_VALUE)
+ die ("accounts out of balance");
+}
+
+
+static void
+print_accounts (void)
+{
+ int i;
+
+ for (i=0; i < N_ACCOUNT; i++)
+ printf ("account %d: %6d\n", i, account[i]);
+}
+
+
+/* Get a a random integer value in the range 0 to HIGH. */
+static unsigned int
+get_rand (int high)
+{
+ return (unsigned int)(1+(int)((double)(high+1)*rand ()/(RAND_MAX+1.0))) - 1;
+}
+
+
+/* Pick a random account. Note that this fucntion is not
+ thread-safe. */
+static int
+pick_account (void)
+{
+ return get_rand (N_ACCOUNT - 1);
+}
+
+
+/* Pick a random value for a transaction. This is not thread-safe. */
+static int
+pick_value (void)
+{
+ return get_rand (MAX_TRANSACTION_VALUE);
+}
+
+
+/* This is the revision department. */
+static THREAD_RET_TYPE
+revision_thread (void *arg)
+{
+ gpg_err_code_t rc;
+ int i;
+
+ (void)arg;
+
+ while (!stop_revision_thread)
+ {
+ rc = gpgrt_lock_lock (&accounts_lock);
+ if (rc)
+ fail ("gpgrt_lock_lock failed at %d: %s", __LINE__, gpg_strerror (rc));
+
+ check_accounts ();
+ rc = gpgrt_lock_unlock (&accounts_lock);
+ if (rc)
+ fail ("gpgrt_lock_unlock failed at %d: %s", __LINE__,gpg_strerror (rc));
+ if (!(++i%7))
+ gpgrt_yield ();
+ }
+ return THREAD_RET_VALUE;
+}
+
+
+/* This is one of our accountants. */
+static THREAD_RET_TYPE
+accountant_thread (void *arg)
+{
+ gpg_err_code_t rc;
+ int i;
+ int acc1, acc2;
+ int value;
+
+ (void)arg;
+
+ for (i = 0; i < N_TRANSACTIONS; i++)
+ {
+ rc = gpgrt_lock_lock (&accounts_lock);
+ if (rc)
+ fail ("gpgrt_lock_lock failed at %d: %s", __LINE__, gpg_strerror (rc));
+
+ acc1 = pick_account ();
+ acc2 = pick_account ();
+ value = pick_value ();
+ account[acc1] += value;
+ account[acc2] -= value;
+
+ rc = gpgrt_lock_unlock (&accounts_lock);
+ if (rc)
+ fail ("gpgrt_lock_unlock failed at %d: %s", __LINE__,gpg_strerror (rc));
+ if (i && !(i%8))
+ gpgrt_yield ();
+ }
+ return THREAD_RET_VALUE;
+}
+
+
+static void
+run_test (void)
+{
+#ifdef _WIN32
+ HANDLE rthread;
+ HANDLE athreads[N_ACCOUNTANTS];
+ int i;
+ int rc;
+
+ stop_revision_thread = 0;
+ rthread = CreateThread (NULL, 0, revision_thread, NULL, 0, NULL);
+ if (!rthread)
+ die ("error creating revision thread: rc=%d", (int)GetLastError ());
+
+ for (i=0; i < N_ACCOUNTANTS; i++)
+ {
+ athreads[i] = CreateThread (NULL, 0, accountant_thread, NULL, 0, NULL);
+ if (!athreads[i])
+ die ("error creating accountant thread %d: rc=%d",
+ i, (int)GetLastError ());
+ }
+
+ for (i=0; i < N_ACCOUNTANTS; i++)
+ {
+ rc = WaitForSingleObject (athreads[i], INFINITE);
+ if (rc == WAIT_OBJECT_0)
+ show ("accountant thread %d has terminated", i);
+ else
+ fail ("waiting for accountant thread %d failed: %d",
+ i, (int)GetLastError ());
+ CloseHandle (athreads[i]);
+ }
+ stop_revision_thread = 1;
+
+ rc = WaitForSingleObject (rthread, INFINITE);
+ if (rc == WAIT_OBJECT_0)
+ show ("revision thread has terminated");
+ else
+ fail ("waiting for revision thread failed: %d", (int)GetLastError ());
+ CloseHandle (rthread);
+
+#else /*!_WIN32*/
+ pthread_t rthread;
+ pthread_t athreads[N_ACCOUNTANTS];
+ int i;
+
+ stop_revision_thread = 0;
+ pthread_create (&rthread, NULL, revision_thread, NULL);
+
+ for (i=0; i < N_ACCOUNTANTS; i++)
+ pthread_create (&athreads[i], NULL, accountant_thread, NULL);
+
+ for (i=0; i < N_ACCOUNTANTS; i++)
+ {
+ pthread_join (athreads[i], NULL);
+ show ("accountant thread %d has terminated", i);
+ }
+
+ stop_revision_thread = 1;
+ pthread_join (rthread, NULL);
+ show ("revision thread has terminated");
+
+#endif /*!_WIN32*/
+
+ gpgrt_lock_destroy (&accounts_lock);
+}
+
+
+int
+main (int argc, char **argv)
+{
+ int last_argc = -1;
+ int rc;
+
+ if (argc)
+ {
+ argc--; argv++;
+ }
+ while (argc && last_argc != argc )
+ {
+ last_argc = argc;
+ if (!strcmp (*argv, "--help"))
+ {
+ puts (
+"usage: ./t-lock [options]\n"
+"\n"
+"Options:\n"
+" --verbose Show what is going on\n"
+" --debug Flyswatter\n"
+);
+ exit (0);
+ }
+ if (!strcmp (*argv, "--verbose"))
+ {
+ verbose = 1;
+ argc--; argv++;
+ }
+ else if (!strcmp (*argv, "--debug"))
+ {
+ verbose = debug = 1;
+ argc--; argv++;
+ }
+ }
+
+ srand (time(NULL)*getpid());
+
+ if (!gpg_error_check_version (GPG_ERROR_VERSION))
+ {
+ die ("gpg_error_check_version returned an error");
+ errorcount++;
+ }
+
+ init_accounts ();
+ check_accounts ();
+ run_test ();
+ check_accounts ();
+ /* Run a second time to check deinit code. */
+ run_test ();
+ check_accounts ();
+ /* And a third time to test an explicit init. */
+ rc = gpgrt_lock_init (&accounts_lock);
+ if (rc)
+ fail ("gpgrt_lock_init failed at %d: %s", __LINE__, gpg_strerror (rc));
+ run_test ();
+ check_accounts ();
+ if (verbose)
+ print_accounts ();
+
+ return errorcount ? 1 : 0;
+}