diff options
author | cinap_lenrek <cinap_lenrek@gmx.de> | 2013-03-11 00:48:35 +0100 |
---|---|---|
committer | cinap_lenrek <cinap_lenrek@gmx.de> | 2013-03-11 00:48:35 +0100 |
commit | 48b0c10681bb4e0785fa9f737d287531d06fecb7 (patch) | |
tree | 568f7cdd23400f1e781b82aa044ad39748ff9d78 /sys/src/ape/lib/ap/stdio/fclose.c | |
parent | 530a2bc5e99ad83c46ce015544cf30173c145bcc (diff) |
ape/stdio: make fopen() quasi threadsafe for python
python uses processes sharing memory. it requires at least fopen() to
be called by multiple threads at once so we introduce _IO_newfile()
which allocates the FILE structure slot under a lock.
Diffstat (limited to 'sys/src/ape/lib/ap/stdio/fclose.c')
-rw-r--r-- | sys/src/ape/lib/ap/stdio/fclose.c | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/sys/src/ape/lib/ap/stdio/fclose.c b/sys/src/ape/lib/ap/stdio/fclose.c index d14e83746..c3b157022 100644 --- a/sys/src/ape/lib/ap/stdio/fclose.c +++ b/sys/src/ape/lib/ap/stdio/fclose.c @@ -3,12 +3,28 @@ */ #include "iolib.h" int fclose(FILE *f){ - int error=0; + int d, error=0; + char *p; + if(!f) return EOF; if(f->state==CLOSED) return EOF; if(fflush(f)==EOF) error=EOF; - if(f->flags&BALLOC) free(f->buf); - if(!(f->flags&STRING) && close(f->fd)<0) error=EOF; + if(f->flags&BALLOC){ + if((p = f->buf)!=0){ + f->buf = 0; + f->wp = 0; + f->rp = 0; + f->lp = 0; + free(p); + } + } + if(!(f->flags&STRING)){ + if((d = f->fd)>=0){ + f->fd = -1; + if(close(d) < 0) + error = EOF; + } + } f->state=CLOSED; f->flags=0; return error; |