searchengine/util/tsrc/cpixtoolsunittest/src/pooltests.cpp
changeset 0 671dee74050a
child 8 6547bf8ca13a
equal deleted inserted replaced
-1:000000000000 0:671dee74050a
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 #include <wchar.h>
       
    19 
       
    20 #include <pthread.h>
       
    21 #include <unistd.h>
       
    22 
       
    23 #include <string>
       
    24 #include <iostream>
       
    25 
       
    26 #include "itk.h"
       
    27 
       
    28 #include "cpixsyncpool.h"
       
    29 
       
    30 
       
    31 namespace
       
    32 {
       
    33 
       
    34 
       
    35     class TestPoolItemCounter
       
    36     {
       
    37     private:
       
    38 
       
    39         static TestPoolItemCounter  * instance_;
       
    40 
       
    41 
       
    42         size_t ctorInvocationCount_;
       
    43         size_t dtorInvocationCount_;
       
    44         size_t instanceCount_;
       
    45 
       
    46     public:
       
    47 
       
    48         static TestPoolItemCounter * instance()
       
    49         {
       
    50             if (instance_ == NULL)
       
    51                 {
       
    52                     instance_ = new TestPoolItemCounter;
       
    53                 }
       
    54 
       
    55             return instance_;
       
    56         }
       
    57 
       
    58 
       
    59         void ctor()
       
    60         {
       
    61             ++ctorInvocationCount_;
       
    62             ++instanceCount_;
       
    63         }
       
    64 
       
    65         void dtor()
       
    66         {
       
    67             ++dtorInvocationCount_;
       
    68             --instanceCount_;
       
    69         }
       
    70 
       
    71 
       
    72         void printStatus()
       
    73         {
       
    74             printf("TestPoolItem ctor %ld, dtor %ld, count %ld\n",
       
    75                    ctorInvocationCount_,
       
    76                    dtorInvocationCount_,
       
    77                    instanceCount_);
       
    78         }
       
    79 
       
    80         
       
    81         void reset()
       
    82         {
       
    83             ctorInvocationCount_ = 0;
       
    84             dtorInvocationCount_ = 0;
       
    85             instanceCount_ = 0;
       
    86         }
       
    87 
       
    88 
       
    89     private:
       
    90         TestPoolItemCounter()
       
    91             : ctorInvocationCount_(0),
       
    92               dtorInvocationCount_(0),
       
    93               instanceCount_(0)
       
    94         {
       
    95             ;
       
    96         }
       
    97     };
       
    98 
       
    99 
       
   100 
       
   101     TestPoolItemCounter * TestPoolItemCounter::instance_ = NULL;
       
   102 
       
   103 
       
   104     class TestPoolItem
       
   105     {
       
   106     public:
       
   107         TestPoolItem()
       
   108         {
       
   109             TestPoolItemCounter::instance()->ctor();
       
   110         }
       
   111 
       
   112 
       
   113         ~TestPoolItem()
       
   114         {
       
   115             TestPoolItemCounter::instance()->dtor();
       
   116         }
       
   117     };
       
   118 
       
   119 
       
   120     enum { ITEMCOUNT = 4 };
       
   121 
       
   122 
       
   123     void PrintStatus(TestPoolItem   ** items,
       
   124                      const char      * name = NULL)
       
   125     {
       
   126         TestPoolItemCounter::instance()->printStatus();
       
   127 
       
   128         int
       
   129             count = 0;
       
   130 
       
   131         for (int i = 0; i < ITEMCOUNT; ++i)
       
   132             {
       
   133                 if (*(items + i) != NULL)
       
   134                     {
       
   135                         ++count;
       
   136                     }
       
   137             }
       
   138         
       
   139 
       
   140         if (name == NULL)
       
   141             {
       
   142                 printf("Items in use: %d\n",
       
   143                        count);
       
   144             }
       
   145         else
       
   146             {
       
   147                 printf("Thread %s: items in use: %d\n",
       
   148                        name,
       
   149                        count);
       
   150             }
       
   151     }
       
   152 
       
   153 
       
   154     struct ThreadParam
       
   155     {
       
   156         const char                        * name_;
       
   157         Itk::TestMgr                      * testMgr_;
       
   158         Cpt::SyncPool<TestPoolItem>       * pool_;
       
   159         TestPoolItem                     ** items_;
       
   160     };
       
   161 
       
   162 
       
   163 
       
   164     void ItemsArrayDeleter(void * p)
       
   165     {
       
   166         /*TestPoolItem
       
   167             ** items = reinterpret_cast<TestPoolItem**>(p);
       
   168           */
       
   169         delete[] p;
       
   170     }
       
   171     
       
   172 
       
   173     pthread_key_t     NameKey;
       
   174     pthread_key_t     ItemsKey;
       
   175 
       
   176 
       
   177     void PrintStatus()
       
   178     {
       
   179         void
       
   180             * p = pthread_getspecific(ItemsKey);
       
   181 
       
   182         if (p == NULL)
       
   183             {
       
   184                 printf("Cannot get thread specific key: ItemsKey\n");
       
   185                 ITK_PANIC("ItemsKey");
       
   186             }
       
   187 
       
   188         TestPoolItem
       
   189             ** items = reinterpret_cast<TestPoolItem **>(p);
       
   190 
       
   191         p = pthread_getspecific(NameKey);
       
   192 
       
   193         const char
       
   194             * name = reinterpret_cast<const char*>(p);
       
   195         
       
   196         PrintStatus(items,
       
   197                     name);
       
   198 
       
   199         sleep(1);
       
   200     }
       
   201 
       
   202 
       
   203 
       
   204 } // namespace
       
   205 
       
   206 
       
   207 
       
   208 
       
   209 
       
   210 void testSingleThreadedUse(Itk::TestMgr * )
       
   211 {
       
   212     using namespace Cpt;
       
   213 
       
   214     TestPoolItemCounter::instance()->reset();
       
   215 
       
   216     static int 
       
   217         minPoolItemCount = 2;
       
   218 
       
   219     SyncPool<TestPoolItem>
       
   220         pool(minPoolItemCount);
       
   221 
       
   222     TestPoolItem
       
   223         * items[ITEMCOUNT];
       
   224     for (int i = 0; i < ITEMCOUNT; ++i)
       
   225         {
       
   226             items[i] = NULL;
       
   227         }
       
   228 
       
   229     items[0] = pool.acquire();
       
   230     PrintStatus(items);
       
   231 
       
   232     items[1] = pool.acquire();
       
   233     PrintStatus(items);
       
   234 
       
   235     pool.release(items[0]);
       
   236     items[0] = NULL;
       
   237     PrintStatus(items);
       
   238 
       
   239     items[2] = pool.acquire();
       
   240     PrintStatus(items);
       
   241 
       
   242     items[3] = pool.acquire();
       
   243     PrintStatus(items);
       
   244     
       
   245     items[0] = pool.acquire();
       
   246     PrintStatus(items);
       
   247 
       
   248     pool.release(items[1]);
       
   249     items[1] = NULL;
       
   250     PrintStatus(items);
       
   251     
       
   252     pool.release(items[0]);
       
   253     items[0] = NULL;
       
   254     PrintStatus(items);
       
   255 
       
   256     pool.release(items[3]);
       
   257     items[3] = NULL;
       
   258     PrintStatus(items);
       
   259 
       
   260     pool.release(items[2]);
       
   261     items[2] = NULL;
       
   262     PrintStatus(items);
       
   263 }
       
   264 
       
   265 
       
   266 
       
   267 void * ThreadFunc(void * param)
       
   268 {
       
   269     ThreadParam
       
   270         * p = reinterpret_cast<ThreadParam*>(param);
       
   271 
       
   272     int
       
   273         result = pthread_setspecific(NameKey,
       
   274                                      p->name_);
       
   275     if (result != 0)
       
   276         {
       
   277             printf("Could not set thread specific NameKey\n");
       
   278             ITK_PANIC("NameKey setting");
       
   279         }
       
   280 
       
   281     result = pthread_setspecific(ItemsKey,
       
   282                                  p->items_);
       
   283 
       
   284     if (result != 0)
       
   285         {
       
   286             printf("Could not set thread specific ItemsKey\n");
       
   287             ITK_PANIC("ItemsKey setting");
       
   288         }
       
   289 
       
   290     Cpt::SyncPool<TestPoolItem>
       
   291         & pool = *p->pool_;;
       
   292 
       
   293     TestPoolItem
       
   294         ** items = p->items_;
       
   295     for (int i = 0; i < ITEMCOUNT; ++i)
       
   296         {
       
   297             items[i] = NULL;
       
   298         }
       
   299 
       
   300     items[0] = pool.acquire();
       
   301     PrintStatus();
       
   302 
       
   303     items[1] = pool.acquire();
       
   304     PrintStatus();
       
   305 
       
   306     pool.release(items[0]);
       
   307     items[0] = NULL;
       
   308     PrintStatus();
       
   309 
       
   310     items[2] = pool.acquire();
       
   311     PrintStatus();
       
   312 
       
   313     items[3] = pool.acquire();
       
   314     PrintStatus();
       
   315     
       
   316     items[0] = pool.acquire();
       
   317     PrintStatus();
       
   318 
       
   319     pool.release(items[1]);
       
   320     items[1] = NULL;
       
   321     PrintStatus();
       
   322     
       
   323     pool.release(items[0]);
       
   324     items[0] = NULL;
       
   325     PrintStatus();
       
   326 
       
   327     pool.release(items[3]);
       
   328     items[3] = NULL;
       
   329     PrintStatus();
       
   330 
       
   331     pool.release(items[2]);
       
   332     items[2] = NULL;
       
   333     PrintStatus();
       
   334 
       
   335     printf("Thread %s is DONE.\n",
       
   336            p->name_);
       
   337 
       
   338     return NULL;
       
   339 }
       
   340 
       
   341 
       
   342 
       
   343 
       
   344 void testMultiThreadedUse(Itk::TestMgr * testMgr)
       
   345 {
       
   346     TestPoolItemCounter::instance()->reset();
       
   347 
       
   348     int
       
   349         result = pthread_key_create(&NameKey,
       
   350                                     NULL);
       
   351     ITK_ASSERT(testMgr,
       
   352                result == 0,
       
   353                "Could not create thread specific key: NameKey");
       
   354 
       
   355     result = pthread_key_create(&ItemsKey,
       
   356                                 ItemsArrayDeleter);
       
   357     ITK_ASSERT(testMgr,
       
   358                result == 0,
       
   359                "Could not create thread specific key: ItemsKey");
       
   360     
       
   361     static int 
       
   362         minPoolItemCount = 2;
       
   363 
       
   364     Cpt::SyncPool<TestPoolItem>
       
   365         pool(minPoolItemCount);
       
   366 
       
   367     TestPoolItem
       
   368         ** tpi1 = new TestPoolItem*[ITEMCOUNT],
       
   369         ** tpi2 = new TestPoolItem*[ITEMCOUNT];
       
   370 
       
   371     ThreadParam threadParams[2] = {
       
   372         {
       
   373             "main",
       
   374             testMgr,
       
   375             &pool,
       
   376             tpi1
       
   377         },
       
   378 
       
   379         {
       
   380             "extra",
       
   381             testMgr,
       
   382             &pool,
       
   383             tpi2
       
   384         }
       
   385     };
       
   386 
       
   387     pthread_t
       
   388         extra;
       
   389 
       
   390     result = pthread_create(&extra,
       
   391                             NULL,
       
   392                             &ThreadFunc,
       
   393                             threadParams + 1);
       
   394 
       
   395     ITK_ASSERT(testMgr,
       
   396                result == 0,
       
   397                "Could not create extra thread");
       
   398     
       
   399     ThreadFunc(threadParams + 0);
       
   400 
       
   401     void
       
   402         * retVal = NULL;
       
   403 
       
   404     printf("main: Joining extra\n");
       
   405 
       
   406     result = pthread_join(extra,
       
   407                           &retVal);
       
   408 
       
   409     ITK_ASSERT(testMgr,
       
   410                result == 0,
       
   411                "Could not join extra thread");
       
   412 
       
   413     printf("main: joined extra\n");
       
   414 }
       
   415 
       
   416 
       
   417 
       
   418 
       
   419 Itk::TesterBase * CreatePoolTests()
       
   420 {
       
   421     using namespace Itk;
       
   422 
       
   423     SuiteTester
       
   424         * poolTests = new SuiteTester("pool");
       
   425 
       
   426 #define TEST "singleThreadedUse"
       
   427     poolTests->add(TEST,
       
   428                    testSingleThreadedUse,
       
   429                    TEST);
       
   430 #undef TEST
       
   431 
       
   432 
       
   433 #define TEST "multiThreadedUse"
       
   434     poolTests->add(TEST,
       
   435                    testMultiThreadedUse,
       
   436                    TEST);
       
   437 #undef TEST
       
   438 
       
   439 
       
   440     // ... add more tests to suite
       
   441     
       
   442     return poolTests;
       
   443 }