#include #include "locks.hfa" #include #include #include static unsigned int taskCount = 4; static unsigned int threadCount = 2; static unsigned int lockCount = 1; static unsigned int total_times = 320000; static unsigned int num_times; static const int workBufferSize = 16; static unsigned int work_unlocked = 10000; static unsigned int work_locked = 10000; // taken from martin's thread_test static inline void dowork(volatile int* buffer, unsigned int steps) { int value = 0; for (unsigned int i = 0; i < steps; i += 1) { // a little more work than just a single memory access helps with stability value += (buffer[i % workBufferSize] * 17) / 23 + 55; } buffer[0] += value; } thread worker { linear_backoff_then_block_lock * locks; bool improved; }; void ?{}( worker & w, linear_backoff_then_block_lock * locks, bool improved ) { w.locks = locks; w.improved = improved; } void main( worker & this ) with(this) { int buffer[workBufferSize]; for (int i = 0; i < workBufferSize; i += 1) buffer[i] = rand() % 1024; unsigned int lck = rand() % lockCount; linear_backoff_then_block_lock * curr_lock = &locks[lck]; for (unsigned int i = 0; i < num_times; i++) { dowork(buffer, work_unlocked); if (improved) lock_improved(*curr_lock); else lock(*curr_lock); dowork(buffer, work_locked); unlock(*curr_lock); lck = rand() % lockCount; curr_lock = &locks[lck]; } } int main(int argc, char* argv[]) { switch (argc) { case 7: work_unlocked = atoi(argv[5]); case 6: work_locked = atoi(argv[5]); case 5: num_times = atoi(argv[4]); case 4: lockCount = atoi(argv[3]); case 3: threadCount = atoi(argv[2]); case 2: taskCount = atoi(argv[1]); case 1: break; default: break; } processor p[threadCount]; linear_backoff_then_block_lock locks[lockCount]; worker * worker_arr[taskCount]; num_times = total_times / taskCount; //printf("Start Test: martin lock simple\n"); clock_t begin = clock(); for (unsigned int i = 0; i < taskCount; i++) { worker_arr[i] = new( locks, false ); } for (unsigned int i = 0; i < taskCount; i++) { delete( worker_arr[i] ); } clock_t end = clock(); double time_spent = (double)(end - begin) / CLOCKS_PER_SEC; printf("norm: %f\n", time_spent); //printf("Start Test: martin lock improved\n"); begin = clock(); for (unsigned int i = 0; i < taskCount; i++) { worker_arr[i] = new( locks, true ); } for (unsigned int i = 0; i < taskCount; i++) { delete( worker_arr[i] ); } end = clock(); time_spent = (double)(end - begin) / CLOCKS_PER_SEC; printf("improved: %f\n", time_spent); }