mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-28 00:02:28 +00:00
Use re-entrant locks on stdio
This commit is contained in:
parent
4e9662cbc7
commit
1f229e4efc
78 changed files with 427 additions and 179 deletions
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue