-
-
Notifications
You must be signed in to change notification settings - Fork 645
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce stack virtual memory consumption on Linux
- Loading branch information
Showing
31 changed files
with
583 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include <pthread.h> | ||
#include <stdio.h> | ||
|
||
// how to spawn a thread | ||
|
||
void *my_thread(void *arg) { | ||
printf("my_thread(%p) is running\n", arg); | ||
return (void *)0x456L; | ||
} | ||
|
||
int main(int argc, char *argv[]) { | ||
void *res; | ||
pthread_t th; | ||
pthread_create(&th, 0, my_thread, (void *)0x123L); | ||
pthread_join(th, &res); | ||
printf("my_thread() returned %p\n", res); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#ifndef COSMOPOLITAN_LIBC_INTRIN_LOCKLESS_H_ | ||
#define COSMOPOLITAN_LIBC_INTRIN_LOCKLESS_H_ | ||
#include "libc/atomic.h" | ||
#include "libc/intrin/atomic.h" | ||
COSMOPOLITAN_C_START_ | ||
|
||
// lockless memory transactions | ||
// | ||
// - one writer | ||
// - many readers | ||
// - generation is monotonic | ||
// - even numbers mean memory is ready | ||
// - odd numbers mean memory is actively being changed | ||
// - always use acquire semantics inside your read transaction | ||
// | ||
// let's say you want to be able to atomically read and write to 128-bit | ||
// values, but you've only got a 64-bit system. if you expect that it'll | ||
// frequently written, then you should use a mutex. but if you expect it | ||
// to be frequently read and rarely written, then it's possible to do it | ||
// without a mutex; in fact you don't even need the x86 lock instruction | ||
// prefix; all that is required is a series of carefully ordered mov ops | ||
// which are designed to exploit the strong ordering of the architecture | ||
|
||
static inline unsigned lockless_write_begin(atomic_uint* genptr) { | ||
unsigned gen = atomic_load_explicit(genptr, memory_order_acquire); | ||
atomic_store_explicit(genptr, gen + 1, memory_order_release); | ||
return gen; | ||
} | ||
|
||
static inline void lockless_write_end(atomic_uint* genptr, unsigned gen) { | ||
atomic_store_explicit(genptr, gen + 2, memory_order_release); | ||
} | ||
|
||
static inline unsigned lockless_read_begin(atomic_uint* genptr) { | ||
return atomic_load_explicit(genptr, memory_order_acquire); | ||
} | ||
|
||
static inline bool lockless_read_end(atomic_uint* genptr, unsigned* want) { | ||
unsigned gen1 = *want; | ||
unsigned gen2 = atomic_load_explicit(genptr, memory_order_acquire); | ||
unsigned is_being_actively_changed = gen1 & 1; | ||
unsigned we_lost_race_with_writers = gen1 ^ gen2; | ||
if (!(is_being_actively_changed | we_lost_race_with_writers)) | ||
return true; | ||
*want = gen2; | ||
return false; | ||
} | ||
|
||
COSMOPOLITAN_C_END_ | ||
#endif /* COSMOPOLITAN_LIBC_INTRIN_LOCKLESS_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.