os/kernelhwsrv/kerneltest/e32test/nkernsa/testdfc.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// e32test\nkernsa\testdfc.cpp
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include <nktest/nkutils.h>
sl@0
    19
sl@0
    20
#ifndef __SMP__
sl@0
    21
#define iNThreadBaseSpare7 iSpare7
sl@0
    22
class NSchedulable;
sl@0
    23
#endif
sl@0
    24
sl@0
    25
extern "C" TUint32 set_bit0_if_nonnull(TUint32&);
sl@0
    26
extern "C" void flip_bit0(TUint32&);
sl@0
    27
extern "C" TUint32 swap_out_if_bit0_clear(TUint32&);
sl@0
    28
sl@0
    29
#ifdef __SMP__
sl@0
    30
class TAddDfc : public TGenericIPI
sl@0
    31
#else
sl@0
    32
class TAddDfc : public NTimer
sl@0
    33
#endif
sl@0
    34
	{
sl@0
    35
public:
sl@0
    36
	TAddDfc();
sl@0
    37
	TDfc* Add(TDfc* aDfc, TUint32 aCpuMask);
sl@0
    38
	static TAddDfc* New();
sl@0
    39
#ifdef __SMP__
sl@0
    40
	static void Isr(TGenericIPI*);
sl@0
    41
#else
sl@0
    42
	static void TimerCallBack(TAny*);
sl@0
    43
	void WaitCompletion();
sl@0
    44
#endif
sl@0
    45
public:
sl@0
    46
	TDfc* iDfc;
sl@0
    47
	};
sl@0
    48
sl@0
    49
TAddDfc::TAddDfc()
sl@0
    50
#ifdef __SMP__
sl@0
    51
	:	iDfc(0)
sl@0
    52
#else
sl@0
    53
	:	NTimer(&TimerCallBack, this),
sl@0
    54
		iDfc(0)
sl@0
    55
#endif
sl@0
    56
	{
sl@0
    57
	}
sl@0
    58
sl@0
    59
TAddDfc* TAddDfc::New()
sl@0
    60
	{
sl@0
    61
	TAddDfc* p = new TAddDfc;
sl@0
    62
	TEST_OOM(p);
sl@0
    63
	return p;
sl@0
    64
	}
sl@0
    65
sl@0
    66
#ifdef __SMP__
sl@0
    67
void TAddDfc::Isr(TGenericIPI* a)
sl@0
    68
#else
sl@0
    69
void TAddDfc::TimerCallBack(TAny* a)
sl@0
    70
#endif
sl@0
    71
	{
sl@0
    72
	TAddDfc& adder = *(TAddDfc*)a;
sl@0
    73
	TDfc* dfc = (TDfc*)__e32_atomic_swp_ord_ptr(&adder.iDfc, 0);
sl@0
    74
	if (dfc)
sl@0
    75
		dfc->Add();
sl@0
    76
	}
sl@0
    77
sl@0
    78
TDfc* TAddDfc::Add(TDfc* aDfc, TUint32 aCpuMask)
sl@0
    79
	{
sl@0
    80
	TDfc* old = (TDfc*)__e32_atomic_swp_ord_ptr(&iDfc, aDfc);
sl@0
    81
#ifdef __SMP__
sl@0
    82
	Queue(&Isr, aCpuMask);
sl@0
    83
#else
sl@0
    84
	(void)aCpuMask;
sl@0
    85
	OneShot(1);
sl@0
    86
#endif
sl@0
    87
	return old;
sl@0
    88
	}
sl@0
    89
sl@0
    90
#ifndef __SMP__
sl@0
    91
void TAddDfc::WaitCompletion()
sl@0
    92
	{
sl@0
    93
	while (iDfc)
sl@0
    94
		{}
sl@0
    95
	}
sl@0
    96
#endif
sl@0
    97
sl@0
    98
class TTestDfc : public TDfc
sl@0
    99
	{
sl@0
   100
public:
sl@0
   101
	TTestDfc(TUint aId);
sl@0
   102
	TTestDfc(TUint aId, TDfcQue* aQ, TInt aPri);
sl@0
   103
	static void Run(TAny* aPtr);
sl@0
   104
sl@0
   105
	static void CheckEmpty(TInt aLine);
sl@0
   106
	static void CheckFirstEntry(TInt aLine, TUint32 aCpu, TUint32 aContext, TDfcQue* aQ, TUint32 aId);
sl@0
   107
sl@0
   108
	static CircBuf* Buffer;
sl@0
   109
	static volatile TBool Full;
sl@0
   110
	static volatile TUint32 Last;
sl@0
   111
sl@0
   112
	enum {EBufferSlots=1024};
sl@0
   113
	};
sl@0
   114
sl@0
   115
#define CHECK_EMPTY()	TTestDfc::CheckEmpty(__LINE__)
sl@0
   116
#define CHECK_FIRST_ENTRY(cpu, ctx, q, id)	TTestDfc::CheckFirstEntry(__LINE__, cpu, ctx, q, id)
sl@0
   117
sl@0
   118
CircBuf* TTestDfc::Buffer;
sl@0
   119
volatile TBool TTestDfc::Full = FALSE;
sl@0
   120
volatile TUint32 TTestDfc::Last;
sl@0
   121
sl@0
   122
TTestDfc::TTestDfc(TUint aId)
sl@0
   123
	:	TDfc(&Run, (TAny*)aId)
sl@0
   124
	{
sl@0
   125
	}
sl@0
   126
sl@0
   127
TTestDfc::TTestDfc(TUint aId, TDfcQue* aQ, TInt aPri)
sl@0
   128
	:	TDfc(&Run, (TAny*)aId, aQ, aPri)
sl@0
   129
	{
sl@0
   130
	}
sl@0
   131
sl@0
   132
void TTestDfc::Run(TAny* aPtr)
sl@0
   133
	{
sl@0
   134
	TUint32 id = (TUint32)aPtr;
sl@0
   135
	TUint32 tid = 0;
sl@0
   136
	TUint32 ctx = NKern::CurrentContext();
sl@0
   137
	TUint32 cpu = NKern::CurrentCpu();
sl@0
   138
	if (ctx == NKern::EThread)
sl@0
   139
		{
sl@0
   140
		NThread* t = NKern::CurrentThread();
sl@0
   141
		tid = t->iNThreadBaseSpare7;
sl@0
   142
		}
sl@0
   143
	TUint32 x = (cpu<<24) | (ctx<<16) | (tid<<8) | id;
sl@0
   144
	TInt r = Buffer->TryPut(x);
sl@0
   145
	if (r != KErrNone)
sl@0
   146
		Full = TRUE;
sl@0
   147
	Last = id;
sl@0
   148
	}
sl@0
   149
sl@0
   150
void TTestDfc::CheckEmpty(TInt aLine)
sl@0
   151
	{
sl@0
   152
	TInt c = Buffer->Count();
sl@0
   153
	TUint32 x;
sl@0
   154
	Buffer->TryGet(x);
sl@0
   155
	if (c!=0)
sl@0
   156
		{
sl@0
   157
		TEST_PRINT3("Line %d Buffer not empty C:%d X:%08x", aLine, c, x);
sl@0
   158
		}
sl@0
   159
	}
sl@0
   160
sl@0
   161
void TTestDfc::CheckFirstEntry(TInt aLine, TUint32 aCpu, TUint32 aContext, TDfcQue* aQ, TUint32 aId)
sl@0
   162
	{
sl@0
   163
	TUint32 tid = aQ ? aQ->iThread->iNThreadBaseSpare7 : 0;
sl@0
   164
	TUint32 expected = (aCpu<<24) | (aContext<<16) | (tid << 8) | aId;
sl@0
   165
	TUint32 x;
sl@0
   166
	TInt r = Buffer->TryGet(x);
sl@0
   167
	if (r!=KErrNone)
sl@0
   168
		{
sl@0
   169
		TEST_PRINT2("Line %d Buffer empty, Expected %08x", aLine, expected);
sl@0
   170
		}
sl@0
   171
	else if (x != expected)
sl@0
   172
		{
sl@0
   173
		TEST_PRINT3("Line %d Got %08x Expected %08x", aLine, x, expected);
sl@0
   174
		}
sl@0
   175
	}
sl@0
   176
sl@0
   177
class TPauseIDFC : public TDfc
sl@0
   178
	{
sl@0
   179
public:
sl@0
   180
	TPauseIDFC();
sl@0
   181
	void Pause(TInt aCpu);
sl@0
   182
	void Release();
sl@0
   183
	static void Run(TAny*);
sl@0
   184
public:
sl@0
   185
	volatile TInt iFlag;
sl@0
   186
	};
sl@0
   187
sl@0
   188
TPauseIDFC::TPauseIDFC()
sl@0
   189
	:	TDfc(&Run, this),
sl@0
   190
		iFlag(-1)
sl@0
   191
	{
sl@0
   192
	}
sl@0
   193
sl@0
   194
void TPauseIDFC::Pause(TInt aCpu)
sl@0
   195
	{
sl@0
   196
	TAddDfc adder;
sl@0
   197
	iFlag = -1;
sl@0
   198
	__e32_memory_barrier();
sl@0
   199
	adder.Add(this, 1u<<aCpu);
sl@0
   200
	adder.WaitCompletion();
sl@0
   201
	while (iFlag == -1)
sl@0
   202
		{}
sl@0
   203
	}
sl@0
   204
sl@0
   205
void TPauseIDFC::Release()
sl@0
   206
	{
sl@0
   207
	__e32_atomic_store_ord32(&iFlag, 1);
sl@0
   208
	}
sl@0
   209
sl@0
   210
void TPauseIDFC::Run(TAny* aPtr)
sl@0
   211
	{
sl@0
   212
	TPauseIDFC* p = (TPauseIDFC*)aPtr;
sl@0
   213
	__e32_atomic_store_ord32(&p->iFlag, 0);
sl@0
   214
	while (__e32_atomic_load_acq32(&p->iFlag) == 0)
sl@0
   215
		{}
sl@0
   216
	}
sl@0
   217
sl@0
   218
class TPauseDFC : public TDfc
sl@0
   219
	{
sl@0
   220
public:
sl@0
   221
	TPauseDFC(TDfcQue* aQ);
sl@0
   222
	void Pause(TInt aWait=0);
sl@0
   223
	void BusyPause();
sl@0
   224
	void Release();
sl@0
   225
	static void Run(TAny*);
sl@0
   226
public:
sl@0
   227
	NFastSemaphore* volatile iSem;
sl@0
   228
	volatile TInt iWait;
sl@0
   229
	};
sl@0
   230
sl@0
   231
TPauseDFC::TPauseDFC(TDfcQue* aQ)
sl@0
   232
	:	TDfc(&Run, this, aQ, 0),
sl@0
   233
		iSem(0)
sl@0
   234
	{
sl@0
   235
	}
sl@0
   236
sl@0
   237
void TPauseDFC::Pause(TInt aWait)
sl@0
   238
	{
sl@0
   239
	iWait = aWait;
sl@0
   240
	NFastSemaphore entrySem(0);
sl@0
   241
	iSem = &entrySem;
sl@0
   242
	Enque();
sl@0
   243
	NKern::FSWait(&entrySem);
sl@0
   244
	}
sl@0
   245
sl@0
   246
void TPauseDFC::BusyPause()
sl@0
   247
	{
sl@0
   248
	volatile TInt& flag = (volatile TInt&)iSem;
sl@0
   249
	__e32_atomic_store_ord32(&flag, 0xfffffffe);
sl@0
   250
	Enque();
sl@0
   251
	while (__e32_atomic_load_acq32(&flag) == 0xfffffffe)
sl@0
   252
		{}
sl@0
   253
	}
sl@0
   254
sl@0
   255
void TPauseDFC::Release()
sl@0
   256
	{
sl@0
   257
	NFastSemaphore* s = (NFastSemaphore*)__e32_atomic_swp_ord_ptr(&iSem, 0);
sl@0
   258
	if (((TInt)s)==-1)
sl@0
   259
		{
sl@0
   260
		volatile TInt& flag = (volatile TInt&)iSem;
sl@0
   261
		__e32_atomic_store_ord32(&flag, 0);
sl@0
   262
		}
sl@0
   263
	else
sl@0
   264
		NKern::FSSignal(s);
sl@0
   265
	}
sl@0
   266
sl@0
   267
void TPauseDFC::Run(TAny* aPtr)
sl@0
   268
	{
sl@0
   269
	TPauseDFC* p = (TPauseDFC*)aPtr;
sl@0
   270
	volatile TInt& flag = (volatile TInt&)p->iSem;
sl@0
   271
	if (flag == -2)
sl@0
   272
		{
sl@0
   273
		flag = -1;
sl@0
   274
		__e32_memory_barrier();
sl@0
   275
		while (flag == -1)
sl@0
   276
			{}
sl@0
   277
		}
sl@0
   278
	else
sl@0
   279
		{
sl@0
   280
		NFastSemaphore exitSem(0);
sl@0
   281
		NFastSemaphore* s = (NFastSemaphore*)__e32_atomic_swp_ord_ptr(&p->iSem, &exitSem);
sl@0
   282
		if (p->iWait)
sl@0
   283
			{
sl@0
   284
			nfcfspin(__microseconds_to_norm_fast_counter(10000));
sl@0
   285
			NKern::Sleep(p->iWait);
sl@0
   286
			}
sl@0
   287
		NKern::FSSignal(s);
sl@0
   288
		NKern::FSWait(&exitSem);
sl@0
   289
		}
sl@0
   290
	}
sl@0
   291
sl@0
   292
void DoDFCTest1()
sl@0
   293
	{
sl@0
   294
	TEST_PRINT("DFCTest1");
sl@0
   295
	TInt cpu;
sl@0
   296
	for_each_cpu(cpu)
sl@0
   297
		{
sl@0
   298
		TDfcQue* q = CreateDfcQ("DfcQ0", 1, cpu);
sl@0
   299
		DestroyDfcQ(q);
sl@0
   300
		q = CreateDfcQ("DfcQ1", 32, cpu);
sl@0
   301
		DestroyDfcQ(q);
sl@0
   302
		}
sl@0
   303
	}
sl@0
   304
sl@0
   305
TBool QueueDfc(TDfc* aDfc, TInt aMode, TBool aExRet)
sl@0
   306
	{
sl@0
   307
	if (aMode==0)
sl@0
   308
		return !aExRet == !aDfc->Enque();
sl@0
   309
	else if (aMode>0)
sl@0
   310
		{
sl@0
   311
		TAddDfc adder;
sl@0
   312
		TInt cpu = aMode - 1;
sl@0
   313
		adder.Add(aDfc, 1u<<cpu);
sl@0
   314
		adder.WaitCompletion();
sl@0
   315
		nfcfspin(__microseconds_to_norm_fast_counter(10000));
sl@0
   316
		return TRUE;
sl@0
   317
		}
sl@0
   318
	else if (aMode==-1)
sl@0
   319
		{
sl@0
   320
		NKern::Lock();
sl@0
   321
		TBool ret = aDfc->Add();
sl@0
   322
		NKern::Unlock();
sl@0
   323
		return !aExRet == !ret;
sl@0
   324
		}
sl@0
   325
	return FALSE;
sl@0
   326
	}
sl@0
   327
sl@0
   328
#define QUEUE_DFC(dfc, mode, exret)	TEST_RESULT(QueueDfc(dfc,mode,exret),"")
sl@0
   329
sl@0
   330
void DoDFCTest2()
sl@0
   331
	{
sl@0
   332
	TEST_PRINT("DFCTest2");
sl@0
   333
	TInt num_cpus = NKern::NumberOfCpus();
sl@0
   334
	TInt this_cpu = NKern::CurrentCpu();
sl@0
   335
	TDfcQue* q;
sl@0
   336
	q = CreateDfcQ("DfcQ2", 1, this_cpu);
sl@0
   337
	TEST_OOM(q);
sl@0
   338
	q->iThread->iNThreadBaseSpare7 = 1;
sl@0
   339
sl@0
   340
	TTestDfc* d1 = new TTestDfc(1, q, 1);
sl@0
   341
	TEST_OOM(d1);
sl@0
   342
	TEST_RESULT(!d1->IsIDFC(), "");
sl@0
   343
	TTestDfc* d2 = new TTestDfc(2, q, 2);
sl@0
   344
	TEST_OOM(d2);
sl@0
   345
	TEST_RESULT(!d2->IsIDFC(), "");
sl@0
   346
	TTestDfc* d3 = new TTestDfc(3, q, 2);
sl@0
   347
	TEST_OOM(d3);
sl@0
   348
	TEST_RESULT(!d3->IsIDFC(), "");
sl@0
   349
	TTestDfc* d4 = new TTestDfc(4, q, 3);
sl@0
   350
	TEST_OOM(d4);
sl@0
   351
	TEST_RESULT(!d4->IsIDFC(), "");
sl@0
   352
sl@0
   353
	TInt mode;
sl@0
   354
	for (mode=-1; mode<=num_cpus; ++mode)
sl@0
   355
		{
sl@0
   356
		TEST_PRINT1("Mode %d", mode);
sl@0
   357
		CHECK_EMPTY();
sl@0
   358
		TEST_RESULT(!d1->Queued(), "");
sl@0
   359
		QUEUE_DFC(d1, mode, TRUE);
sl@0
   360
		TEST_RESULT(d1->Queued(), "");
sl@0
   361
		QUEUE_DFC(d1, mode, FALSE);
sl@0
   362
		TEST_RESULT(d1->Queued(), "");
sl@0
   363
		QUEUE_DFC(d2, mode, TRUE);
sl@0
   364
		QUEUE_DFC(d3, mode, TRUE);
sl@0
   365
		QUEUE_DFC(d4, mode, TRUE);
sl@0
   366
		CHECK_EMPTY();
sl@0
   367
		NKern::Sleep(30);
sl@0
   368
		CHECK_FIRST_ENTRY(this_cpu, NKern::EThread, q, 4);
sl@0
   369
		CHECK_FIRST_ENTRY(this_cpu, NKern::EThread, q, 2);
sl@0
   370
		CHECK_FIRST_ENTRY(this_cpu, NKern::EThread, q, 3);
sl@0
   371
		CHECK_FIRST_ENTRY(this_cpu, NKern::EThread, q, 1);
sl@0
   372
		CHECK_EMPTY();
sl@0
   373
		QUEUE_DFC(d4, mode, TRUE);
sl@0
   374
		QUEUE_DFC(d3, mode, TRUE);
sl@0
   375
		QUEUE_DFC(d2, mode, TRUE);
sl@0
   376
		QUEUE_DFC(d1, mode, TRUE);
sl@0
   377
		CHECK_EMPTY();
sl@0
   378
		NKern::Sleep(30);
sl@0
   379
		CHECK_FIRST_ENTRY(this_cpu, NKern::EThread, q, 4);
sl@0
   380
		CHECK_FIRST_ENTRY(this_cpu, NKern::EThread, q, 3);
sl@0
   381
		CHECK_FIRST_ENTRY(this_cpu, NKern::EThread, q, 2);
sl@0
   382
		CHECK_FIRST_ENTRY(this_cpu, NKern::EThread, q, 1);
sl@0
   383
		CHECK_EMPTY();
sl@0
   384
		QUEUE_DFC(d4, mode, TRUE);
sl@0
   385
		QUEUE_DFC(d3, mode, TRUE);
sl@0
   386
		QUEUE_DFC(d2, mode, TRUE);
sl@0
   387
		QUEUE_DFC(d1, mode, TRUE);
sl@0
   388
		TEST_RESULT(d4->Queued(), "");
sl@0
   389
		TEST_RESULT(d4->Cancel(), "");
sl@0
   390
		TEST_RESULT(!d4->Cancel(), "");
sl@0
   391
		TEST_RESULT(!d4->Queued(), "");
sl@0
   392
		CHECK_EMPTY();
sl@0
   393
		NKern::Sleep(30);
sl@0
   394
		CHECK_FIRST_ENTRY(this_cpu, NKern::EThread, q, 3);
sl@0
   395
		CHECK_FIRST_ENTRY(this_cpu, NKern::EThread, q, 2);
sl@0
   396
		CHECK_FIRST_ENTRY(this_cpu, NKern::EThread, q, 1);
sl@0
   397
		CHECK_EMPTY();
sl@0
   398
		QUEUE_DFC(d4, mode, TRUE);
sl@0
   399
		QUEUE_DFC(d3, mode, TRUE);
sl@0
   400
		QUEUE_DFC(d2, mode, TRUE);
sl@0
   401
		QUEUE_DFC(d1, mode, TRUE);
sl@0
   402
		TEST_RESULT(d3->Queued(), "");
sl@0
   403
		TEST_RESULT(d3->Cancel(), "");
sl@0
   404
		TEST_RESULT(!d3->Queued(), "");
sl@0
   405
		CHECK_EMPTY();
sl@0
   406
		NKern::Sleep(30);
sl@0
   407
		CHECK_FIRST_ENTRY(this_cpu, NKern::EThread, q, 4);
sl@0
   408
		CHECK_FIRST_ENTRY(this_cpu, NKern::EThread, q, 2);
sl@0
   409
		CHECK_FIRST_ENTRY(this_cpu, NKern::EThread, q, 1);
sl@0
   410
		CHECK_EMPTY();
sl@0
   411
		QUEUE_DFC(d4, mode, TRUE);
sl@0
   412
		QUEUE_DFC(d3, mode, TRUE);
sl@0
   413
		QUEUE_DFC(d2, mode, TRUE);
sl@0
   414
		QUEUE_DFC(d1, mode, TRUE);
sl@0
   415
		TEST_RESULT(d3->Queued(), "");
sl@0
   416
		TEST_RESULT(d2->Queued(), "");
sl@0
   417
		TEST_RESULT(d3->Cancel(), "");
sl@0
   418
		TEST_RESULT(d2->Cancel(), "");
sl@0
   419
		TEST_RESULT(!d3->Queued(), "");
sl@0
   420
		TEST_RESULT(!d2->Queued(), "");
sl@0
   421
		CHECK_EMPTY();
sl@0
   422
		NKern::Sleep(30);
sl@0
   423
		CHECK_FIRST_ENTRY(this_cpu, NKern::EThread, q, 4);
sl@0
   424
		CHECK_FIRST_ENTRY(this_cpu, NKern::EThread, q, 1);
sl@0
   425
		CHECK_EMPTY();
sl@0
   426
		QUEUE_DFC(d4, mode, TRUE);
sl@0
   427
		QUEUE_DFC(d3, mode, TRUE);
sl@0
   428
		QUEUE_DFC(d2, mode, TRUE);
sl@0
   429
		QUEUE_DFC(d1, mode, TRUE);
sl@0
   430
		TEST_RESULT(d3->Cancel(), "");
sl@0
   431
		TEST_RESULT(d2->Cancel(), "");
sl@0
   432
		TEST_RESULT(d4->Cancel(), "");
sl@0
   433
		TEST_RESULT(d1->Cancel(), "");
sl@0
   434
		CHECK_EMPTY();
sl@0
   435
		NKern::Sleep(30);
sl@0
   436
		CHECK_EMPTY();
sl@0
   437
		QUEUE_DFC(d4, mode, TRUE);
sl@0
   438
		QUEUE_DFC(d3, mode, TRUE);
sl@0
   439
		QUEUE_DFC(d2, mode, TRUE);
sl@0
   440
		QUEUE_DFC(d1, mode, TRUE);
sl@0
   441
		TEST_RESULT(d1->Queued(), "");
sl@0
   442
		TEST_RESULT(d3->Cancel(), "");
sl@0
   443
		TEST_RESULT(d2->Cancel(), "");
sl@0
   444
		TEST_RESULT(d4->Cancel(), "");
sl@0
   445
		TEST_RESULT(d1->Cancel(), "");
sl@0
   446
		TEST_RESULT(!d1->Queued(), "");
sl@0
   447
		QUEUE_DFC(d1, mode, TRUE);
sl@0
   448
		TEST_RESULT(d1->Queued(), "");
sl@0
   449
		QUEUE_DFC(d1, mode, FALSE);
sl@0
   450
		TEST_RESULT(d1->Queued(), "");
sl@0
   451
		CHECK_EMPTY();
sl@0
   452
		NKern::Sleep(30);
sl@0
   453
		CHECK_FIRST_ENTRY(this_cpu, NKern::EThread, q, 1);
sl@0
   454
		CHECK_EMPTY();
sl@0
   455
		}
sl@0
   456
sl@0
   457
	delete d4;
sl@0
   458
	delete d3;
sl@0
   459
	delete d2;
sl@0
   460
	delete d1;
sl@0
   461
	DestroyDfcQ(q);
sl@0
   462
	}
sl@0
   463
sl@0
   464
void DoDFCTest3(TInt aCpu)
sl@0
   465
	{
sl@0
   466
	TEST_PRINT1("DFCTest3 CPU %d", aCpu);
sl@0
   467
	TInt num_cpus = NKern::NumberOfCpus();
sl@0
   468
	TInt this_cpu = NKern::CurrentCpu();
sl@0
   469
	TBool same_cpu = (aCpu==this_cpu);
sl@0
   470
	TDfcQue* q;
sl@0
   471
	q = CreateDfcQ("DfcQ2", 32, aCpu);
sl@0
   472
	TEST_OOM(q);
sl@0
   473
	q->iThread->iNThreadBaseSpare7 = 1;
sl@0
   474
	TPauseDFC pauser(q);
sl@0
   475
sl@0
   476
	TTestDfc* d1 = new TTestDfc(1, q, 1);
sl@0
   477
	TEST_OOM(d1);
sl@0
   478
	TEST_RESULT(!d1->IsIDFC(), "");
sl@0
   479
	TTestDfc* d2 = new TTestDfc(2, q, 2);
sl@0
   480
	TEST_OOM(d2);
sl@0
   481
	TEST_RESULT(!d2->IsIDFC(), "");
sl@0
   482
	TTestDfc* d3 = new TTestDfc(3, q, 2);
sl@0
   483
	TEST_OOM(d3);
sl@0
   484
	TEST_RESULT(!d3->IsIDFC(), "");
sl@0
   485
	TTestDfc* d4 = new TTestDfc(4, q, 3);
sl@0
   486
	TEST_OOM(d4);
sl@0
   487
	TEST_RESULT(!d4->IsIDFC(), "");
sl@0
   488
sl@0
   489
	TInt mode;
sl@0
   490
	for (mode=-1; mode<=num_cpus; ++mode)
sl@0
   491
		{
sl@0
   492
		TEST_PRINT1("Mode %d", mode);
sl@0
   493
		CHECK_EMPTY();
sl@0
   494
		TEST_RESULT(!d1->Queued(), "");
sl@0
   495
		QUEUE_DFC(d1, mode, TRUE);
sl@0
   496
		if (!same_cpu)
sl@0
   497
			while (d1->Queued()) {}
sl@0
   498
		TEST_RESULT(!d1->Queued(), "");
sl@0
   499
		QUEUE_DFC(d1, mode, TRUE);
sl@0
   500
		if (!same_cpu)
sl@0
   501
			while (d1->Queued()) {}
sl@0
   502
		TEST_RESULT(!d1->Queued(), "");
sl@0
   503
		QUEUE_DFC(d2, mode, TRUE);
sl@0
   504
		if (!same_cpu)
sl@0
   505
			while (d2->Queued()) {}
sl@0
   506
		QUEUE_DFC(d3, mode, TRUE);
sl@0
   507
		if (!same_cpu)
sl@0
   508
			while (d3->Queued()) {}
sl@0
   509
		QUEUE_DFC(d4, mode, TRUE);
sl@0
   510
		if (!same_cpu)
sl@0
   511
			while (d4->Queued()) {}
sl@0
   512
		CHECK_FIRST_ENTRY(aCpu, NKern::EThread, q, 1);
sl@0
   513
		CHECK_FIRST_ENTRY(aCpu, NKern::EThread, q, 1);
sl@0
   514
		CHECK_FIRST_ENTRY(aCpu, NKern::EThread, q, 2);
sl@0
   515
		CHECK_FIRST_ENTRY(aCpu, NKern::EThread, q, 3);
sl@0
   516
		CHECK_FIRST_ENTRY(aCpu, NKern::EThread, q, 4);
sl@0
   517
		CHECK_EMPTY();
sl@0
   518
		QUEUE_DFC(d4, mode, TRUE);
sl@0
   519
		QUEUE_DFC(d3, mode, TRUE);
sl@0
   520
		QUEUE_DFC(d2, mode, TRUE);
sl@0
   521
		QUEUE_DFC(d1, mode, TRUE);
sl@0
   522
		if (!same_cpu)
sl@0
   523
			while (d1->Queued()) {}
sl@0
   524
		CHECK_FIRST_ENTRY(aCpu, NKern::EThread, q, 4);
sl@0
   525
		CHECK_FIRST_ENTRY(aCpu, NKern::EThread, q, 3);
sl@0
   526
		CHECK_FIRST_ENTRY(aCpu, NKern::EThread, q, 2);
sl@0
   527
		CHECK_FIRST_ENTRY(aCpu, NKern::EThread, q, 1);
sl@0
   528
		CHECK_EMPTY();
sl@0
   529
		QUEUE_DFC(d4, mode, TRUE);
sl@0
   530
		QUEUE_DFC(d3, mode, TRUE);
sl@0
   531
		QUEUE_DFC(d2, mode, TRUE);
sl@0
   532
		QUEUE_DFC(d1, mode, TRUE);
sl@0
   533
		if (!same_cpu)
sl@0
   534
			while (d1->Queued()) {}
sl@0
   535
		TEST_RESULT(!d4->Queued(), "");
sl@0
   536
		TEST_RESULT(!d4->Cancel(), "");
sl@0
   537
		TEST_RESULT(!d4->Queued(), "");
sl@0
   538
		CHECK_FIRST_ENTRY(aCpu, NKern::EThread, q, 4);
sl@0
   539
		CHECK_FIRST_ENTRY(aCpu, NKern::EThread, q, 3);
sl@0
   540
		CHECK_FIRST_ENTRY(aCpu, NKern::EThread, q, 2);
sl@0
   541
		CHECK_FIRST_ENTRY(aCpu, NKern::EThread, q, 1);
sl@0
   542
		CHECK_EMPTY();
sl@0
   543
		pauser.Pause();
sl@0
   544
		CHECK_EMPTY();
sl@0
   545
		TEST_RESULT(!d1->Queued(), "");
sl@0
   546
		QUEUE_DFC(d1, mode, TRUE);
sl@0
   547
		TEST_RESULT(d1->Queued(), "");
sl@0
   548
		QUEUE_DFC(d1, mode, FALSE);
sl@0
   549
		TEST_RESULT(d1->Queued(), "");
sl@0
   550
		QUEUE_DFC(d2, mode, TRUE);
sl@0
   551
		QUEUE_DFC(d3, mode, TRUE);
sl@0
   552
		QUEUE_DFC(d4, mode, TRUE);
sl@0
   553
		QUEUE_DFC(d4, mode, FALSE);
sl@0
   554
		CHECK_EMPTY();
sl@0
   555
		pauser.Release();
sl@0
   556
		pauser.Pause();
sl@0
   557
		pauser.Release();
sl@0
   558
		CHECK_FIRST_ENTRY(aCpu, NKern::EThread, q, 4);
sl@0
   559
		CHECK_FIRST_ENTRY(aCpu, NKern::EThread, q, 2);
sl@0
   560
		CHECK_FIRST_ENTRY(aCpu, NKern::EThread, q, 3);
sl@0
   561
		CHECK_FIRST_ENTRY(aCpu, NKern::EThread, q, 1);
sl@0
   562
		CHECK_EMPTY();
sl@0
   563
		pauser.Pause();
sl@0
   564
		CHECK_EMPTY();
sl@0
   565
		TEST_RESULT(!d1->Queued(), "");
sl@0
   566
		QUEUE_DFC(d1, mode, TRUE);
sl@0
   567
		TEST_RESULT(d1->Queued(), "");
sl@0
   568
		QUEUE_DFC(d1, mode, FALSE);
sl@0
   569
		TEST_RESULT(d1->Queued(), "");
sl@0
   570
		QUEUE_DFC(d4, mode, TRUE);
sl@0
   571
		QUEUE_DFC(d3, mode, TRUE);
sl@0
   572
		QUEUE_DFC(d2, mode, TRUE);
sl@0
   573
		CHECK_EMPTY();
sl@0
   574
		pauser.Release();
sl@0
   575
		pauser.Pause();
sl@0
   576
		pauser.Release();
sl@0
   577
		CHECK_FIRST_ENTRY(aCpu, NKern::EThread, q, 4);
sl@0
   578
		CHECK_FIRST_ENTRY(aCpu, NKern::EThread, q, 3);
sl@0
   579
		CHECK_FIRST_ENTRY(aCpu, NKern::EThread, q, 2);
sl@0
   580
		CHECK_FIRST_ENTRY(aCpu, NKern::EThread, q, 1);
sl@0
   581
		CHECK_EMPTY();
sl@0
   582
		pauser.Pause();
sl@0
   583
		CHECK_EMPTY();
sl@0
   584
		TEST_RESULT(!d1->Queued(), "");
sl@0
   585
		QUEUE_DFC(d1, mode, TRUE);
sl@0
   586
		TEST_RESULT(d1->Queued(), "");
sl@0
   587
		QUEUE_DFC(d1, mode, FALSE);
sl@0
   588
		TEST_RESULT(d1->Queued(), "");
sl@0
   589
		QUEUE_DFC(d2, mode, TRUE);
sl@0
   590
		QUEUE_DFC(d3, mode, TRUE);
sl@0
   591
		QUEUE_DFC(d4, mode, TRUE);
sl@0
   592
		CHECK_EMPTY();
sl@0
   593
		TEST_RESULT(d1->Cancel(), "");
sl@0
   594
		TEST_RESULT(!d1->Queued(), "");
sl@0
   595
		pauser.Release();
sl@0
   596
		pauser.Pause();
sl@0
   597
		pauser.Release();
sl@0
   598
		CHECK_FIRST_ENTRY(aCpu, NKern::EThread, q, 4);
sl@0
   599
		CHECK_FIRST_ENTRY(aCpu, NKern::EThread, q, 2);
sl@0
   600
		CHECK_FIRST_ENTRY(aCpu, NKern::EThread, q, 3);
sl@0
   601
		CHECK_EMPTY();
sl@0
   602
		}
sl@0
   603
sl@0
   604
	delete d4;
sl@0
   605
	delete d3;
sl@0
   606
	delete d2;
sl@0
   607
	delete d1;
sl@0
   608
	DestroyDfcQ(q);
sl@0
   609
	}
sl@0
   610
sl@0
   611
TBool QueueIDfc(TDfc* aDfc, TInt aMode, TBool aExRet)
sl@0
   612
	{
sl@0
   613
	if (aMode==0)
sl@0
   614
		return !aExRet == !aDfc->RawAdd();
sl@0
   615
	else if (aMode>0)
sl@0
   616
		{
sl@0
   617
		TTestDfc::Last = 0xffffffffu;
sl@0
   618
		TAddDfc adder;
sl@0
   619
		TInt cpu = (aMode&0xff) - 1;
sl@0
   620
		adder.Add(aDfc, 1u<<cpu);
sl@0
   621
		adder.WaitCompletion();
sl@0
   622
		if (!(aMode&0x100))
sl@0
   623
			{
sl@0
   624
			while (TTestDfc::Last != (TUint32)aDfc->iPtr)
sl@0
   625
				{}
sl@0
   626
			}
sl@0
   627
		return TRUE;
sl@0
   628
		}
sl@0
   629
	else if (aMode==-1)
sl@0
   630
		{
sl@0
   631
		NKern::Lock();
sl@0
   632
		TBool ret = aDfc->Add();
sl@0
   633
		NKern::Unlock();
sl@0
   634
		return !aExRet == !ret;
sl@0
   635
		}
sl@0
   636
	return FALSE;
sl@0
   637
	}
sl@0
   638
sl@0
   639
#define QUEUE_IDFC(dfc, mode, exret)	TEST_RESULT(QueueIDfc(dfc,mode,exret),"")
sl@0
   640
sl@0
   641
void DoIDFCTest1()
sl@0
   642
	{
sl@0
   643
	TEST_PRINT("IDFCTest1");
sl@0
   644
sl@0
   645
	TInt num_cpus = NKern::NumberOfCpus();
sl@0
   646
	TInt this_cpu = NKern::CurrentCpu();
sl@0
   647
sl@0
   648
	TTestDfc* d1 = new TTestDfc(1);
sl@0
   649
	TEST_OOM(d1);
sl@0
   650
	TEST_RESULT(d1->IsIDFC(), "");
sl@0
   651
	TTestDfc* d2 = new TTestDfc(2);
sl@0
   652
	TEST_OOM(d2);
sl@0
   653
	TEST_RESULT(d2->IsIDFC(), "");
sl@0
   654
	TTestDfc* d3 = new TTestDfc(3);
sl@0
   655
	TEST_OOM(d3);
sl@0
   656
	TEST_RESULT(d3->IsIDFC(), "");
sl@0
   657
	TTestDfc* d4 = new TTestDfc(4);
sl@0
   658
	TEST_OOM(d4);
sl@0
   659
	TEST_RESULT(d4->IsIDFC(), "");
sl@0
   660
sl@0
   661
	TInt mode;
sl@0
   662
	for (mode=-1; mode<=num_cpus; ++mode)
sl@0
   663
		{
sl@0
   664
		TInt xcpu = (mode>0) ? (mode-1) : this_cpu;
sl@0
   665
		TEST_PRINT1("Mode %d", mode);
sl@0
   666
		CHECK_EMPTY();
sl@0
   667
		TEST_RESULT(!d1->Queued(), "");
sl@0
   668
		QUEUE_IDFC(d1, mode, TRUE);
sl@0
   669
		TEST_RESULT(!d1->Queued(), "");
sl@0
   670
		QUEUE_IDFC(d1, mode, TRUE);
sl@0
   671
		TEST_RESULT(!d1->Queued(), "");
sl@0
   672
		QUEUE_IDFC(d2, mode, TRUE);
sl@0
   673
		QUEUE_IDFC(d3, mode, TRUE);
sl@0
   674
		QUEUE_IDFC(d4, mode, TRUE);
sl@0
   675
		CHECK_FIRST_ENTRY(xcpu, NKern::EIDFC, 0, 1);
sl@0
   676
		CHECK_FIRST_ENTRY(xcpu, NKern::EIDFC, 0, 1);
sl@0
   677
		CHECK_FIRST_ENTRY(xcpu, NKern::EIDFC, 0, 2);
sl@0
   678
		CHECK_FIRST_ENTRY(xcpu, NKern::EIDFC, 0, 3);
sl@0
   679
		CHECK_FIRST_ENTRY(xcpu, NKern::EIDFC, 0, 4);
sl@0
   680
		CHECK_EMPTY();
sl@0
   681
		QUEUE_IDFC(d4, mode, TRUE);
sl@0
   682
		QUEUE_IDFC(d3, mode, TRUE);
sl@0
   683
		QUEUE_IDFC(d2, mode, TRUE);
sl@0
   684
		QUEUE_IDFC(d1, mode, TRUE);
sl@0
   685
		CHECK_FIRST_ENTRY(xcpu, NKern::EIDFC, 0, 4);
sl@0
   686
		CHECK_FIRST_ENTRY(xcpu, NKern::EIDFC, 0, 3);
sl@0
   687
		CHECK_FIRST_ENTRY(xcpu, NKern::EIDFC, 0, 2);
sl@0
   688
		CHECK_FIRST_ENTRY(xcpu, NKern::EIDFC, 0, 1);
sl@0
   689
		CHECK_EMPTY();
sl@0
   690
		}
sl@0
   691
	TInt irq = NKern::DisableAllInterrupts();
sl@0
   692
	TEST_RESULT(d1->RawAdd(), "");
sl@0
   693
	TEST_RESULT(d1->Queued(), "");
sl@0
   694
	CHECK_EMPTY();
sl@0
   695
	NKern::RestoreInterrupts(irq);
sl@0
   696
	TEST_RESULT(!d1->Queued(), "");
sl@0
   697
	CHECK_FIRST_ENTRY(this_cpu, NKern::EIDFC, 0, 1);
sl@0
   698
sl@0
   699
	NKern::Lock();
sl@0
   700
	TEST_RESULT(d1->Add(), "");
sl@0
   701
	TEST_RESULT(d3->Add(), "");
sl@0
   702
	TEST_RESULT(d2->Add(), "");
sl@0
   703
	TEST_RESULT(d4->Add(), "");
sl@0
   704
	TEST_RESULT(!d1->Add(), "");
sl@0
   705
	TEST_RESULT(d1->Queued(), "");
sl@0
   706
	TEST_RESULT(d2->Queued(), "");
sl@0
   707
	TEST_RESULT(d3->Queued(), "");
sl@0
   708
	TEST_RESULT(d4->Queued(), "");
sl@0
   709
	CHECK_EMPTY();
sl@0
   710
	NKern::Unlock();
sl@0
   711
	TEST_RESULT(!d1->Queued(), "");
sl@0
   712
	TEST_RESULT(!d2->Queued(), "");
sl@0
   713
	TEST_RESULT(!d3->Queued(), "");
sl@0
   714
	TEST_RESULT(!d4->Queued(), "");
sl@0
   715
	CHECK_FIRST_ENTRY(this_cpu, NKern::EIDFC, 0, 1);
sl@0
   716
	CHECK_FIRST_ENTRY(this_cpu, NKern::EIDFC, 0, 3);
sl@0
   717
	CHECK_FIRST_ENTRY(this_cpu, NKern::EIDFC, 0, 2);
sl@0
   718
	CHECK_FIRST_ENTRY(this_cpu, NKern::EIDFC, 0, 4);
sl@0
   719
	CHECK_EMPTY();
sl@0
   720
sl@0
   721
	NKern::Lock();
sl@0
   722
	TEST_RESULT(d1->Add(), "");
sl@0
   723
	TEST_RESULT(d3->Add(), "");
sl@0
   724
	TEST_RESULT(d2->Add(), "");
sl@0
   725
	TEST_RESULT(d4->Add(), "");
sl@0
   726
	TEST_RESULT(d1->Queued(), "");
sl@0
   727
	TEST_RESULT(d2->Queued(), "");
sl@0
   728
	TEST_RESULT(d3->Queued(), "");
sl@0
   729
	TEST_RESULT(d4->Queued(), "");
sl@0
   730
	TEST_RESULT(d3->Cancel(), "");
sl@0
   731
	TEST_RESULT(!d3->Queued(), "");
sl@0
   732
	TEST_RESULT(!d3->Cancel(), "");
sl@0
   733
	CHECK_EMPTY();
sl@0
   734
	NKern::Unlock();
sl@0
   735
	TEST_RESULT(!d1->Queued(), "");
sl@0
   736
	TEST_RESULT(!d2->Queued(), "");
sl@0
   737
	TEST_RESULT(!d4->Queued(), "");
sl@0
   738
	CHECK_FIRST_ENTRY(this_cpu, NKern::EIDFC, 0, 1);
sl@0
   739
	CHECK_FIRST_ENTRY(this_cpu, NKern::EIDFC, 0, 2);
sl@0
   740
	CHECK_FIRST_ENTRY(this_cpu, NKern::EIDFC, 0, 4);
sl@0
   741
	CHECK_EMPTY();
sl@0
   742
sl@0
   743
	NKern::Lock();
sl@0
   744
	TEST_RESULT(d1->Add(), "");
sl@0
   745
	TEST_RESULT(d3->Add(), "");
sl@0
   746
	TEST_RESULT(d2->Add(), "");
sl@0
   747
	TEST_RESULT(d4->Add(), "");
sl@0
   748
	TEST_RESULT(d1->Queued(), "");
sl@0
   749
	TEST_RESULT(d2->Queued(), "");
sl@0
   750
	TEST_RESULT(d3->Queued(), "");
sl@0
   751
	TEST_RESULT(d4->Queued(), "");
sl@0
   752
	TEST_RESULT(d3->Cancel(), "");
sl@0
   753
	TEST_RESULT(d1->Cancel(), "");
sl@0
   754
	TEST_RESULT(d2->Cancel(), "");
sl@0
   755
	TEST_RESULT(d4->Cancel(), "");
sl@0
   756
	TEST_RESULT(!d1->Queued(), "");
sl@0
   757
	TEST_RESULT(!d2->Queued(), "");
sl@0
   758
	TEST_RESULT(!d3->Queued(), "");
sl@0
   759
	TEST_RESULT(!d4->Queued(), "");
sl@0
   760
	CHECK_EMPTY();
sl@0
   761
	NKern::Unlock();
sl@0
   762
	CHECK_EMPTY();
sl@0
   763
sl@0
   764
	TPauseIDFC pauser;
sl@0
   765
	TInt cpu;
sl@0
   766
	for_each_cpu(cpu)
sl@0
   767
		{
sl@0
   768
		if (cpu == this_cpu)
sl@0
   769
			continue;
sl@0
   770
		mode = cpu + 0x101;
sl@0
   771
		TEST_PRINT1("CPU %d", cpu);
sl@0
   772
		pauser.Pause(cpu);
sl@0
   773
		CHECK_EMPTY();
sl@0
   774
		TEST_RESULT(!d1->Queued(), "");
sl@0
   775
		QUEUE_IDFC(d1, mode, TRUE);
sl@0
   776
		TEST_RESULT(d1->Queued(), "");
sl@0
   777
		QUEUE_IDFC(d1, mode, FALSE);
sl@0
   778
		TEST_RESULT(d1->Queued(), "");
sl@0
   779
		QUEUE_IDFC(d2, mode, TRUE);
sl@0
   780
		QUEUE_IDFC(d3, mode, TRUE);
sl@0
   781
		QUEUE_IDFC(d4, mode, TRUE);
sl@0
   782
		CHECK_EMPTY();
sl@0
   783
		pauser.Release();
sl@0
   784
		pauser.Pause(cpu);
sl@0
   785
		pauser.Release();
sl@0
   786
		CHECK_FIRST_ENTRY(cpu, NKern::EIDFC, 0, 1);
sl@0
   787
		CHECK_FIRST_ENTRY(cpu, NKern::EIDFC, 0, 2);
sl@0
   788
		CHECK_FIRST_ENTRY(cpu, NKern::EIDFC, 0, 3);
sl@0
   789
		CHECK_FIRST_ENTRY(cpu, NKern::EIDFC, 0, 4);
sl@0
   790
		CHECK_EMPTY();
sl@0
   791
		pauser.Pause(cpu);
sl@0
   792
		CHECK_EMPTY();
sl@0
   793
		TEST_RESULT(!d1->Queued(), "");
sl@0
   794
		QUEUE_IDFC(d1, mode, TRUE);
sl@0
   795
		TEST_RESULT(d1->Queued(), "");
sl@0
   796
		QUEUE_IDFC(d2, mode, TRUE);
sl@0
   797
		QUEUE_IDFC(d3, mode, TRUE);
sl@0
   798
		QUEUE_IDFC(d4, mode, TRUE);
sl@0
   799
		TEST_RESULT(d1->Cancel(), "");
sl@0
   800
		TEST_RESULT(!d1->Queued(), "");
sl@0
   801
		TEST_RESULT(!d1->Cancel(), "");
sl@0
   802
		CHECK_EMPTY();
sl@0
   803
		pauser.Release();
sl@0
   804
		pauser.Pause(cpu);
sl@0
   805
		pauser.Release();
sl@0
   806
		CHECK_FIRST_ENTRY(cpu, NKern::EIDFC, 0, 2);
sl@0
   807
		CHECK_FIRST_ENTRY(cpu, NKern::EIDFC, 0, 3);
sl@0
   808
		CHECK_FIRST_ENTRY(cpu, NKern::EIDFC, 0, 4);
sl@0
   809
		CHECK_EMPTY();
sl@0
   810
		pauser.Pause(cpu);
sl@0
   811
		CHECK_EMPTY();
sl@0
   812
		TEST_RESULT(!d1->Queued(), "");
sl@0
   813
		QUEUE_IDFC(d1, mode, TRUE);
sl@0
   814
		TEST_RESULT(d1->Queued(), "");
sl@0
   815
		QUEUE_IDFC(d2, mode, TRUE);
sl@0
   816
		QUEUE_IDFC(d3, mode, TRUE);
sl@0
   817
		QUEUE_IDFC(d4, mode, TRUE);
sl@0
   818
		TEST_RESULT(d1->Cancel(), "");
sl@0
   819
		TEST_RESULT(!d1->Queued(), "");
sl@0
   820
		TEST_RESULT(d4->Cancel(), "");
sl@0
   821
		TEST_RESULT(d2->Cancel(), "");
sl@0
   822
		TEST_RESULT(d3->Cancel(), "");
sl@0
   823
		CHECK_EMPTY();
sl@0
   824
		pauser.Release();
sl@0
   825
		pauser.Pause(cpu);
sl@0
   826
		pauser.Release();
sl@0
   827
		CHECK_EMPTY();
sl@0
   828
		}
sl@0
   829
sl@0
   830
	delete d4;
sl@0
   831
	delete d3;
sl@0
   832
	delete d2;
sl@0
   833
	delete d1;
sl@0
   834
	}
sl@0
   835
sl@0
   836
void DoIdleDFCTest1(TInt aCpu)
sl@0
   837
	{
sl@0
   838
#ifdef __SMP__
sl@0
   839
	TEST_PRINT2("IdleDFCTest1 CPU %d (%08x)", aCpu, TheScheduler.iCpusNotIdle);
sl@0
   840
#else
sl@0
   841
	TEST_PRINT1("IdleDFCTest1 CPU %d (%08x)", aCpu);
sl@0
   842
#endif
sl@0
   843
//	TInt num_cpus = NKern::NumberOfCpus();
sl@0
   844
	TInt this_cpu = NKern::CurrentCpu();
sl@0
   845
	TBool same_cpu = (aCpu==this_cpu);
sl@0
   846
	TDfcQue* q = 0;
sl@0
   847
	TPauseDFC* pauser = 0;
sl@0
   848
	if (!same_cpu)
sl@0
   849
		{
sl@0
   850
		q = CreateDfcQ("DfcQ3", 1, aCpu);
sl@0
   851
		TEST_OOM(q);
sl@0
   852
		pauser = new TPauseDFC(q);
sl@0
   853
		TEST_OOM(pauser);
sl@0
   854
		}
sl@0
   855
sl@0
   856
	TTestDfc* d1 = new TTestDfc(1);
sl@0
   857
	TEST_OOM(d1);
sl@0
   858
	TEST_RESULT(d1->IsIDFC(), "");
sl@0
   859
	TTestDfc* d2 = new TTestDfc(2);
sl@0
   860
	TEST_OOM(d2);
sl@0
   861
	TEST_RESULT(d2->IsIDFC(), "");
sl@0
   862
	TTestDfc* d3 = new TTestDfc(3);
sl@0
   863
	TEST_OOM(d3);
sl@0
   864
	TEST_RESULT(d3->IsIDFC(), "");
sl@0
   865
	TTestDfc* d4 = new TTestDfc(4);
sl@0
   866
	TEST_OOM(d4);
sl@0
   867
	TEST_RESULT(d4->IsIDFC(), "");
sl@0
   868
sl@0
   869
	TEST_RESULT(!d1->Queued(), "");
sl@0
   870
	TEST_RESULT(d1->QueueOnIdle(), "");
sl@0
   871
	TEST_RESULT(d1->Queued(), "");
sl@0
   872
	TEST_RESULT(!d1->QueueOnIdle(), "");
sl@0
   873
	CHECK_EMPTY();
sl@0
   874
	if (pauser)
sl@0
   875
		pauser->BusyPause();
sl@0
   876
	NKern::Sleep(1);
sl@0
   877
	if (pauser)
sl@0
   878
		TEST_RESULT(d1->Queued(), "");
sl@0
   879
	else
sl@0
   880
		{
sl@0
   881
		TEST_RESULT(!d1->Queued(), "");
sl@0
   882
		CHECK_FIRST_ENTRY(this_cpu, NKern::EIDFC, 0, 1);
sl@0
   883
		}
sl@0
   884
	CHECK_EMPTY();
sl@0
   885
	TBool ret = d1->QueueOnIdle();
sl@0
   886
	TEST_RESULT(pauser?!ret:ret, "");
sl@0
   887
	TEST_RESULT(d1->Queued(), "");
sl@0
   888
	TEST_RESULT(d1->Cancel(), "");
sl@0
   889
	TEST_RESULT(!d1->Queued(), "");
sl@0
   890
	CHECK_EMPTY();
sl@0
   891
	NKern::Sleep(1);
sl@0
   892
	CHECK_EMPTY();
sl@0
   893
	if (pauser)
sl@0
   894
		pauser->Release();
sl@0
   895
	TEST_RESULT(d4->QueueOnIdle(), "");
sl@0
   896
	TEST_RESULT(d3->QueueOnIdle(), "");
sl@0
   897
	TEST_RESULT(d1->QueueOnIdle(), "");
sl@0
   898
	TEST_RESULT(d2->QueueOnIdle(), "");
sl@0
   899
	TEST_RESULT(d3->Cancel(), "");
sl@0
   900
	CHECK_EMPTY();
sl@0
   901
	TInt xcpu = this_cpu;
sl@0
   902
	if (pauser)
sl@0
   903
		{
sl@0
   904
		xcpu = aCpu;
sl@0
   905
		pauser->Pause(1);
sl@0
   906
		pauser->Release();
sl@0
   907
		}
sl@0
   908
	else
sl@0
   909
		NKern::Sleep(1);
sl@0
   910
	CHECK_FIRST_ENTRY(xcpu, NKern::EIDFC, 0, 4);
sl@0
   911
	CHECK_FIRST_ENTRY(xcpu, NKern::EIDFC, 0, 1);
sl@0
   912
	CHECK_FIRST_ENTRY(xcpu, NKern::EIDFC, 0, 2);
sl@0
   913
	CHECK_EMPTY();
sl@0
   914
sl@0
   915
	delete d4;
sl@0
   916
	delete d3;
sl@0
   917
	delete d2;
sl@0
   918
	delete d1;
sl@0
   919
	delete pauser;
sl@0
   920
	if (q)
sl@0
   921
		DestroyDfcQ(q);
sl@0
   922
	}
sl@0
   923
sl@0
   924
TDfc* Dfcs[6];
sl@0
   925
NFastSemaphore* IdleDFCTest2Fs;
sl@0
   926
sl@0
   927
void IdleDFCTest2Fn(TAny* a)
sl@0
   928
	{
sl@0
   929
	TUint32 id = (TUint32)a;
sl@0
   930
	if (id==1)
sl@0
   931
		{
sl@0
   932
		TEST_RESULT(Dfcs[1]->Cancel(), "");
sl@0
   933
		TEST_RESULT(Dfcs[3]->QueueOnIdle(), "");
sl@0
   934
		TEST_RESULT(Dfcs[4]->QueueOnIdle(), "");
sl@0
   935
		TEST_RESULT(Dfcs[5]->QueueOnIdle(), "");
sl@0
   936
		}
sl@0
   937
	if (id==1 || id==4)
sl@0
   938
		IdleDFCTest2Fs->Signal();
sl@0
   939
	if (id==3)
sl@0
   940
		{
sl@0
   941
		TEST_RESULT(Dfcs[5]->Cancel(), "");
sl@0
   942
		}
sl@0
   943
	TTestDfc::Run(a);
sl@0
   944
	}
sl@0
   945
sl@0
   946
void DoIdleDFCTest2()
sl@0
   947
	{
sl@0
   948
	TEST_PRINT("IdleDFCTest2");
sl@0
   949
	NFastSemaphore sem(0);
sl@0
   950
	TInt this_cpu = NKern::CurrentCpu();
sl@0
   951
	TInt i;
sl@0
   952
	for (i=0; i<6; ++i)
sl@0
   953
		{
sl@0
   954
		Dfcs[i] = new TDfc(&IdleDFCTest2Fn, (TAny*)(i+1));
sl@0
   955
		TEST_OOM(Dfcs[i]);
sl@0
   956
		}
sl@0
   957
	TEST_RESULT(Dfcs[0]->QueueOnIdle(), "");
sl@0
   958
	TEST_RESULT(Dfcs[1]->QueueOnIdle(), "");
sl@0
   959
	TEST_RESULT(Dfcs[2]->QueueOnIdle(), "");
sl@0
   960
	IdleDFCTest2Fs = &sem;
sl@0
   961
	CHECK_EMPTY();
sl@0
   962
	NKern::FSWait(&sem);
sl@0
   963
	CHECK_FIRST_ENTRY(this_cpu, NKern::EIDFC, 0, 1);
sl@0
   964
	CHECK_FIRST_ENTRY(this_cpu, NKern::EIDFC, 0, 3);
sl@0
   965
	CHECK_EMPTY();
sl@0
   966
	NKern::FSWait(&sem);
sl@0
   967
	CHECK_FIRST_ENTRY(this_cpu, NKern::EIDFC, 0, 4);
sl@0
   968
	CHECK_FIRST_ENTRY(this_cpu, NKern::EIDFC, 0, 5);
sl@0
   969
	CHECK_EMPTY();
sl@0
   970
	for (i=0; i<6; ++i)
sl@0
   971
		delete Dfcs[i];
sl@0
   972
	}
sl@0
   973
sl@0
   974
#ifdef __SMP__
sl@0
   975
sl@0
   976
class TDfcStress;
sl@0
   977
class TDfcX
sl@0
   978
	{
sl@0
   979
public:
sl@0
   980
	enum
sl@0
   981
		{
sl@0
   982
		EFlag_IdleDFC=1,
sl@0
   983
		EFlag_IDFC=2,
sl@0
   984
		EFlag_DFC=4,
sl@0
   985
		EFlag_Timer=8,
sl@0
   986
		EFlag_Tied=16
sl@0
   987
		};
sl@0
   988
public:
sl@0
   989
	TDfcX();
sl@0
   990
	~TDfcX();
sl@0
   991
	static void IDfcFn(TAny*);
sl@0
   992
	static void DfcFn(TAny*);
sl@0
   993
	static void TimerIsrFn(TAny*);
sl@0
   994
	static void TimerDfcFn(TAny*);
sl@0
   995
	void Update();
sl@0
   996
	TBool Add(TAny* a=0);
sl@0
   997
	TBool Cancel(TAny* a=0);
sl@0
   998
	TBool Enque(TAny* a=0);
sl@0
   999
	TBool QueueOnIdle(TAny* a=0);
sl@0
  1000
	TBool SafeAdd();
sl@0
  1001
	TBool SafeCancel();
sl@0
  1002
	TBool SafeEnque();
sl@0
  1003
	TBool SafeQueueOnIdle();
sl@0
  1004
	void CreateDfcOrTimer();
sl@0
  1005
	void GetDesc(char* aDesc);
sl@0
  1006
	inline TBool IsIDFC()
sl@0
  1007
		{return (iFlags & (EFlag_IDFC|EFlag_Timer)) == EFlag_IDFC;}
sl@0
  1008
	inline TBool IsIdler()
sl@0
  1009
		{return iFlags & EFlag_IdleDFC;}
sl@0
  1010
	void ThreadActivity();
sl@0
  1011
public:
sl@0
  1012
	union
sl@0
  1013
		{
sl@0
  1014
		TDfc* volatile iDfc;
sl@0
  1015
		NTimer* volatile iTimer;
sl@0
  1016
		};
sl@0
  1017
	TDfcQue* iDfcQ;
sl@0
  1018
	TUint32 iQueueCount;
sl@0
  1019
	TUint32 iRunCount[KMaxCpus];
sl@0
  1020
	TUint32 iCancelCount;
sl@0
  1021
	TUint32 iExclusionFailCount;
sl@0
  1022
	TUint32 iId;
sl@0
  1023
	TUint32 iFlags;
sl@0
  1024
	TDfcStress* iS;
sl@0
  1025
	TUint64 iTimeQueued;
sl@0
  1026
	TUint64 iMaxTime;
sl@0
  1027
	TUint64 iSumTime;
sl@0
  1028
	TSpinLock iSpinLock;
sl@0
  1029
	NSchedulable* iXTied;
sl@0
  1030
	volatile TUint32* iExclusionCheck;
sl@0
  1031
	};
sl@0
  1032
sl@0
  1033
TDfcX::TDfcX()
sl@0
  1034
	: iSpinLock(TSpinLock::EOrderGenericIrqLow1)
sl@0
  1035
	{
sl@0
  1036
	memclr(this,sizeof(TDfcX));
sl@0
  1037
	new (&iSpinLock) TSpinLock(TSpinLock::EOrderGenericIrqLow1);
sl@0
  1038
	}
sl@0
  1039
sl@0
  1040
TDfcX::~TDfcX()
sl@0
  1041
	{
sl@0
  1042
	TAny* p = __e32_atomic_swp_ord_ptr(&iDfc, 0);
sl@0
  1043
	if (p)
sl@0
  1044
		{
sl@0
  1045
		if (iFlags & EFlag_Timer)
sl@0
  1046
			delete ((NTimer*)p);
sl@0
  1047
		else
sl@0
  1048
			delete ((TDfc*)p);
sl@0
  1049
		}
sl@0
  1050
	}
sl@0
  1051
sl@0
  1052
class TDfcStress
sl@0
  1053
	{
sl@0
  1054
public:
sl@0
  1055
	enum
sl@0
  1056
		{
sl@0
  1057
		EMode_Wait			=0x00000001u,
sl@0
  1058
		EMode_AllowCancel	=0x00000002u,
sl@0
  1059
		EMode_AllowIdle		=0x00000004u,
sl@0
  1060
		EMode_AllowEnque	=0x00000008u,
sl@0
  1061
		EMode_Recycle		=0x00000010u,
sl@0
  1062
		EMode_UseTied		=0x00000020u,
sl@0
  1063
		EMode_Migrate		=0x00000040u,
sl@0
  1064
		EMode_SelfMigrate	=0x00000080u,
sl@0
  1065
		EMode_Exit			=0x80000000u
sl@0
  1066
		};
sl@0
  1067
public:
sl@0
  1068
	enum {EMaxDfc=48, EMaxDfcQ=8};
sl@0
  1069
sl@0
  1070
	TDfcStress();
sl@0
  1071
	static TDfcStress* New(TInt aNumDfc, TInt aNumDfcQ, TBool aTimerTest);
sl@0
  1072
	void Create();
sl@0
  1073
	static void DoThreadFn(TAny*);
sl@0
  1074
	void ThreadFn();
sl@0
  1075
	static void BackStopFn(TAny*);
sl@0
  1076
	void Run();
sl@0
  1077
	void Close();
sl@0
  1078
	void DoTestPhase(TInt aMode, TInt aTime, TInt aCount);
sl@0
  1079
	void GetModeText(char* aName);
sl@0
  1080
public:
sl@0
  1081
	TDfcX* NewDfc(TUint32 aId, TUint32 aFlags, TInt aDfcQ);
sl@0
  1082
	TDfcX* NewIDfc(TUint32 aId, TUint32 aFlags, NSchedulable* aTied=0);
sl@0
  1083
	TDfcX* NewTimer(TUint32 aId, TUint32 aFlags, TInt aDfcQ, NSchedulable* aTied);
sl@0
  1084
	void CreateDfc(TUint32 aId);
sl@0
  1085
public:
sl@0
  1086
	TInt iNumDfc;
sl@0
  1087
	TDfcX* iDfcX[EMaxDfc];
sl@0
  1088
	TInt iNumDfcQ;
sl@0
  1089
	TBool iTimerTest;
sl@0
  1090
	TDfcQue* iDfcQ[EMaxDfcQ];
sl@0
  1091
	NThread* iThread[KMaxCpus];
sl@0
  1092
	volatile TBool iStop;
sl@0
  1093
	volatile TInt iRunning;
sl@0
  1094
	volatile TInt iMode;
sl@0
  1095
	NFastSemaphore* iExitSem;
sl@0
  1096
	TDfcX* volatile iGarbage;
sl@0
  1097
	TUint32 iRandomTimeLimit;
sl@0
  1098
	TDfc* iBackStopIdleDfc;
sl@0
  1099
	TDfcQue* iBackStopIdleDfcQ;
sl@0
  1100
	};
sl@0
  1101
sl@0
  1102
void TDfcX::Update()
sl@0
  1103
	{
sl@0
  1104
	TUint32 exc0 = 0;
sl@0
  1105
	TUint32 exc1 = 0;
sl@0
  1106
	if (iExclusionCheck)
sl@0
  1107
		exc0 = *iExclusionCheck;
sl@0
  1108
	TInt cpu = NKern::CurrentCpu();
sl@0
  1109
	__e32_atomic_add_ord32(&iRunCount[cpu], 1);
sl@0
  1110
	TInt ctx = NKern::CurrentContext();
sl@0
  1111
	TBool is_idfc = IsIDFC();
sl@0
  1112
	TBool is_timer = iFlags & EFlag_Timer;
sl@0
  1113
	TBool is_tied = iFlags & EFlag_Tied;
sl@0
  1114
	if ((is_idfc||is_timer) && is_tied && !(iS->iMode & (TDfcStress::EMode_Migrate|TDfcStress::EMode_SelfMigrate)))
sl@0
  1115
		{
sl@0
  1116
		TInt cpu = NKern::CurrentCpu();
sl@0
  1117
		TInt xcpu = iXTied->iCpuAffinity;
sl@0
  1118
		if (cpu != xcpu)
sl@0
  1119
			{
sl@0
  1120
			__crash();
sl@0
  1121
			}
sl@0
  1122
		}
sl@0
  1123
	TInt irq=0;
sl@0
  1124
	if ((ctx!=NKern::EThread) && (iS->iMode & TDfcStress::EMode_AllowCancel))
sl@0
  1125
		irq = iSpinLock.LockIrqSave();
sl@0
  1126
	TUint64 now = fast_counter();
sl@0
  1127
	TUint64 delta = now - iTimeQueued;
sl@0
  1128
	if (TInt64(delta)>=0)
sl@0
  1129
		{
sl@0
  1130
		if (delta > iMaxTime)
sl@0
  1131
			iMaxTime = delta;
sl@0
  1132
		iSumTime += delta;
sl@0
  1133
		}
sl@0
  1134
	if ((ctx!=NKern::EThread) && (iS->iMode & TDfcStress::EMode_AllowCancel))
sl@0
  1135
		iSpinLock.UnlockIrqRestore(irq);
sl@0
  1136
	if (IsIdler())
sl@0
  1137
		{
sl@0
  1138
		TInt i;
sl@0
  1139
		NKern::Lock();
sl@0
  1140
		for (i=0; i<KMaxCpus; ++i)
sl@0
  1141
			{
sl@0
  1142
			NThread* t = iS->iThread[i];
sl@0
  1143
			if (t)
sl@0
  1144
				t->iRequestSemaphore.Reset();
sl@0
  1145
			}
sl@0
  1146
		NKern::Unlock();
sl@0
  1147
		iS->iBackStopIdleDfc->Cancel();
sl@0
  1148
		}
sl@0
  1149
	if (iExclusionCheck)
sl@0
  1150
		exc1 = *iExclusionCheck;
sl@0
  1151
	if (exc0!=exc1)
sl@0
  1152
		__e32_atomic_add_ord32(&iExclusionFailCount, 1);
sl@0
  1153
	}
sl@0
  1154
sl@0
  1155
void TDfcStress::BackStopFn(TAny* a)
sl@0
  1156
	{
sl@0
  1157
	TDfcStress* s = (TDfcStress*)a;
sl@0
  1158
	TInt i;
sl@0
  1159
	NKern::Lock();
sl@0
  1160
	for (i=0; i<KMaxCpus; ++i)
sl@0
  1161
		{
sl@0
  1162
		NThread* t = s->iThread[i];
sl@0
  1163
		if (t)
sl@0
  1164
			t->iRequestSemaphore.Reset();
sl@0
  1165
		}
sl@0
  1166
	NKern::Unlock();
sl@0
  1167
	}
sl@0
  1168
sl@0
  1169
void TDfcX::IDfcFn(TAny* a)
sl@0
  1170
	{
sl@0
  1171
	TDfcX* d = (TDfcX*)a;
sl@0
  1172
	d->Update();
sl@0
  1173
	}
sl@0
  1174
sl@0
  1175
void TDfcX::DfcFn(TAny* a)
sl@0
  1176
	{
sl@0
  1177
	TDfcX* d = (TDfcX*)a;
sl@0
  1178
	d->ThreadActivity();
sl@0
  1179
	d->Update();
sl@0
  1180
	d->ThreadActivity();
sl@0
  1181
	}
sl@0
  1182
sl@0
  1183
void TDfcX::TimerDfcFn(TAny* a)
sl@0
  1184
	{
sl@0
  1185
	TDfcX* d = (TDfcX*)a;
sl@0
  1186
	d->ThreadActivity();
sl@0
  1187
	d->Update();
sl@0
  1188
	d->ThreadActivity();
sl@0
  1189
	}
sl@0
  1190
sl@0
  1191
void TDfcX::TimerIsrFn(TAny* a)
sl@0
  1192
	{
sl@0
  1193
	TDfcX* d = (TDfcX*)a;
sl@0
  1194
	d->Update();
sl@0
  1195
	}
sl@0
  1196
sl@0
  1197
void TDfcX::ThreadActivity()
sl@0
  1198
	{
sl@0
  1199
	TInt ncpus = NKern::NumberOfCpus();
sl@0
  1200
	TInt ocpu = NKern::CurrentCpu();
sl@0
  1201
	NThread* pC = NKern::CurrentThread();
sl@0
  1202
	volatile TUint32* pX = (volatile TUint32*)&pC->iRunCount32[1];	// HACK!
sl@0
  1203
	TInt cpu = ocpu;
sl@0
  1204
	TInt i;
sl@0
  1205
	if ((iS->iMode & TDfcStress::EMode_SelfMigrate) && !pC->iEvents.IsEmpty())
sl@0
  1206
		{
sl@0
  1207
		for (i=0; i<ncpus; ++i)
sl@0
  1208
			{
sl@0
  1209
			++*pX;
sl@0
  1210
			if (++cpu == ncpus)
sl@0
  1211
				cpu = 0;
sl@0
  1212
			NKern::ThreadSetCpuAffinity(pC, cpu);
sl@0
  1213
			}
sl@0
  1214
		}
sl@0
  1215
	else
sl@0
  1216
		{
sl@0
  1217
		++*pX;
sl@0
  1218
		++*pX;
sl@0
  1219
		++*pX;
sl@0
  1220
		++*pX;
sl@0
  1221
		++*pX;
sl@0
  1222
		++*pX;
sl@0
  1223
		++*pX;
sl@0
  1224
		++*pX;
sl@0
  1225
		}
sl@0
  1226
	++*pX;
sl@0
  1227
	++*pX;
sl@0
  1228
	++*pX;
sl@0
  1229
	}
sl@0
  1230
sl@0
  1231
TBool TDfcX::Add(TAny* a)
sl@0
  1232
	{
sl@0
  1233
	TBool is_timer = iFlags & EFlag_Timer;
sl@0
  1234
	if (!a)
sl@0
  1235
		a = iDfc;
sl@0
  1236
	TUint64 time = fast_counter();
sl@0
  1237
	TBool ok;
sl@0
  1238
	if (is_timer)
sl@0
  1239
		ok = ((NTimer*)a)->OneShot(1) == KErrNone;
sl@0
  1240
	else
sl@0
  1241
		ok = ((TDfc*)a)->Add();
sl@0
  1242
	if (ok)
sl@0
  1243
		{
sl@0
  1244
		iTimeQueued = time;
sl@0
  1245
		__e32_atomic_add_ord32(&iQueueCount, 1);
sl@0
  1246
		}
sl@0
  1247
	return ok;
sl@0
  1248
	}
sl@0
  1249
sl@0
  1250
TBool TDfcX::Cancel(TAny* a)
sl@0
  1251
	{
sl@0
  1252
	TBool is_timer = iFlags & EFlag_Timer;
sl@0
  1253
	if (!a)
sl@0
  1254
		a = iDfc;
sl@0
  1255
	TBool ok;
sl@0
  1256
	if (is_timer)
sl@0
  1257
		ok = ((NTimer*)a)->Cancel();
sl@0
  1258
	else
sl@0
  1259
		ok = ((TDfc*)a)->Cancel();
sl@0
  1260
	if (ok)
sl@0
  1261
		__e32_atomic_add_ord32(&iCancelCount, 1);
sl@0
  1262
	return ok;
sl@0
  1263
	}
sl@0
  1264
sl@0
  1265
TBool TDfcX::Enque(TAny* a)
sl@0
  1266
	{
sl@0
  1267
	TBool is_timer = iFlags & EFlag_Timer;
sl@0
  1268
	if (!a)
sl@0
  1269
		a = iDfc;
sl@0
  1270
	TUint64 time = fast_counter();
sl@0
  1271
	TBool ok;
sl@0
  1272
	if (is_timer)
sl@0
  1273
		ok = ((NTimer*)a)->Again(2) == KErrNone;
sl@0
  1274
	else
sl@0
  1275
		ok = ((TDfc*)a)->Enque();
sl@0
  1276
	if (ok)
sl@0
  1277
		{
sl@0
  1278
		iTimeQueued = time;
sl@0
  1279
		__e32_atomic_add_ord32(&iQueueCount, 1);
sl@0
  1280
		}
sl@0
  1281
	return ok;
sl@0
  1282
	}
sl@0
  1283
sl@0
  1284
TBool TDfcX::QueueOnIdle(TAny* a)
sl@0
  1285
	{
sl@0
  1286
	TBool is_timer = iFlags & EFlag_Timer;
sl@0
  1287
	if (is_timer)
sl@0
  1288
		return FALSE;
sl@0
  1289
	if (!a)
sl@0
  1290
		a = iDfc;
sl@0
  1291
	TUint64 time = fast_counter();
sl@0
  1292
	TBool ok = ((TDfc*)a)->QueueOnIdle();
sl@0
  1293
	if (ok)
sl@0
  1294
		{
sl@0
  1295
		iTimeQueued = time;
sl@0
  1296
		__e32_atomic_add_ord32(&iQueueCount, 1);
sl@0
  1297
		}
sl@0
  1298
	return ok;
sl@0
  1299
	}
sl@0
  1300
sl@0
  1301
TBool TDfcX::SafeAdd()
sl@0
  1302
	{
sl@0
  1303
	TBool ret = FALSE;
sl@0
  1304
	TUint32 x = set_bit0_if_nonnull((TUint32&)iDfc);
sl@0
  1305
	if (x && !(x&1))
sl@0
  1306
		{
sl@0
  1307
		TDfc* p = (TDfc*)x;
sl@0
  1308
		ret = Add(p);
sl@0
  1309
		flip_bit0((TUint32&)iDfc);
sl@0
  1310
		}
sl@0
  1311
	return ret;
sl@0
  1312
	}
sl@0
  1313
sl@0
  1314
TBool TDfcX::SafeEnque()
sl@0
  1315
	{
sl@0
  1316
	TBool ret = FALSE;
sl@0
  1317
	TUint32 x = set_bit0_if_nonnull((TUint32&)iDfc);
sl@0
  1318
	if (x && !(x&1))
sl@0
  1319
		{
sl@0
  1320
		TDfc* p = (TDfc*)x;
sl@0
  1321
		ret = Enque(p);
sl@0
  1322
		flip_bit0((TUint32&)iDfc);
sl@0
  1323
		}
sl@0
  1324
	return ret;
sl@0
  1325
	}
sl@0
  1326
sl@0
  1327
TBool TDfcX::SafeQueueOnIdle()
sl@0
  1328
	{
sl@0
  1329
	TBool ret = FALSE;
sl@0
  1330
	TUint32 x = set_bit0_if_nonnull((TUint32&)iDfc);
sl@0
  1331
	if (x && !(x&1))
sl@0
  1332
		{
sl@0
  1333
		TDfc* p = (TDfc*)x;
sl@0
  1334
		ret = QueueOnIdle(p);
sl@0
  1335
		flip_bit0((TUint32&)iDfc);
sl@0
  1336
		}
sl@0
  1337
	return ret;
sl@0
  1338
	}
sl@0
  1339
sl@0
  1340
TBool TDfcX::SafeCancel()
sl@0
  1341
	{
sl@0
  1342
	TBool ret = FALSE;
sl@0
  1343
	TUint32 x = swap_out_if_bit0_clear((TUint32&)iDfc);
sl@0
  1344
	if (x && !(x&1))
sl@0
  1345
		{
sl@0
  1346
		if (iFlags & EFlag_Timer)
sl@0
  1347
			{
sl@0
  1348
			NTimer* p = (NTimer*)x;
sl@0
  1349
			ret = Cancel(p);
sl@0
  1350
			p->~NTimer();
sl@0
  1351
			memset(p, 0xbb, sizeof(NTimer));
sl@0
  1352
			free(p);
sl@0
  1353
			}
sl@0
  1354
		else
sl@0
  1355
			{
sl@0
  1356
			TDfc* p = (TDfc*)x;
sl@0
  1357
			ret = Cancel(p);
sl@0
  1358
			p->~TDfc();
sl@0
  1359
			memset(p, 0xbb, sizeof(TDfc));
sl@0
  1360
			free(p);
sl@0
  1361
			}
sl@0
  1362
		CreateDfcOrTimer();
sl@0
  1363
		}
sl@0
  1364
	return ret;
sl@0
  1365
	}
sl@0
  1366
sl@0
  1367
void TDfcX::GetDesc(char* a)
sl@0
  1368
	{
sl@0
  1369
	memset(a, 0x20, 8);
sl@0
  1370
	if (iFlags & EFlag_Timer)
sl@0
  1371
		*a++ = 'T';
sl@0
  1372
	if (iFlags & EFlag_IDFC)
sl@0
  1373
		*a++ = 'I';
sl@0
  1374
	if (iFlags & EFlag_DFC)
sl@0
  1375
		*a++ = 'D';
sl@0
  1376
	if (iFlags & EFlag_IdleDFC)
sl@0
  1377
		*a++ = 'i';
sl@0
  1378
	if (iFlags & EFlag_Tied)
sl@0
  1379
		*a++ = 't';
sl@0
  1380
	}
sl@0
  1381
sl@0
  1382
TDfcStress::TDfcStress()
sl@0
  1383
	{
sl@0
  1384
	memclr(this, sizeof(*this));
sl@0
  1385
	}
sl@0
  1386
sl@0
  1387
TDfcX* TDfcStress::NewDfc(TUint32 aId, TUint32 aFlags, TInt aDfcQ)
sl@0
  1388
	{
sl@0
  1389
	TDfcX* d = new TDfcX;
sl@0
  1390
	TEST_OOM(d);
sl@0
  1391
	d->iId = aId;
sl@0
  1392
	d->iFlags = aFlags;
sl@0
  1393
	d->iS = this;
sl@0
  1394
sl@0
  1395
	d->iDfcQ = iDfcQ[aDfcQ];
sl@0
  1396
	d->CreateDfcOrTimer();
sl@0
  1397
	return d;
sl@0
  1398
	}
sl@0
  1399
sl@0
  1400
TDfcX* TDfcStress::NewIDfc(TUint32 aId, TUint32 aFlags, NSchedulable* aTied)
sl@0
  1401
	{
sl@0
  1402
	TDfcX* d = new TDfcX;
sl@0
  1403
	TEST_OOM(d);
sl@0
  1404
	d->iId = aId;
sl@0
  1405
	d->iFlags = aFlags;
sl@0
  1406
	d->iS = this;
sl@0
  1407
sl@0
  1408
	d->iXTied = aTied;
sl@0
  1409
	d->CreateDfcOrTimer();
sl@0
  1410
	return d;
sl@0
  1411
	}
sl@0
  1412
sl@0
  1413
TDfcX* TDfcStress::NewTimer(TUint32 aId, TUint32 aFlags, TInt aDfcQ, NSchedulable* aTied)
sl@0
  1414
	{
sl@0
  1415
	TDfcX* d = new TDfcX;
sl@0
  1416
	TEST_OOM(d);
sl@0
  1417
	d->iId = aId;
sl@0
  1418
	d->iFlags = aFlags;
sl@0
  1419
	d->iS = this;
sl@0
  1420
sl@0
  1421
	d->iDfcQ = (aFlags & TDfcX::EFlag_DFC) ? iDfcQ[aDfcQ] : 0;
sl@0
  1422
	d->iXTied = (aFlags & TDfcX::EFlag_Tied) ? aTied : 0;
sl@0
  1423
	d->CreateDfcOrTimer();
sl@0
  1424
	return d;
sl@0
  1425
	}
sl@0
  1426
sl@0
  1427
sl@0
  1428
void TDfcX::CreateDfcOrTimer()
sl@0
  1429
	{
sl@0
  1430
//	volatile TUint32* xc = 0;
sl@0
  1431
	NThreadBase* t = iS->iDfcQ[0]->iThread;
sl@0
  1432
	volatile TUint32* xc = &t->iRunCount32[1];	// HACK!
sl@0
  1433
	if (!(iFlags & EFlag_Timer))
sl@0
  1434
		{
sl@0
  1435
		TDfc* d = 0;
sl@0
  1436
		if (!(iFlags & EFlag_IDFC))
sl@0
  1437
			{
sl@0
  1438
			d = new TDfc(&TDfcX::DfcFn, this, iDfcQ, 1);
sl@0
  1439
			xc = (volatile TUint32*)&iDfcQ->iThread->iRunCount32[1];
sl@0
  1440
			}
sl@0
  1441
		else if (iFlags & EFlag_Tied)
sl@0
  1442
			{
sl@0
  1443
			d = new TDfc(iXTied, &TDfcX::IDfcFn, this);
sl@0
  1444
			xc = (volatile TUint32*)&iXTied->iRunCount32[1];
sl@0
  1445
			}
sl@0
  1446
		else
sl@0
  1447
			d = new TDfc(&TDfcX::IDfcFn, this);
sl@0
  1448
		__NK_ASSERT_ALWAYS(d!=0);
sl@0
  1449
		__e32_atomic_store_rel_ptr(&iDfc, d);
sl@0
  1450
		}
sl@0
  1451
	else
sl@0
  1452
		{
sl@0
  1453
		NTimer* tmr = 0;
sl@0
  1454
		if (iFlags & EFlag_DFC)
sl@0
  1455
			{
sl@0
  1456
			tmr = new NTimer(&TDfcX::TimerDfcFn, this, iDfcQ, 1);
sl@0
  1457
			xc = (volatile TUint32*)&iDfcQ->iThread->iRunCount32[1];
sl@0
  1458
			}
sl@0
  1459
		else if (iFlags & EFlag_Tied)
sl@0
  1460
			{
sl@0
  1461
			tmr = new NTimer(iXTied, &TDfcX::TimerIsrFn, this);
sl@0
  1462
			xc = (volatile TUint32*)&iXTied->iRunCount32[1];
sl@0
  1463
			}
sl@0
  1464
		else
sl@0
  1465
			tmr = new NTimer(&TDfcX::TimerIsrFn, this);
sl@0
  1466
		__NK_ASSERT_ALWAYS(tmr!=0);
sl@0
  1467
		__e32_atomic_store_rel_ptr(&iTimer, tmr);
sl@0
  1468
		}
sl@0
  1469
	iExclusionCheck = xc;
sl@0
  1470
	}
sl@0
  1471
sl@0
  1472
TDfcStress* TDfcStress::New(TInt aNumDfc, TInt aNumDfcQ, TBool aTimerTest)
sl@0
  1473
	{
sl@0
  1474
	TDfcStress* p = new TDfcStress;
sl@0
  1475
	TEST_OOM(p);
sl@0
  1476
	p->iTimerTest = aTimerTest;
sl@0
  1477
	p->iNumDfc = aNumDfc;
sl@0
  1478
	p->iNumDfcQ = aNumDfcQ;
sl@0
  1479
	p->Create();
sl@0
  1480
	return p;
sl@0
  1481
	}
sl@0
  1482
sl@0
  1483
void TDfcStress::Create()
sl@0
  1484
	{
sl@0
  1485
DEBUGPRINT("TDfcStress @ %08x", this);
sl@0
  1486
	TInt i;
sl@0
  1487
	TInt num_cpus = NKern::NumberOfCpus();
sl@0
  1488
	TInt cpu = 0;
sl@0
  1489
	iExitSem = new NFastSemaphore(0);
sl@0
  1490
	TEST_OOM(iExitSem);
sl@0
  1491
	for (i=0; i<iNumDfcQ; ++i)
sl@0
  1492
		{
sl@0
  1493
		char c[8] = "DFCQ*";
sl@0
  1494
		c[4] = (char)('0'+i);
sl@0
  1495
		TDfcQue* q = CreateDfcQ(c, 32, cpu);
sl@0
  1496
		TEST_OOM(q);
sl@0
  1497
		iDfcQ[i] = q;
sl@0
  1498
		if (++cpu == num_cpus)
sl@0
  1499
			cpu = 0;
sl@0
  1500
		NThreadBase* t = q->iThread;
sl@0
  1501
DEBUGPRINT("DfcQ %2d @ %08x Thread @%08x Stack %08x+%08x", i, iDfcQ[i], t, t->iStackBase, t->iStackSize);
sl@0
  1502
		}
sl@0
  1503
	iBackStopIdleDfcQ = CreateDfcQ("BackStop", 1, 0);
sl@0
  1504
	TEST_OOM(iBackStopIdleDfcQ);
sl@0
  1505
	iBackStopIdleDfc = new TDfc(&BackStopFn, this, iBackStopIdleDfcQ, 1);
sl@0
  1506
	TEST_OOM(iBackStopIdleDfc);
sl@0
  1507
	for (i=0; i<num_cpus; ++i)
sl@0
  1508
		{
sl@0
  1509
		char c[8] = "THRD*";
sl@0
  1510
		c[4] = (char)('0'+i);
sl@0
  1511
		NThread* t = CreateUnresumedThreadSignalOnExit(c, &DoThreadFn, 11, this, 0, -1, iExitSem, i);
sl@0
  1512
		TEST_OOM(t);
sl@0
  1513
		iThread[i] = t;
sl@0
  1514
DEBUGPRINT("Thread %2d @ %08x (Stack %08x+%08x)", i, iThread[i], t->iStackBase, t->iStackSize);
sl@0
  1515
		}
sl@0
  1516
	for (i=0; i<iNumDfc; ++i)
sl@0
  1517
		{
sl@0
  1518
		CreateDfc(i);
sl@0
  1519
DEBUGPRINT("DfcX %2d @ %08x (DFC @ %08x)", i, iDfcX[i], iDfcX[i]->iDfc);
sl@0
  1520
		}
sl@0
  1521
	}
sl@0
  1522
sl@0
  1523
void TDfcStress::CreateDfc(TUint32 aId)
sl@0
  1524
	{
sl@0
  1525
	TUint32 type = aId & 7;
sl@0
  1526
	TUint32 q = aId % iNumDfcQ;
sl@0
  1527
	TDfcX* d = 0;
sl@0
  1528
	switch (type)
sl@0
  1529
		{
sl@0
  1530
		case 0:
sl@0
  1531
		case 1:
sl@0
  1532
		case 2:
sl@0
  1533
		case 3:
sl@0
  1534
			if (iTimerTest)
sl@0
  1535
				d = NewTimer(aId, TDfcX::EFlag_Timer|TDfcX::EFlag_DFC, q, 0);
sl@0
  1536
			else
sl@0
  1537
				d = NewDfc(aId, TDfcX::EFlag_DFC, q);
sl@0
  1538
			break;
sl@0
  1539
		case 4:
sl@0
  1540
		case 5:
sl@0
  1541
			if (iTimerTest)
sl@0
  1542
				d = NewTimer(aId, TDfcX::EFlag_Timer|TDfcX::EFlag_Tied, 0, iDfcQ[iNumDfcQ-1-(type&1)]->iThread);
sl@0
  1543
			else
sl@0
  1544
				{
sl@0
  1545
				if (aId>=16 && aId<32 && iNumDfcQ>2)
sl@0
  1546
					d = NewIDfc(aId, TDfcX::EFlag_IDFC|TDfcX::EFlag_Tied, iDfcQ[2]->iThread);
sl@0
  1547
				else
sl@0
  1548
					d = NewIDfc(aId, TDfcX::EFlag_IDFC);
sl@0
  1549
				}
sl@0
  1550
			break;
sl@0
  1551
		case 6:
sl@0
  1552
		case 7:
sl@0
  1553
			if (iTimerTest)
sl@0
  1554
				d = NewTimer(aId, TDfcX::EFlag_Timer, 0, 0);
sl@0
  1555
			else
sl@0
  1556
				d = NewDfc(aId, TDfcX::EFlag_DFC|TDfcX::EFlag_IdleDFC, q);
sl@0
  1557
			break;
sl@0
  1558
		};
sl@0
  1559
	__e32_atomic_store_rel_ptr(&iDfcX[aId], d);
sl@0
  1560
	}
sl@0
  1561
sl@0
  1562
void TDfcStress::Close()
sl@0
  1563
	{
sl@0
  1564
	TInt i;
sl@0
  1565
sl@0
  1566
	// delete DFCs before the DFC queues they might be on
sl@0
  1567
	for (i=0; i<iNumDfc; ++i)
sl@0
  1568
		{
sl@0
  1569
		TDfcX* d = iDfcX[i];
sl@0
  1570
		delete d;
sl@0
  1571
		}
sl@0
  1572
	delete iBackStopIdleDfc;
sl@0
  1573
sl@0
  1574
	for (i=0; i<iNumDfcQ; ++i)
sl@0
  1575
		DestroyDfcQ(iDfcQ[i]);
sl@0
  1576
	DestroyDfcQ(iBackStopIdleDfcQ);
sl@0
  1577
sl@0
  1578
	delete iExitSem;
sl@0
  1579
	delete this;
sl@0
  1580
	}
sl@0
  1581
sl@0
  1582
void TDfcStress::DoThreadFn(TAny* a)
sl@0
  1583
	{
sl@0
  1584
	((TDfcStress*)a)->ThreadFn();
sl@0
  1585
	}
sl@0
  1586
sl@0
  1587
void append(char*& a, const char* s)
sl@0
  1588
	{
sl@0
  1589
	while(*s)
sl@0
  1590
		*a++ = *s++;
sl@0
  1591
	*a=0;
sl@0
  1592
	}
sl@0
  1593
sl@0
  1594
void TDfcStress::GetModeText(char* a)
sl@0
  1595
	{
sl@0
  1596
	memclr(a,128);
sl@0
  1597
	if (iMode==0)
sl@0
  1598
		{
sl@0
  1599
		append(a, "Add only");
sl@0
  1600
		return;
sl@0
  1601
		}
sl@0
  1602
	if (iMode & EMode_Wait)
sl@0
  1603
		append(a, "Wait ");
sl@0
  1604
	if (iMode & EMode_AllowCancel)
sl@0
  1605
		append(a, "Cancel ");
sl@0
  1606
	if (iMode & EMode_AllowIdle)
sl@0
  1607
		append(a, "Idle ");
sl@0
  1608
	if (iMode & EMode_AllowEnque)
sl@0
  1609
		append(a, "Enque ");
sl@0
  1610
	if (iMode & EMode_Recycle)
sl@0
  1611
		append(a, "Recycle ");
sl@0
  1612
	if (iMode & EMode_Migrate)
sl@0
  1613
		append(a, "Migrate ");
sl@0
  1614
	if (iMode & EMode_SelfMigrate)
sl@0
  1615
		append(a, "SelfMigrate ");
sl@0
  1616
	}
sl@0
  1617
sl@0
  1618
/*
sl@0
  1619
Test Mode:
sl@0
  1620
sl@0
  1621
Bit 31	If set causes thread to exit
sl@0
  1622
Bit 0	If set does random wait after each operation
sl@0
  1623
Bit 1	Allows Cancel operations if set
sl@0
  1624
Bit 2	Allows idle operations if set
sl@0
  1625
Bit 3	Test Enque() as well as Add()
sl@0
  1626
Bit 4	Use SafeXXX operations
sl@0
  1627
Bit 5	Use tied IDFCs
sl@0
  1628
Bit 6	Migrate threads with things tied to them
sl@0
  1629
Bit 7	Threads with things tied to them migrate themselves during execution
sl@0
  1630
sl@0
  1631
*/
sl@0
  1632
void TDfcStress::ThreadFn()
sl@0
  1633
	{
sl@0
  1634
	TBool finish = FALSE;
sl@0
  1635
	TUint32 seed[2];
sl@0
  1636
	seed[0] = NKern::CurrentCpu() ^ 0xddb3d743;
sl@0
  1637
	seed[1] = 0;
sl@0
  1638
	FOREVER
sl@0
  1639
		{
sl@0
  1640
		if (iStop)
sl@0
  1641
			{
sl@0
  1642
			__e32_atomic_add_ord32(&iRunning, (TUint32)(-1));
sl@0
  1643
			while (iStop)
sl@0
  1644
				{
sl@0
  1645
				if (iMode<0)
sl@0
  1646
					{
sl@0
  1647
					finish = TRUE;
sl@0
  1648
					break;
sl@0
  1649
					}
sl@0
  1650
				}
sl@0
  1651
			if (finish)
sl@0
  1652
				break;
sl@0
  1653
			else
sl@0
  1654
				__e32_atomic_add_ord32(&iRunning, 1);
sl@0
  1655
			}
sl@0
  1656
		if (iMode & EMode_Wait)
sl@0
  1657
			{
sl@0
  1658
			TUint32 wait = random(seed);
sl@0
  1659
			wait %= iRandomTimeLimit;
sl@0
  1660
			wait += 1;
sl@0
  1661
			fcfspin(wait);
sl@0
  1662
			}
sl@0
  1663
		TUint32 action = random(seed);
sl@0
  1664
		TUint32 action2 = random(seed);
sl@0
  1665
		if (action & 0xff000000)
sl@0
  1666
			{
sl@0
  1667
			// queue or cancel a DFC or timer
sl@0
  1668
			TBool cancel = action2 & 2;
sl@0
  1669
			TUint32 id = action % iNumDfc;
sl@0
  1670
			TDfcX* d = iDfcX[id];
sl@0
  1671
			if (iMode & EMode_Recycle)
sl@0
  1672
				{
sl@0
  1673
				TBool isIDFC = d->IsIDFC();
sl@0
  1674
				TBool isIdler = d->IsIdler();
sl@0
  1675
				if (cancel)
sl@0
  1676
					d->SafeCancel();
sl@0
  1677
				else if (isIdler)
sl@0
  1678
					d->SafeQueueOnIdle();
sl@0
  1679
				else if ((iMode & EMode_AllowEnque) && (action2 & 1) && !isIDFC)
sl@0
  1680
					d->SafeEnque();
sl@0
  1681
				else
sl@0
  1682
					{
sl@0
  1683
					NKern::Lock();
sl@0
  1684
					d->SafeAdd();
sl@0
  1685
					NKern::Unlock();
sl@0
  1686
					}
sl@0
  1687
				}
sl@0
  1688
			else
sl@0
  1689
				{
sl@0
  1690
				if (cancel && (iMode & EMode_AllowCancel))
sl@0
  1691
					{
sl@0
  1692
					d->Cancel();
sl@0
  1693
					}
sl@0
  1694
				else if (!d->IsIdler())
sl@0
  1695
					{
sl@0
  1696
					if ((iMode & EMode_AllowEnque) && (action2 & 1) && !d->IsIDFC())
sl@0
  1697
						{
sl@0
  1698
						d->Enque();
sl@0
  1699
						}
sl@0
  1700
					else
sl@0
  1701
						{
sl@0
  1702
						NKern::Lock();
sl@0
  1703
						d->Add();
sl@0
  1704
						NKern::Unlock();
sl@0
  1705
						}
sl@0
  1706
					}
sl@0
  1707
				else
sl@0
  1708
					{
sl@0
  1709
					d->QueueOnIdle();
sl@0
  1710
					}
sl@0
  1711
				}
sl@0
  1712
			continue;
sl@0
  1713
			}
sl@0
  1714
		if (iMode & EMode_AllowIdle)
sl@0
  1715
			{
sl@0
  1716
			iBackStopIdleDfc->QueueOnIdle();
sl@0
  1717
			NKern::WaitForAnyRequest();
sl@0
  1718
			}
sl@0
  1719
		}
sl@0
  1720
	}
sl@0
  1721
sl@0
  1722
void StopTimeout(TAny*)
sl@0
  1723
	{
sl@0
  1724
	__crash();
sl@0
  1725
	}
sl@0
  1726
sl@0
  1727
void TDfcStress::DoTestPhase(TInt aMode, TInt aTime, TInt aCount)
sl@0
  1728
	{
sl@0
  1729
	char mode_text[128];
sl@0
  1730
	TInt i;
sl@0
  1731
	TUint32 maxavg = 0;
sl@0
  1732
	TInt n;
sl@0
  1733
	iMode = aMode;
sl@0
  1734
	iStop = FALSE;
sl@0
  1735
	GetModeText(mode_text);
sl@0
  1736
	TEST_PRINT1("Testing with: %s", mode_text);
sl@0
  1737
	for (i=0; i<aCount; ++i)
sl@0
  1738
		{
sl@0
  1739
		NKern::Sleep(aTime);
sl@0
  1740
		DebugPrint(".",1);
sl@0
  1741
		}
sl@0
  1742
	DebugPrint("\r\n",2);
sl@0
  1743
	TEST_PRINT("Stopping ...");
sl@0
  1744
	iStop = TRUE;
sl@0
  1745
	NTimer timer(&StopTimeout, 0);
sl@0
  1746
	timer.OneShot(2000);
sl@0
  1747
	BackStopFn(this);
sl@0
  1748
	n = 0;
sl@0
  1749
	while (iRunning && ++n<=100)
sl@0
  1750
		NKern::Sleep(10);
sl@0
  1751
	if (iRunning)
sl@0
  1752
		{
sl@0
  1753
		__crash();
sl@0
  1754
		}
sl@0
  1755
	iBackStopIdleDfc->Cancel();
sl@0
  1756
	timer.Cancel();
sl@0
  1757
	TEST_PRINT("Threads stopped");
sl@0
  1758
	for (i=0; i<iNumDfcQ; ++i)
sl@0
  1759
		{
sl@0
  1760
		TUint32 ev = iDfcQ[i]->iThread->iEventState;
sl@0
  1761
		DEBUGPRINT("DfcThread %d EventState = %08x", i, ev);
sl@0
  1762
		TEST_RESULT(!(ev & NSchedulable::EEventCountMask), "");
sl@0
  1763
		}
sl@0
  1764
	for (i=0; i<NKern::NumberOfCpus(); ++i)
sl@0
  1765
		{
sl@0
  1766
		TUint32 ev = iThread[i]->iEventState;
sl@0
  1767
		DEBUGPRINT("Thread    %d EventState = %08x", i, ev);
sl@0
  1768
		TEST_RESULT(!(ev & NSchedulable::EEventCountMask), "");
sl@0
  1769
		}
sl@0
  1770
	NKern::Sleep(10);
sl@0
  1771
	for (i=0; i<iNumDfc; ++i)
sl@0
  1772
		{
sl@0
  1773
		TDfcX* d = iDfcX[i];
sl@0
  1774
		d->Cancel();
sl@0
  1775
		TUint32 qc = d->iQueueCount;
sl@0
  1776
		TUint32* rc = d->iRunCount;
sl@0
  1777
		TUint32 totrc = rc[0] + rc[1] + rc[2] + rc[3] + rc[4] + rc[5] + rc[6] + rc[7];
sl@0
  1778
		TUint32 cc = d->iCancelCount;
sl@0
  1779
		TUint32 f = d->iFlags;
sl@0
  1780
//		TUint32 imm = d->IsIDFC()?1:0;
sl@0
  1781
		TUint32 max = d->iMaxTime;
sl@0
  1782
		TUint32 avg = 0;
sl@0
  1783
		TUint32 xfc = d->iExclusionFailCount;
sl@0
  1784
		if (totrc)
sl@0
  1785
			avg = TUint32(d->iSumTime / TUint64(totrc));
sl@0
  1786
		if (avg > maxavg)
sl@0
  1787
			maxavg = avg;
sl@0
  1788
		char desc[16];
sl@0
  1789
		memclr(desc,16);
sl@0
  1790
		d->GetDesc(desc);
sl@0
  1791
		DEBUGPRINT("%2d: %s QC %9d RC %9d CC %9d MAX %9d AVG %9d XFC %9d RC %9d %9d %9d %9d %9d %9d %9d %9d", i, desc, qc, totrc, cc, max, avg, xfc, rc[0], rc[1], rc[2], rc[3], rc[4], rc[5], rc[6], rc[7]);
sl@0
  1792
		TInt diff = (TInt)(qc - (totrc+cc));
sl@0
  1793
		TEST_RESULT1(diff==0, "Counts mismatched, diff=%d", diff);
sl@0
  1794
		TEST_RESULT(!(f&TDfcX::EFlag_Tied) || xfc==0, "Exclusion Failure!");
sl@0
  1795
		d->iQueueCount = 0;
sl@0
  1796
		memclr(d->iRunCount, sizeof(d->iRunCount));
sl@0
  1797
		d->iCancelCount = 0;
sl@0
  1798
		d->iMaxTime = 0;
sl@0
  1799
		d->iSumTime = 0;
sl@0
  1800
		}
sl@0
  1801
	if (!iRandomTimeLimit)
sl@0
  1802
		iRandomTimeLimit = maxavg + (maxavg>>1);
sl@0
  1803
	}
sl@0
  1804
sl@0
  1805
void TDfcStress::Run()
sl@0
  1806
	{
sl@0
  1807
	TInt i;
sl@0
  1808
	NThread* t;
sl@0
  1809
	iStop = FALSE;
sl@0
  1810
	iMode = 0;
sl@0
  1811
	TInt num_cpus = NKern::NumberOfCpus();
sl@0
  1812
	iRunning = num_cpus;
sl@0
  1813
	for (i=0; i<KMaxCpus; ++i)
sl@0
  1814
		{
sl@0
  1815
		t = iThread[i];
sl@0
  1816
		if (t)
sl@0
  1817
			NKern::ThreadResume(t);
sl@0
  1818
		}
sl@0
  1819
	TEST_PRINT("Threads resumed");
sl@0
  1820
sl@0
  1821
sl@0
  1822
	DoTestPhase(0x00, 10000, 1);
sl@0
  1823
	if (iTimerTest)
sl@0
  1824
		{
sl@0
  1825
		const TInt N = 20;
sl@0
  1826
		DoTestPhase(EMode_AllowCancel|EMode_AllowEnque|EMode_Recycle, 10000, N);
sl@0
  1827
		DoTestPhase(EMode_AllowCancel|EMode_AllowEnque|EMode_Wait|EMode_Recycle, 10000, N);
sl@0
  1828
		DoTestPhase(EMode_AllowCancel|EMode_AllowEnque|EMode_Recycle|EMode_SelfMigrate, 10000, N);
sl@0
  1829
		DoTestPhase(EMode_AllowCancel|EMode_AllowEnque|EMode_Wait|EMode_Recycle|EMode_SelfMigrate, 10000, N);
sl@0
  1830
		DoTestPhase(EMode_AllowCancel|EMode_AllowEnque, 10000, N);
sl@0
  1831
		DoTestPhase(EMode_AllowCancel|EMode_AllowEnque|EMode_Wait, 10000, N);
sl@0
  1832
		DoTestPhase(EMode_AllowCancel|EMode_AllowEnque|EMode_SelfMigrate, 10000, N);
sl@0
  1833
		DoTestPhase(EMode_AllowCancel|EMode_AllowEnque|EMode_Wait|EMode_SelfMigrate, 10000, N);
sl@0
  1834
		}
sl@0
  1835
	else
sl@0
  1836
		{
sl@0
  1837
		DoTestPhase(EMode_AllowCancel|EMode_AllowEnque, 10000, 20);
sl@0
  1838
		DoTestPhase(EMode_AllowCancel|EMode_AllowIdle|EMode_AllowEnque, 10000, 20);
sl@0
  1839
		DoTestPhase(EMode_AllowIdle|EMode_AllowEnque, 10000, 20);
sl@0
  1840
		DoTestPhase(EMode_AllowCancel|EMode_AllowIdle, 10000, 20);
sl@0
  1841
		DoTestPhase(EMode_AllowCancel|EMode_Wait, 10000, 20);
sl@0
  1842
		DoTestPhase(EMode_AllowCancel, 10000, 20);
sl@0
  1843
		DoTestPhase(EMode_AllowCancel|EMode_AllowIdle|EMode_AllowEnque|EMode_Recycle, 10000, 20);
sl@0
  1844
		}
sl@0
  1845
sl@0
  1846
	iMode = EMode_Exit;
sl@0
  1847
	TEST_PRINT("Terminating threads");
sl@0
  1848
	for (i=0; i<num_cpus; ++i)
sl@0
  1849
		NKern::FSWait(iExitSem);
sl@0
  1850
	TEST_PRINT("Done");
sl@0
  1851
	}
sl@0
  1852
sl@0
  1853
void DoStressTest(TBool aTimerTest)
sl@0
  1854
	{
sl@0
  1855
	TEST_PRINT("Stress test...");
sl@0
  1856
	TInt ndfcs=0;
sl@0
  1857
	TInt ndfcq=0;
sl@0
  1858
	switch (NKern::NumberOfCpus())
sl@0
  1859
		{
sl@0
  1860
		case 1:	ndfcs=16; ndfcq=2; break;
sl@0
  1861
		case 2:	ndfcs=16; ndfcq=2; break;
sl@0
  1862
		case 3:	ndfcs=24; ndfcq=2; break;
sl@0
  1863
		case 4:	ndfcs=32; ndfcq=3; break;
sl@0
  1864
		case 5:	ndfcs=32; ndfcq=3; break;
sl@0
  1865
		case 6:	ndfcs=48; ndfcq=4; break;
sl@0
  1866
		case 7:	ndfcs=48; ndfcq=4; break;
sl@0
  1867
		case 8:	ndfcs=48; ndfcq=4; break;
sl@0
  1868
		default:
sl@0
  1869
			__NK_ASSERT_ALWAYS(0);
sl@0
  1870
			break;
sl@0
  1871
		}
sl@0
  1872
	TDfcStress* ds = TDfcStress::New(ndfcs, ndfcq, aTimerTest);
sl@0
  1873
	ds->Run();
sl@0
  1874
	ds->Close();
sl@0
  1875
	}
sl@0
  1876
sl@0
  1877
#endif
sl@0
  1878
sl@0
  1879
void TestDFCs()
sl@0
  1880
	{
sl@0
  1881
	TEST_PRINT("Testing DFCs...");
sl@0
  1882
sl@0
  1883
	TTestDfc::Buffer = CircBuf::New(TTestDfc::EBufferSlots);
sl@0
  1884
	TInt cpu;
sl@0
  1885
	(void)cpu;
sl@0
  1886
#ifdef __SMP__
sl@0
  1887
	DoStressTest(TRUE);
sl@0
  1888
#endif
sl@0
  1889
sl@0
  1890
	DoDFCTest1();
sl@0
  1891
	DoDFCTest2();
sl@0
  1892
	for_each_cpu(cpu)
sl@0
  1893
		{
sl@0
  1894
		DoDFCTest3(cpu);
sl@0
  1895
		}
sl@0
  1896
	DoIDFCTest1();
sl@0
  1897
	for_each_cpu(cpu)
sl@0
  1898
		{
sl@0
  1899
		DoIdleDFCTest1(cpu);
sl@0
  1900
		}
sl@0
  1901
	DoIdleDFCTest2();
sl@0
  1902
sl@0
  1903
#ifdef __SMP__
sl@0
  1904
	DoStressTest(FALSE);
sl@0
  1905
#endif
sl@0
  1906
sl@0
  1907
	delete TTestDfc::Buffer;
sl@0
  1908
	TTestDfc::Buffer = 0;
sl@0
  1909
	}