mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-27 23:08:31 +00:00
Make locks more reliable
This change switches most of the core locks to be re-entrant, in order to reduce the chance of deadlocking code that does, clever things with asynchronous signal handlers. This change implements it it in pthreads so we're one step closer to having a standardized threading primitives
This commit is contained in:
parent
5ea618f0af
commit
c260345e06
35 changed files with 369 additions and 258 deletions
|
@ -1,5 +1,6 @@
|
|||
#ifndef COSMOPOLITAN_LIBC_STDIO_FFLUSH_H_
|
||||
#define COSMOPOLITAN_LIBC_STDIO_FFLUSH_H_
|
||||
#include "libc/intrin/pthread.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
COSMOPOLITAN_C_START_
|
||||
|
@ -10,7 +11,7 @@ struct StdioFlushHandles {
|
|||
};
|
||||
|
||||
struct StdioFlush {
|
||||
int lock;
|
||||
pthread_mutex_t lock;
|
||||
struct StdioFlushHandles handles;
|
||||
FILE *handles_initmem[8];
|
||||
};
|
||||
|
|
|
@ -21,6 +21,7 @@
|
|||
#include "libc/bits/pushpop.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/intrin/pthread.h"
|
||||
#include "libc/intrin/spinlock.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/mem/mem.h"
|
||||
|
@ -40,7 +41,7 @@ int fflush_unlocked(FILE *f) {
|
|||
int rc = 0;
|
||||
size_t i;
|
||||
if (!f) {
|
||||
_spinlock(&__fflush.lock);
|
||||
pthread_mutex_lock(&__fflush.lock);
|
||||
for (i = __fflush.handles.i; i; --i) {
|
||||
if ((f = __fflush.handles.p[i - 1])) {
|
||||
if (fflush(f) == -1) {
|
||||
|
@ -48,7 +49,7 @@ int fflush_unlocked(FILE *f) {
|
|||
}
|
||||
}
|
||||
}
|
||||
_spunlock(&__fflush.lock);
|
||||
pthread_mutex_unlock(&__fflush.lock);
|
||||
} else if (f->fd != -1) {
|
||||
if (__fflush_impl(f) == -1) {
|
||||
rc = -1;
|
||||
|
@ -63,7 +64,7 @@ textstartup int __fflush_register(FILE *f) {
|
|||
int rc;
|
||||
size_t i;
|
||||
struct StdioFlush *sf;
|
||||
_spinlock(&__fflush.lock);
|
||||
pthread_mutex_lock(&__fflush.lock);
|
||||
sf = &__fflush;
|
||||
if (!sf->handles.p) {
|
||||
sf->handles.p = sf->handles_initmem;
|
||||
|
@ -73,19 +74,19 @@ textstartup int __fflush_register(FILE *f) {
|
|||
for (i = sf->handles.i; i; --i) {
|
||||
if (!sf->handles.p[i - 1]) {
|
||||
sf->handles.p[i - 1] = f;
|
||||
_spunlock(&__fflush.lock);
|
||||
pthread_mutex_unlock(&__fflush.lock);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
rc = append(&sf->handles, &f);
|
||||
_spunlock(&__fflush.lock);
|
||||
pthread_mutex_unlock(&__fflush.lock);
|
||||
return rc;
|
||||
}
|
||||
|
||||
void __fflush_unregister(FILE *f) {
|
||||
size_t i;
|
||||
struct StdioFlush *sf;
|
||||
_spinlock(&__fflush.lock);
|
||||
pthread_mutex_lock(&__fflush.lock);
|
||||
sf = &__fflush;
|
||||
sf = pushpop(sf);
|
||||
for (i = sf->handles.i; i; --i) {
|
||||
|
@ -94,5 +95,5 @@ void __fflush_unregister(FILE *f) {
|
|||
break;
|
||||
}
|
||||
}
|
||||
_spunlock(&__fflush.lock);
|
||||
pthread_mutex_unlock(&__fflush.lock);
|
||||
}
|
||||
|
|
|
@ -16,30 +16,11 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#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 reentrant lock on stdio object, blocking if needed.
|
||||
*/
|
||||
void flockfile(FILE *f) {
|
||||
int me, owner;
|
||||
unsigned tries;
|
||||
if (__threaded) {
|
||||
for (tries = 0, me = gettid();;) {
|
||||
owner = 0;
|
||||
if (_lockcmpxchgp(&f->lock, &owner, me) || owner == me) {
|
||||
break;
|
||||
}
|
||||
if (++tries & 7) {
|
||||
__builtin_ia32_pause();
|
||||
} else {
|
||||
sched_yield();
|
||||
}
|
||||
}
|
||||
}
|
||||
++f->reent;
|
||||
pthread_mutex_lock(&f->lock);
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/intrin/pthread.h"
|
||||
#include "libc/intrin/spinlock.h"
|
||||
#include "libc/stdio/fflush.internal.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
|
@ -28,7 +29,7 @@
|
|||
void _flushlbf(void) {
|
||||
int i;
|
||||
FILE *f;
|
||||
_spinlock(&__fflush.lock);
|
||||
pthread_mutex_lock(&__fflush.lock);
|
||||
for (i = 0; i < __fflush.handles.i; ++i) {
|
||||
if ((f = __fflush.handles.p[i])) {
|
||||
flockfile(f);
|
||||
|
@ -38,5 +39,5 @@ void _flushlbf(void) {
|
|||
funlockfile(f);
|
||||
}
|
||||
}
|
||||
_spunlock(&__fflush.lock);
|
||||
pthread_mutex_unlock(&__fflush.lock);
|
||||
}
|
||||
|
|
|
@ -16,9 +16,6 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/intrin/lockcmpxchgp.h"
|
||||
#include "libc/nexgen32e/threaded.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
|
||||
/**
|
||||
|
@ -27,15 +24,5 @@
|
|||
* @return 0 on success, or non-zero if another thread owns the lock
|
||||
*/
|
||||
int ftrylockfile(FILE *f) {
|
||||
int me, owner = 0;
|
||||
if (__threaded) {
|
||||
me = gettid();
|
||||
if (!_lockcmpxchgp(&f->lock, &owner, me) && owner == me) {
|
||||
owner = 0;
|
||||
}
|
||||
}
|
||||
if (!owner) {
|
||||
++f->reent;
|
||||
}
|
||||
return owner;
|
||||
return pthread_mutex_trylock(&f->lock);
|
||||
}
|
||||
|
|
|
@ -16,24 +16,11 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/assert.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/nexgen32e/threaded.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
|
||||
/**
|
||||
* Releases lock on stdio object.
|
||||
*/
|
||||
void funlockfile(FILE *f) {
|
||||
int owner;
|
||||
bool shouldunlock;
|
||||
assert(f->reent > 0);
|
||||
shouldunlock = --f->reent <= 0;
|
||||
if (__threaded) {
|
||||
assert(f->lock == gettid());
|
||||
if (shouldunlock) {
|
||||
owner = 0;
|
||||
__atomic_store(&f->lock, &owner, __ATOMIC_RELAXED);
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&f->lock);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#ifndef COSMOPOLITAN_LIBC_STDIO_STDIO_H_
|
||||
#define COSMOPOLITAN_LIBC_STDIO_STDIO_H_
|
||||
#include "libc/fmt/pflink.h"
|
||||
#include "libc/intrin/pthread.h"
|
||||
#include "libc/runtime/symbolic.h"
|
||||
|
||||
#define FILENAME_MAX PATH_MAX
|
||||
|
@ -24,9 +25,8 @@ typedef struct FILE {
|
|||
uint32_t size; /* 0x20 */
|
||||
uint32_t nofree; /* 0x24 */
|
||||
int pid; /* 0x28 */
|
||||
int lock; /* 0x2c */
|
||||
int reent; /* 0x30 */
|
||||
char *getln; /* 0x38 */
|
||||
char *getln;
|
||||
pthread_mutex_t lock;
|
||||
} FILE;
|
||||
|
||||
extern FILE *stdin;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue