thread_message.c 6.31 KB
Newer Older
Diederik van Arkel's avatar
Diederik van Arkel committed
1
2
3
4
5
6

#include <stdio.h>
#include <windows.h>
#include "Clean.h"
#include "thread_message.h"

7
8
static int CleanCompiler_message_nunber;

Diederik van Arkel's avatar
Diederik van Arkel committed
9
10
11
12
13
14
15
16
17
18
int get_message_number (void)
{
	return RegisterWindowMessage ("CleanCompiler");
}

int get_current_thread_id (void)
{
	return GetCurrentThreadId();
}

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
int compiler_result_handler_installed=0;

extern void (*dispatch_null_message_hook) (MSG*);

#define MAX_N_COMPILERS 32

int compiler_finished[MAX_N_COMPILERS];
int compiler_exit_codes[MAX_N_COMPILERS];

void compiler_result_handler (MSG *msg)
{	
	if (msg->message==CleanCompiler_message_nunber){
		unsigned int compiler_n;

		compiler_n=msg->wParam;

		if (compiler_n<MAX_N_COMPILERS){
			compiler_exit_codes[compiler_n]=msg->lParam;
			compiler_finished[compiler_n]=1;
		}
	}
}

void install_compiler_result_handler (void)
{
	CleanCompiler_message_nunber=get_message_number();

	dispatch_null_message_hook = &compiler_result_handler;
}

Diederik van Arkel's avatar
Diederik van Arkel committed
49
int start_compiler_process (CleanString compiler_path,CleanString compiler_directory,CleanString command,
John van Groningen's avatar
John van Groningen committed
50
							int *compiler_thread_id_p,size_t *compiler_thread_handle_p,size_t *compiler_process_handle_p)
Diederik van Arkel's avatar
Diederik van Arkel committed
51
52
53
54
55
56
{
	PSTR application_name,command_line,env,dir;
	STARTUPINFO si;
	PROCESS_INFORMATION pi;
	int r;

57
58
59
60
61
	if (!compiler_result_handler_installed){
		install_compiler_result_handler();
		compiler_result_handler_installed=1;
	}

Diederik van Arkel's avatar
Diederik van Arkel committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
	application_name=CleanStringCharacters (compiler_path);
	dir=CleanStringCharacters (compiler_directory);
	command_line=CleanStringCharacters (command);
	env=NULL;
	
	si.cb = sizeof (STARTUPINFO);
	si.lpReserved = NULL;
	si.lpReserved2 = NULL;
	si.cbReserved2 = 0;
	si.lpDesktop = NULL;
	si.lpTitle = NULL;
	si.dwFlags = 0;

	r=CreateProcess (application_name,command_line,NULL,NULL,TRUE,DETACHED_PROCESS/*0*/,env,dir,&si,&pi);

	if (r!=0){
		*compiler_thread_id_p=pi.dwThreadId;
		*compiler_thread_handle_p=pi.hThread;
		*compiler_process_handle_p=pi.hProcess;
	} else {
		*compiler_thread_id_p=0;
		*compiler_thread_handle_p=0;
		*compiler_process_handle_p=0;
	}
	
	return r;
}

int get_integers_from_message (int wm_number,int *i1_p,int *i2_p)
{
	MSG message;
	int r;

	r=GetMessage (&message,NULL,wm_number,wm_number);

	if (r!=0){
		*i1_p=message.wParam;
		*i2_p=message.lParam;
	} else {
		*i1_p=0;
		*i2_p=0;
	}
	
	return r;
}

108
109
#define PM_QS_POSTMESSAGE   ((QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER) << 16)

John van Groningen's avatar
John van Groningen committed
110
int get_integers_from_thread_message (int wm_number,size_t thread_handle,int *i1_p,int *i2_p)
Diederik van Arkel's avatar
Diederik van Arkel committed
111
112
113
114
{
	MSG message;
	int r;

115
	r=PeekMessage (&message,INVALID_HANDLE_VALUE,wm_number,wm_number,PM_NOREMOVE | (QS_POSTMESSAGE<<16));
Diederik van Arkel's avatar
Diederik van Arkel committed
116
117
118
119
120
121
122
123
124
	while (r==0){
		r=MsgWaitForMultipleObjects (1,&thread_handle,0,INFINITE,QS_POSTMESSAGE);

		if (r==-1 || r==WAIT_OBJECT_0 || r==WAIT_ABANDONED_0){
			*i1_p=0;
			*i2_p=0;
			return 0;
		}

125
		r=PeekMessage (&message,INVALID_HANDLE_VALUE,wm_number,wm_number,PM_NOREMOVE | (QS_POSTMESSAGE<<16));
Diederik van Arkel's avatar
Diederik van Arkel committed
126
	}
127

128
129
	r=PeekMessage (&message,INVALID_HANDLE_VALUE,wm_number,wm_number,PM_REMOVE | (QS_POSTMESSAGE<<16));
/*	r=GetMessage (&message,INVALID_HANDLE_VALUE,wm_number,wm_number); */
Diederik van Arkel's avatar
Diederik van Arkel committed
130
131
132
133
134
135
136
137
138
139
140
141

	if (r!=0){
		*i1_p=message.wParam;
		*i2_p=message.lParam;
	} else {
		*i1_p=0;
		*i2_p=0;
	}
	
	return r;
}

John van Groningen's avatar
John van Groningen committed
142
int get_string_from_file_map_and_delete_map (size_t file_map,CleanString s)
Diederik van Arkel's avatar
Diederik van Arkel committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
{
	int l,i;
	char *chars,*p;
	
	chars=CleanStringCharacters (s);
	l=CleanStringLength (s);
	
	p=MapViewOfFile ((HANDLE)file_map,FILE_MAP_ALL_ACCESS,0,0,l);
	if (p==NULL)
		return 0;

	for (i=0; i<l; ++i)
		chars[i]=p[i];
				
	UnmapViewOfFile (p);

	CloseHandle ((HANDLE)file_map);
	
	return 1;
}

John van Groningen's avatar
John van Groningen committed
164
int send_string_to_thread (int thread_id,size_t process_handle,int wm_number,CleanString s)
Diederik van Arkel's avatar
Diederik van Arkel committed
165
166
167
168
169
170
171
172
173
174
175
176
177
178
{
	HANDLE file_map,file_map2;
	char *chars,*p1;
	int r,l;
	/*
	int a;
	*/

	chars=CleanStringCharacters (s);
	l=CleanStringLength (s);
	
	if (l==0 || chars[l-1]!='\0')
		return 0;

John van Groningen's avatar
John van Groningen committed
179
	file_map=CreateFileMapping ((HANDLE)(-1),NULL,PAGE_READWRITE,0,l,NULL);
Diederik van Arkel's avatar
Diederik van Arkel committed
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
	if (file_map==NULL)
		return 0;

	p1=MapViewOfFile (file_map,FILE_MAP_ALL_ACCESS,0,0,l);
	if (p1==NULL)
		return 0;
		
	{
		char *s_p,*d_p,c;
		
		s_p=chars;
		d_p=p1;
		do {
			c=*s_p++;
			*d_p++=c;
		} while (c!='\0');
	}
	
	UnmapViewOfFile (p1);

	r=DuplicateHandle (GetCurrentProcess(),file_map,(HANDLE)process_handle,&file_map2,0,0,DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS);

	if (r==0)
		return 0;

	do {
		/*
		r=PostThreadMessage (thread_id,wm_number,a,l);
		*/
		r=PostThreadMessage (thread_id,wm_number,l,(int)file_map2);
	} while (r==0);
	
	return r;
}

int send_integers_to_thread (int thread_id,int wm_number,int i1,int i2)
{
	int r;
		
	do {
		r=PostThreadMessage (thread_id,wm_number,i1,i2);
	} while (r==0);
	
	return r;
}
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252

int compiler_id=-1;

int set_compiler_id (int compiler_id_p)
{
	compiler_id=compiler_id_p;
	return compiler_id_p;
}

int get_compiler_id (void)
{
	return compiler_id;
}

int get_finished_compiler_id_and_exit_code (int *exit_code_p)
{
	int compiler_n;
	
	for (compiler_n=0; compiler_n<MAX_N_COMPILERS; ++compiler_n)
		if (compiler_finished[compiler_n]){
			*exit_code_p=compiler_exit_codes[compiler_n];
			compiler_finished[compiler_n]=0;
			return compiler_n;
		}
	
	*exit_code_p=0;
	return -1;
}
John van Groningen's avatar
John van Groningen committed
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300

void win_create_process (PSTR commandline,PSTR dir,int *success,int *exitcode)
{
	SECURITY_ATTRIBUTES sa;
	STARTUPINFO si;
	PROCESS_INFORMATION pi;
	BOOL fsuccess;

	*success = FALSE;
	*exitcode = -1;

	sa.nLength = sizeof (SECURITY_ATTRIBUTES);
	sa.bInheritHandle = TRUE;
	sa.lpSecurityDescriptor = NULL;

	si.cb = sizeof (STARTUPINFO);
	si.lpReserved = NULL;
	si.lpReserved2 = NULL;
	si.cbReserved2 = 0;
	si.lpDesktop = NULL;
	si.lpTitle = NULL;
	si.dwFlags = 0;
	si.hStdInput = NULL;
	si.hStdOutput = NULL;
	si.hStdError = NULL;

	fsuccess =
		CreateProcess (NULL,				/* pointer to name of executable module		*/
					   commandline,			/* pointer to command line string			*/
					   NULL,				/* pointer to process security attributes	*/
					   NULL,				/* pointer to thread security attributes	*/
					   TRUE,				/* handle inheritance flag					*/
					   DETACHED_PROCESS,	/* creation flags							*/
					   NULL,				/* pointer to new environment block			*/
					   dir,					/* pointer to current directory name		*/
					   &si,					/* pointer to STARTUPINFO					*/
					   &pi					/* pointer to PROCESS_INFORMATION			*/
		);

	if (fsuccess) {
		WaitForSingleObject (pi.hProcess, INFINITE);
		GetExitCodeProcess (pi.hProcess, (unsigned long *) exitcode);
		*success = TRUE;
	} else {
		*success = FALSE;
		*exitcode = -1;
	}
}