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.
This commit is contained in:
parent
530a2bc5e9
commit
48b0c10681
14 changed files with 79 additions and 24 deletions
27
sys/src/ape/lib/ap/stdio/_IO_newfile.c
Normal file
27
sys/src/ape/lib/ap/stdio/_IO_newfile.c
Normal file
|
@ -0,0 +1,27 @@
|
|||
/*
|
||||
* pANS stdio -- fopen
|
||||
*/
|
||||
#include "iolib.h"
|
||||
|
||||
#define _PLAN9_SOURCE
|
||||
#include <lock.h>
|
||||
|
||||
FILE *_IO_newfile(void)
|
||||
{
|
||||
static FILE *fx=0;
|
||||
static Lock fl;
|
||||
FILE *f;
|
||||
int i;
|
||||
|
||||
lock(&fl);
|
||||
for(i=0; i<FOPEN_MAX; i++){
|
||||
if(fx==0 || ++fx >= &_IO_stream[FOPEN_MAX]) fx=_IO_stream;
|
||||
if(fx->state==CLOSED)
|
||||
break;
|
||||
}
|
||||
f = fx;
|
||||
unlock(&fl);
|
||||
if(f->state!=CLOSED)
|
||||
return NULL;
|
||||
return f;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue