void wake_child(int itsid, int broadcast_flag) { double starttime; pthread_mutex_lock(&child_mutex); while (child_waiting[itsid] == 0) { pthread_mutex_unlock(&child_mutex); sched_yield(); pthread_mutex_lock(&child_mutex); } pthread_mutex_unlock(&child_mutex); if (broadcast_flag) { starttime = d_gettimeofday(); if (pthread_cond_broadcast(&condlist[itsid]) != 0) { perror("pthread_cond_broadcast"); exit(-1); } } else { starttime = d_gettimeofday(); if (pthread_cond_signal(&condlist[itsid]) != 0) { perror("pthread_cond_signal"); exit(-1); } } for (;;) { pthread_mutex_lock(&child_mutex); if (child_waiting[itsid] == 2) { break; } pthread_mutex_unlock(&child_mutex); poll(NULL, 0, 10); } latency = (unsigned long)((endtime - starttime) * 1000000.); pthread_mutex_unlock(&child_mutex); }
void * childfunc(void *arg) { int myid = (intptr_t)arg; pthread_cond_t *cp; volatile int *cw; cp = &condlist[myid]; cw = &child_waiting[myid]; while (*cw == 0) { pthread_mutex_lock(&child_mutex); *cw = 1; if (pthread_cond_wait(cp, &child_mutex) != 0) { perror("pthread_cond_wait"); exit(-1); } endtime = d_gettimeofday(); *cw = 2; pthread_mutex_unlock(&child_mutex); while (*cw == 2) { poll(NULL, 0, 10); } } pthread_exit(NULL); }
void * childfunc(void *arg) { pthread_cond_t *cp = (pthread_cond_t *)arg; while (child_waiting == 0) { pthread_mutex_lock(&child_mutex); child_waiting = 1; if (pthread_cond_wait(cp, &child_mutex) != 0) { perror("pthread_cond_wait"); exit(-1); } endtime = d_gettimeofday(); child_waiting = 2; pthread_mutex_unlock(&child_mutex); while (child_waiting == 2) { poll(NULL, 0, 10); } } pthread_exit(NULL); }
void test_signal(int broadcast_flag, int iter) { pthread_attr_t attr; pthread_t childid; pthread_cond_t cond = PTHREAD_COND_INITIALIZER; int i; int prio; struct sched_param schparm; double starttime; prio = sched_get_priority_max(SCHED_FIFO); if (prio == -1) { perror("sched_get_priority_max"); exit(-1); } schparm.sched_priority = prio; if (sched_setscheduler(getpid(), SCHED_FIFO, &schparm) != 0) { perror("sched_setscheduler"); exit(-1); } if (pthread_attr_init(&attr) != 0) { perror("pthread_attr_init"); exit(-1); } if (pthread_attr_setschedpolicy(&attr, SCHED_FIFO) != 0) { perror("pthread_attr_setschedpolicy"); exit(-1); } if (pthread_attr_setschedparam(&attr, &schparm) != 0) { perror("pthread_attr_setschedparam"); exit(-1); } if (pthread_create(&childid, &attr, childfunc, (void *)&cond) != 0) { perror("pthread_create"); exit(-1); } for (i = 0; i < iter; i++) { pthread_mutex_lock(&child_mutex); child_waiting = 0; while (child_waiting == 0) { pthread_mutex_unlock(&child_mutex); sched_yield(); pthread_mutex_lock(&child_mutex); } pthread_mutex_unlock(&child_mutex); if (broadcast_flag) { starttime = d_gettimeofday(); if (pthread_cond_broadcast(&cond) != 0) { perror("pthread_cond_broadcast"); exit(-1); } } else { starttime = d_gettimeofday(); if (pthread_cond_signal(&cond) != 0) { perror("pthread_cond_signal"); exit(-1); } } for (;;) { pthread_mutex_lock(&child_mutex); if (child_waiting == 2) { break; } pthread_mutex_unlock(&child_mutex); poll(NULL, 0, 10); } printf("%s() latency: %d microseconds\n", (broadcast_flag ? "pthread_cond_broadcast" : "pthread_cond_signal"), (int)((endtime - starttime) * 1000000.)); pthread_mutex_unlock(&child_mutex); } pthread_mutex_lock(&child_mutex); child_waiting = 3; pthread_mutex_unlock(&child_mutex); if (pthread_join(childid, NULL) != 0) { perror("pthread_join"); exit(-1); } }
void* run(void* arg) { unsigned long i; char iamreader; int key; long t = (long) arg; long nbn;//same cache line for the best rand! double start, end; node_t **hd; lock_t *lck; set_affinity(t); /************ init *****************/ thd_ins = 0; thd_del = 0; thd_sea = 0; nbmallocs = 0; nbretry = 0; nbrelink = 0; nbreprev = 0; nbfl = 0; thread=t; thd_nbupdaters=nbupdaters; setsignals(); iamreader = t >= nbupdaters ? 1 : 0; if(!iamreader) prealloc(); mysrand(t^time(NULL)); /************** init done **************/ /************** barrier ****************/ atomic_xadd4(&ready, 1); while(!go) memory_barrier(); /******************* START ****************/ start = d_gettimeofday(); #ifdef RCU rcu_register_thread(); #endif i=0; do{ key = myrand()%nbkeys; //key = rand_r(&thd_seed)%nbthreads; //key = (t+key)%nbkeys; //key = random() % nbkeys; //if(i%100000) printf("%d %d\n", thread, key); get_buckets(key, &hd, &lck); if(iamreader) { thd_sea += search(key, hd, lck); if(i>= NB_TEST) break; } else { if(i%2) thd_ins += insert(key, hd, lck); else thd_del += delete(key, hd, lck); if(done) { //printf("writer stopped\n"); break; } } //if(!(i%10000)) //printf("%d loop %d\n", t, i); #ifdef RCU_QSBR #if ((defined RCU_QSBR_ACCELERATE) || (defined RCU_Q10)) #ifdef RCU_Q10 if(!(i%10)) #else if(!(i%100)) #endif #endif rcu_quiescent_state(); #endif }while(++i); #ifdef RCU //if(!iamreader) rcu_barrier(); //printf("iamreader %d, ops %d\n", iamreader, i); rcu_unregister_thread(); #endif end = d_gettimeofday(); /******************* END ****************/ //number of ops done thd_ops[t] = i; //printf("nbmallocs %g\n", nbmallocs); thd_mallocs[t] = nbmallocs; thd_retry[t] = nbretry; thd_relink[t] = nbrelink; thd_reprev[t] = nbreprev; //if(t==0) printf("%lu | nbblocked %g\n", t, nbblockeds); #ifdef RCU thd_blockeds[t] = atomic_read_ptr(&rcublockeds); #else thd_blockeds[t] = nbblockeds; #endif //average time per ops avg_time[t] = (((end - start))/i); //total time thd_time[t] = (end - start); suc_ins[t] = thd_ins; suc_sea[t] = thd_sea; suc_del[t] = thd_del; return NULL; }