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:
cinap_lenrek 2013-03-11 00:48:35 +01:00
parent 530a2bc5e9
commit 48b0c10681
14 changed files with 79 additions and 24 deletions

View 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;
}