summaryrefslogtreecommitdiff
path: root/lib/atomic.h
diff options
context:
space:
mode:
authorNikos Mavrogiannopoulos <nmav@redhat.com>2017-07-21 11:43:27 +0200
committerNikos Mavrogiannopoulos <nmav@redhat.com>2017-07-25 09:17:20 +0200
commit7978a733460f92b31033affd0e487c86d66c643d (patch)
treea244e526a992fa702f1ca052521238c7c0b354df /lib/atomic.h
parentae6b9b282173b422cb56895509aa27c33223f9ed (diff)
downloadgnutls-7978a733460f92b31033affd0e487c86d66c643d.tar.gz
Introduced atomic.h to simplify handling of atomic integers
Signed-off-by: Nikos Mavrogiannopoulos <nmav@redhat.com>
Diffstat (limited to 'lib/atomic.h')
-rw-r--r--lib/atomic.h81
1 files changed, 81 insertions, 0 deletions
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