summaryrefslogtreecommitdiff
path: root/libguile/atomics-internal.h
diff options
context:
space:
mode:
authorAndy Wingo <wingo@pobox.com>2016-08-31 19:00:27 +0200
committerAndy Wingo <wingo@pobox.com>2016-08-31 19:10:35 +0200
commitb8a53b98b33dc89b0ed526ca66232655d24f2ce8 (patch)
tree82d587231e835060cfbc04eb97e08acbceda3d06 /libguile/atomics-internal.h
parentcc9e72bd2b896048af2a65c8af9a57868df4352f (diff)
downloadguile-b8a53b98b33dc89b0ed526ca66232655d24f2ce8.tar.gz
Only ptob->close() after read/write finish
* libguile/Makefile.am (noinst_HEADERS): Add atomics-internal.h. * libguile/atomics-internal.h: New file. * libguile/ports-internal.h (refcount): New member. * libguile/ports.c (release_port, scm_dynwind_acquire_port): New facility for acquiring a port within a dynwind. (scm_port_poll, scm_i_read_bytes, scm_setvbuf, scm_end_input) (scm_i_write_bytes, scm_char_ready_p, scm_seek) (scm_truncate_file, trampoline_to_c_read) (trampoline_to_c_write): Acquire port. (scm_c_make_port_with_encoding): Init refcount to 1. (scm_close_port): Release port. * doc/ref/api-io.texi (I/O Extensions): Add documentation
Diffstat (limited to 'libguile/atomics-internal.h')
-rw-r--r--libguile/atomics-internal.h85
1 files changed, 85 insertions, 0 deletions
diff --git a/libguile/atomics-internal.h b/libguile/atomics-internal.h
new file mode 100644
index 000000000..1859daa92
--- /dev/null
+++ b/libguile/atomics-internal.h
@@ -0,0 +1,85 @@
+#ifndef SCM_ATOMICS_INTERNAL_H
+#define SCM_ATOMICS_INTERNAL_H
+
+/* Copyright (C) 2016
+ * Free Software Foundation, Inc.
+ *
+ * This library 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 3 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 library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
+
+
+
+
+#include <stdint.h>
+
+
+
+
+#define HAVE_C11_ATOMICS (__STDC_VERSION__ >= 201112L && !defined(__STDC_NO_ATOMICS__))
+
+#if HAVE_C11_ATOMICS
+
+#include <stdatomic.h>
+static inline uint32_t
+scm_atomic_subtract_uint32 (uint32_t *obj, uint32_t arg)
+{
+ return atomic_fetch_sub (obj, arg);
+}
+static inline _Bool
+scm_atomic_compare_and_swap_uint32 (uint32_t *obj, uint32_t *expected,
+ uint32_t desired)
+{
+ return atomic_compare_exchange_weak (obj, expected, desired);
+}
+
+#else /* HAVE_C11_ATOMICS */
+
+/* Fallback implementation using locks. */
+#include "libguile/threads.h"
+static scm_i_pthread_mutex_t atomics_lock = SCM_I_PTHREAD_MUTEX_INITIALIZER;
+static inline uint32_t
+scm_atomic_subtract_uint32 (uint32_t *obj, uint32_t arg)
+{
+ uint32_t ret;
+ scm_i_pthread_mutex_lock (&atomics_lock);
+ ret = *obj;
+ *obj -= arg;
+ scm_i_pthread_mutex_unlock (&atomics_lock);
+ return ret;
+}
+static inline int
+scm_atomic_compare_and_swap_uint32 (uint32_t *obj, uint32_t *expected,
+ uint32_t desired)
+{
+ int ret;
+ scm_i_pthread_mutex_lock (&atomics_lock);
+ if (*obj == *expected)
+ {
+ *obj = desired;
+ ret = 1;
+ }
+ else
+ {
+ *expected = *obj;
+ ret = 0;
+ }
+ scm_i_pthread_mutex_unlock (&atomics_lock);
+ return ret;
+}
+
+#endif /* HAVE_C11_ATOMICS */
+
+#endif /* SCM_ATOMICS_INTERNAL_H */