Update contrib.
1 // Copyright (c) 1998-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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // C routines for creating EPOC32 threads
19 #include <estlib.h> /* for SpawnPosixServerThread */
21 #include <stdio.h> /* for popen3 */
22 #include <stdlib.h> /* for mbstowcs */
24 #include <sys/errno.h> /* for errno */
27 typedef void (*FUNC)();
33 TRequestStatus iStatus;
37 TInt threadhelper (TAny* aFn)
47 IMPORT_C void NewProcessId(); // WINS bodges for multiple "processes"
48 IMPORT_C void NextProcessFn(TAny*);
50 TInt processhelper (TAny* aFn)
52 // Do the MCRT0.OBJ things straight away
54 SpawnPosixServerThread();
56 getcwd(wd, sizeof(wd)); // connect to CPosixServer
57 return threadhelper(aFn);
65 EXPORT_C int start_posix_server()
67 start_redirection_server();
68 return SpawnPosixServerThread();
71 EXPORT_C void* create_thread(void (*aFn)(), char* aName)
74 TPtrC8 ptr((TText8*)aName);
78 TPtrC8 name((TText8*)aName);
80 struct cthread* t = new cthread;
81 // 16k stack, share parent's heap
82 TInt err=t->iThread.Create(name, threadhelper, 0x4000, NULL, (TAny*)aFn);
83 t->iThread.Logon(t->iStatus);
89 EXPORT_C void start_thread(void* aThread)
91 struct cthread* t=REINTERPRET_CAST(struct cthread*,aThread);
95 EXPORT_C int wait_for_thread(void* aThread)
97 struct cthread* t=REINTERPRET_CAST(struct cthread*,aThread);
98 User::WaitForRequest(t->iStatus);
99 int ret=t->iThread.ExitReason();
105 EXPORT_C void* create_process(void (*aFn)(), char* aName, char* mode, int fids[3])
108 TPtrC8 ptr((TText8*)aName);
112 TPtrC8 name((TText8*)aName);
113 #endif /* _UNICODE */
114 struct cthread* t = new cthread;
115 TFileName this_exe = t->iProcess.FileName();
117 cmd.Format(_L("%S %S"),&this_exe,&name);
121 wchar_t wmode[MAXPATHLEN+1];
122 mbstowcs(wmode, mode, MAXPATHLEN);
123 t->pid=wpopen3((const wchar_t*)cmd.Ptr(), wmode, 0, fids);
125 t->pid=popen3((const char*)cmd.Ptr(), mode, 0, fids);
129 User::After(1000000); // 1 Second
133 EXPORT_C void start_process(void* /*aProcess*/)
135 // too late, it's already running!
138 EXPORT_C int wait_for_process(void* aProcess)
140 struct cthread* t=REINTERPRET_CAST(struct cthread*,aProcess);
142 int pid=waitpid(t->pid, &exit, 0);
148 EXPORT_C int wait_for_process_id(void* aProcess, int procid, int opt, int* status)
150 // added function to enable calling of waitpid with specific parameters
151 int pid=waitpid(procid, status, opt);
155 EXPORT_C int get_proc_id(void* aProcess)
157 // return the pid of a process
158 struct cthread* t=REINTERPRET_CAST(struct cthread*,aProcess);
162 // Testing the dodgy asynchronous form of select
164 EXPORT_C int async_ioctl(int aFid, int aCmd, void* aParam, int* status)
166 TRequestStatus& theStatus = *(TRequestStatus*)status;
167 return ioctl(aFid,aCmd,aParam,theStatus);
170 EXPORT_C int async_ioctl_completion(int aFid, int aCmd, void* aParam, int* status)
172 TRequestStatus& theStatus = *(TRequestStatus*)status;
173 User::WaitForRequest(theStatus);
174 return ioctl_complete(aFid,aCmd,aParam,theStatus);