Use re-entrant locks on stdio

This commit is contained in:
Justine Tunney 2022-05-22 08:13:13 -07:00
parent 4e9662cbc7
commit 1f229e4efc
78 changed files with 427 additions and 179 deletions

View file

@ -16,12 +16,28 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/intrin/spinlock.h"
#include "libc/calls/calls.h"
#include "libc/intrin/cmpxchg.h"
#include "libc/intrin/lockcmpxchgp.h"
#include "libc/nexgen32e/threaded.h"
#include "libc/stdio/stdio.h"
/**
* Acquires lock on stdio object, blocking if needed.
* Acquires reentrant lock on stdio object, blocking if needed.
*/
void flockfile(FILE *f) {
_spinlock(&f->lock);
int me, owner;
unsigned tries;
if (!__threaded) return;
for (tries = 0, me = gettid();;) {
owner = 0;
if (_lockcmpxchgp(&f->lock, &owner, me) || owner == me) {
return;
}
if (++tries & 7) {
__builtin_ia32_pause();
} else {
sched_yield();
}
}
}