os/ossrv/genericopenlibs/cstdlib/TSTLIB/CTHREAD.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 "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
// C routines for creating EPOC32 threads
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include <e32std.h>
sl@0
    19
#include <estlib.h>		/* for SpawnPosixServerThread */
sl@0
    20
#include <unistd.h>
sl@0
    21
#include <stdio.h>		/* for popen3 */
sl@0
    22
#include <stdlib.h>		/* for mbstowcs */
sl@0
    23
#include "CTEST.H"
sl@0
    24
#include <sys/errno.h>		/* for errno */
sl@0
    25
#include <sys/wait.h>
sl@0
    26
sl@0
    27
typedef void (*FUNC)();
sl@0
    28
sl@0
    29
struct cthread 
sl@0
    30
{
sl@0
    31
	RThread iThread;
sl@0
    32
	RProcess iProcess;
sl@0
    33
	TRequestStatus iStatus;
sl@0
    34
	int pid;
sl@0
    35
};
sl@0
    36
sl@0
    37
TInt threadhelper (TAny* aFn)
sl@0
    38
	{
sl@0
    39
	CTrapCleanup::New();
sl@0
    40
	FUNC f=(FUNC)aFn;
sl@0
    41
	(*f)();
sl@0
    42
	return 0;
sl@0
    43
	}
sl@0
    44
sl@0
    45
#if defined(__WINS__)
sl@0
    46
sl@0
    47
IMPORT_C void NewProcessId();		// WINS bodges for multiple "processes"
sl@0
    48
IMPORT_C void NextProcessFn(TAny*);	
sl@0
    49
sl@0
    50
TInt processhelper (TAny* aFn)
sl@0
    51
	{
sl@0
    52
	// Do the MCRT0.OBJ things straight away
sl@0
    53
	NewProcessId();
sl@0
    54
	SpawnPosixServerThread();
sl@0
    55
	char wd[80];
sl@0
    56
	getcwd(wd, sizeof(wd));		// connect to CPosixServer
sl@0
    57
	return threadhelper(aFn);
sl@0
    58
	}
sl@0
    59
sl@0
    60
#endif //__WINS__
sl@0
    61
sl@0
    62
sl@0
    63
extern "C" {
sl@0
    64
sl@0
    65
EXPORT_C int start_posix_server()
sl@0
    66
	{
sl@0
    67
	start_redirection_server();
sl@0
    68
	return SpawnPosixServerThread();
sl@0
    69
	}
sl@0
    70
sl@0
    71
EXPORT_C void* create_thread(void (*aFn)(), char* aName)
sl@0
    72
	{
sl@0
    73
#ifdef _UNICODE
sl@0
    74
	TPtrC8 ptr((TText8*)aName);
sl@0
    75
	TBuf<80> name;
sl@0
    76
	name.Copy(ptr);
sl@0
    77
#else
sl@0
    78
	TPtrC8 name((TText8*)aName);
sl@0
    79
#endif /* _UNICODE */
sl@0
    80
	struct cthread* t = new cthread;
sl@0
    81
	// 16k stack, share parent's heap
sl@0
    82
	TInt err=t->iThread.Create(name, threadhelper, 0x4000, NULL, (TAny*)aFn);
sl@0
    83
	t->iThread.Logon(t->iStatus);
sl@0
    84
	if (err!= KErrNone)
sl@0
    85
		return 0;
sl@0
    86
	return (void*)t;
sl@0
    87
	}
sl@0
    88
sl@0
    89
EXPORT_C void start_thread(void* aThread)
sl@0
    90
	{
sl@0
    91
	struct cthread* t=REINTERPRET_CAST(struct cthread*,aThread);
sl@0
    92
	t->iThread.Resume();
sl@0
    93
	}
sl@0
    94
sl@0
    95
EXPORT_C int wait_for_thread(void* aThread)
sl@0
    96
	{
sl@0
    97
	struct cthread* t=REINTERPRET_CAST(struct cthread*,aThread);
sl@0
    98
	User::WaitForRequest(t->iStatus);
sl@0
    99
	int ret=t->iThread.ExitReason();
sl@0
   100
	t->iThread.Close();
sl@0
   101
	delete t;
sl@0
   102
	return ret;
sl@0
   103
	}
sl@0
   104
sl@0
   105
EXPORT_C void* create_process(void (*aFn)(), char* aName, char* mode, int fids[3])
sl@0
   106
	{
sl@0
   107
#ifdef _UNICODE
sl@0
   108
	TPtrC8 ptr((TText8*)aName);
sl@0
   109
	TBuf<80> name;
sl@0
   110
	name.Copy(ptr);
sl@0
   111
#else
sl@0
   112
	TPtrC8 name((TText8*)aName);
sl@0
   113
#endif /* _UNICODE */
sl@0
   114
	struct cthread* t = new cthread;
sl@0
   115
	TFileName this_exe = t->iProcess.FileName();
sl@0
   116
	TBuf<256> cmd;
sl@0
   117
	cmd.Format(_L("%S %S"),&this_exe,&name);
sl@0
   118
	cmd.ZeroTerminate();
sl@0
   119
sl@0
   120
#ifdef _UNICODE
sl@0
   121
	wchar_t wmode[MAXPATHLEN+1];
sl@0
   122
	mbstowcs(wmode, mode, MAXPATHLEN);
sl@0
   123
	t->pid=wpopen3((const wchar_t*)cmd.Ptr(), wmode, 0, fids);
sl@0
   124
#else
sl@0
   125
	t->pid=popen3((const char*)cmd.Ptr(), mode, 0, fids);
sl@0
   126
#endif
sl@0
   127
	if (t->pid < 0)
sl@0
   128
		return 0;
sl@0
   129
	User::After(1000000); // 1 Second
sl@0
   130
	return (void*)t;
sl@0
   131
	}
sl@0
   132
sl@0
   133
EXPORT_C void start_process(void* /*aProcess*/)
sl@0
   134
	{
sl@0
   135
	// too late, it's already running!
sl@0
   136
	}
sl@0
   137
sl@0
   138
EXPORT_C int wait_for_process(void* aProcess)
sl@0
   139
	{
sl@0
   140
	struct cthread* t=REINTERPRET_CAST(struct cthread*,aProcess);
sl@0
   141
	int exit=-1;
sl@0
   142
	int pid=waitpid(t->pid, &exit, 0);
sl@0
   143
	if (pid<0)
sl@0
   144
		return errno;
sl@0
   145
	return exit;
sl@0
   146
	}
sl@0
   147
sl@0
   148
EXPORT_C int wait_for_process_id(void* aProcess, int procid, int opt, int* status)
sl@0
   149
	{
sl@0
   150
	// added function to enable calling of waitpid with specific parameters
sl@0
   151
	int pid=waitpid(procid, status, opt);
sl@0
   152
	return pid;
sl@0
   153
	}
sl@0
   154
	
sl@0
   155
EXPORT_C int get_proc_id(void* aProcess)
sl@0
   156
	{
sl@0
   157
	// return the pid of a process
sl@0
   158
	struct cthread* t=REINTERPRET_CAST(struct cthread*,aProcess);
sl@0
   159
	return t->pid;
sl@0
   160
	}
sl@0
   161
sl@0
   162
// Testing the dodgy asynchronous form of select
sl@0
   163
sl@0
   164
EXPORT_C int async_ioctl(int aFid, int aCmd, void* aParam, int* status)
sl@0
   165
	{
sl@0
   166
	TRequestStatus& theStatus = *(TRequestStatus*)status;
sl@0
   167
	return ioctl(aFid,aCmd,aParam,theStatus);
sl@0
   168
	}
sl@0
   169
sl@0
   170
EXPORT_C int async_ioctl_completion(int aFid, int aCmd, void* aParam, int* status)
sl@0
   171
	{
sl@0
   172
	TRequestStatus& theStatus = *(TRequestStatus*)status;
sl@0
   173
	User::WaitForRequest(theStatus);
sl@0
   174
	return ioctl_complete(aFid,aCmd,aParam,theStatus);
sl@0
   175
	}
sl@0
   176
sl@0
   177
} // extern "C"