os/ossrv/genericopenlibs/cstdlib/TSTLIB/TMTHREAD.CPP
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // Test code for multi-threaded file descriptors etc.
    15 // 
    16 //
    17 
    18 #include <e32std.h>
    19 #include <e32svr.h>	// for RDebug
    20 #include <stdlib.h>
    21 #include <unistd.h>
    22 #include <stdio.h>
    23 #include <string.h>
    24 #include <sys/fcntl.h>
    25 #include <sys/errno.h>
    26 
    27 extern "C" {
    28 #include "CTEST.H"
    29 }
    30 
    31 #include <estlib.h>	// for multi-threading control
    32 
    33 
    34 #ifdef _DEBUG
    35 	#define ttest_Next(testname)	\
    36 	RDebug::Print(_L("t%d: %s"), (TInt)thread, testname); \
    37 	printf("t%d: ", (TInt)thread); \
    38 	test_Next(testname)
    39 #else
    40 	//don't use RDebug::Print when it's a release build
    41 	#define ttest_Next(testname)   test_Next(testname)
    42 #endif
    43 
    44 test_Data;
    45 RSemaphore waiting[2];
    46 
    47 void over(TInt n)
    48 	{
    49 	// RDebug::Print(_L("Over(%d)"),n);
    50 	waiting[1-n].Signal();
    51 	waiting[n].Wait();
    52 	}
    53 
    54 #define THREAD0		if (thread==0)
    55 #define THREAD1		if (thread==(TAny*)1)
    56 #define OVER		over((TInt)thread)
    57 
    58 // Shared variables
    59 
    60 int fd;
    61 
    62 /**
    63 Interleaved test code
    64 
    65 @SYMTestCaseID          SYSLIB-STDLIB-CT-1071
    66 @SYMTestCaseDesc	    Tests for multi-threaded file descriptors
    67 @SYMTestPriority 	    High
    68 @SYMTestActions  	    Run threads to open,read,close test files
    69 @SYMTestExpectedResults Test must not fail
    70 @SYMREQ                 REQ0000
    71 */		
    72 TInt testfunction(TAny* thread)
    73 	{
    74 	int n;
    75 	char *p;
    76 	ttest_Next("Entering testfunction");
    77 	//
    78 	ttest_Next("Competing console reads - press 'A' then 'B'...");
    79 	fflush(stdout);
    80 	int c=getchar();
    81 	fprintf(stderr, "t%d: read char %d\r\n", thread, c);
    82 	//
    83 	THREAD0
    84 		{
    85 		waiting[0].Wait();	// until Thread1 says OVER
    86 		ttest_Next("Create test file");
    87 		fd=open("c:\\testfile", O_RDWR+O_CREAT+O_TRUNC, 0777);
    88 		test_ok(fd>=0);
    89 		}
    90 	//
    91 	OVER;
    92 	ttest_Next("Get the sequencing sorted out...");
    93 	OVER;
    94 	THREAD1
    95 		{
    96 		ttest_Next("Write to test file");
    97 		p="Hello from thread 1\r\n";
    98 		n=write(fd,p,strlen(p));
    99 		test_ok(n==strlen(p));
   100 		}
   101 	THREAD0
   102 		{
   103 		ttest_Next("Close test file");
   104 		close(fd);
   105 		}
   106 	OVER;
   107 	THREAD1
   108 		{
   109 		ttest_Next("Reopen test file");
   110 		fd=open("c:\\testfile",O_RDONLY,0);
   111 		test_ok(fd>=0);
   112 		}
   113 	THREAD0
   114 		{
   115 		ttest_Next("Read from test file");
   116 		char buf[80];
   117 		buf[6]='\0';
   118 		n=read(fd,buf,6);
   119 		test_ok(n==6);
   120 		test(strncmp(buf,"Hello ",6)==0);
   121 		printf("Read >%s<... \r\n",buf);
   122 		fflush(stdout);
   123 		}
   124 	OVER;
   125 	THREAD1
   126 		{
   127 		ttest_Next("Read from test file");
   128 		close(0);
   129 		n=dup2(fd,0);
   130 		test(n>=0);	// associate stdin with "testfile"
   131 		char buf[80];
   132 		p=fgets(buf,80,stdin);
   133 		test_ok(p==buf);
   134 		fprintf(stderr, "Read >%s<\r\n", buf);
   135 		}
   136 	THREAD0
   137 		{
   138 		ttest_Next("Close test file");
   139 		close(fd);
   140 		}
   141 	OVER;
   142 	ttest_Next("Completed testfunction");
   143 	waiting[0].Signal();	// allow thread0 to continue
   144 	waiting[1].Signal();	// allow thread1 to continue
   145 	return 0;
   146 	}
   147 
   148 // Thread management - main thread is thread0
   149 
   150 void init_threads()
   151 	{
   152 	waiting[0].CreateLocal(0);
   153 	waiting[1].CreateLocal(0);
   154 
   155 	RThread thread1;
   156 	TInt err=thread1.Create(_L("Thread1"),testfunction,0x10000,NULL,(TAny*)1);
   157 	test(err==KErrNone);
   158 	test_Next("Starting thread1");
   159 	thread1.Resume();
   160 	test_Next("entering main test...");
   161 	}
   162 
   163 int main(int argc, char *argv[])
   164 	{
   165 	// SpawnPosixServerThread(); - provided by MCRT0.OBJ
   166 
   167 	start_redirection_server();
   168 
   169 	test_Title("TMTHREAD");
   170 	init_threads();
   171 	testfunction(0);
   172 	test_Close();
   173 	return KErrNone;
   174 	}