kerneltest/e32test/prime/t_semutx.cpp
changeset 152 657f875b013e
parent 0 a41df078684a
child 177 a232af6b0b1f
equal deleted inserted replaced
139:95f71bcdcdb7 152:657f875b013e
    36 // Failures and causes:
    36 // Failures and causes:
    37 // Base Port information:
    37 // Base Port information:
    38 // 
    38 // 
    39 //
    39 //
    40 
    40 
       
    41 #define __E32TEST_EXTENSION__
    41 #include <e32test.h>
    42 #include <e32test.h>
       
    43 #include <u32std.h>
       
    44 #include <e32svr.h>
    42 
    45 
    43 const TInt KMaxBufferSize=10;
    46 const TInt KMaxBufferSize=10;
    44 const TInt KMaxArraySize=10;
    47 const TInt KMaxArraySize=10;
    45 const TInt KNumProducerItems=100;
    48 const TInt KNumProducerItems=100;
    46 
    49 
   219 	}
   222 	}
   220 
   223 
   221 void StartWaitSemThread(RThread& aT, SWaitSem& aW, TThreadPriority aP=EPriorityLess)
   224 void StartWaitSemThread(RThread& aT, SWaitSem& aW, TThreadPriority aP=EPriorityLess)
   222 	{
   225 	{
   223 	TInt r = aT.Create(KNullDesC, &WaitSemThread, 0x1000, 0x1000, 0x1000, &aW);
   226 	TInt r = aT.Create(KNullDesC, &WaitSemThread, 0x1000, 0x1000, 0x1000, &aW);
   224 	test(r==KErrNone);
   227 	test_KErrNone(r);
   225 	aT.SetPriority(aP);
   228 	aT.SetPriority(aP);
   226 	aT.Resume();
   229 	aT.Resume();
   227 	}
   230 	}
   228 
   231 
   229 void WaitForWaitSemThread(RThread& aT, TInt aResult)
   232 void WaitForWaitSemThread(RThread& aT, TInt aResult)
   230 	{
   233 	{
   231 	TRequestStatus s;
   234 	TRequestStatus s;
   232 	aT.Logon(s);
   235 	aT.Logon(s);
   233 	User::WaitForRequest(s);
   236 	User::WaitForRequest(s);
   234 	test(aT.ExitType()==EExitKill);
   237 	test_Equal(EExitKill, aT.ExitType());
   235 	test(aT.ExitReason()==aResult);
   238 	test_Equal(aResult, aT.ExitReason());
   236 	test(s.Int()==aResult);
   239 	test_Equal(aResult, s.Int());
   237 	CLOSE_AND_WAIT(aT);
   240 	CLOSE_AND_WAIT(aT);
   238 	}
   241 	}
   239 
   242 
   240 TInt DummyThread(TAny*)
   243 TInt DummyThread(TAny*)
   241 	{
   244 	{
   249 	RThread t;
   252 	RThread t;
   250 	TTime initial;
   253 	TTime initial;
   251 	TTime final;
   254 	TTime final;
   252 	TInt elapsed=0;
   255 	TInt elapsed=0;
   253 	TInt r = ws.iSem.CreateLocal(0);
   256 	TInt r = ws.iSem.CreateLocal(0);
   254 	test(r==KErrNone);
   257 	test_KErrNone(r);
   255 
   258 
   256 	RThread().SetPriority(EPriorityAbsoluteVeryLow);
   259 	RThread().SetPriority(EPriorityAbsoluteVeryLow);
   257 	TInt threadcount=0;
   260 	TInt threadcount=0;
   258 	initial.HomeTime();
   261 	initial.HomeTime();
   259 	while (elapsed<1000000)
   262 	while (elapsed<1000000)
   260 		{
   263 		{
   261 		r = t.Create(KNullDesC, &DummyThread, 0x1000, NULL, NULL);
   264 		r = t.Create(KNullDesC, &DummyThread, 0x1000, NULL, NULL);
   262 		test(r==KErrNone);
   265 		test_KErrNone(r);
   263 		t.SetPriority(EPriorityMore);
   266 		t.SetPriority(EPriorityMore);
   264 		t.Resume();
   267 		t.Resume();
   265 		t.Close();
   268 		t.Close();
   266 		++threadcount;
   269 		++threadcount;
   267 		final.HomeTime();
   270 		final.HomeTime();
   305 	t.Suspend();
   308 	t.Suspend();
   306 	ws.iSem.Signal();
   309 	ws.iSem.Signal();
   307 	User::After(200000);
   310 	User::After(200000);
   308 	t.Resume();
   311 	t.Resume();
   309 	WaitForWaitSemThread(t, KErrTimedOut);
   312 	WaitForWaitSemThread(t, KErrTimedOut);
   310 	test(ws.iSem.Wait(1)==KErrNone);
   313 	test_KErrNone(ws.iSem.Wait(1));
   311 
   314 
   312 	ws.iTimeout=100000;
   315 	ws.iTimeout=100000;
   313 	StartWaitSemThread(t, ws, EPriorityMore);
   316 	StartWaitSemThread(t, ws, EPriorityMore);
   314 	t.Suspend();
   317 	t.Suspend();
   315 	ws.iSem.Signal();
   318 	ws.iSem.Signal();
   316 	User::After(50000);
   319 	User::After(50000);
   317 	t.Resume();
   320 	t.Resume();
   318 	WaitForWaitSemThread(t, KErrNone);
   321 	WaitForWaitSemThread(t, KErrNone);
   319 	test(ws.iSem.Wait(1)==KErrTimedOut);
   322 	test_Equal(KErrTimedOut, ws.iSem.Wait(1));
   320 
   323 
   321 	RThread t2;
   324 	RThread t2;
   322 	ws.iTimeout=100000;
   325 	ws.iTimeout=100000;
   323 	StartWaitSemThread(t, ws, EPriorityMuchMore);
   326 	StartWaitSemThread(t, ws, EPriorityMuchMore);
   324 	StartWaitSemThread(t2, ws, EPriorityMore);
   327 	StartWaitSemThread(t2, ws, EPriorityMore);
   325 	t.Suspend();
   328 	t.Suspend();
   326 	ws.iSem.Signal();
   329 	ws.iSem.Signal();
   327 	test(t2.ExitType()==EExitKill);
   330 	test_Equal(EExitKill, t2.ExitType());
   328 	test(t.ExitType()==EExitPending);
   331 	test_Equal(EExitPending, t.ExitType());
   329 	t.Resume();
   332 	t.Resume();
   330 	WaitForWaitSemThread(t, KErrTimedOut);
   333 	WaitForWaitSemThread(t, KErrTimedOut);
   331 	WaitForWaitSemThread(t2, KErrNone);
   334 	WaitForWaitSemThread(t2, KErrNone);
   332 	test(ws.iSem.Wait(1)==KErrTimedOut);
   335 	test_Equal(KErrTimedOut, ws.iSem.Wait(1));
   333 
   336 
   334 	ws.iTimeout=1000000;
   337 	ws.iTimeout=1000000;
   335 	initial.HomeTime();
   338 	initial.HomeTime();
   336 	StartWaitSemThread(t2, ws, EPriorityMore);
   339 	StartWaitSemThread(t2, ws, EPriorityMore);
   337 	StartWaitSemThread(t, ws, EPriorityMuchMore);
   340 	StartWaitSemThread(t, ws, EPriorityMuchMore);
   374 
   377 
   375 	ws.iTimeout=1000000;
   378 	ws.iTimeout=1000000;
   376 	initial.HomeTime();
   379 	initial.HomeTime();
   377 	StartWaitSemThread(t, ws, EPriorityMore);
   380 	StartWaitSemThread(t, ws, EPriorityMore);
   378 	StartWaitSemThread(t2, ws, EPriorityMuchMore);
   381 	StartWaitSemThread(t2, ws, EPriorityMuchMore);
   379 	test(t.ExitType()==EExitPending);
   382 	test_Equal(EExitPending, t.ExitType());
   380 	test(t2.ExitType()==EExitPending);
   383 	test_Equal(EExitPending, t2.ExitType());
   381 	ws.iSem.Close();
   384 	ws.iSem.Close();
   382 	test(t.ExitType()==EExitKill);
   385 	test_Equal(EExitKill, t.ExitType());
   383 	test(t2.ExitType()==EExitKill);
   386 	test_Equal(EExitKill, t2.ExitType());
   384 	WaitForWaitSemThread(t2, KErrGeneral);
   387 	WaitForWaitSemThread(t2, KErrGeneral);
   385 	WaitForWaitSemThread(t, KErrGeneral);
   388 	WaitForWaitSemThread(t, KErrGeneral);
   386 	final.HomeTime();
   389 	final.HomeTime();
   387 	elapsed = I64INT(final.Int64()-initial.Int64());
   390 	elapsed = I64INT(final.Int64()-initial.Int64());
   388 	test.Printf(_L("Time taken = %dus\n"), elapsed);
   391 	test.Printf(_L("Time taken = %dus\n"), elapsed);
   412 	test(find.Next(fullName)== KErrNone);	
   415 	test(find.Next(fullName)== KErrNone);	
   413 */
   416 */
   414 	test.Next(_L("Producer/Consumer scenario"));
   417 	test.Next(_L("Producer/Consumer scenario"));
   415 	// Test Rsemaphore with the producer/consumer scenario	RThread thread1, thread2;
   418 	// Test Rsemaphore with the producer/consumer scenario	RThread thread1, thread2;
   416 	TRequestStatus stat1, stat2;
   419 	TRequestStatus stat1, stat2;
   417 	test(mutex.CreateLocal()==KErrNone);
   420 	test_KErrNone(mutex.CreateLocal());
   418 	test(slotAvailable.CreateLocal(KMaxBufferSize)==KErrNone);
   421 	test_KErrNone(slotAvailable.CreateLocal(KMaxBufferSize));
   419 	test(itemAvailable.CreateLocal(0)==KErrNone);
   422 	test_KErrNone(itemAvailable.CreateLocal(0));
   420 	test(thread1.Create(_L("Thread1"),Producer,KDefaultStackSize,0x200,0x200,NULL)==KErrNone);
   423 	test_KErrNone(thread1.Create(_L("Thread1"),Producer,KDefaultStackSize,0x200,0x200,NULL));
   421 	test(thread2.Create(_L("Thread2"),Consumer,KDefaultStackSize,0x200,0x200,NULL)==KErrNone);
   424 	test_KErrNone(thread2.Create(_L("Thread2"),Consumer,KDefaultStackSize,0x200,0x200,NULL));
   422 	thread1.Logon(stat1);
   425 	thread1.Logon(stat1);
   423 	thread2.Logon(stat2);
   426 	thread2.Logon(stat2);
   424 	test(stat1==KRequestPending);
   427 	test_Equal(KRequestPending, stat1.Int());
   425 	test(stat2==KRequestPending);
   428 	test_Equal(KRequestPending, stat2.Int());
   426 	thread1.Resume(); 
   429 	thread1.Resume(); 
   427 	thread2.Resume();
   430 	thread2.Resume();
   428 	User::WaitForRequest(stat1);
   431 	User::WaitForRequest(stat1);
   429 	User::WaitForRequest(stat2);
   432 	User::WaitForRequest(stat2);
   430 	test(stat1==KErrNone);
   433 	test_KErrNone(stat1.Int());
   431 	test(stat2==KErrNone);
   434 	test_KErrNone(stat2.Int());
   432 	for(TInt jj=0;jj<KNumProducerItems;jj++)
   435 	for(TInt jj=0;jj<KNumProducerItems;jj++)
   433 		test(consumerArray[jj]==jj);		
   436 		test_Equal(jj, consumerArray[jj]);		
   434 	
   437 	
   435 	test.Next(_L("Close"));
   438 	test.Next(_L("Close"));
   436 	mutex.Close();
   439 	mutex.Close();
   437 	CLOSE_AND_WAIT(thread1);
   440 	CLOSE_AND_WAIT(thread1);
   438 	CLOSE_AND_WAIT(thread2);
   441 	CLOSE_AND_WAIT(thread2);
   441 
   444 
   442 void TestMutex2()
   445 void TestMutex2()
   443 	{
   446 	{
   444 	RMutex m;
   447 	RMutex m;
   445 	test.Start(_L("Create"));
   448 	test.Start(_L("Create"));
   446 	test(m.CreateLocal()==KErrNone);
   449 	test_KErrNone(m.CreateLocal());
   447 
   450 
   448 	// Test RMutex::IsHeld()
   451 	// Test RMutex::IsHeld()
   449 	test.Next(_L("IsHeld ?"));
   452 	test.Next(_L("IsHeld ?"));
   450 	test(!m.IsHeld());
   453 	test(!m.IsHeld());
   451 	test.Next(_L("Wait"));
   454 	test.Next(_L("Wait"));
   461 	}
   464 	}
   462 
   465 
   463 void TestMutex()
   466 void TestMutex()
   464 	{
   467 	{
   465 	test.Start(_L("Create"));
   468 	test.Start(_L("Create"));
   466 	test(mutex.CreateLocal()==KErrNone);
   469 	test_KErrNone(mutex.CreateLocal());
   467 	
   470 	
   468 	test.Next(_L("Threads writing to arrays test"));
   471 	test.Next(_L("Threads writing to arrays test"));
   469 //
   472 //
   470 // Create two threads which write to two arrays. The arrays and indexs
   473 // Create two threads which write to two arrays. The arrays and indexs
   471 // are global and each thread writes an identifier to the arrays. For
   474 // are global and each thread writes an identifier to the arrays. For
   475 // each array. For the mutex controlled array the actual instances
   478 // each array. For the mutex controlled array the actual instances
   476 // written to the array should always be the same as the threads think.
   479 // written to the array should always be the same as the threads think.
   477 //
   480 //
   478 	arrayIndex=0;
   481 	arrayIndex=0;
   479 	RThread thread1,thread2;	
   482 	RThread thread1,thread2;	
   480 	test(thread1.Create(_L("Thread1"),MutexThreadEntryPoint1,KDefaultStackSize,0x2000,0x2000,NULL)==KErrNone);
   483 	test_KErrNone(thread1.Create(_L("Thread1"),MutexThreadEntryPoint1,KDefaultStackSize,0x2000,0x2000,NULL));
   481 	test(thread2.Create(_L("Thread2"),MutexThreadEntryPoint2,KDefaultStackSize,0x2000,0x2000,NULL)==KErrNone);			 
   484 	test_KErrNone(thread2.Create(_L("Thread2"),MutexThreadEntryPoint2,KDefaultStackSize,0x2000,0x2000,NULL));			 
   482 	TRequestStatus stat1,stat2;
   485 	TRequestStatus stat1,stat2;
   483 	thread1.Logon(stat1);
   486 	thread1.Logon(stat1);
   484 	thread2.Logon(stat2);
   487 	thread2.Logon(stat2);
   485 	test(stat1==KRequestPending);
   488 	test_Equal(KRequestPending, stat1.Int());
   486 	test(stat2==KRequestPending);
   489 	test_Equal(KRequestPending, stat2.Int());
   487 	thread1.Resume(); 
   490 	thread1.Resume(); 
   488 	thread2.Resume();
   491 	thread2.Resume();
   489 	User::WaitForRequest(stat1);
   492 	User::WaitForRequest(stat1);
   490 	User::WaitForRequest(stat2);
   493 	User::WaitForRequest(stat2);
   491 	test(stat1==KErrNone);
   494 	test_KErrNone(stat1.Int());
   492 	test(stat2==KErrNone); 
   495 	test_KErrNone(stat2.Int()); 
   493 	TInt thread1ActualCount=0; 
   496 	TInt thread1ActualCount=0; 
   494 	TInt thread2ActualCount=0;
   497 	TInt thread2ActualCount=0;
   495 	TInt ii=0;
   498 	TInt ii=0;
   496 	while(ii<KMaxArraySize)
   499 	while(ii<KMaxArraySize)
   497 		{
   500 		{
   500 		if (array[ii]==EThread2ID)
   503 		if (array[ii]==EThread2ID)
   501 			thread2ActualCount++;
   504 			thread2ActualCount++;
   502 		ii++;
   505 		ii++;
   503 		}
   506 		}
   504 	test.Printf(_L("T1 %d T1ACT %d T2 %d T2ACT %d"),thread1Count,thread1ActualCount,thread2Count,thread2ActualCount);
   507 	test.Printf(_L("T1 %d T1ACT %d T2 %d T2ACT %d"),thread1Count,thread1ActualCount,thread2Count,thread2ActualCount);
   505 	test(thread1ActualCount==thread1Count);
   508 	test_Equal(thread1Count, thread1ActualCount);
   506 	test(thread2ActualCount==thread2Count);
   509 	test_Equal(thread2Count, thread2ActualCount);
   507 	test(thread1Count==thread2Count);
   510 	test_Equal(thread2Count, thread1Count);
   508 	test(thread1Count==(KMaxArraySize>>1));
   511 	test_Equal((KMaxArraySize>>1), thread1Count);
   509 	
   512 	
   510 	test.Next(_L("Close"));
   513 	test.Next(_L("Close"));
   511 	CLOSE_AND_WAIT(thread1);
   514 	CLOSE_AND_WAIT(thread1);
   512 	CLOSE_AND_WAIT(thread2);
   515 	CLOSE_AND_WAIT(thread2);
   513 	mutex.Close();
   516 	mutex.Close();
   519 //As TestMutex, but for RCriticalSection
   522 //As TestMutex, but for RCriticalSection
   520 //
   523 //
   521 	{
   524 	{
   522 	
   525 	
   523 	test.Start(_L("Create"));
   526 	test.Start(_L("Create"));
   524 	test(criticalSn.CreateLocal()==KErrNone);
   527 	test_KErrNone(criticalSn.CreateLocal());
   525 
   528 
   526 /***************** TO DO ***********************
   529 /***************** TO DO ***********************
   527 
   530 
   528 	test.Next(_L("Find"));
   531 	test.Next(_L("Find"));
   529 //
   532 //
   549 // actual instances written to the array should always be the same as the
   552 // actual instances written to the array should always be the same as the
   550 // threads think.
   553 // threads think.
   551 //
   554 //
   552 	arrayIndex=0;
   555 	arrayIndex=0;
   553 	RThread thread1,thread2;	
   556 	RThread thread1,thread2;	
   554 	test(thread1.Create(_L("Thread1"),CriticalSnThreadEntryPoint1,KDefaultStackSize,0x2000,0x2000,NULL)==KErrNone);
   557 	test_KErrNone(thread1.Create(_L("Thread1"),CriticalSnThreadEntryPoint1,KDefaultStackSize,0x2000,0x2000,NULL));
   555 	test(thread2.Create(_L("Thread2"),CriticalSnThreadEntryPoint2,KDefaultStackSize,0x2000,0x2000,NULL)==KErrNone);			 
   558 	test_KErrNone(thread2.Create(_L("Thread2"),CriticalSnThreadEntryPoint2,KDefaultStackSize,0x2000,0x2000,NULL));			 
   556 	TRequestStatus stat1,stat2;
   559 	TRequestStatus stat1,stat2;
   557 	thread1.Logon(stat1);
   560 	thread1.Logon(stat1);
   558 	thread2.Logon(stat2);
   561 	thread2.Logon(stat2);
   559 	test(stat1==KRequestPending);
   562 	test_Equal(KRequestPending, stat1.Int());
   560 	test(stat2==KRequestPending);
   563 	test_Equal(KRequestPending, stat2.Int());
   561 	thread1.Resume(); 
   564 	thread1.Resume(); 
   562 	thread2.Resume();
   565 	thread2.Resume();
   563 	User::WaitForRequest(stat1);
   566 	User::WaitForRequest(stat1);
   564 	User::WaitForRequest(stat2);
   567 	User::WaitForRequest(stat2);
   565 	test(stat1==KErrNone);
   568 	test_KErrNone(stat1.Int());
   566 	test(stat2==KErrNone); 
   569 	test_KErrNone(stat2.Int()); 
   567 	TInt thread1ActualCount=0; 
   570 	TInt thread1ActualCount=0; 
   568 	TInt thread2ActualCount=0;
   571 	TInt thread2ActualCount=0;
   569 	TInt ii=0;
   572 	TInt ii=0;
   570 	while(ii<KMaxArraySize)
   573 	while(ii<KMaxArraySize)
   571 		{
   574 		{
   573 			thread1ActualCount++;
   576 			thread1ActualCount++;
   574 		if (array[ii]==EThread2ID)
   577 		if (array[ii]==EThread2ID)
   575 			thread2ActualCount++;
   578 			thread2ActualCount++;
   576 		ii++;
   579 		ii++;
   577 		}
   580 		}
   578 	test(thread1ActualCount==thread1Count);
   581 	test_Equal(thread1Count, thread1ActualCount);
   579 	test(thread2ActualCount==thread2Count);
   582 	test_Equal(thread2Count, thread2ActualCount);
   580 	test(thread1Count==thread2Count);
   583 	test_Equal(thread2Count, thread1Count);
   581 	test(thread1Count==(KMaxArraySize>>1));
   584 	test_Equal((KMaxArraySize>>1), thread1Count);
   582 
   585 
   583 	test.Next(_L("Close"));
   586 	test.Next(_L("Close"));
   584 	CLOSE_AND_WAIT(thread1);
   587 	CLOSE_AND_WAIT(thread1);
   585 	CLOSE_AND_WAIT(thread2);
   588 	CLOSE_AND_WAIT(thread2);
   586 	criticalSn.Close();
   589 	criticalSn.Close();
   588 	}
   591 	}
   589 
   592 
   590 
   593 
   591 GLDEF_C TInt E32Main()
   594 GLDEF_C TInt E32Main()
   592 	{	
   595 	{	
       
   596 	TInt cpus = UserSvr::HalFunction(EHalGroupKernel, EKernelHalNumLogicalCpus, 0, 0);
       
   597 	if (cpus != 1)
       
   598 		{
       
   599 		test(cpus>1);
       
   600 		// This test will require compatibility mode (and probably other changes)
       
   601 		// to work on SMP - it depends on explicit scheduling order.
       
   602 		test.Printf(_L("T_SEMUTX skipped, does not work on SMP\n"));
       
   603 		return KErrNone;
       
   604 		}	
       
   605 	
   593 
   606 
   594 	test.Title();
   607 	test.Title();
   595  	__UHEAP_MARK;
   608  	__UHEAP_MARK;
   596 	test.Start(_L("Test RSemaphore"));
   609 	test.Start(_L("Test RSemaphore"));
   597 	TestSemaphore();
   610 	TestSemaphore();