Back to index

lightning-sunbird  0.9+nobinonly
tpd.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039 ** File:        tpd.cpp
00040 ** Description: Exercising the thread private data bailywick.
00041 */
00042 
00043 #include "prlog.h"
00044 #include "prprf.h"
00045 #include "rcthread.h"
00046 
00047 #include <string.h>
00048 
00049 #include "plgetopt.h"
00050 
00051 /*
00052 ** class MyThread
00053 */
00054 class MyThread: public RCThread
00055 {
00056 public:
00057     MyThread();
00058 
00059 private:
00060     ~MyThread();
00061     void RootFunction();
00062 };  /* MyThread */
00063 
00064 /*
00065 ** class MyPrivateData
00066 */
00067 class MyPrivateData: public RCThreadPrivateData
00068 {
00069 public:
00070     virtual ~MyPrivateData();
00071 
00072     MyPrivateData();
00073     MyPrivateData(char*);
00074     MyPrivateData(const MyPrivateData&);
00075 
00076     void Release();
00077 
00078 private:
00079     char *string;
00080 };  /* MyPrivateData */
00081 
00082 static PRUintn key[128];
00083 static PRIntn debug = 0;
00084 static PRBool failed = PR_FALSE;
00085 static PRBool should = PR_TRUE;
00086 static PRBool did = PR_TRUE;
00087 static PRFileDesc *fout = NULL;
00088 
00089 static void PrintProgress(PRIntn line)
00090 {
00091     failed = failed || (should && !did);
00092     failed = failed || (!should && did);
00093     if (debug > 0)
00094     {
00095         PR_fprintf(
00096             fout, "@ line %d destructor should %shave been called and was%s\n",
00097             line, ((should) ? "" : "NOT "), ((did) ? "" : " NOT"));
00098     }
00099 }  /* PrintProgress */
00100 
00101 static void MyAssert(const char *expr, const char *file, PRIntn line)
00102 {
00103     if (debug > 0)
00104         (void)PR_fprintf(fout, "'%s' in file: %s: %d\n", expr, file, line);
00105 }  /* MyAssert */
00106 
00107 #define MY_ASSERT(_expr) \
00108     ((_expr)?((void)0):MyAssert(# _expr,__FILE__,__LINE__))
00109 
00110 int main(PRIntn argc, char *argv[])
00111 {
00112     PRStatus rv;
00113     PRUintn keys;
00114     MyThread *thread;
00115     const RCThreadPrivateData *pd;
00116     PLOptStatus os;
00117     PLOptState *opt = PL_CreateOptState(argc, argv, "d");
00118     RCThread *primordial = RCThread::WrapPrimordialThread();
00119     while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
00120     {
00121         if (PL_OPT_BAD == os) continue;
00122         switch (opt->option)
00123         {
00124         case 'd':  /* debug mode */
00125             debug = PR_TRUE;
00126             break;
00127          default:
00128             break;
00129         }
00130     }
00131     PL_DestroyOptState(opt);
00132 
00133     fout = PR_STDOUT;
00134 
00135     MyPrivateData extension = MyPrivateData("EXTENSION");
00136     MyPrivateData key_string[] = {
00137         "Key #0", "Key #1", "Key #2", "Key #3",
00138         "Bogus #5", "Bogus #6", "Bogus #7", "Bogus #8"};
00139     
00140 
00141     did = should = PR_FALSE;
00142     for (keys = 0; keys < 4; ++keys)
00143     {
00144         rv = RCThread::NewPrivateIndex(&key[keys]);
00145         key[keys + 4] = key[keys] + 4;
00146         MY_ASSERT(PR_SUCCESS == rv);
00147     }
00148     PrintProgress(__LINE__);
00149 
00150     /* the first four should be bu null, the last four undefined and null */
00151     did = should = PR_FALSE;
00152     for (keys = 0; keys < 8; ++keys)
00153     {
00154         pd = RCThread::GetPrivateData(key[keys]);
00155         MY_ASSERT(NULL == pd);
00156     }
00157     PrintProgress(__LINE__);
00158 
00159     /* initially set private data for new keys */
00160     did = should = PR_FALSE;
00161     for (keys = 0; keys < 4; ++keys)
00162     {
00163         rv = RCThread::SetPrivateData(key[keys], &key_string[keys]);
00164         MY_ASSERT(PR_SUCCESS == rv);
00165     }
00166     PrintProgress(__LINE__);
00167 
00168     /* re-assign the private data, albeit the same content */    
00169     did = PR_FALSE; should = PR_TRUE;
00170     for (keys = 0; keys < 4; ++keys)
00171     {
00172         pd = RCThread::GetPrivateData(key[keys]);
00173         PR_ASSERT(NULL != pd);
00174         rv = RCThread::SetPrivateData(key[keys], &key_string[keys]);
00175         MY_ASSERT(PR_SUCCESS == rv);
00176     }
00177     PrintProgress(__LINE__);
00178 
00179     /* set private to <empty> */
00180     did = PR_FALSE; should = PR_TRUE;
00181     for (keys = 0; keys < 4; ++keys)
00182     {
00183         rv = RCThread::SetPrivateData(key[keys]);
00184         MY_ASSERT(PR_SUCCESS == rv);
00185     }
00186     PrintProgress(__LINE__);
00187 
00188     /* should all be null now */
00189     did = should = PR_FALSE;
00190     for (keys = 0; keys < 4; ++keys)
00191     {
00192         pd = RCThread::GetPrivateData(key[keys]);
00193         PR_ASSERT(NULL == pd);
00194     }
00195     PrintProgress(__LINE__);
00196 
00197     /* allocate another batch of keys and assign data to them */
00198     did = should = PR_FALSE;
00199     for (keys = 8; keys < 127; ++keys)
00200     {
00201         rv = RCThread::NewPrivateIndex(&key[keys]);
00202         MY_ASSERT(PR_SUCCESS == rv);
00203         rv = RCThread::SetPrivateData(key[keys], &extension);
00204         MY_ASSERT(PR_SUCCESS == rv);
00205     }
00206     PrintProgress(__LINE__);
00207 
00208     /* set all the extended slots to <empty> */
00209     did = PR_FALSE; should = PR_TRUE;
00210     for (keys = 8; keys < 127; ++keys)
00211     {
00212         rv = RCThread::SetPrivateData(key[keys]);
00213         MY_ASSERT(PR_SUCCESS == rv);
00214     }
00215     PrintProgress(__LINE__);
00216 
00217     /* set all the extended slots to <empty> again (noop) */
00218     did = should = PR_FALSE;
00219     for (keys = 8; keys < 127; ++keys)
00220     {
00221         rv = RCThread::SetPrivateData(key[keys]);
00222         MY_ASSERT(PR_SUCCESS == rv);
00223     }
00224 
00225     if (debug) PR_fprintf(fout, "Creating thread\n");
00226     thread = new MyThread();
00227     if (debug) PR_fprintf(fout, "Starting thread\n");
00228     thread->Start();
00229     if (debug) PR_fprintf(fout, "Joining thread\n");
00230     (void)thread->Join();
00231     if (debug) PR_fprintf(fout, "Joined thread\n");
00232 
00233     failed |= (PR_FAILURE == RCPrimordialThread::Cleanup());
00234 
00235     (void)PR_fprintf(
00236         fout, "%s\n",((PR_TRUE == failed) ? "FAILED" : "PASSED"));
00237 
00238     return (failed) ? 1 : 0;
00239 
00240 }  /* main */
00241 
00242 /*
00243 ** class MyPrivateData
00244 */
00245 MyPrivateData::~MyPrivateData()
00246 {
00247     PR_fprintf(
00248         fout, "MyPrivateData::~MyPrivateData[%s]\n",
00249         (NULL != string) ? string : "NULL");
00250 }  /* MyPrivateData::~MyPrivateData */
00251 
00252 MyPrivateData::MyPrivateData(): RCThreadPrivateData()
00253 {
00254     PR_fprintf(fout, "MyPrivateData::MyPrivateData()\n");
00255     string = NULL;
00256 }  /* MyPrivateData::MyPrivateData */
00257 
00258 MyPrivateData::MyPrivateData(char* data): RCThreadPrivateData()
00259 {
00260     PR_fprintf(fout, "MyPrivateData::MyPrivateData(char* data)\n");
00261     string = data;
00262 }  /* MyPrivateData:: MyPrivateData */
00263 
00264 MyPrivateData::MyPrivateData(const MyPrivateData& him): RCThreadPrivateData(him)
00265 {
00266     PR_fprintf(fout, "MyPrivateData::MyPrivateData(const MyPrivateData& him)\n");
00267     string = him.string;
00268 }  /* MyPrivateData:: MyPrivateData */
00269 
00270 void MyPrivateData::Release()
00271 {
00272     if (should) did = PR_TRUE;
00273     else failed = PR_TRUE;
00274 }  /* MyPrivateData::operator= */
00275 
00276 /*
00277 ** class MyThread
00278 */
00279 MyThread::~MyThread() { }
00280 MyThread::MyThread(): RCThread(RCThread::global, RCThread::joinable) { }
00281 
00282 
00283 void MyThread::RootFunction()
00284 {
00285     PRStatus rv;
00286     PRUintn keys;
00287     const RCThreadPrivateData *pd;
00288     
00289     MyPrivateData extension = MyPrivateData("EXTENSION");
00290     MyPrivateData key_string[] = {
00291         "Key #0", "Key #1", "Key #2", "Key #3",
00292         "Bogus #5", "Bogus #6", "Bogus #7", "Bogus #8"};
00293     
00294     did = should = PR_FALSE;
00295     for (keys = 0; keys < 8; ++keys)
00296     {
00297         pd = GetPrivateData(key[keys]);
00298         MY_ASSERT(NULL == pd);
00299     }
00300     PrintProgress(__LINE__);
00301 
00302     did = should = PR_FALSE;
00303     for (keys = 0; keys < 4; ++keys)
00304     {
00305         rv = SetPrivateData(keys, &key_string[keys]);
00306         MY_ASSERT(PR_SUCCESS == rv);
00307     }
00308     PrintProgress(__LINE__);
00309 
00310 #if !defined(DEBUG)
00311     did = should = PR_FALSE;
00312     for (keys = 4; keys < 8; ++keys)
00313     {
00314         rv = SetPrivateData(keys, &key_string[keys]);
00315         MY_ASSERT(PR_FAILURE == rv);
00316     }
00317     PrintProgress(__LINE__);
00318 #endif
00319     
00320     did = PR_FALSE; should = PR_TRUE;
00321     for (keys = 0; keys < 4; ++keys)
00322     {
00323         rv = SetPrivateData(key[keys], &key_string[keys]);
00324         MY_ASSERT(PR_SUCCESS == rv);
00325     }
00326     PrintProgress(__LINE__);
00327 
00328     did = PR_FALSE; should = PR_TRUE;
00329     for (keys = 0; keys < 4; ++keys)
00330     {
00331         rv = SetPrivateData(key[keys]);
00332         MY_ASSERT(PR_SUCCESS == rv);
00333     }
00334     PrintProgress(__LINE__);
00335 
00336     did = should = PR_FALSE;
00337     for (keys = 0; keys < 4; ++keys)
00338     {
00339         rv = SetPrivateData(key[keys]);
00340         MY_ASSERT(PR_SUCCESS == rv);
00341     }
00342     PrintProgress(__LINE__);
00343 
00344     did = should = PR_FALSE;
00345     for (keys = 8; keys < 127; ++keys)
00346     {
00347         rv = SetPrivateData(key[keys], &extension);
00348         MY_ASSERT(PR_SUCCESS == rv);
00349     }
00350     PrintProgress(__LINE__);
00351 
00352     did = PR_FALSE; should = PR_TRUE;
00353     for (keys = 8; keys < 127; ++keys)
00354     {
00355         rv = SetPrivateData(key[keys]);
00356         MY_ASSERT(PR_SUCCESS == rv);
00357     }
00358     PrintProgress(__LINE__);
00359 
00360     did = should = PR_FALSE;
00361     for (keys = 8; keys < 127; ++keys)
00362     {
00363         rv = SetPrivateData(key[keys]);
00364         MY_ASSERT(PR_SUCCESS == rv);
00365     }
00366 }  /* MyThread::RootFunction */
00367 
00368 /* tpd.c */