summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNikos Mavrogiannopoulos <nmav@redhat.com>2017-07-21 11:43:27 +0200
committerNikos Mavrogiannopoulos <nmav@gnutls.org>2017-07-25 07:00:50 +0000
commitc3584fde92d29b0d9c3f62b5073edfe795dfbd39 (patch)
treea244e526a992fa702f1ca052521238c7c0b354df
parent4ec47cb8e4f678caa42772aa902808496c595952 (diff)
downloadgnutls-c3584fde92d29b0d9c3f62b5073edfe795dfbd39.tar.gz
Introduced atomic.h to simplify handling of atomic integers
Signed-off-by: Nikos Mavrogiannopoulos <nmav@redhat.com>
-rw-r--r--lib/Makefile.am2
-rw-r--r--lib/atomic.h81
2 files changed, 82 insertions, 1 deletions
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 9fb2347588..9e22bb92cf 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -70,7 +70,7 @@ COBJECTS = range.c record.c compress.c debug.c cipher.c \
priority.c hash_int.c cipher_int.c session.c db.c x509_b64.c \
extensions.c auth.c sslv2_compat.c datum.c session_pack.c mpi.c \
pk.c cert.c global.c constate.c anon_cred.c pkix_asn1_tab.c gnutls_asn1_tab.c \
- mem.c fingerprint.c tls-sig.c ecc.c alert.c privkey_raw.c \
+ mem.c fingerprint.c tls-sig.c ecc.c alert.c privkey_raw.c atomic.h \
system/certs.c system/threads.c system/fastopen.c system/sockets.c \
system/inet_ntop.c str-iconv.c system/vasprintf.c vasprintf.h system.c \
str.c str-unicode.c str-idna.c state.c x509.c file.c supplemental.c \
diff --git a/lib/atomic.h b/lib/atomic.h
new file mode 100644
index 0000000000..3e943a81b8
--- /dev/null
+++ b/lib/atomic.h
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2017 Red Hat, Inc.
+ *
+ * Author: Nikos Mavrogiannopoulos
+ *
+ * This file is part of GnuTLS.
+ *
+ * The GnuTLS 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.
+ *
+ * This library 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/>
+ *
+ */
+
+#ifndef GNUTLS_ATOMIC_H
+#define GNUTLS_ATOMIC_H
+
+#ifdef HAVE_STDATOMIC_H
+# include <stdatomic.h>
+
+# define gnutls_atomic_uint_t atomic_uint
+# define DEF_ATOMIC_INT(x) atomic_uint x
+# define gnutls_atomic_increment(x) (*x)++
+# define gnutls_atomic_decrement(x) (*x)--
+# define gnutls_atomic_init(x) (*x)=(0)
+# define gnutls_atomic_deinit(x)
+# define gnutls_atomic_val(x) (*x)
+#else
+# include "locks.h"
+
+struct gnutls_atomic_uint_st {
+ void *lock;
+ unsigned int value;
+};
+typedef struct gnutls_atomic_uint_st *gnutls_atomic_uint_t;
+
+# define DEF_ATOMIC_INT(x) struct gnutls_atomic_uint_st x
+
+inline static unsigned gnutls_atomic_val(gnutls_atomic_uint_t x)
+{
+ unsigned int t;
+ gnutls_mutex_lock(x->lock);
+ t = x->value;
+ gnutls_mutex_unlock(x->lock);
+ return t;
+}
+
+inline static void gnutls_atomic_increment(gnutls_atomic_uint_t x)
+{
+ gnutls_mutex_lock(x->lock);
+ x->value++;
+ gnutls_mutex_unlock(x->lock);
+}
+
+inline static void gnutls_atomic_decrement(gnutls_atomic_uint_t x)
+{
+ gnutls_mutex_lock(x->lock);
+ x->value--;
+ gnutls_mutex_unlock(x->lock);
+}
+
+inline static void gnutls_atomic_init(gnutls_atomic_uint_t x)
+{
+ gnutls_mutex_init(&x->lock);
+ x->value = 0;
+}
+inline static void gnutls_atomic_deinit(gnutls_atomic_uint_t x)
+{
+ gnutls_mutex_deinit(&x->lock);
+}
+#endif
+
+#endif