Skip to content

Instantly share code, notes, and snippets.

@MasterDuke17
Created January 13, 2022 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MasterDuke17/cc3a088dcaecf4da635e851eeeb7a2ab to your computer and use it in GitHub Desktop.
Save MasterDuke17/cc3a088dcaecf4da635e851eeeb7a2ab to your computer and use it in GitHub Desktop.
diff --git src/6model/6model.c src/6model/6model.c
index e0baa2042..832cfb139 100644
--- src/6model/6model.c
+++ src/6model/6model.c
@@ -77,12 +77,10 @@ void MVM_6model_never_repossess(MVMThreadContext *tc, MVMObject *obj) {
/* Set the debug name on a type. */
void MVM_6model_set_debug_name(MVMThreadContext *tc, MVMObject *type, MVMString *name) {
char *orig_debug_name;
- uv_mutex_lock(&(tc->instance->mutex_free_at_safepoint));
orig_debug_name = STABLE(type)->debug_name;
if (orig_debug_name)
MVM_free_at_safepoint(tc, orig_debug_name);
STABLE(type)->debug_name = name && MVM_string_graphs(tc, name)
? MVM_string_utf8_encode_C_string(tc, name)
: NULL;
- uv_mutex_unlock(&(tc->instance->mutex_free_at_safepoint));
}
diff --git src/core/alloc.h src/core/alloc.h
index 2878b9ca9..4ed9d6d0a 100644
--- src/core/alloc.h
+++ src/core/alloc.h
@@ -68,8 +68,20 @@ MVM_STATIC_INLINE void MVM_free(void *p) {
(addr) = NULL; \
} while (0)
-MVM_STATIC_INLINE void MVM_free_at_safepoint(MVMThreadContext *tc, void *ptr) {
- MVM_VECTOR_PUSH(tc->instance->free_at_safepoint, ptr);
+/* Entry in the "free at safe point" linked list. */
+struct MVMAllocSafepointFreeListEntry {
+ void *to_free;
+ MVMAllocSafepointFreeListEntry *next;
+};
+
+MVM_STATIC_INLINE void MVM_free_at_safepoint(MVMThreadContext *tc, void *to_free) {
+ MVMAllocSafepointFreeListEntry *orig;
+ MVMAllocSafepointFreeListEntry *to_add = MVM_malloc(sizeof(MVMAllocSafepointFreeListEntry));
+ to_add->to_free = to_free;
+ do {
+ orig = tc->instance->free_at_safepoint;
+ to_add->next = orig;
+ } while (!MVM_trycas(&(tc->instance->free_at_safepoint), orig, to_add));
}
MVM_STATIC_INLINE void * MVM_realloc_at_safepoint(MVMThreadContext *tc, void *p, size_t old_bytes, size_t new_bytes) {
@@ -82,7 +94,13 @@ MVM_STATIC_INLINE void * MVM_realloc_at_safepoint(MVMThreadContext *tc, void *p,
}
MVM_STATIC_INLINE void MVM_alloc_safepoint(MVMThreadContext *tc) {
- /* No need to acquire mutex since we're in the GC when calling this. */
- while (MVM_VECTOR_ELEMS(tc->instance->free_at_safepoint))
- MVM_free(MVM_VECTOR_POP(tc->instance->free_at_safepoint));
+ MVMAllocSafepointFreeListEntry *cur, *next;
+ cur = tc->instance->free_at_safepoint;
+ while (cur) {
+ next = cur->next;
+ MVM_free(cur->to_free);
+ MVM_free(cur);
+ cur = next;
+ }
+ tc->instance->free_at_safepoint = NULL;
}
diff --git src/core/instance.h src/core/instance.h
index f655641a8..3d9436f36 100644
--- src/core/instance.h
+++ src/core/instance.h
@@ -219,12 +219,13 @@ struct MVMInstance {
uv_mutex_t mutex_object_ids;
/* Fixed size allocator. */
- MVMFixedSizeAlloc *fsa;
+ //MVMFixedSizeAlloc *fsa;
/* Vector of memory to free at the next safepoint, and a mutex to guard
* access to it. */
- MVM_VECTOR_DECL(void *, free_at_safepoint);
- uv_mutex_t mutex_free_at_safepoint;
+ //MVM_VECTOR_DECL(void *, free_at_safepoint);
+ //uv_mutex_t mutex_free_at_safepoint;
+ MVMAllocSafepointFreeListEntry *free_at_safepoint;
/* Whether the --full-cleanup flag was passed. */
MVMuint32 full_cleanup;
diff --git src/moar.c src/moar.c
index bc76f5a5c..836a3d93d 100644
--- src/moar.c
+++ src/moar.c
@@ -155,7 +155,7 @@ MVMInstance * MVM_vm_create_instance(void) {
init_cond(instance->cond_blocked_can_continue, "GC thread unblock");
/* Safe point free list. */
- init_mutex(instance->mutex_free_at_safepoint, "safepoint free list");
+ instance->free_at_safepoint = NULL;
/* Set up REPR registry mutex. */
init_mutex(instance->mutex_repr_registry, "REPR registry");
@@ -653,9 +653,12 @@ void MVM_vm_destroy_instance(MVMInstance *instance) {
uv_cond_destroy(&instance->cond_blocked_can_continue);
uv_mutex_destroy(&instance->mutex_gc_orchestrate);
- /* Clean up safepoint free vector. */
- MVM_VECTOR_DESTROY(instance->free_at_safepoint);
- uv_mutex_destroy(&instance->mutex_free_at_safepoint);
+ /* Clean up safepoint free list. */
+ MVMAllocSafepointFreeListEntry *cur = instance->free_at_safepoint;
+ while (cur) {
+ MVM_free(cur->to_free);
+ cur = cur->next;
+ }
/* Clean up Hash of HLLConfig. */
uv_mutex_destroy(&instance->mutex_hllconfigs);
diff --git src/moar.h src/moar.h
index 81d694fa4..fbad8310b 100644
--- src/moar.h
+++ src/moar.h
@@ -109,6 +109,9 @@ typedef double MVMnum64;
# define MVM_USED_BY_JIT
#endif
+/* Returns non-zero for success. Use for both AO_t numbers and pointers. */
+#define MVM_trycas(addr, old, new) AO_compare_and_swap_full((volatile AO_t *)(addr), (AO_t)(old), (AO_t)(new))
+
/* Hashes */
#define HASH_DEBUG_ITER 0
#define MVM_HASH_RANDOMIZE 1
@@ -298,9 +301,6 @@ AO_t AO_fetch_compare_and_swap_emulation(volatile AO_t *addr, AO_t old_val, AO_t
#define MVM_decr(addr) AO_fetch_and_sub1_full((volatile AO_t *)(addr))
#define MVM_add(addr, add) AO_fetch_and_add_full((volatile AO_t *)(addr), (AO_t)(add))
-/* Returns non-zero for success. Use for both AO_t numbers and pointers. */
-#define MVM_trycas(addr, old, new) AO_compare_and_swap_full((volatile AO_t *)(addr), (AO_t)(old), (AO_t)(new))
-
/* Returns the old value dereferenced at addr. */
#define MVM_cas(addr, old, new) AO_fetch_compare_and_swap_full((addr), (old), (new))
diff --git src/types.h src/types.h
index e1a6b5bd1..02ab8c4c6 100644
--- src/types.h
+++ src/types.h
@@ -63,6 +63,7 @@ typedef struct MVMRegionBlock MVMRegionBlock;
typedef struct MVMFixedSizeAlloc MVMFixedSizeAlloc;
typedef struct MVMFixedSizeAllocFreeListEntry MVMFixedSizeAllocFreeListEntry;
typedef struct MVMFixedSizeAllocSafepointFreeListEntry MVMFixedSizeAllocSafepointFreeListEntry;
+typedef struct MVMAllocSafepointFreeListEntry MVMAllocSafepointFreeListEntry;
typedef struct MVMFixedSizeAllocSizeClass MVMFixedSizeAllocSizeClass;
typedef struct MVMFixedSizeAllocThread MVMFixedSizeAllocThread;
typedef struct MVMFixedSizeAllocThreadSizeClass MVMFixedSizeAllocThreadSizeClass;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment