From ab81b661aa815cc67b22506f17380dcbdb0a89a0 Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Thu, 11 Aug 2011 02:59:20 +0000 Subject: Add more complete test of multi-threaded worker, work queue and thread local storage. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1156460 13f79535-47bb-0310-9956-ffa450edef68 --- sca-cpp/trunk/kernel/parallel-test.cpp | 96 +++++++++++++++++++++++++++++++--- 1 file changed, 89 insertions(+), 7 deletions(-) (limited to 'sca-cpp/trunk/kernel') diff --git a/sca-cpp/trunk/kernel/parallel-test.cpp b/sca-cpp/trunk/kernel/parallel-test.cpp index 2969dd0637..ab832f1472 100644 --- a/sca-cpp/trunk/kernel/parallel-test.cpp +++ b/sca-cpp/trunk/kernel/parallel-test.cpp @@ -116,19 +116,80 @@ const int mtsquare(const int x) { return x * x; } -bool checkResults(const list > r, int i) { +const list > submitSquares(worker& w, const int max, const int i) { + if (i == max) + return list >(); + const lambda func = curry(lambda (mtsquare), i); + return cons(submit(w, func), submitSquares(w, max, i + 1)); +} + +bool checkSquareResults(const list > r, int i) { if (isNil(r)) return true; assert(car(r) == i * i); - checkResults(cdr(r), i + 1); + checkSquareResults(cdr(r), i + 1); return true; } -const list > submitSquares(worker& w, const int max, const int i) { +__thread long int tlsv = 0; + +const long int tlsset(gc_ptr> wq, gc_ptr> xq) { + const long int v = tlsv; + tlsv = threadId(); + enqueue(*xq, true); + dequeue(*wq); + return v; +} + +const bool tlscheck(gc_ptr> wq, gc_ptr> xq) { + const bool r = tlsv == threadId(); + enqueue(*xq, true); + dequeue(*wq); + return r; +} + +const bool waitForWorkers(wqueue& xq, const int n) { + if (n == 0) + return true; + dequeue(xq); + return waitForWorkers(xq, n - 1); +} + +const bool unblockWorkers(wqueue& wq, const int n) { + if (n == 0) + return true; + enqueue(wq, true); + return unblockWorkers(wq, n - 1); +} + +const list > submitTLSSets(worker& w, wqueue& wq, wqueue& xq, const int max, const int i) { if (i == max) - return list >(); - const lambda func = curry(lambda (mtsquare), i); - return cons(submit(w, func), submitSquares(w, max, i + 1)); + return list >(); + const lambda func = curry(lambda>, gc_ptr>)>(tlsset), (gc_ptr>)&wq, (gc_ptr>)&xq); + return cons(submit(w, func), submitTLSSets(w, wq, xq, max, i + 1)); +} + +bool checkTLSSets(const list > s) { + if (isNil(s)) + return true; + assert(car(s) == 0); + checkTLSSets(cdr(s)); + return true; +} + +const list > submitTLSChecks(worker& w, wqueue& wq, wqueue& xq, const int max, const int i) { + if (i == max) + return list >(); + const lambda func = curry(lambda>, gc_ptr>)>(tlscheck), (gc_ptr>)&wq, (gc_ptr>)&xq); + return cons(submit(w, func), submitTLSChecks(w, wq, xq, max, i + 1)); +} + +bool checkTLSResults(const list > r) { + if (isNil(r)) + return true; + assert(car(r) == true); + checkTLSResults(cdr(r)); + return true; } bool testWorker() { @@ -140,7 +201,28 @@ bool testWorker() { { const int max = 20; const list > r(submitSquares(w, max, 0)); - checkResults(r, 0); + checkSquareResults(r, 0); + } + { + const int max = 20; + wqueue wq(max); + unblockWorkers(wq, max); + waitForWorkers(wq, max); + unblockWorkers(wq, max); + waitForWorkers(wq, max); + } + { + const int max = 20; + wqueue wq(max); + wqueue xq(max); + const list > s(submitTLSSets(w, wq, xq, max, 0)); + waitForWorkers(xq, max); + unblockWorkers(wq, max); + checkTLSSets(s); + const list > r(submitTLSChecks(w, wq, xq, max, 0)); + waitForWorkers(xq, max); + unblockWorkers(wq, max); + checkTLSResults(r); } shutdown(w); return true; -- cgit v1.2.3