Author: Remi Meier Branch: c7-refactor Changeset: r868:a39bcb6b49c1 Date: 2014-02-26 14:18 +0100 http://bitbucket.org/pypy/stmgc/changeset/a39bcb6b49c1/
Log: start adapting duhton diff --git a/c7/stmgc.h b/c7/stmgc.h --- a/c7/stmgc.h +++ b/c7/stmgc.h @@ -66,16 +66,17 @@ void _stm_start_transaction(stm_thread_local_t *, stm_jmpbuf_t *); void _stm_collectable_safe_point(void); +/* for tests, but also used in duhton: */ +object_t *_stm_allocate_old(ssize_t size_rounded_up); +char *_stm_real_address(object_t *o); #ifdef STM_TESTS bool _stm_was_read(object_t *obj); bool _stm_was_written(object_t *obj); uint8_t _stm_creation_marker(object_t *obj); bool _stm_in_nursery(object_t *obj); bool _stm_in_transaction(stm_thread_local_t *tl); -char *_stm_real_address(object_t *o); char *_stm_get_segment_base(long index); void _stm_test_switch(stm_thread_local_t *tl); -object_t *_stm_allocate_old(ssize_t size_rounded_up); void _stm_largemalloc_init_arena(char *data_start, size_t data_size); int _stm_largemalloc_resize_arena(size_t new_size); char *_stm_largemalloc_data_start(void); diff --git a/duhton/Makefile b/duhton/Makefile --- a/duhton/Makefile +++ b/duhton/Makefile @@ -14,7 +14,7 @@ duhton_debug: *.c *.h $(C7SOURCES) $(C7HEADERS) - clang -pthread -g -DDu_DEBUG -o duhton_debug *.c ../c7/stmgc.c -Wall + clang -DSTM_DEBUGPRINT -pthread -g -DDu_DEBUG -o duhton_debug *.c ../c7/stmgc.c -Wall clean: - rm -f duhton duhton_debug + rm -f duhton duhton_debug duhton_release diff --git a/duhton/duhton.c b/duhton/duhton.c --- a/duhton/duhton.c +++ b/duhton/duhton.c @@ -41,24 +41,24 @@ printf("))) "); fflush(stdout); } - stm_start_inevitable_transaction(); + stm_start_inevitable_transaction(&stm_thread_local); DuObject *code = Du_Compile(filename, interactive); if (code == NULL) { printf("\n"); break; } - + DuObject *res = Du_Eval(code, Du_Globals); if (interactive) { Du_Print(res, 1); } _du_save1(stm_thread_local_obj); - _stm_minor_collect(); /* hack... */ + stm_collect(0); /* hack... */ _du_restore1(stm_thread_local_obj); - - stm_stop_transaction(); + + stm_commit_transaction(); Du_TransactionRun(); if (!interactive) diff --git a/duhton/duhton.h b/duhton/duhton.h --- a/duhton/duhton.h +++ b/duhton/duhton.h @@ -11,6 +11,7 @@ the program */ #define DEFAULT_NUM_THREADS 2 +extern __thread stm_thread_local_t stm_thread_local; struct DuObject_s { struct object_s header; @@ -191,23 +192,26 @@ #endif -#ifdef NDEBUG -# define _push_root(ob) stm_push_root((object_t *)ob) -# define _pop_root() stm_pop_root() -#else +#ifndef NDEBUG # define _check_not_free(ob) \ assert(_DuObject_TypeNum(ob) > DUTYPE_INVALID && \ _DuObject_TypeNum(ob) < _DUTYPE_TOTAL) +#endif + static inline void _push_root(DuObject *ob) { + #ifndef NDEBUG if (ob) _check_not_free(ob); - stm_push_root((object_t *)ob); + #endif + STM_PUSH_ROOT(stm_thread_local, ob); } static inline object_t *_pop_root(void) { - object_t *ob = stm_pop_root(); + object_t *ob; + STM_POP_ROOT(stm_thread_local, ob); + #ifndef NDEBUG if (ob) _check_not_free(ob); + #endif return ob; } -#endif extern pthread_t *all_threads; extern int all_threads_count; diff --git a/duhton/frame.c b/duhton/frame.c --- a/duhton/frame.c +++ b/duhton/frame.c @@ -48,15 +48,17 @@ void init_prebuilt_frame_objects(void) { du_empty_framenode = (DuFrameNodeObject *) - stm_allocate_prebuilt(sizeof(DuFrameNodeObject)); + _stm_allocate_old(sizeof(DuFrameNodeObject)); du_empty_framenode->ob_base.type_id = DUTYPE_FRAMENODE; du_empty_framenode->ob_count = 0; DuFrameObject *g = (DuFrameObject *) - stm_allocate_prebuilt(sizeof(DuFrameObject)); + _stm_allocate_old(sizeof(DuFrameObject)); g->ob_base.type_id = DUTYPE_FRAME; g->ob_nodes = du_empty_framenode; Du_Globals = (DuObject *)g; + + _du_save2(du_empty_framenode, Du_Globals); } DuObject *DuFrame_New() @@ -203,7 +205,7 @@ _du_save1(frame); dictentry_t *e = find_entry((DuFrameObject *)frame, sym, 1); _du_restore1(frame); - + _du_write1(frame); /* e is part of frame or a new object */ e->builtin_macro = func; } diff --git a/duhton/glob.c b/duhton/glob.c --- a/duhton/glob.c +++ b/duhton/glob.c @@ -147,12 +147,12 @@ /* _du_read1(cons); IMMUTABLE */ DuObject *expr = _DuCons_CAR(cons); DuObject *next = _DuCons_NEXT(cons); - + _du_save2(next, locals); DuObject *obj = Du_Eval(expr, locals); result = DuInt_AsInt(obj); _du_restore2(next, locals); - + cons = next; while (cons != Du_None) { @@ -167,7 +167,7 @@ cons = next; } - + return DuInt_FromInt(result); } @@ -177,12 +177,12 @@ /* _du_read1(cons); IMMUTABLE */ DuObject *expr = _DuCons_CAR(cons); DuObject *next = _DuCons_NEXT(cons); - + _du_save2(next, locals); DuObject *obj = Du_Eval(expr, locals); result = DuInt_AsInt(obj); _du_restore2(next, locals); - + cons = next; while (cons != Du_None) { @@ -197,7 +197,7 @@ cons = next; } - + return DuInt_FromInt(result); } @@ -207,12 +207,12 @@ /* _du_read1(cons); IMMUTABLE */ DuObject *expr = _DuCons_CAR(cons); DuObject *next = _DuCons_NEXT(cons); - + _du_save2(next, locals); DuObject *obj = Du_Eval(expr, locals); result = DuInt_AsInt(obj); _du_restore2(next, locals); - + cons = next; while (cons != Du_None) { @@ -227,7 +227,7 @@ cons = next; } - + return DuInt_FromInt(result); } @@ -686,14 +686,14 @@ Du_FatalError("run-transactions: expected no argument"); _du_save1(stm_thread_local_obj); - _stm_minor_collect(); /* hack... */ + stm_collect(0); /* hack... */ _du_restore1(stm_thread_local_obj); - - stm_stop_transaction(); - + + stm_commit_transaction(); + Du_TransactionRun(); - - stm_start_inevitable_transaction(); + + stm_start_inevitable_transaction(&stm_thread_local); return Du_None; } @@ -718,7 +718,7 @@ long mtime; gettimeofday(¤t, NULL); - + mtime = ((current.tv_sec) * 1000 + current.tv_usec/1000.0) + 0.5; return DuInt_FromInt(mtime & 0x7fffffff); /* make it always positive 32bit */ } @@ -771,15 +771,17 @@ void Du_Initialize(int num_threads) { stm_setup(); - stm_setup_pthread(); + stm_register_thread_local(&stm_thread_local); - stm_start_inevitable_transaction(); + stm_start_inevitable_transaction(&stm_thread_local); + /* allocate old and push on shadowstack: */ init_prebuilt_object_objects(); init_prebuilt_symbol_objects(); init_prebuilt_list_objects(); init_prebuilt_frame_objects(); init_prebuilt_transaction_objects(); + /* prebuilt objs stay on the shadowstack forever */ all_threads_count = num_threads; all_threads = (pthread_t*)malloc(sizeof(pthread_t) * num_threads); @@ -827,11 +829,11 @@ DuFrame_SetBuiltinMacro(Du_Globals, "pair?", du_pair); DuFrame_SetBuiltinMacro(Du_Globals, "assert", du_assert); DuFrame_SetSymbolStr(Du_Globals, "None", Du_None); - stm_stop_transaction(); + stm_commit_transaction(); } void Du_Finalize(void) { - stm_teardown_pthread(); + stm_unregister_thread_local(&stm_thread_local); stm_teardown(); } diff --git a/duhton/listobject.c b/duhton/listobject.c --- a/duhton/listobject.c +++ b/duhton/listobject.c @@ -99,7 +99,7 @@ DuTupleObject *newitems = DuTuple_New(overallocated_size(newcount)); newitems->ob_count = newcount; _du_restore3(ob, x, olditems); - + _du_write1(ob); for (i=0; i<newcount-1; i++) @@ -107,7 +107,7 @@ newitems->ob_items[newcount-1] = x; ob->ob_tuple = newitems; - } + } } void DuList_Append(DuObject *ob, DuObject *item) @@ -206,10 +206,11 @@ void init_prebuilt_list_objects(void) { du_empty_tuple = (DuTupleObject *) - stm_allocate_prebuilt(sizeof(DuTupleObject)); + _stm_allocate_old(sizeof(DuTupleObject)); du_empty_tuple->ob_base.type_id = DUTYPE_TUPLE; du_empty_tuple->ob_count = 0; du_empty_tuple->ob_capacity = 0; + _du_save1(du_empty_tuple); } DuObject *DuList_New() diff --git a/duhton/object.c b/duhton/object.c --- a/duhton/object.c +++ b/duhton/object.c @@ -17,7 +17,7 @@ /* callback: get the size of an object */ -size_t stmcb_size(struct object_s *obj) +ssize_t stmcb_size_rounded_up(struct object_s *obj) { DuType *tp = Du_Types[((struct DuObject_s *)obj)->type_id]; size_t result = tp->dt_size; @@ -69,8 +69,9 @@ void init_prebuilt_object_objects(void) { - Du_None = (DuObject *)stm_allocate_prebuilt(sizeof(DuObject)); + Du_None = (DuObject *)_stm_allocate_old(sizeof(DuObject)); Du_None->type_id = DUTYPE_NONE; + _du_save1(Du_None); } void Du_FatalError(char *msg, ...) diff --git a/duhton/symbol.c b/duhton/symbol.c --- a/duhton/symbol.c +++ b/duhton/symbol.c @@ -56,11 +56,12 @@ void init_prebuilt_symbol_objects(void) { _Du_AllSymbols = (DuSymbolObject *) - stm_allocate_prebuilt(sizeof(DuSymbolObject)); + _stm_allocate_old(sizeof(DuSymbolObject)); _Du_AllSymbols->ob_base.type_id = DUTYPE_SYMBOL; _Du_AllSymbols->myid = 0; _Du_AllSymbols->name = ""; _Du_AllSymbols->next = NULL; + _du_save1(_Du_AllSymbols); } DuObject *DuSymbol_FromString(const char *name) diff --git a/duhton/transaction.c b/duhton/transaction.c --- a/duhton/transaction.c +++ b/duhton/transaction.c @@ -2,7 +2,7 @@ #include <pthread.h> #include <unistd.h> - +__thread stm_thread_local_t stm_thread_local; static DuConsObject *du_pending_transactions; void init_prebuilt_transaction_objects(void) @@ -10,10 +10,12 @@ assert(Du_None); /* already created */ du_pending_transactions = (DuConsObject *) - stm_allocate_prebuilt(sizeof(DuConsObject)); + _stm_allocate_old(sizeof(DuConsObject)); du_pending_transactions->ob_base.type_id = DUTYPE_CONS; du_pending_transactions->car = NULL; du_pending_transactions->cdr = Du_None; + + _du_save1(du_pending_transactions); }; static pthread_mutex_t mutex_sleep = PTHREAD_MUTEX_INITIALIZER; @@ -61,14 +63,14 @@ if (stm_thread_local_obj == NULL) return; - stm_start_inevitable_transaction(); - + stm_start_inevitable_transaction(&stm_thread_local); + DuConsObject *root = du_pending_transactions; _du_write1(root); root->cdr = stm_thread_local_obj; - - stm_stop_transaction(); - + + stm_commit_transaction(); + stm_thread_local_obj = NULL; run_all_threads(); @@ -79,18 +81,15 @@ static DuObject *next_cell(void) { DuObject *pending = stm_thread_local_obj; - jmpbufptr_t here; if (pending == NULL) { /* fish from the global list of pending transactions */ DuConsObject *root; - while (__builtin_setjmp(here) == 1) { } restart: - // stm_start_transaction(&here); /* this code is critical enough so that we want it to be serialized perfectly using inevitable transactions */ - stm_start_inevitable_transaction(); + stm_start_inevitable_transaction(&stm_thread_local); root = du_pending_transactions; _du_read1(root); /* not immutable... */ @@ -103,12 +102,12 @@ DuObject *result = _DuCons_CAR(cell); root->cdr = _DuCons_NEXT(cell); - stm_stop_transaction(); + stm_commit_transaction(); return result; } else { - stm_stop_transaction(); + stm_commit_transaction(); /* nothing to do, wait */ int ts = __sync_add_and_fetch(&thread_sleeping, 1); @@ -134,10 +133,8 @@ /* we have at least one thread-local transaction pending */ stm_thread_local_obj = NULL; - while (__builtin_setjmp(here) == 1) { } - //stm_start_transaction(&here); - stm_start_inevitable_transaction(); - + stm_start_inevitable_transaction(&stm_thread_local); + /* _du_read1(pending); IMMUTABLE */ DuObject *result = _DuCons_CAR(pending); DuObject *next = _DuCons_NEXT(pending); @@ -161,7 +158,7 @@ root->cdr = next; } - stm_stop_transaction(); + stm_commit_transaction(); return result; } @@ -175,8 +172,8 @@ void *run_thread(void *thread_id) { - jmpbufptr_t here; - stm_setup_pthread(); + stm_jmpbuf_t here; + stm_register_thread_local(&stm_thread_local); stm_thread_local_obj = NULL; @@ -186,20 +183,19 @@ break; assert(stm_thread_local_obj == NULL); - while (__builtin_setjmp(here) == 1) { } - stm_start_transaction(&here); - + STM_START_TRANSACTION(&stm_thread_local, here); + run_transaction(cell); - + _du_save1(stm_thread_local_obj); - _stm_minor_collect(); /* hack.. */ + stm_collect(0); /* hack.. */ _du_restore1(stm_thread_local_obj); - - stm_stop_transaction(); + + stm_commit_transaction(); } - stm_teardown_pthread(); + stm_unregister_thread_local(&stm_thread_local); return NULL; } _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit