aboutsummaryrefslogtreecommitdiffstats
path: root/src/w32-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 /src/w32-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 'src/w32-lock.c')
-rw-r--r--src/w32-lock.c119
1 files changed, 119 insertions, 0 deletions
diff --git a/src/w32-lock.c b/src/w32-lock.c
new file mode 100644
index 0000000..0ad9409
--- /dev/null
+++ b/src/w32-lock.c
@@ -0,0 +1,119 @@
+/* w32-lock.c - GPGRT lock functions for Windows
+ Copyright (C) 2014 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
+
+#ifndef HAVE_W32_SYSTEM
+# error This module may only be build for Windows.
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <gpg-error.h>
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+
+#include "lock.h"
+#include "w32-lock-obj.h"
+
+
+static _gpgrt_lock_t *
+get_lock_object (gpgrt_lock_t *lockhd)
+{
+ _gpgrt_lock_t *lock = (_gpgrt_lock_t*)lockhd;
+
+ if (lock->vers != LOCK_ABI_VERSION)
+ abort ();
+
+ return lock;
+}
+
+
+gpg_err_code_t
+gpgrt_lock_init (gpgrt_lock_t *lockhd)
+{
+ _gpgrt_lock_t *lock = get_lock_object (lockhd);
+
+ if (sizeof (gpgrt_lock_t) < sizeof (_gpgrt_lock_t))
+ abort ();
+ InitializeCriticalSection (&lock->csec);
+ lock->initdone = 1;
+}
+
+
+gpg_err_code_t
+gpgrt_lock_lock (gpgrt_lock_t *lockhd)
+{
+ _gpgrt_lock_t *lock = get_lock_object (lockhd);
+
+ if (!lock->initdone)
+ {
+ if (!InterlockedIncrement (&lock->started))
+ {
+ /* The new value of started is 0. Because the initial value
+ if the variable was -1 we known that this thread is the
+ first who needs this lock. Thus we initialize now. All
+ other threads won't get 0 back from InterlockedIncrement
+ and thus fall into the wait loop below. We ignore that
+ STARTED may in theory overflow if this thread starves for
+ too long. */
+ gpgrt_lock_init (lockhd);
+ }
+ else
+ {
+ while (!lock->initdone)
+ Sleep (0);
+ }
+ }
+
+ EnterCriticalSection (&lock->csec);
+ return 0;
+}
+
+
+gpg_err_code_t
+gpgrt_lock_unlock (gpgrt_lock_t *lockhd)
+{
+ _gpgrt_lock_t *lock = get_lock_object (lockhd);
+
+ if (!lock->initdone)
+ return GPG_ERR_INV_LOCK_OBJ;
+ LeaveCriticalSection (&lock->csec);
+ return 0;
+}
+
+
+/* Note: Use this function only if no other thread holds or waits for
+ this lock. */
+gpg_err_code_t
+gpgrt_lock_destroy (gpgrt_lock_t *lockhd)
+{
+ _gpgrt_lock_t *lock = get_lock_object (lockhd);
+
+ if (!lock->initdone)
+ return GPG_ERR_INV_LOCK_OBJ;
+ DeleteCriticalSection (&lock->csec);
+ lock->initdone = 0;
+ lock->started = -1;
+ return 0;
+}