/* * Written 2008 by * This code is in the public domain * See http://www.mgix.com/snippets/?LockFree for details * * Load test for the lock-free LIFO stack. */ #include #include #include #include #include #include #include #include #include // Code control static const int nbThreads = 7; static const double maxSeconds = 30.0; static const int maxLoops = 500 * 1000 * 1000; // Shared state static int32_t volatile threadCounts = 0; static int32_t volatile *histo = 0; static Lifo lifo; struct Node:public Lifo::Node { uint32_t id; Node(uint32_t i) : id(i) {} }; #define CHCK(x) \ if(false==(x)) \ { \ printf( \ "%s:%d Assertion %s fails\n", \ __FILE__, \ __LINE__, \ #x \ ); \ exit(1); \ } \ static void *stress( void *seed ) { Node *head = 0; int32_t ownedNodes = 0; intptr_t tId = (intptr_t)seed; printf( " thread %2d started ...\n", (int)tId); int32_t failedPops = 0; int32_t attemptedPops = 0; int32_t successfulPops = 0; int32_t failedPushes = 0; int32_t attemptedPushes = 0; int32_t successfulPushes = 0; int i=0; RNG rng(tId); bool useTry = 0==(tId%2); double nano0 = Timer::nano(); while(inext = head; ++ownedNodes; head = node; } } else { Node *node = head; if(node!=0) { head = (Node*)node->next; if(false==useTry) lifo.push(node); else { while(1) { ++attemptedPushes; if(lifo.tryPush(node)) break; ++failedPushes; } } ++successfulPushes; --ownedNodes; } } if(0==(i&0xFFFF)) { double nano = Timer::nano()-nano0; if((maxSeconds*1e9)next; atomicAdd32(head->id + histo, 1); head = next; } return 0; } int main( int , const char *[] ) { printf("\n"); // See what platform we're running on #if defined(__i386__) printf("plaftorm: i386\n"); #elif defined(__x86_64__) printf("plaftorm: x86-64\n"); #else #error "????" #endif printf("\n"); // Push nodes in the LIFO const int n = 10000; Node **nodes = new Node*[n]; printf("pushing %d nodes into shared LIFO ... ", n); for(int i=0; inext; ++(histo[p->id]); p = next; ++count; } printf("done\n"); // Check that all nodes are still here and here exactly once printf("shared LIFO still contains %d nodes\n", count); printf("verifying histogram ... all entries should be exactly 1 ..."); for(int i=0; i