void run_test(const char *test, pthread_handler handler, int n, int m) { pthread_t *threads; ulonglong now= my_getsystime(); int i; thread_number= timeouts= 0; litmus= 0; threads= (pthread_t *)my_malloc(sizeof(void *)*n, MYF(0)); if (!threads) { diag("Out of memory"); abort(); } diag("Running %s with %d threads, %d iterations... ", test, n, m); rt_num_threads= n; for (i= 0; i < n ; i++) if (pthread_create(threads+i, 0, handler, &m)) { diag("Could not create thread"); abort(); } for (i= 0 ; i < n ; i++) pthread_join(threads[i], 0); now= my_getsystime()-now; ok(litmus == 0, "Finished %s in %g secs (%d)", test, ((double)now)/1e7, litmus); my_free((void*)threads, MYF(0)); }
static DWORD get_milliseconds(const struct timespec *abstime) { #ifndef HAVE_STRUCT_TIMESPEC long long millis; union ft64 now; if (abstime == NULL) return INFINITE; GetSystemTimeAsFileTime(&now.ft); /* Calculate time left to abstime - subtract start time from current time(values are in 100ns units) - convert to millisec by dividing with 10000 */ millis= (abstime->tv.i64 - now.i64) / 10000; /* Don't allow the timeout to be negative */ if (millis < 0) return 0; /* Make sure the calculated timeout does not exceed original timeout value which could cause "wait for ever" if system time changes */ if (millis > abstime->max_timeout_msec) millis= abstime->max_timeout_msec; if (millis > UINT_MAX) millis= UINT_MAX; return (DWORD)millis; #else /* Convert timespec to millis and subtract current time. my_getsystime() returns time in 100 ns units. */ if (abstime == NULL) return INFINITE; ulonglong future= abstime->tv_sec * 1000 + abstime->tv_nsec / 1000000; ulonglong now= my_getsystime() / 10000; /* Don't allow the timeout to be negative. */ if (future < now) return 0; return (DWORD)(future - now); #endif }
void test_concurrently(const char *test, pthread_handler handler, int n, int m) { pthread_t t; ulonglong now= my_getsystime(); bad= 0; diag("Testing %s with %d threads, %d iterations... ", test, n, m); for (running_threads= n ; n ; n--) { if (pthread_create(&t, &thr_attr, handler, &m) != 0) { diag("Could not create thread"); abort(); } } pthread_mutex_lock(&mutex); while (running_threads) pthread_cond_wait(&cond, &mutex); pthread_mutex_unlock(&mutex); now= my_getsystime()-now; ok(!bad, "tested %s in %g secs (%d)", test, ((double)now)/1e7, bad); }
int main(int argc __attribute__((unused)), char **argv) { int i; MY_INIT(argv[0]); my_init(); pthread_mutex_init(&rt_mutex, 0); plan(40); if (my_atomic_initialize()) return exit_status(); tablockman_init(&tablockman, &loid2lo1, 50); for (i= 0; i < Nlos; i++) { pthread_mutex_init(&mutexes[i], MY_MUTEX_INIT_FAST); pthread_cond_init (&conds[i], 0); loarray1[i].active_locks= 0; loarray1[i].waiting_lock= 0; loarray1[i].waiting_for= 0; loarray1[i].mutex= &mutexes[i]; loarray1[i].cond= &conds[i]; loarray1[i].loid= i+1; } for (i= 0; i < Ntbls; i++) { tablockman_init_locked_table(ltarray+i, Nlos); } test_tablockman_simple(); #define CYCLES 10000 #define THREADS Nlos /* don't change this line */ /* mixed load, stress-test with random locks */ Nrows= 100; Ntables= 10; table_lock_ratio= 10; run_test("\"random lock\" stress test", test_lockman, THREADS, CYCLES); #if 0 /* "real-life" simulation - many rows, no table locks */ Nrows= 1000000; Ntables= 10; table_lock_ratio= 0; run_test("\"real-life\" simulation test", test_lockman, THREADS, CYCLES*10); #endif for (i= 0; i < Nlos; i++) { tablockman_release_locks(&tablockman, &loarray1[i]); pthread_mutex_destroy(loarray1[i].mutex); pthread_cond_destroy(loarray1[i].cond); } { ulonglong now= my_getsystime(); for (i= 0; i < Ntbls; i++) { tablockman_destroy_locked_table(ltarray+i); } tablockman_destroy(&tablockman); now= my_getsystime()-now; diag("lockman_destroy: %g secs", ((double)now)/1e7); } pthread_mutex_destroy(&rt_mutex); my_end(0); return exit_status(); }