/*-------------------------------------------------------------------------*/ OperInf *Delete_Oper(AtomInf *atom,int type) { int key=Make_Oper_Key(atom,type); return (OperInf *) Hash_Lookup(oper_tbl,(char *) &key,H_DELETE); }
/*-------------------------------------------------------------------------*/ PredInf *Create_Pred(AtomInf *functor,int arity,int module_nb,CodePtr codep) { PredTbl tbl=(module_nb==0) ? pred_tbl : module_tbl[module_nb].pred_tbl; PredInf pred_info; PredInf *pred; pred_info.f_n =Make_Pred_Key(functor,arity); pred_info.owner_mod_nb=module_nb; pred_info.codep =codep; if (codep) pred_info.dynamic=NULL; else { if ((pred_info.dynamic=(DynPInf *) Lib1(malloc,sizeof(DynPInf)))==NULL) Fatal_Error(ERR_ALLOC_FAULT); Init_Dynamic_Info(pred_info.dynamic); } pred=(PredInf *) Hash_Lookup(tbl,(char *) &pred_info,H_UPDATE); if ((int) pred == -1) Fatal_Error(ERR_PRED_TBL_FULL); return pred; }
/*-------------------------------------------------------------------------*/ void Delete_Pred(AtomInf *functor,int arity,int module_nb) { PredTbl tbl=(module_nb==0) ? pred_tbl : module_tbl[module_nb].pred_tbl; int key=Make_Pred_Key(functor,arity); Hash_Lookup(tbl,(char *) &key,H_DELETE); }
/*-------------------------------------------------------------------------*/ void Create_Swt_Element(SwtTbl t,int key,CodePtr codep) { SwtInf swt_info; swt_info.key =key; swt_info.c.codep=codep; Hash_Lookup(t,(char *) &swt_info,H_CREATE); }
/*-------------------------------------------------------------------------*/ OperInf *Create_Oper(AtomInf *atom,int type,int prec,int left,int right) { OperInf oper_info; OperInf *oper; oper_info.a_t =Make_Oper_Key(atom,type); oper_info.prec =prec; oper_info.left =left; oper_info.right=right; oper=(OperInf *) Hash_Lookup(oper_tbl,(char *) &oper_info,H_UPDATE); if ((int) oper == -1) Fatal_Error(ERR_OPER_TBL_FULL); return oper; }
void* thread_code(void *arg) { struct hash_job_desc *jd = (struct hash_job_desc *) arg; int numOfOps = NUM_OPS_PER_THREAD; int i = 0; int *addlist = malloc(sizeof(int) * numOfOps); if (addlist == NULL) { printf(" cannot allocate more ..., reduce space \n"); exit(-1); } while (i < numOfOps) { // addlist[i] = rand() % (numOfOps * num_threads); // since key is unique... addlist[i] = i + (numOfOps * jd->tid); Hash_Insert(&h, addlist + i, addlist[i]); (jd->ic)++; i++; } i = 0; while (i < numOfOps) { if (i % 3 == 0) { Hash_Delete(&h, addlist[i]); (jd->dc)++; } i++; } i = 0; while (i < numOfOps) { if (Hash_Lookup(&h, addlist[i]) != NULL) (jd->lc)++; i++; } // printf(" END Thread-%04d with %6d ic, %6d dc, %6d lc\n", // jd->tid, jd->ic, jd->dc, jd->lc); free(addlist); return NULL; }
/*-------------------------------------------------------------------------*/ AtomInf *Lookup_Atom(char *name) { return (AtomInf *) Hash_Lookup(atom_tbl,(char *) (&name),H_FIND); }
/*-------------------------------------------------------------------------*/ AtomInf *Create_Atom(char *name) { AtomInf atom_info; AtomInf *atom; char *p; int c_type; int lg; Bool indentifier; Bool symbols; if (atom=(AtomInf *) Hash_Lookup(atom_tbl,(char *) (&name),H_FIND)) return atom; /* already exists */ atom_info.name =name; atom_info.has_quote =FALSE; indentifier=(*name && char_type[*name]==SL); /* small letter */ symbols =(*name); for(lg=0,p=name;*p;p++) { c_type=char_type[*p]; if ((c_type & (UL | CL | SL | DI))==0) indentifier=FALSE; if (c_type!=SY) symbols=FALSE; if (c_type==QT) atom_info.has_quote=TRUE; } lg=p-name; if (indentifier) { atom_info.type=IDENTIFIER_ATOM; atom_info.needs_quote=FALSE; } else if (symbols) { atom_info.type=SYMBOL_ATOM; atom_info.needs_quote=(lg==1 && *name=='.'); } else if (lg==1 && char_type[*name]==SC) { atom_info.type=SOLO_ATOM; atom_info.needs_quote=FALSE; } else { atom_info.type=OTHER_ATOM; atom_info.needs_quote=! (lg==2 && (name[0]=='[' && name[1]==']' || name[0]=='{' && name[1]=='}') ); } atom_info.length=lg; atom_info.g_elem.size=0; atom_info.g_elem.val =G_VAR_INITIAL_VALUE; atom=(AtomInf *) Hash_Lookup(atom_tbl,(char *) &atom_info,H_UPDATE); if ((int) atom == -1) Fatal_Error(ERR_ATOM_TBL_FULL); return atom; }
int main() { pthread_t thread[3]; counter_t counter; printf("Testing Counter...\n"); printf("Begin single thread test\n"); printf("Counter Init\n"); Counter_Init(&counter, 0); printf("Counter_GetValue\n"); int counter_val = Counter_GetValue(&counter); printf("expected 0 got %d\n", counter_val); printf("Counter_Increment\n"); Counter_Increment(&counter); counter_val = counter_val = Counter_GetValue(&counter); printf("expected 1 got %d\n", counter_val); printf("Counter_Decrement\n"); Counter_Decrement(&counter); counter_val = Counter_GetValue(&counter); printf("expected 0 got %d\n", counter_val); printf("Begin multithreaded test\n"); printf("Counter Init\n"); Counter_Init(&counter, 0); pthread_create(&thread[0], NULL, Counter_Increment, (void*)&counter); printf("Counter is at %d after 1 increment call\n", Counter_GetValue(&counter)); pthread_create(&thread[1], NULL, Counter_Increment, (void*)&counter); printf("Counter is at %d after 2 increment calls\n", Counter_GetValue(&counter)); pthread_create(&thread[2], NULL, Counter_Increment, (void*)&counter); printf("Counter is at %d after 3 increment calls\n", Counter_GetValue(&counter)); pthread_join(thread[0], NULL); pthread_join(thread[1], NULL); pthread_join(thread[2], NULL); printf("Count after 3 threads = %d\n", Counter_GetValue(&counter)); printf("Done Testing Counter\n\n"); list_insert_arg insert_arg[3]; list_lookup_arg lookup_arg[3]; list_t list; printf("Testing list...\n"); printf("Begin single-thread test\n"); printf("List_Init()\n"); List_Init(&list); printf("List_Insert()\n"); List_Insert(&list, (void *)0x1, 1); List_Insert(&list, (void *)0x2, 2); List_Insert(&list, (void *)0x3, 3); printf("Expected %p got %p\n", (void *)0x1, List_Lookup(&list, 1)); printf("Expected %p got %p\n", (void *)0x3, List_Lookup(&list, 3)); printf("Expected %p got %p\n", (void *)0x2, List_Lookup(&list, 2)); printf("List_Delete()\n"); List_Delete(&list, 1); printf("Expected %p got %p\n", (void *)0x2, List_Lookup(&list, 2)); printf("Expected %p got %p\n", (void *)0x3, List_Lookup(&list, 3)); printf("Expected %p got %p\n", NULL, List_Lookup(&list, 1)); printf("Single-thread test completed\n"); printf("Begin multi-thread test\n"); printf("List_Init()\n"); List_Init(&list); printf("Parallel List_Insert()\n"); insert_arg[0].list = &list; insert_arg[0].element = (void *)0x1; insert_arg[0].key = 1; pthread_create(&thread[0], NULL, pthread_list_insert, &insert_arg[0]); insert_arg[1].list = &list; insert_arg[1].element = (void *)0x2; insert_arg[1].key = 2; pthread_create(&thread[1], NULL, pthread_list_insert, &insert_arg[1]); insert_arg[2].list = &list; insert_arg[2].element = (void *)0x3; insert_arg[2].key = 3; pthread_create(&thread[2], NULL, pthread_list_insert, &insert_arg[2]); pthread_join(thread[0], NULL); pthread_join(thread[1], NULL); pthread_join(thread[2], NULL); printf("Parallel List_Insert() completed\n"); printf("Parallel List_Lookup()\n"); lookup_arg[0].list = &list; lookup_arg[0].key = 1; pthread_create(&thread[0], NULL, pthread_list_lookup, &lookup_arg[0]); lookup_arg[1].list = &list; lookup_arg[1].key = 2; pthread_create(&thread[1], NULL, pthread_list_lookup, &lookup_arg[1]); lookup_arg[2].list = &list; lookup_arg[2].key = 3; pthread_create(&thread[2], NULL, pthread_list_lookup, &lookup_arg[2]); pthread_join(thread[0], NULL); pthread_join(thread[1], NULL); pthread_join(thread[2], NULL); printf("Parallel List_Lookup() completed\n"); printf("List_Delete()\n"); List_Delete(&list, 1); printf("Expected %p got %p\n", (void *)0x2, List_Lookup(&list, 2)); printf("Expected %p got %p\n", (void *)0x3, List_Lookup(&list, 3)); printf("Expected %p got %p\n", NULL, List_Lookup(&list, 1)); printf("Multi-thread test completed\n"); printf("Done testing list\n\n"); hash_t hash; printf("Testing hash...\n"); printf("Begin single-thread test\n"); printf("Hash_Init()\n"); Hash_Init(&hash, 2); printf("Hash_Insert()\n"); Hash_Insert(&hash, (void *)0x1, 1); Hash_Insert(&hash, (void *)0x2, 2); Hash_Insert(&hash, (void *)0x3, 3); printf("Hash_Lookup()\n"); printf("Expected %p got %p\n", (void *)0x2, Hash_Lookup(&hash, 2)); printf("Expected %p got %p\n", (void *)0x3, Hash_Lookup(&hash, 3)); printf("Expected %p got %p\n", (void *)0x1, Hash_Lookup(&hash, 1)); printf("Hash_Delete()\n"); Hash_Delete(&hash, 1); printf("Hash_Lookup()\n"); printf("Expected %p got %p\n", (void *)0x2, Hash_Lookup(&hash, 2)); printf("Expected %p got %p\n", (void *)0x3, Hash_Lookup(&hash, 3)); printf("Expected %p got %p\n", NULL, Hash_Lookup(&hash, 1)); printf("Single-thread test completed\n"); printf("Done testing hash...\n"); return 0; }