os/kernelhwsrv/kerneltest/e32test/thread/t_thread2.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) 1998-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\thread\t_thread2.cpp
sl@0
    15
// More tests the RThread class (T_THREAD was getting too big)
sl@0
    16
// Overview:
sl@0
    17
// Tests the RThread class
sl@0
    18
// API Information:
sl@0
    19
// RThread
sl@0
    20
// Details:
sl@0
    21
// - Test running a thread that suspends itself.  This activity has 
sl@0
    22
// deadlocked the Emulator in the past.
sl@0
    23
// - Test a server panicking a thread in another process that's already exited
sl@0
    24
// Platforms/Drives/Compatibility:
sl@0
    25
// All.
sl@0
    26
// Assumptions/Requirement/Pre-requisites:
sl@0
    27
// Failures and causes:
sl@0
    28
// Base Port information:
sl@0
    29
// 
sl@0
    30
//
sl@0
    31
sl@0
    32
#define __E32TEST_EXTENSION__
sl@0
    33
#include <e32test.h>
sl@0
    34
#include <e32panic.h>
sl@0
    35
#include <e32debug.h>
sl@0
    36
sl@0
    37
const TInt KHeapSize=0x200;
sl@0
    38
sl@0
    39
_LIT(KMyName, "T_THREAD2");
sl@0
    40
sl@0
    41
LOCAL_D RTest test(KMyName);
sl@0
    42
sl@0
    43
LOCAL_C TInt SuspendThread(TAny*)
sl@0
    44
	{
sl@0
    45
	
sl@0
    46
	RThread().Suspend();
sl@0
    47
	return(KErrNone);
sl@0
    48
	}
sl@0
    49
sl@0
    50
sl@0
    51
LOCAL_D void TestSelfSuspend(TOwnerType anOwnerType)
sl@0
    52
//
sl@0
    53
// Test running a thread that suspends itself.  This activity has 
sl@0
    54
// deadlocked the Emulator in the past
sl@0
    55
//
sl@0
    56
	{
sl@0
    57
sl@0
    58
	RThread suspendThread;
sl@0
    59
	TInt r;
sl@0
    60
	TRequestStatus s;
sl@0
    61
	TInt jit=User::JustInTime();
sl@0
    62
	test.Start(_L("Test running a thread which suspends itself"));
sl@0
    63
	test.Next(_L("Create the thread"));
sl@0
    64
	r=suspendThread.Create(KNullDesC,SuspendThread,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)NULL,anOwnerType);
sl@0
    65
	test(r==KErrNone);
sl@0
    66
	suspendThread.Logon(s);
sl@0
    67
	suspendThread.Resume();
sl@0
    68
	test.Next(_L("Wait a second"));
sl@0
    69
	User::After(1000000);
sl@0
    70
	User::SetJustInTime(EFalse);
sl@0
    71
	suspendThread.Panic(_L("FEDCBA9876543210fedcba"),999);
sl@0
    72
	User::WaitForRequest(s);
sl@0
    73
	User::SetJustInTime(jit);
sl@0
    74
	test(suspendThread.ExitType()==EExitPanic);
sl@0
    75
	test(suspendThread.ExitReason()==999);
sl@0
    76
	test(suspendThread.ExitCategory()==_L("FEDCBA9876543210"));
sl@0
    77
	CLOSE_AND_WAIT(suspendThread);
sl@0
    78
	test.End();
sl@0
    79
	}
sl@0
    80
sl@0
    81
sl@0
    82
_LIT(KServerName,"MyServer");
sl@0
    83
sl@0
    84
RSemaphore TheSemaphore;
sl@0
    85
sl@0
    86
class CMySession : public CSession2
sl@0
    87
	{
sl@0
    88
public:
sl@0
    89
	CMySession();
sl@0
    90
	virtual void ServiceL(const RMessage2& aMessage);
sl@0
    91
	};
sl@0
    92
sl@0
    93
class CMyServer : public CServer2
sl@0
    94
	{
sl@0
    95
public:
sl@0
    96
	CMyServer();
sl@0
    97
	virtual CSession2* NewSessionL(const TVersion& aVersion, const RMessage2& aMessage) const;
sl@0
    98
	};
sl@0
    99
sl@0
   100
class RSession : public RSessionBase
sl@0
   101
	{
sl@0
   102
public:
sl@0
   103
	TInt Open();
sl@0
   104
	void Test(TRequestStatus& aStatus);
sl@0
   105
	};
sl@0
   106
sl@0
   107
CMySession::CMySession()
sl@0
   108
	{
sl@0
   109
	}
sl@0
   110
sl@0
   111
CMyServer::CMyServer() :
sl@0
   112
	CServer2(0)
sl@0
   113
	{
sl@0
   114
	}
sl@0
   115
sl@0
   116
CSession2* CMyServer::NewSessionL(const TVersion&, const RMessage2&) const
sl@0
   117
	{
sl@0
   118
	RDebug::Printf("Server: create session");
sl@0
   119
	return new (ELeave) CMySession;
sl@0
   120
	}
sl@0
   121
sl@0
   122
void CMySession::ServiceL(const RMessage2& aMessage)
sl@0
   123
	{
sl@0
   124
	RDebug::Printf("Server: receive message");
sl@0
   125
	TheSemaphore.Wait();
sl@0
   126
	RDebug::Printf("Server: panic client");
sl@0
   127
	aMessage.Panic(_L("!!!"), 1);
sl@0
   128
	CActiveScheduler::Stop();
sl@0
   129
	}
sl@0
   130
sl@0
   131
TInt RSession::Open()
sl@0
   132
	{
sl@0
   133
	return CreateSession(KServerName, TVersion());
sl@0
   134
	}
sl@0
   135
sl@0
   136
void RSession::Test(TRequestStatus& aStatus)
sl@0
   137
	{
sl@0
   138
	RDebug::Printf("Client: send message");
sl@0
   139
	SendReceive(0, TIpcArgs(), aStatus);
sl@0
   140
	RDebug::Printf("Client: send done");
sl@0
   141
	}
sl@0
   142
sl@0
   143
TInt ServerThread(TAny*)
sl@0
   144
	{
sl@0
   145
	RDebug::Printf("Server: start");
sl@0
   146
	
sl@0
   147
	CTrapCleanup* cleanup = CTrapCleanup::New();
sl@0
   148
sl@0
   149
	CActiveScheduler* pR = new CActiveScheduler;
sl@0
   150
	if (pR == NULL)
sl@0
   151
		return KErrNoMemory;
sl@0
   152
	CActiveScheduler::Install(pR);
sl@0
   153
	
sl@0
   154
	CMyServer* pS = new CMyServer;
sl@0
   155
	if (pS == NULL)
sl@0
   156
		return KErrNoMemory;
sl@0
   157
	TInt r = pS->Start(KServerName);
sl@0
   158
	if (r != KErrNone)
sl@0
   159
		return r;
sl@0
   160
	RThread::Rendezvous(KErrNone);
sl@0
   161
	
sl@0
   162
	TRAP(r, CActiveScheduler::Start());
sl@0
   163
	if (r != KErrNone)
sl@0
   164
		return r;
sl@0
   165
	
sl@0
   166
	delete pS;
sl@0
   167
	delete pR;
sl@0
   168
	delete cleanup;
sl@0
   169
	
sl@0
   170
	RDebug::Printf("Server: exit");
sl@0
   171
	return KErrNone;
sl@0
   172
	}
sl@0
   173
sl@0
   174
TInt ClientProcess()
sl@0
   175
	{
sl@0
   176
	RDebug::Printf("Client: open session");
sl@0
   177
	RSession session;
sl@0
   178
	session.Open();
sl@0
   179
	TRequestStatus status;
sl@0
   180
	RDebug::Printf("Client: send request");
sl@0
   181
	session.Test(status);
sl@0
   182
	RDebug::Printf("Client: exit");
sl@0
   183
	return KErrNone;
sl@0
   184
	}
sl@0
   185
sl@0
   186
void TestServerPanic()
sl@0
   187
	{
sl@0
   188
	TRequestStatus status;
sl@0
   189
	
sl@0
   190
	test_KErrNone(TheSemaphore.CreateLocal(0));
sl@0
   191
	
sl@0
   192
	RDebug::Printf("Main: start server");
sl@0
   193
	RThread serverThread;
sl@0
   194
	test_KErrNone(serverThread.Create(_L("server"), ServerThread, 4096, NULL, NULL));
sl@0
   195
	serverThread.Rendezvous(status);
sl@0
   196
	serverThread.Resume();
sl@0
   197
	User::WaitForRequest(status);
sl@0
   198
	test_KErrNone(status.Int());
sl@0
   199
sl@0
   200
	RDebug::Printf("Main: start client");
sl@0
   201
	RProcess clientProcess;
sl@0
   202
	test_KErrNone(clientProcess.Create(KMyName, _L("client")));
sl@0
   203
	clientProcess.Resume();
sl@0
   204
	clientProcess.Logon(status);
sl@0
   205
	User::WaitForRequest(status);
sl@0
   206
	test_KErrNone(clientProcess.ExitReason());
sl@0
   207
	test_Equal(EExitKill, clientProcess.ExitType());
sl@0
   208
sl@0
   209
	RDebug::Printf("Main: kick server");
sl@0
   210
	TheSemaphore.Signal();
sl@0
   211
	
sl@0
   212
	RDebug::Printf("Main: wait for server to exit");
sl@0
   213
	serverThread.Logon(status);
sl@0
   214
	User::WaitForRequest(status);
sl@0
   215
	test_KErrNone(serverThread.ExitReason());
sl@0
   216
	test_Equal(EExitKill, serverThread.ExitType());
sl@0
   217
sl@0
   218
	User::After(1);
sl@0
   219
	RDebug::Printf("Main: exit");
sl@0
   220
	}
sl@0
   221
sl@0
   222
sl@0
   223
GLDEF_C TInt E32Main()
sl@0
   224
//
sl@0
   225
// Main
sl@0
   226
//
sl@0
   227
	{	
sl@0
   228
sl@0
   229
	if (User::CommandLineLength())
sl@0
   230
		return ClientProcess();
sl@0
   231
sl@0
   232
	test.Title();
sl@0
   233
	__UHEAP_MARK;
sl@0
   234
	
sl@0
   235
	test.Start(_L("Test threads"));
sl@0
   236
sl@0
   237
	test.Next(_L("Test a thread suspending itself"));
sl@0
   238
	TestSelfSuspend(EOwnerProcess);
sl@0
   239
	TestSelfSuspend(EOwnerThread);
sl@0
   240
sl@0
   241
	test.Next(_L("Test a server panicking a thread in another process that's already exited"));
sl@0
   242
	TestServerPanic();
sl@0
   243
	
sl@0
   244
	test.End();
sl@0
   245
	__UHEAP_MARKEND;
sl@0
   246
	return(KErrNone);
sl@0
   247
	}