os/kernelhwsrv/kerneltest/e32test/dll/t_tls.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) 1996-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\dll\t_tls.cpp
sl@0
    15
// Overview:
sl@0
    16
// Test and benchmark TLS (Thread Local Storage) functions
sl@0
    17
// API Information:
sl@0
    18
// UserSvr
sl@0
    19
// Details:
sl@0
    20
// - Use the UserSvr methods: DllTls, DllSetTls and DllFreeTls to test 
sl@0
    21
// the thread local storage. Verify the expected results. Verify that 
sl@0
    22
// the heap was not corrupted by any of the tests.
sl@0
    23
// - In a separate thread, use the UserSvr methods: DllTls, DllSetTls and 
sl@0
    24
// DllFreeTls to test the thread local storage. Verify the expected results.
sl@0
    25
// - Test the TLS cleanup handlers and verify results are as expected. Verify 
sl@0
    26
// that the heap was not corrupted by any of the tests.
sl@0
    27
// - Benchmark how many DllSetTls, DllTls and DllFreeTls operations can be
sl@0
    28
// performed in 1/2 second.
sl@0
    29
// Platforms/Drives/Compatibility:
sl@0
    30
// All.
sl@0
    31
// Assumptions/Requirement/Pre-requisites:
sl@0
    32
// Failures and causes:
sl@0
    33
// Base Port information:
sl@0
    34
// Tests and benchmarks Tls functions
sl@0
    35
// 14/10/96 Modified scheme on Arm release
sl@0
    36
// SetTls - 352	~1300
sl@0
    37
// Tls - 3510 ~1600
sl@0
    38
// 15/10/96 Array scheme on Arm release
sl@0
    39
// Set Tls - ~1900
sl@0
    40
// Tls - ~2550
sl@0
    41
// 
sl@0
    42
//
sl@0
    43
sl@0
    44
#define __E32TEST_EXTENSION__
sl@0
    45
sl@0
    46
#include <e32test.h>
sl@0
    47
#include <e32svr.h>
sl@0
    48
#include <f32dbg.h>
sl@0
    49
#include <e32def.h>
sl@0
    50
#include <e32def_private.h>
sl@0
    51
#include "u32std.h"
sl@0
    52
sl@0
    53
// Can't tell whether allocations will be on the kernel or user heap, so perform all heap check
sl@0
    54
// operations on both
sl@0
    55
#define HEAP_MARK			__UHEAP_MARK; __KHEAP_MARK
sl@0
    56
#define HEAP_MARKEND		__UHEAP_MARKEND; __KHEAP_MARKEND
sl@0
    57
#define HEAP_RESET			__UHEAP_RESET; __KHEAP_RESET
sl@0
    58
#define HEAP_CHECK(x)		__UHEAP_CHECK(x); __KHEAP_CHECK(x)
sl@0
    59
#define HEAP_FAILNEXT(x)	__UHEAP_FAILNEXT(x); __KHEAP_FAILNEXT(x)
sl@0
    60
sl@0
    61
TInt const KCheckHandle=67338721;
sl@0
    62
TUint8* const KCheckValue=(TUint8*)8525124;
sl@0
    63
sl@0
    64
LOCAL_D RTest test(_L("T_TLS"));
sl@0
    65
sl@0
    66
_LIT(KTestDllName, "t_tlsdll");
sl@0
    67
const TInt KTestDllOrdSet	= 1;
sl@0
    68
const TInt KTestDllOrdGet	= 2;
sl@0
    69
const TInt KTestDllOrdFree	= 3;
sl@0
    70
sl@0
    71
typedef TInt (*TTestDllSetFn)(TAny*);
sl@0
    72
typedef TAny* (*TTestDllGetFn)();
sl@0
    73
typedef void (*TTestDllFreeFn)();
sl@0
    74
sl@0
    75
TInt TestDllSetTls(RLibrary aLib, TAny* aValue)
sl@0
    76
	{
sl@0
    77
	TTestDllSetFn f = (TTestDllSetFn)aLib.Lookup(KTestDllOrdSet);
sl@0
    78
	return (*f)(aValue);
sl@0
    79
	}
sl@0
    80
sl@0
    81
TAny* TestDllGetTls(RLibrary aLib)
sl@0
    82
	{
sl@0
    83
	TTestDllGetFn f = (TTestDllGetFn)aLib.Lookup(KTestDllOrdGet);
sl@0
    84
	return (*f)();
sl@0
    85
	}
sl@0
    86
sl@0
    87
void TestDllFreeTls(RLibrary aLib)
sl@0
    88
	{
sl@0
    89
	TTestDllFreeFn f = (TTestDllFreeFn)aLib.Lookup(KTestDllOrdFree);
sl@0
    90
	(*f)();
sl@0
    91
	}
sl@0
    92
sl@0
    93
#define DISPLAY_PROGRESS	test.Printf(_L("Line %d\n"), __LINE__)
sl@0
    94
sl@0
    95
void Test()
sl@0
    96
	{
sl@0
    97
	test.Start(_L("Stuff without Setting"));
sl@0
    98
sl@0
    99
	HEAP_MARK;
sl@0
   100
sl@0
   101
	test(UserSvr::DllTls(KCheckHandle)==NULL);
sl@0
   102
	UserSvr::DllFreeTls(KCheckHandle);
sl@0
   103
	test(UserSvr::DllTls(KCheckHandle)==NULL);
sl@0
   104
	test(UserSvr::DllTls(0)==NULL);
sl@0
   105
	UserSvr::DllFreeTls(0);
sl@0
   106
	test(UserSvr::DllTls(0)==NULL);
sl@0
   107
sl@0
   108
	HEAP_CHECK(0);
sl@0
   109
sl@0
   110
	test.Next(_L("Set"));
sl@0
   111
	test(UserSvr::DllSetTls(KCheckHandle,KCheckValue)==KErrNone);
sl@0
   112
	test.Next(_L("Get"));
sl@0
   113
	test(UserSvr::DllTls(KCheckHandle)==KCheckValue);
sl@0
   114
	test.Next(_L("Free"));
sl@0
   115
	UserSvr::DllFreeTls(KCheckHandle);
sl@0
   116
	test(UserSvr::DllTls(KCheckHandle)==NULL);
sl@0
   117
sl@0
   118
	HEAP_CHECK(0);
sl@0
   119
sl@0
   120
	test.Next(_L("Set lots"));
sl@0
   121
	TInt i=0;
sl@0
   122
	for(;i<1000;i+=3)
sl@0
   123
		{
sl@0
   124
		test(UserSvr::DllSetTls(KCheckHandle+i,KCheckValue-i)==KErrNone);
sl@0
   125
		test(UserSvr::DllTls(KCheckHandle+i)==KCheckValue-i);
sl@0
   126
		}
sl@0
   127
	for(i=999;i>0;i-=3)
sl@0
   128
		{
sl@0
   129
		test(UserSvr::DllTls(KCheckHandle+i)==KCheckValue-i);
sl@0
   130
		UserSvr::DllFreeTls(KCheckHandle+i);
sl@0
   131
		test(UserSvr::DllTls(KCheckHandle+i)==NULL);
sl@0
   132
		}
sl@0
   133
	test(UserSvr::DllTls(KCheckHandle+i)==KCheckValue-i);
sl@0
   134
	UserSvr::DllFreeTls(KCheckHandle+i);
sl@0
   135
	test(UserSvr::DllTls(KCheckHandle+i)==NULL);
sl@0
   136
sl@0
   137
	HEAP_MARKEND;
sl@0
   138
sl@0
   139
sl@0
   140
	test.Next(_L("Test with DLL ID"));
sl@0
   141
sl@0
   142
	HEAP_MARK;
sl@0
   143
sl@0
   144
	test(UserSvr::DllTls(KCheckHandle, 1)==0);
sl@0
   145
	test(UserSvr::DllTls(KCheckHandle, 2)==0);
sl@0
   146
	DISPLAY_PROGRESS;
sl@0
   147
#ifdef _DEBUG
sl@0
   148
	HEAP_FAILNEXT(1);
sl@0
   149
	DISPLAY_PROGRESS;
sl@0
   150
	test(UserSvr::DllSetTls(KCheckHandle, 1, KCheckValue)==KErrNoMemory);
sl@0
   151
#endif
sl@0
   152
	test(UserSvr::DllSetTls(KCheckHandle, 1, KCheckValue)==KErrNone);
sl@0
   153
	test(UserSvr::DllTls(KCheckHandle, 1)==KCheckValue);
sl@0
   154
	test(UserSvr::DllTls(KCheckHandle, 2)==0);
sl@0
   155
	DISPLAY_PROGRESS;
sl@0
   156
	HEAP_FAILNEXT(1);
sl@0
   157
	DISPLAY_PROGRESS;
sl@0
   158
	test(UserSvr::DllSetTls(KCheckHandle, 3, KCheckValue)==KErrNone);
sl@0
   159
sl@0
   160
#ifdef _DEBUG
sl@0
   161
	RSemaphore s;
sl@0
   162
	test(s.CreateLocal(0)==KErrNoMemory);
sl@0
   163
#endif
sl@0
   164
sl@0
   165
	HEAP_RESET;
sl@0
   166
sl@0
   167
	test(UserSvr::DllTls(KCheckHandle, 1)==0);
sl@0
   168
	test(UserSvr::DllTls(KCheckHandle, 2)==0);
sl@0
   169
	test(UserSvr::DllTls(KCheckHandle, 3)==KCheckValue);
sl@0
   170
	UserSvr::DllFreeTls(KCheckHandle);
sl@0
   171
sl@0
   172
	DISPLAY_PROGRESS;
sl@0
   173
	HEAP_MARKEND;
sl@0
   174
	DISPLAY_PROGRESS;
sl@0
   175
sl@0
   176
	test.Next(_L("Test reloading DLL"));
sl@0
   177
sl@0
   178
	RLibrary l;
sl@0
   179
	TInt r = l.Load(KTestDllName);
sl@0
   180
	test(r==KErrNone);
sl@0
   181
	for (i=0; i<2; ++i)
sl@0
   182
		{
sl@0
   183
		test.Printf(_L("i=%d\n"),i);
sl@0
   184
		test(TestDllGetTls(l)==0);
sl@0
   185
		test(TestDllSetTls(l, KCheckValue)==KErrNone);
sl@0
   186
		test(TestDllGetTls(l)==KCheckValue);
sl@0
   187
		if (i==0)
sl@0
   188
			{
sl@0
   189
			TestDllFreeTls(l);
sl@0
   190
			test(TestDllGetTls(l)==0);
sl@0
   191
			}
sl@0
   192
		l.Close();
sl@0
   193
		r = l.Load(KTestDllName);
sl@0
   194
		test(r==KErrNone);
sl@0
   195
		test(TestDllGetTls(l)==0);
sl@0
   196
		TestDllFreeTls(l);
sl@0
   197
		}
sl@0
   198
	l.Close();
sl@0
   199
sl@0
   200
	test.End();
sl@0
   201
	}
sl@0
   202
sl@0
   203
TInt TestThread(TAny*)
sl@0
   204
	{
sl@0
   205
	RTest test(_L("T_TLS Thread"));
sl@0
   206
	test.Start(_L("Stuff without Setting"));
sl@0
   207
	test(UserSvr::DllTls(KCheckHandle)==NULL);
sl@0
   208
	UserSvr::DllFreeTls(KCheckHandle);
sl@0
   209
	test(UserSvr::DllTls(KCheckHandle)==NULL);
sl@0
   210
	test(UserSvr::DllTls(0)==NULL);
sl@0
   211
	UserSvr::DllFreeTls(0);
sl@0
   212
	test(UserSvr::DllTls(0)==NULL);
sl@0
   213
	test.Next(_L("Set"));
sl@0
   214
	test(UserSvr::DllSetTls(KCheckHandle,KCheckValue)==KErrNone);
sl@0
   215
	test.Next(_L("Get"));
sl@0
   216
	test(UserSvr::DllTls(KCheckHandle)==KCheckValue);
sl@0
   217
	test.Next(_L("Free"));
sl@0
   218
	UserSvr::DllFreeTls(KCheckHandle);
sl@0
   219
	test(UserSvr::DllTls(KCheckHandle)==NULL);
sl@0
   220
	test.Next(_L("Set lots"));
sl@0
   221
	TInt i=0;
sl@0
   222
	for(;i<1000;i+=3)
sl@0
   223
		{
sl@0
   224
		test(UserSvr::DllSetTls(KCheckHandle+i,KCheckValue-i)==KErrNone);
sl@0
   225
		test(UserSvr::DllTls(KCheckHandle+i)==KCheckValue-i);
sl@0
   226
		}
sl@0
   227
	for(i=999;i>=0;i-=3)
sl@0
   228
		{
sl@0
   229
		test(UserSvr::DllTls(KCheckHandle+i)==KCheckValue-i);
sl@0
   230
		UserSvr::DllFreeTls(KCheckHandle+i);
sl@0
   231
		test(UserSvr::DllTls(KCheckHandle+i)==NULL);
sl@0
   232
		}
sl@0
   233
sl@0
   234
	test.Close();
sl@0
   235
	return KErrNone;
sl@0
   236
	}
sl@0
   237
sl@0
   238
TInt HeapSize(RHeap* aHeap = NULL)
sl@0
   239
	{
sl@0
   240
	if (!aHeap)
sl@0
   241
		aHeap = &User::Heap();
sl@0
   242
	TInt size;
sl@0
   243
	aHeap->AllocSize(size);
sl@0
   244
	return size;
sl@0
   245
	}
sl@0
   246
sl@0
   247
TBool TLSStoredOnUserHeap()
sl@0
   248
	{
sl@0
   249
	TInt initCount = HeapSize();
sl@0
   250
	TInt i;
sl@0
   251
	const TInt KMax = 2;
sl@0
   252
	for(i = 0 ; i < KMax ; ++i)
sl@0
   253
		test(UserSvr::DllSetTls(KCheckHandle+i,KCheckValue-i) == KErrNone);
sl@0
   254
	TInt finalCount = HeapSize();
sl@0
   255
	for(i = 0 ; i < KMax ; ++i)
sl@0
   256
		UserSvr::DllFreeTls(KCheckHandle+i);
sl@0
   257
	return initCount != finalCount;
sl@0
   258
	}
sl@0
   259
sl@0
   260
class RDummyAllocator : public RAllocator
sl@0
   261
	{
sl@0
   262
public:
sl@0
   263
	TInt AccessCount() { return iAccessCount; }
sl@0
   264
	};
sl@0
   265
sl@0
   266
RHeap* MainHeap = NULL;
sl@0
   267
sl@0
   268
TInt HeapAccessCount(TAny* aHeap)
sl@0
   269
	{
sl@0
   270
	RDummyAllocator* heap = (RDummyAllocator*)aHeap;
sl@0
   271
	return heap->AccessCount();
sl@0
   272
	}
sl@0
   273
sl@0
   274
TBool HeapExists(RHeap* aHeap)
sl@0
   275
	{
sl@0
   276
	RThread thread;
sl@0
   277
	test_KErrNone(thread.Create(_L("HeapExistsThread"), HeapAccessCount, 0x1000, MainHeap, (TAny*)aHeap));
sl@0
   278
	TRequestStatus status;
sl@0
   279
	thread.Logon(status);
sl@0
   280
	thread.Resume();
sl@0
   281
	User::WaitForRequest(status);
sl@0
   282
sl@0
   283
	TInt r;
sl@0
   284
	if (thread.ExitType() == EExitKill)
sl@0
   285
		{
sl@0
   286
		r = thread.ExitReason();
sl@0
   287
		test_NotNegative(r);
sl@0
   288
		}
sl@0
   289
	else
sl@0
   290
		{
sl@0
   291
		test_Equal(EExitPanic, thread.ExitType());
sl@0
   292
		test_Equal(3, thread.ExitReason());
sl@0
   293
		r = 0;
sl@0
   294
		}
sl@0
   295
	thread.Close();
sl@0
   296
	
sl@0
   297
	return r != 0;
sl@0
   298
	}
sl@0
   299
sl@0
   300
TInt TestUserSideTls1Thread(TAny*)
sl@0
   301
	{
sl@0
   302
	// Ensure TLS uses initial heap
sl@0
   303
	if (UserSvr::DllSetTls(KCheckHandle,KCheckValue) != KErrNone)
sl@0
   304
		return __LINE__;
sl@0
   305
	UserSvr::DllFreeTls(KCheckHandle);
sl@0
   306
sl@0
   307
	RHeap* tlsHeap = &User::Heap();
sl@0
   308
	TInt tlsInitSize = HeapSize(tlsHeap);
sl@0
   309
sl@0
   310
	// Switch heap
sl@0
   311
	RHeap* newHeap = User::ChunkHeap(NULL, 0x1000, 0x1000);
sl@0
   312
	if (!newHeap)
sl@0
   313
		return __LINE__;
sl@0
   314
	TInt newInitSize = HeapSize(newHeap);
sl@0
   315
	User::SwitchHeap(newHeap);
sl@0
   316
	tlsHeap->Close();
sl@0
   317
sl@0
   318
	// Allocate more TLS data
sl@0
   319
	for(TInt i = 0 ; i < 100 ; ++i)
sl@0
   320
		{
sl@0
   321
		if (UserSvr::DllSetTls(KCheckHandle+i,KCheckValue-i) != KErrNone)
sl@0
   322
			return __LINE__;
sl@0
   323
		}
sl@0
   324
sl@0
   325
	// Test TLS data was allocated on original heap
sl@0
   326
	if (!(HeapSize(tlsHeap) > tlsInitSize))
sl@0
   327
		return __LINE__;
sl@0
   328
	if (HeapSize(newHeap) != newInitSize)
sl@0
   329
		return __LINE__;
sl@0
   330
	
sl@0
   331
	return KErrNone;
sl@0
   332
	}
sl@0
   333
sl@0
   334
void TestUserSideTls1()
sl@0
   335
	{
sl@0
   336
	test.Next(_L("Test user-side TLS behaviour when switching heaps"));
sl@0
   337
sl@0
   338
	RThread thread;
sl@0
   339
	test_KErrNone(thread.Create(_L("TestUserSideTls1Thread"), TestUserSideTls1Thread, 0x1000, 0x1000, 0x1000, 0));
sl@0
   340
	
sl@0
   341
	TRequestStatus status;
sl@0
   342
	thread.Logon(status);
sl@0
   343
	thread.Resume();
sl@0
   344
	User::WaitForRequest(status);
sl@0
   345
	
sl@0
   346
	test_Equal(EExitKill, thread.ExitType());
sl@0
   347
	test_Equal(KErrNone, thread.ExitReason());
sl@0
   348
	thread.Close();
sl@0
   349
	}
sl@0
   350
sl@0
   351
TInt TestUserSideTls2Thread(TAny*)
sl@0
   352
	{
sl@0
   353
	// Allocate some TLS data
sl@0
   354
	for(TInt i = 0 ; i < 100 ; ++i)
sl@0
   355
		{
sl@0
   356
		if (UserSvr::DllSetTls(KCheckHandle+i,KCheckValue-i) != KErrNone)
sl@0
   357
			return __LINE__;
sl@0
   358
		}
sl@0
   359
sl@0
   360
	// Exit normally
sl@0
   361
	return KErrNone;
sl@0
   362
	}
sl@0
   363
sl@0
   364
void TestUserSideTls2()
sl@0
   365
	{
sl@0
   366
	test.Next(_L("Test user-side TLS data cleanup on thread exit"));
sl@0
   367
sl@0
   368
	RHeap* tlsHeap = User::ChunkHeap(NULL, 0x1000, 0x1000);
sl@0
   369
	test_NotNull(tlsHeap);
sl@0
   370
	TInt initSize = HeapSize(tlsHeap);
sl@0
   371
sl@0
   372
	RThread thread;
sl@0
   373
	test_KErrNone(thread.Create(_L("TestUserSideTls2Thread"), TestUserSideTls2Thread, 0x1000, tlsHeap, 0));
sl@0
   374
	TThreadId id = thread.Id();
sl@0
   375
	
sl@0
   376
	TRequestStatus status;
sl@0
   377
	thread.Logon(status);
sl@0
   378
	thread.Resume();
sl@0
   379
	User::WaitForRequest(status);
sl@0
   380
	
sl@0
   381
	test_Equal(EExitKill, thread.ExitType());
sl@0
   382
	test_Equal(KErrNone, thread.ExitReason());
sl@0
   383
	thread.Close();
sl@0
   384
sl@0
   385
	// Check TLS data freed
sl@0
   386
	test_Equal(initSize, HeapSize(tlsHeap));	
sl@0
   387
	tlsHeap->Close();
sl@0
   388
sl@0
   389
	// Check heap no longer exists
sl@0
   390
	test(!HeapExists(tlsHeap));
sl@0
   391
sl@0
   392
	// Check thread no longer exists
sl@0
   393
	RThread thread2;
sl@0
   394
	test_Equal(KErrNotFound, thread2.Open(id));
sl@0
   395
	}
sl@0
   396
sl@0
   397
TInt TestUserSideTls3Thread(TAny*)
sl@0
   398
	{
sl@0
   399
	// Allocate some TLS data
sl@0
   400
	for(TInt i = 0 ; i < 100 ; ++i)
sl@0
   401
		{
sl@0
   402
		if (UserSvr::DllSetTls(KCheckHandle+i,KCheckValue-i) != KErrNone)
sl@0
   403
			return __LINE__;
sl@0
   404
		}
sl@0
   405
sl@0
   406
	// Panic
sl@0
   407
	User::Panic(_L("Test"), 999);
sl@0
   408
	return KErrNone;
sl@0
   409
	}
sl@0
   410
sl@0
   411
void TestUserSideTls3()
sl@0
   412
	{
sl@0
   413
	test.Next(_L("Test user-side TLS data cleanup on thread panic"));
sl@0
   414
sl@0
   415
	RHeap* tlsHeap = User::ChunkHeap(NULL, 0x1000, 0x1000);
sl@0
   416
	test_NotNull(tlsHeap);
sl@0
   417
sl@0
   418
	RThread thread;
sl@0
   419
	test_KErrNone(thread.Create(_L("TestUserSideTls3Thread"), TestUserSideTls3Thread, 0x1000, tlsHeap, 0));
sl@0
   420
	TThreadId id = thread.Id();
sl@0
   421
	
sl@0
   422
	TRequestStatus status;
sl@0
   423
	thread.Logon(status);
sl@0
   424
	thread.Resume();
sl@0
   425
	User::WaitForRequest(status);
sl@0
   426
	
sl@0
   427
	test_Equal(EExitPanic, thread.ExitType());
sl@0
   428
	test_Equal(999, thread.ExitReason());
sl@0
   429
	
sl@0
   430
	thread.Close();
sl@0
   431
	tlsHeap->Close();
sl@0
   432
	
sl@0
   433
	// Check heap no longer exists
sl@0
   434
	test(!HeapExists(tlsHeap));
sl@0
   435
sl@0
   436
	// Check thread no longer exists
sl@0
   437
	RThread thread2;
sl@0
   438
	test_Equal(KErrNotFound, thread2.Open(id));
sl@0
   439
	}
sl@0
   440
sl@0
   441
TInt TestUserSideTls4Thread(TAny*)
sl@0
   442
	{
sl@0
   443
	// Shouldn't ever run
sl@0
   444
	return KErrGeneral;
sl@0
   445
	}
sl@0
   446
sl@0
   447
void TestUserSideTls4()
sl@0
   448
	{
sl@0
   449
	test.Next(_L("Test user-side TLS data cleanup on early thread kill"));
sl@0
   450
	
sl@0
   451
	RHeap* tlsHeap = User::ChunkHeap(NULL, 0x1000, 0x1000);
sl@0
   452
	test_NotNull(tlsHeap);
sl@0
   453
sl@0
   454
	RThread thread;
sl@0
   455
	test_KErrNone(thread.Create(_L("TestUserSideTls4Thread"), TestUserSideTls4Thread, 0x1000, tlsHeap, 0));
sl@0
   456
	TThreadId id = thread.Id();
sl@0
   457
	
sl@0
   458
	thread.Kill(101);
sl@0
   459
sl@0
   460
	TRequestStatus status;
sl@0
   461
	thread.Logon(status);
sl@0
   462
	User::WaitForRequest(status);
sl@0
   463
	
sl@0
   464
	test_Equal(EExitKill, thread.ExitType());
sl@0
   465
	test_Equal(101, thread.ExitReason());
sl@0
   466
	
sl@0
   467
	thread.Close();
sl@0
   468
	tlsHeap->Close();
sl@0
   469
	
sl@0
   470
	// Check heap no longer exists
sl@0
   471
	test(!HeapExists(tlsHeap));
sl@0
   472
sl@0
   473
	// Check thread no longer exists
sl@0
   474
	RThread thread2;
sl@0
   475
	test_Equal(KErrNotFound, thread2.Open(id));
sl@0
   476
	}
sl@0
   477
sl@0
   478
#if 0
sl@0
   479
class CTest : public CBase
sl@0
   480
	{
sl@0
   481
public:
sl@0
   482
	static CTest* New(TInt aSize, TInt aTls, TInt aLinkedTls);
sl@0
   483
	virtual ~CTest();
sl@0
   484
public:
sl@0
   485
	TAny* iAlloc;
sl@0
   486
	TInt iTls;
sl@0
   487
	TInt iLinkedTls;
sl@0
   488
	};
sl@0
   489
sl@0
   490
void TlsCleanup2(TAny* a)
sl@0
   491
	{
sl@0
   492
	delete ((CTest*)a);
sl@0
   493
	}
sl@0
   494
sl@0
   495
CTest::~CTest()
sl@0
   496
	{
sl@0
   497
	RDebug::Print(_L("~CTest %d %d"), iTls, iLinkedTls);
sl@0
   498
	User::Free(iAlloc);
sl@0
   499
	UserSvr::DllFreeTls(iTls);
sl@0
   500
	if (iLinkedTls)
sl@0
   501
		{
sl@0
   502
		CTest* p = (CTest*)UserSvr::DllTls(iLinkedTls);
sl@0
   503
		if (p)
sl@0
   504
			delete p;
sl@0
   505
		}
sl@0
   506
	}
sl@0
   507
sl@0
   508
CTest* CTest::New(TInt aSize, TInt aTls, TInt aLinkedTls)
sl@0
   509
	{
sl@0
   510
	CTest* p = new CTest;
sl@0
   511
	test(p!=NULL);
sl@0
   512
	p->iTls = aTls;
sl@0
   513
	p->iAlloc = User::Alloc(aSize);
sl@0
   514
	test(p->iAlloc!=NULL);
sl@0
   515
	p->iLinkedTls = aLinkedTls;
sl@0
   516
	test(UserSvr::DllSetTls(aTls, p, TlsCleanup2)==KErrNone);
sl@0
   517
	return p;
sl@0
   518
	}
sl@0
   519
sl@0
   520
sl@0
   521
void TlsCleanupCheck(TAny*)
sl@0
   522
	{
sl@0
   523
	__UHEAP_MARKEND;
sl@0
   524
	}
sl@0
   525
sl@0
   526
void TlsCleanup1(TAny* a)
sl@0
   527
	{
sl@0
   528
	User::Free(a);
sl@0
   529
	}
sl@0
   530
sl@0
   531
TInt TlsCleanupThread1(TAny* a)
sl@0
   532
	{
sl@0
   533
	__UHEAP_MARK;
sl@0
   534
	TInt n = (TInt)a;
sl@0
   535
	TInt i;
sl@0
   536
	TInt r = UserSvr::DllSetTls(0, NULL, TlsCleanupCheck);
sl@0
   537
	if (r!=KErrNone)
sl@0
   538
		return r;
sl@0
   539
	for (i=0; i<n; ++i)
sl@0
   540
		{
sl@0
   541
		TAny* p = User::Alloc(64);
sl@0
   542
		if (!p)
sl@0
   543
			return KErrNoMemory;
sl@0
   544
		r = UserSvr::DllSetTls(i+1, p, TlsCleanup1);
sl@0
   545
		if (r!=KErrNone)
sl@0
   546
			return r;
sl@0
   547
		}
sl@0
   548
	return KErrNone;
sl@0
   549
	}
sl@0
   550
sl@0
   551
TInt TlsCleanupThread2(TAny*)
sl@0
   552
	{
sl@0
   553
	__UHEAP_MARK;
sl@0
   554
	TInt r = UserSvr::DllSetTls(0, NULL, TlsCleanupCheck);
sl@0
   555
	if (r!=KErrNone)
sl@0
   556
		return r;
sl@0
   557
sl@0
   558
	CTest::New(256, 1, 2);
sl@0
   559
	CTest::New(256, 2, 3);
sl@0
   560
	CTest::New(256, 3, 0);
sl@0
   561
	CTest::New(256, 6, 5);
sl@0
   562
	CTest::New(256, 5, 4);
sl@0
   563
	CTest::New(256, 4, 0);
sl@0
   564
	CTest::New(256, 9, 8);
sl@0
   565
	CTest::New(256, 8, 7);
sl@0
   566
	CTest::New(256, 7, 9);
sl@0
   567
	CTest::New(256, 10, 11);
sl@0
   568
	CTest* p = CTest::New(256, 11, 12);
sl@0
   569
	CTest::New(256, 12, 0);
sl@0
   570
sl@0
   571
	delete p;
sl@0
   572
sl@0
   573
	return KErrNone;
sl@0
   574
	}
sl@0
   575
sl@0
   576
void DisplayExitInfo(RThread t)
sl@0
   577
	{
sl@0
   578
	TInt exitType = t.ExitType();
sl@0
   579
	TInt exitReason = t.ExitReason();
sl@0
   580
	TBuf<32> exitCat = t.ExitCategory();
sl@0
   581
	test.Printf(_L("Exit Info: %d,%d,%S\n"), exitType, exitReason, &exitCat);
sl@0
   582
	}
sl@0
   583
sl@0
   584
void DoTestCleanupHandler(TThreadFunction aFunc, TAny* aArg)
sl@0
   585
	{
sl@0
   586
	RThread t;
sl@0
   587
	TInt r = t.Create(KNullDesC, aFunc, 0x1000, NULL, aArg);
sl@0
   588
	test(r==KErrNone);
sl@0
   589
	TRequestStatus s;
sl@0
   590
	t.Logon(s);
sl@0
   591
	t.Resume();
sl@0
   592
	User::WaitForRequest(s);
sl@0
   593
	DisplayExitInfo(t);
sl@0
   594
	test(t.ExitType()==EExitKill);
sl@0
   595
	test(t.ExitReason()==KErrNone);
sl@0
   596
	test(s==KErrNone);
sl@0
   597
	CLOSE_AND_WAIT(t);
sl@0
   598
	}
sl@0
   599
sl@0
   600
void TestCleanupHandler()
sl@0
   601
	{
sl@0
   602
	HEAP_MARK;
sl@0
   603
sl@0
   604
	test.Start(_L("Test TLS cleanup handlers"));
sl@0
   605
	DoTestCleanupHandler(TlsCleanupThread1, (TAny*)1 );
sl@0
   606
	DoTestCleanupHandler(TlsCleanupThread1, (TAny*)2 );
sl@0
   607
	DoTestCleanupHandler(TlsCleanupThread1, (TAny*)3 );
sl@0
   608
	DoTestCleanupHandler(TlsCleanupThread2, NULL );
sl@0
   609
sl@0
   610
	test.End();
sl@0
   611
sl@0
   612
	HEAP_MARKEND;
sl@0
   613
	}
sl@0
   614
#endif
sl@0
   615
sl@0
   616
void Benchmark()
sl@0
   617
	{
sl@0
   618
	const TInt KMaxEntries=(512*1024)/sizeof(STls); // limits TLS entries to 512K of storage
sl@0
   619
	const TInt KMaxTime=500000; // limits time to half a second
sl@0
   620
	
sl@0
   621
	HEAP_MARK;
sl@0
   622
sl@0
   623
	test.Start(_L("SetTls()"));//Note: much slower if done in reverse order
sl@0
   624
	TInt count=0;
sl@0
   625
	TTime start;
sl@0
   626
	TTime finish;
sl@0
   627
	TTime now;
sl@0
   628
	start.HomeTime();
sl@0
   629
	finish.HomeTime();
sl@0
   630
	finish+=TTimeIntervalMicroSeconds(KMaxTime);
sl@0
   631
	while (now.HomeTime(),now<finish && count<KMaxEntries)
sl@0
   632
		{
sl@0
   633
		test(UserSvr::DllSetTls(count,KCheckValue)==KErrNone);
sl@0
   634
		count++;
sl@0
   635
		}
sl@0
   636
	TTimeIntervalMicroSeconds elapsed=now.MicroSecondsFrom(start);
sl@0
   637
	TInt scount=(TInt)((count*500000.0)/elapsed.Int64()); // scale count up to 1/2 second
sl@0
   638
	test.Printf(_L("     Done %d in 1/2 sec\n"),scount);
sl@0
   639
	if (count==KMaxEntries)
sl@0
   640
		test.Printf(_L("     (%d in %f sec)\n"),count,elapsed.Int64()/1000000.0);
sl@0
   641
sl@0
   642
	TInt max=count;
sl@0
   643
sl@0
   644
	test.Next(_L("Tls()"));
sl@0
   645
	TInt i=0;
sl@0
   646
	count=0;
sl@0
   647
	finish.HomeTime();
sl@0
   648
	finish+=TTimeIntervalMicroSeconds(KMaxTime);
sl@0
   649
	while (now.HomeTime(),now<finish)
sl@0
   650
		{
sl@0
   651
		test(UserSvr::DllTls(i)==KCheckValue);
sl@0
   652
		count++;
sl@0
   653
		if (++i==max)
sl@0
   654
			i=0;
sl@0
   655
		}
sl@0
   656
	elapsed=now.MicroSecondsFrom(start);
sl@0
   657
	scount=(TInt)((count*500000.0)/elapsed.Int64()); // scale count up to 1/2 second
sl@0
   658
	test.Printf(_L("     Done %d in 1/2 sec\n"),scount);
sl@0
   659
sl@0
   660
	test.Next(_L("FreeTls()"));//Note: much faster if done in reverse order
sl@0
   661
	count=0;
sl@0
   662
	start.HomeTime();
sl@0
   663
	finish.HomeTime();
sl@0
   664
	finish+=TTimeIntervalMicroSeconds(KMaxTime);
sl@0
   665
	while (now.HomeTime(),now<finish)
sl@0
   666
		{
sl@0
   667
		UserSvr::DllFreeTls(count++);
sl@0
   668
		if (count==max)
sl@0
   669
			break;
sl@0
   670
		}
sl@0
   671
	elapsed=now.MicroSecondsFrom(start);
sl@0
   672
	scount=(TInt)((count*500000.0)/elapsed.Int64()); // scale count up to 1/2 second
sl@0
   673
	test.Printf(_L("     Done %d in 1/2 sec\n"),scount);
sl@0
   674
	if (count==max)
sl@0
   675
		test.Printf(_L("     (%d in %f sec)\n"),count,elapsed.Int64()/1000000.0);
sl@0
   676
	
sl@0
   677
	while (count<max)
sl@0
   678
		UserSvr::DllFreeTls(--max);
sl@0
   679
sl@0
   680
	HEAP_MARKEND;
sl@0
   681
	test.End();
sl@0
   682
	}
sl@0
   683
sl@0
   684
TInt E32Main()
sl@0
   685
	{
sl@0
   686
sl@0
   687
	test.Title();
sl@0
   688
	test.Start(_L("Test"));
sl@0
   689
sl@0
   690
	// Turn off lazy dll unloading
sl@0
   691
	RLoader l;
sl@0
   692
	test(l.Connect()==KErrNone);
sl@0
   693
	test(l.CancelLazyDllUnload()==KErrNone);
sl@0
   694
	l.Close();
sl@0
   695
sl@0
   696
	Test();
sl@0
   697
	test.Next(_L("Thread Test"));
sl@0
   698
sl@0
   699
	HEAP_MARK;
sl@0
   700
sl@0
   701
	RThread thread;
sl@0
   702
	TInt r=thread.Create(_L("TLS test thread"),TestThread,KDefaultStackSize,0x1000,0x8000,NULL);
sl@0
   703
	test(r==KErrNone);
sl@0
   704
	TRequestStatus stat;
sl@0
   705
	thread.Logon(stat);
sl@0
   706
	thread.Resume();
sl@0
   707
	User::WaitForRequest(stat);
sl@0
   708
	test(stat==KErrNone);
sl@0
   709
	test(thread.ExitCategory()==_L("Kill"));
sl@0
   710
	test(thread.ExitType()==EExitKill); 
sl@0
   711
	test(thread.ExitReason()==KErrNone); 
sl@0
   712
	CLOSE_AND_WAIT(thread);
sl@0
   713
	
sl@0
   714
	HEAP_MARKEND;
sl@0
   715
sl@0
   716
	if (TLSStoredOnUserHeap())
sl@0
   717
		{
sl@0
   718
		MainHeap = &User::Heap();
sl@0
   719
		TestUserSideTls1();
sl@0
   720
		TestUserSideTls2();
sl@0
   721
		TestUserSideTls3();
sl@0
   722
		TestUserSideTls4();		
sl@0
   723
		}
sl@0
   724
	
sl@0
   725
	User::After(100);
sl@0
   726
	
sl@0
   727
	// On serial screen systems, WSERV may not yet have completed the disconnect
sl@0
   728
	// from the second thread. This causes the kernel heap check to fail.
sl@0
   729
	// So we wait here until WSERV can run.
sl@0
   730
	test.Printf(_L("Wait for WSERV\n"));
sl@0
   731
sl@0
   732
//	TestCleanupHandler();
sl@0
   733
sl@0
   734
	test.Next(_L("Benchmark"));
sl@0
   735
	Benchmark();
sl@0
   736
	test.End();
sl@0
   737
	return KErrNone;
sl@0
   738
	}