mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-22 21:32:31 +00:00
Get rid of kmalloc()
This changes *NSYNC to allocate waiters on the stack so our locks don't need to depend on dynamic memory. This make our runtiem simpler, and it also fixes bugs with thread cancellation support.
This commit is contained in:
parent
77a7873057
commit
a359de7893
57 changed files with 405 additions and 472 deletions
|
@ -16,49 +16,26 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/intrin/atomic.h"
|
||||
#include "libc/intrin/kmalloc.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/stdio/internal.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/thread/thread.h"
|
||||
|
||||
static _Atomic(FILE *) __stdio_freelist;
|
||||
|
||||
FILE *__stdio_alloc(void) {
|
||||
FILE *f;
|
||||
f = atomic_load_explicit(&__stdio_freelist, memory_order_acquire);
|
||||
while (f) {
|
||||
if (atomic_compare_exchange_weak_explicit(
|
||||
&__stdio_freelist, &f,
|
||||
atomic_load_explicit((_Atomic(struct FILE *) *)&f->next,
|
||||
memory_order_acquire),
|
||||
memory_order_release, memory_order_relaxed)) {
|
||||
atomic_store_explicit((_Atomic(struct FILE *) *)&f->next, 0,
|
||||
memory_order_release);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!f) {
|
||||
f = kmalloc(sizeof(FILE));
|
||||
}
|
||||
if (f) {
|
||||
f->lock._type = PTHREAD_MUTEX_RECURSIVE;
|
||||
if ((f = calloc(1, sizeof(FILE)))) {
|
||||
pthread_mutexattr_t attr;
|
||||
pthread_mutexattr_init(&attr);
|
||||
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
|
||||
pthread_mutex_init(&f->lock, &attr);
|
||||
pthread_mutexattr_destroy(&attr);
|
||||
f->dynamic = 1;
|
||||
}
|
||||
return f;
|
||||
}
|
||||
|
||||
void __stdio_free(FILE *f) {
|
||||
FILE *g;
|
||||
bzero(f, sizeof(*f));
|
||||
g = atomic_load_explicit(&__stdio_freelist, memory_order_acquire);
|
||||
for (;;) {
|
||||
atomic_store_explicit((_Atomic(struct FILE *) *)&f->next, g,
|
||||
memory_order_release);
|
||||
if (atomic_compare_exchange_weak_explicit(&__stdio_freelist, &g, f,
|
||||
memory_order_release,
|
||||
memory_order_relaxed)) {
|
||||
break;
|
||||
}
|
||||
pthread_mutex_destroy(&f->lock);
|
||||
if (f->dynamic) {
|
||||
free(f);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,21 +9,22 @@
|
|||
COSMOPOLITAN_C_START_
|
||||
|
||||
struct FILE {
|
||||
uint8_t bufmode; /* 0x00 _IOFBF, etc. (ignored if fd=-1) */
|
||||
char noclose; /* 0x01 for fake dup() todo delete! */
|
||||
uint32_t iomode; /* 0x04 O_RDONLY, etc. (ignored if fd=-1) */
|
||||
int32_t state; /* 0x08 0=OK, -1=EOF, >0=errno */
|
||||
int fd; /* 0x0c ≥0=fd, -1=closed|buffer */
|
||||
uint32_t beg; /* 0x10 */
|
||||
uint32_t end; /* 0x14 */
|
||||
char *buf; /* 0x18 */
|
||||
uint32_t size; /* 0x20 */
|
||||
uint32_t nofree; /* 0x24 */
|
||||
int pid; /* 0x28 */
|
||||
char *getln; /* 0x30 */
|
||||
pthread_mutex_t lock; /* 0x38 */
|
||||
struct FILE *next; /* 0x48 */
|
||||
char mem[BUFSIZ]; /* 0x50 */
|
||||
uint8_t bufmode; /* _IOFBF, etc. (ignored if fd=-1) */
|
||||
char noclose; /* for fake dup() todo delete! */
|
||||
char dynamic; /* did malloc() create this object? */
|
||||
uint32_t iomode; /* O_RDONLY, etc. (ignored if fd=-1) */
|
||||
int32_t state; /* 0=OK, -1=EOF, >0=errno */
|
||||
int fd; /* ≥0=fd, -1=closed|buffer */
|
||||
uint32_t beg;
|
||||
uint32_t end;
|
||||
char *buf;
|
||||
uint32_t size;
|
||||
uint32_t nofree;
|
||||
int pid;
|
||||
char *getln;
|
||||
pthread_mutex_t lock;
|
||||
struct FILE *next;
|
||||
char mem[BUFSIZ];
|
||||
};
|
||||
|
||||
extern uint64_t g_rando;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue