summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjb <jb@138bc75d-0d04-0410-961f-82ee72b054a4>2011-11-01 11:44:12 +0000
committerjb <jb@138bc75d-0d04-0410-961f-82ee72b054a4>2011-11-01 11:44:12 +0000
commit87f8f366a7b036cf2d94ea51f4210d8fae8bba9b (patch)
tree3763f8dc50f9d3254ab5d03e9aebcd277367ab70
parentefa273f5e98836fe1d9dd6be4200fd4d35db2bf5 (diff)
downloadgcc-87f8f366a7b036cf2d94ea51f4210d8fae8bba9b.tar.gz
Cleanup NEWUNIT allocation.
2011-11-01 Janne Blomqvist <jb@gcc.gnu.org> * io/io.h (next_available_newunit): Remove prototype. * io/unit.h (next_available_newunit): Make variable static, initialize it. (init_units): Don't initialize next_available_newunit. (get_unique_unit_number): Use atomic builtin if available. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@180734 138bc75d-0d04-0410-961f-82ee72b054a4
-rw-r--r--libgfortran/ChangeLog8
-rw-r--r--libgfortran/io/io.h4
-rw-r--r--libgfortran/io/unit.c14
3 files changed, 16 insertions, 10 deletions
diff --git a/libgfortran/ChangeLog b/libgfortran/ChangeLog
index cbad61af460..a022ee2ec63 100644
--- a/libgfortran/ChangeLog
+++ b/libgfortran/ChangeLog
@@ -1,3 +1,11 @@
+2011-11-01 Janne Blomqvist <jb@gcc.gnu.org>
+
+ * io/io.h (next_available_newunit): Remove prototype.
+ * io/unit.h (next_available_newunit): Make variable static,
+ initialize it.
+ (init_units): Don't initialize next_available_newunit.
+ (get_unique_unit_number): Use atomic builtin if available.
+
2011-10-31 Janne Blomqvist <jb@gcc.gnu.org>
* io/inquire.c (inquire_via_unit): Check whether we're at the
diff --git a/libgfortran/io/io.h b/libgfortran/io/io.h
index 23f07cae548..3569c543f01 100644
--- a/libgfortran/io/io.h
+++ b/libgfortran/io/io.h
@@ -576,10 +576,6 @@ gfc_unit;
extern gfc_offset max_offset;
internal_proto(max_offset);
-/* Unit number to be assigned when NEWUNIT is used in an OPEN statement. */
-extern GFC_INTEGER_4 next_available_newunit;
-internal_proto(next_available_newunit);
-
/* Unit tree root. */
extern gfc_unit *unit_root;
internal_proto(unit_root);
diff --git a/libgfortran/io/unit.c b/libgfortran/io/unit.c
index b4d10cdbf11..33072fe9ae8 100644
--- a/libgfortran/io/unit.c
+++ b/libgfortran/io/unit.c
@@ -71,8 +71,9 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
/* Subroutines related to units */
-GFC_INTEGER_4 next_available_newunit;
+/* Unit number to be assigned when NEWUNIT is used in an OPEN statement. */
#define GFC_FIRST_NEWUNIT -10
+static GFC_INTEGER_4 next_available_newunit = GFC_FIRST_NEWUNIT;
#define CACHE_SIZE 3
static gfc_unit *unit_cache[CACHE_SIZE];
@@ -525,8 +526,6 @@ init_units (void)
__GTHREAD_MUTEX_INIT_FUNCTION (&unit_lock);
#endif
- next_available_newunit = GFC_FIRST_NEWUNIT;
-
if (options.stdin_unit >= 0)
{ /* STDIN */
u = insert_unit (options.stdin_unit);
@@ -808,16 +807,19 @@ get_unique_unit_number (st_parameter_open *opp)
{
GFC_INTEGER_4 num;
+#ifdef HAVE_SYNC_FETCH_AND_ADD
+ num = __sync_fetch_and_add (&next_available_newunit, -1);
+#else
__gthread_mutex_lock (&unit_lock);
num = next_available_newunit--;
+ __gthread_mutex_unlock (&unit_lock);
+#endif
/* Do not allow NEWUNIT numbers to wrap. */
- if (next_available_newunit >= GFC_FIRST_NEWUNIT )
+ if (num > GFC_FIRST_NEWUNIT )
{
- __gthread_mutex_unlock (&unit_lock);
generate_error (&opp->common, LIBERROR_INTERNAL, "NEWUNIT exhausted");
return 0;
}
- __gthread_mutex_unlock (&unit_lock);
return num;
}