#include "config.h"
#include <stdlib.h>
#include <string.h>
#include "libgfortran.h"
#include "io.h"
#define CACHE_SIZE 3
static gfc_unit *unit_cache[CACHE_SIZE];
gfc_offset max_offset;
gfc_unit *unit_root;
#ifdef __GTHREAD_MUTEX_INIT
__gthread_mutex_t unit_lock = __GTHREAD_MUTEX_INIT;
#else
__gthread_mutex_t unit_lock;
#endif
static int
pseudo_random (void)
{
static int x0 = 5341;
x0 = (22611 * x0 + 10) % 44071;
return x0;
}
static gfc_unit *
rotate_left (gfc_unit * t)
{
gfc_unit *temp;
temp = t->right;
t->right = t->right->left;
temp->left = t;
return temp;
}
static gfc_unit *
rotate_right (gfc_unit * t)
{
gfc_unit *temp;
temp = t->left;
t->left = t->left->right;
temp->right = t;
return temp;
}
static int
compare (int a, int b)
{
if (a < b)
return -1;
if (a > b)
return 1;
return 0;
}
static gfc_unit *
insert (gfc_unit *new, gfc_unit *t)
{
int c;
if (t == NULL)
return new;
c = compare (new->unit_number, t->unit_number);
if (c < 0)
{
t->left = insert (new, t->left);
if (t->priority < t->left->priority)
t = rotate_right (t);
}
if (c > 0)
{
t->right = insert (new, t->right);
if (t->priority < t->right->priority)
t = rotate_left (t);
}
if (c == 0)
internal_error (NULL, "insert(): Duplicate key found!");
return t;
}
static gfc_unit *
insert_unit (int n)
{
gfc_unit *u = get_mem (sizeof (gfc_unit));
memset (u, '\0', sizeof (gfc_unit));
u->unit_number = n;
#ifdef __GTHREAD_MUTEX_INIT
{
__gthread_mutex_t tmp = __GTHREAD_MUTEX_INIT;
u->lock = tmp;
}
#else
__GTHREAD_MUTEX_INIT_FUNCTION (&u->lock);
#endif
__gthread_mutex_lock (&u->lock);
u->priority = pseudo_random ();
unit_root = insert (u, unit_root);
return u;
}
static gfc_unit *
delete_root (gfc_unit * t)
{
gfc_unit *temp;
if (t->left == NULL)
return t->right;
if (t->right == NULL)
return t->left;
if (t->left->priority > t->right->priority)
{
temp = rotate_right (t);
temp->right = delete_root (t);
}
else
{
temp = rotate_left (t);
temp->left = delete_root (t);
}
return temp;
}
static gfc_unit *
delete_treap (gfc_unit * old, gfc_unit * t)
{
int c;
if (t == NULL)
return NULL;
c = compare (old->unit_number, t->unit_number);
if (c < 0)
t->left = delete_treap (old, t->left);
if (c > 0)
t->right = delete_treap (old, t->right);
if (c == 0)
t = delete_root (t);
return t;
}
static void
delete_unit (gfc_unit * old)
{
unit_root = delete_treap (old, unit_root);
}
static gfc_unit *
get_external_unit (int n, int do_create)
{
gfc_unit *p;
int c, created = 0;
__gthread_mutex_lock (&unit_lock);
retry:
for (c = 0; c < CACHE_SIZE; c++)
if (unit_cache[c] != NULL && unit_cache[c]->unit_number == n)
{
p = unit_cache[c];
goto found;
}
p = unit_root;
while (p != NULL)
{
c = compare (n, p->unit_number);
if (c < 0)
p = p->left;
if (c > 0)
p = p->right;
if (c == 0)
break;
}
if (p == NULL && do_create)
{
p = insert_unit (n);
created = 1;
}
if (p != NULL)
{
for (c = 0; c < CACHE_SIZE - 1; c++)
unit_cache[c] = unit_cache[c + 1];
unit_cache[CACHE_SIZE - 1] = p;
}
if (created)
{
__gthread_mutex_unlock (&unit_lock);
return p;
}
found:
if (p != NULL)
{
if (! __gthread_mutex_trylock (&p->lock))
{
__gthread_mutex_unlock (&unit_lock);
return p;
}
inc_waiting_locked (p);
}
__gthread_mutex_unlock (&unit_lock);
if (p != NULL)
{
__gthread_mutex_lock (&p->lock);
if (p->closed)
{
__gthread_mutex_lock (&unit_lock);
__gthread_mutex_unlock (&p->lock);
if (predec_waiting_locked (p) == 0)
free_mem (p);
goto retry;
}
dec_waiting_unlocked (p);
}
return p;
}
gfc_unit *
find_unit (int n)
{
return get_external_unit (n, 0);
}
gfc_unit *
find_or_create_unit (int n)
{
return get_external_unit (n, 1);
}
gfc_unit *
get_internal_unit (st_parameter_dt *dtp)
{
gfc_unit * iunit;
iunit = get_mem (sizeof (gfc_unit));
if (iunit == NULL)
{
generate_error (&dtp->common, ERROR_INTERNAL_UNIT, NULL);
return NULL;
}
memset (iunit, '\0', sizeof (gfc_unit));
#ifdef __GTHREAD_MUTEX_INIT
{
__gthread_mutex_t tmp = __GTHREAD_MUTEX_INIT;
iunit->lock = tmp;
}
#else
__GTHREAD_MUTEX_INIT_FUNCTION (&iunit->lock);
#endif
__gthread_mutex_lock (&iunit->lock);
iunit->recl = dtp->internal_unit_len;
iunit->unit_number = -1;
if (is_array_io (dtp))
{
iunit->rank = GFC_DESCRIPTOR_RANK (dtp->internal_unit_desc);
iunit->ls = (array_loop_spec *)
get_mem (iunit->rank * sizeof (array_loop_spec));
dtp->internal_unit_len *=
init_loop_spec (dtp->internal_unit_desc, iunit->ls);
}
iunit->s = open_internal (dtp->internal_unit, dtp->internal_unit_len);
iunit->bytes_left = iunit->recl;
iunit->last_record=0;
iunit->maxrec=0;
iunit->current_record=0;
iunit->read_bad = 0;
iunit->flags.access = ACCESS_SEQUENTIAL;
iunit->flags.action = ACTION_READWRITE;
iunit->flags.form = FORM_FORMATTED;
iunit->flags.pad = PAD_YES;
iunit->flags.status = STATUS_UNSPECIFIED;
iunit->endfile = NO_ENDFILE;
dtp->u.p.advance_status = ADVANCE_YES;
dtp->u.p.blank_status = BLANK_UNSPECIFIED;
dtp->u.p.seen_dollar = 0;
dtp->u.p.skips = 0;
dtp->u.p.pending_spaces = 0;
dtp->u.p.max_pos = 0;
dtp->u.p.at_eof = 0;
dtp->u.p.unit_is_internal = 1;
return iunit;
}
void
free_internal_unit (st_parameter_dt *dtp)
{
if (!is_internal_unit (dtp))
return;
if (dtp->u.p.current_unit->ls != NULL)
free_mem (dtp->u.p.current_unit->ls);
sclose (dtp->u.p.current_unit->s);
if (dtp->u.p.current_unit != NULL)
free_mem (dtp->u.p.current_unit);
}
gfc_unit *
get_unit (st_parameter_dt *dtp, int do_create)
{
if ((dtp->common.flags & IOPARM_DT_HAS_INTERNAL_UNIT) != 0)
return get_internal_unit(dtp);
dtp->u.p.unit_is_internal = 0;
dtp->internal_unit_desc = NULL;
return get_external_unit (dtp->common.unit, do_create);
}
int
is_internal_unit (st_parameter_dt *dtp)
{
return dtp->u.p.unit_is_internal;
}
int
is_array_io (st_parameter_dt *dtp)
{
return dtp->internal_unit_desc != NULL;
}
int
is_stream_io (st_parameter_dt *dtp)
{
return dtp->u.p.current_unit->flags.access == ACCESS_STREAM;
}
void
init_units (void)
{
gfc_unit *u;
unsigned int i;
#ifndef __GTHREAD_MUTEX_INIT
__GTHREAD_MUTEX_INIT_FUNCTION (&unit_lock);
#endif
if (options.stdin_unit >= 0)
{
u = insert_unit (options.stdin_unit);
u->s = input_stream ();
u->flags.action = ACTION_READ;
u->flags.access = ACCESS_SEQUENTIAL;
u->flags.form = FORM_FORMATTED;
u->flags.status = STATUS_OLD;
u->flags.blank = BLANK_NULL;
u->flags.pad = PAD_YES;
u->flags.position = POSITION_ASIS;
u->recl = options.default_recl;
u->endfile = NO_ENDFILE;
__gthread_mutex_unlock (&u->lock);
}
if (options.stdout_unit >= 0)
{
u = insert_unit (options.stdout_unit);
u->s = output_stream ();
u->flags.action = ACTION_WRITE;
u->flags.access = ACCESS_SEQUENTIAL;
u->flags.form = FORM_FORMATTED;
u->flags.status = STATUS_OLD;
u->flags.blank = BLANK_NULL;
u->flags.position = POSITION_ASIS;
u->recl = options.default_recl;
u->endfile = AT_ENDFILE;
__gthread_mutex_unlock (&u->lock);
}
if (options.stderr_unit >= 0)
{
u = insert_unit (options.stderr_unit);
u->s = error_stream ();
u->flags.action = ACTION_WRITE;
u->flags.access = ACCESS_SEQUENTIAL;
u->flags.form = FORM_FORMATTED;
u->flags.status = STATUS_OLD;
u->flags.blank = BLANK_NULL;
u->flags.position = POSITION_ASIS;
u->recl = options.default_recl;
u->endfile = AT_ENDFILE;
__gthread_mutex_unlock (&u->lock);
}
max_offset = 0;
for (i = 0; i < sizeof (max_offset) * 8 - 1; i++)
max_offset = max_offset + ((gfc_offset) 1 << i);
}
static int
close_unit_1 (gfc_unit *u, int locked)
{
int i, rc;
rc = (u->s == NULL) ? 0 : sclose (u->s) == FAILURE;
u->closed = 1;
if (!locked)
__gthread_mutex_lock (&unit_lock);
for (i = 0; i < CACHE_SIZE; i++)
if (unit_cache[i] == u)
unit_cache[i] = NULL;
delete_unit (u);
if (u->file)
free_mem (u->file);
u->file = NULL;
u->file_len = 0;
if (!locked)
__gthread_mutex_unlock (&u->lock);
if (u->waiting == 0)
free_mem (u);
if (!locked)
__gthread_mutex_unlock (&unit_lock);
return rc;
}
void
unlock_unit (gfc_unit *u)
{
__gthread_mutex_unlock (&u->lock);
}
int
close_unit (gfc_unit *u)
{
return close_unit_1 (u, 0);
}
void
close_units (void)
{
__gthread_mutex_lock (&unit_lock);
while (unit_root != NULL)
close_unit_1 (unit_root, 1);
__gthread_mutex_unlock (&unit_lock);
}