Ignore:
Timestamp:
Mar 2, 2021, 5:21:35 PM (8 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
arm-eh, jacob/cs343-translation, master, new-ast-unique-expr
Children:
10dafa4
Parents:
0f9c010b
Message:

add concurrency lock to IO stream and provide user interface to lock stream

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/fstream.cfa

    r0f9c010b re474cf09  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Jun 19 16:24:54 2020
    13 // Update Count     : 384
     12// Last Modified On : Mon Mar  1 21:12:15 2021
     13// Update Count     : 424
    1414//
    1515
     
    2525#include <errno.h>                                                                              // errno
    2626
    27 
    2827// *********************************** ofstream ***********************************
    2928
     
    3837        os.$prt = false;
    3938        os.$sawNL = false;
     39        os.$acquired = false;
    4040        $sepSetCur( os, sepGet( os ) );
    4141        sepSet( os, " " );
     
    109109        if ( &os == &exit ) exit( EXIT_FAILURE );
    110110        if ( &os == &abort ) abort();
     111        if ( os.$acquired ) { os.$acquired = false; release( os ); }
    111112} // ends
    112113
     
    171172} // fmt
    172173
     174inline void acquire( ofstream & os ) {
     175        lock( os.$lock );
     176        if ( ! os.$acquired ) os.$acquired = true;
     177        else unlock( os.$lock );
     178} // acquire
     179
     180inline void release( ofstream & os ) {
     181        unlock( os.$lock );
     182} // release
     183
     184void ?{}( osacquire & acq, ofstream & os ) { &acq.os = &os; lock( os.$lock ); }
     185void ^?{}( osacquire & acq ) { release( acq.os ); }
     186
    173187static ofstream soutFile = { (FILE *)stdout };
    174188ofstream & sout = soutFile, & stdout = soutFile;
     
    176190ofstream & serr = serrFile, & stderr = serrFile;
    177191
     192static ofstream lsoutFile = { (FILE *)stdout };
     193ofstream & lsout = lsoutFile;
     194
    178195static ofstream exitFile = { (FILE *)stdout };
    179196ofstream & exit = exitFile;
     
    189206        is.$file = file;
    190207        is.$nlOnOff = false;
     208        is.$acquired = false;
    191209} // ?{}
    192210
     
    213231        return is.$file == 0p || ferror( (FILE *)(is.$file) );
    214232} // fail
     233
     234void ends( ifstream & is ) {
     235        if ( is.$acquired ) { is.$acquired = false; release( is ); }
     236} // ends
    215237
    216238int eof( ifstream & is ) {
     
    279301} // fmt
    280302
     303inline void acquire( ifstream & is ) {
     304        lock( is.$lock );
     305        if ( ! is.$acquired ) is.$acquired = true;
     306        else unlock( is.$lock );
     307} // acquire
     308
     309inline void release( ifstream & is ) {
     310        unlock( is.$lock );
     311} // release
     312
     313void ?{}( isacquire & acq, ifstream & is ) { &acq.is = &is; lock( is.$lock ); }
     314void ^?{}( isacquire & acq ) { release( acq.is ); }
     315
    281316static ifstream sinFile = { (FILE *)stdin };
    282317ifstream & sin = sinFile, & stdin = sinFile;
Note: See TracChangeset for help on using the changeset viewer.