int main () { float time = 0; int valid = 1; FILE *f = fopen ("asynclocklessresultsN", "a"); int n = 100; N = 0; while ((N+=1000) < 100000) { int i = 0; time = 0; while (i++ < 10){ clock_t t1, t2; Data data; SynchronisingThread *mainThread; Worker **workers; data.value = 0; t1 = clock (); mainThread = synchronising_thread_new (n, &data); int a = synchronising_thread_start (mainThread); if (a) { printf ("Error creating Main Thread"); } workers = malloc (sizeof (Worker *) *n); int i; for (i = 0; i < n; i++) { workers[i] = worker_new (mainThread, i, worker_thread_func); int ans = worker_start (workers[i]); if (ans) { printf ("Error creating thread %d", i); } } for (i = 0; i < n; i++) { worker_join (workers[i]); free (workers[i]); } free (workers); synchronising_thread_exit (mainThread, 1); synchronising_thread_join (mainThread); synchronising_thread_free (mainThread); //free (mainThread); t2 = clock (); printf ("%d\n", data.value); time += (((float)t2 - (float)t1) / 1000000.0 ) * 1000; } printf ("Timeout: %f for N: %d\n", time/i, N); fprintf (f, "Timeout: %f for N: %d\n", time/i, N); fflush (f); } fclose (f); return 0; }
int main(void) { long i = 0; worker_pool pool1, pool2; puts("Case 1 : 10 actions (5s) + worker_join"); puts("---------------------------------------"); worker_init(&pool1); for(; i < ACTION_NB; i++) { action_to_do[i].perform = my_action; action_to_do[i].args = (void*) i; worker_add(&pool1, &action_to_do[i]); printf("Action #%li added\n", i); } worker_join(&pool1); puts("All actions terminated"); worker_quit(&pool1); puts("---------------------------------------"); puts("Case 2 : 10 actions (5s) + worker_quit after 1s"); puts("---------------------------------------"); worker_init(&pool2); for(i = 0; i < ACTION_NB; i++) { action_to_do[i].perform = my_action; action_to_do[i].args = (void*) i; worker_add(&pool2, &action_to_do[i]); printf("Action #%li added\n", i); } worker_quit(&pool2); puts("All actions canceled"); puts("---------------------------------------"); return 0; }
static int run_paxos(int duelling_proposers) { int i; struct timespec ts; if (sem_init(&g_sem_accept_leader, 0, 0)) abort(); if (pthread_mutex_init(&g_start_lock, 0)) abort(); if (pthread_cond_init(&g_start_cond, 0)) abort(); g_start = 0; memset(g_nodes, 0, sizeof(g_nodes)); memset(g_node_data, 0, sizeof(g_node_data)); for (i = 0; i < g_num_nodes; ++i) { char name[WORKER_NAME_MAX]; snprintf(name, WORKER_NAME_MAX, "node_%3d", i); g_node_data[i].id = i; g_node_data[i].state = NODE_STATE_INIT; reset_remotes(g_node_data[i].remotes); g_node_data[i].seen_pseq = 0; g_node_data[i].prop_pseq = 0; g_node_data[i].prop_leader = -1; g_node_data[i].leader = -1; g_nodes[i] = worker_start(name, paxos_handle_msg, NULL, &g_node_data[i]); if (!g_nodes[i]) { fprintf(stderr, "failed to allocate node %d\n", i); abort(); } } if (g_num_nodes < 3) abort(); send_do_propose(2); if (duelling_proposers) { send_do_propose(0); send_do_propose(1); } /* start acceptors */ pthread_mutex_lock(&g_start_lock); g_start = 1; pthread_cond_broadcast(&g_start_cond); pthread_mutex_unlock(&g_start_lock); /* Wait for consensus. * We only actually need more than half the nodes. However, to make * debugging a little nicer, we'll wait 10 seconds for all the remaining * nodes rather than exiting immediately after we get half. */ for (i = 0; i < 1 + (g_num_nodes / 2); ++i) { TEMP_FAILURE_RETRY(sem_wait(&g_sem_accept_leader)); } if (clock_gettime(CLOCK_REALTIME, &ts) == -1) abort(); ts.tv_sec += 10; for (; i < g_num_nodes; ++i) { TEMP_FAILURE_RETRY(sem_timedwait(&g_sem_accept_leader, &ts)); } /* cleanup */ for (i = 0; i < g_num_nodes; ++i) { worker_stop(g_nodes[i]); } for (i = 0; i < g_num_nodes; ++i) { worker_join(g_nodes[i]); } pthread_cond_destroy(&g_start_cond); g_start = 0; pthread_mutex_destroy(&g_start_lock); sem_destroy(&g_sem_accept_leader); return check_leaders(); }