-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinux_process.cpp
591 lines (531 loc) · 21.6 KB
/
linux_process.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
49
50
51
52
53
54
55
56
57
58
59
60
61
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
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
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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
//
// Copyright 2015-2022 by Kevin L. Goodwin [[email protected]]; All rights reserved
//
// This file is part of K.
//
// K is free software: you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.
//
// K is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along
// with K. If not, see <http://www.gnu.org/licenses/>.
//
#include "ed_main.h"
// #include <functional>
#include <thread>
#include <mutex>
#include <signal.h>
#include <fcntl.h>
#include <sys/wait.h>
enum { INVALID_ProcessId = 0, INVALID_fd = -1, PIPE_RD=0, PIPE_WR=1 };
class piped_forker {
int d_fd = INVALID_fd;
int d_pid = INVALID_ProcessId;
int d_exit_status = -1;
int ReapChild();
public:
piped_forker() {}
bool ForkChildOk( const char *command );
int Status() const { 1 && DBG( "%s d_exit_status=%d", __func__, d_exit_status );
return d_exit_status;
}
ssize_t Read( void *dest, ssize_t sizeofDest );
};
int piped_forker::ReapChild() {
if( d_fd != INVALID_fd ) {
close( d_fd );
d_fd = INVALID_fd;
}
if( d_pid != INVALID_ProcessId ) {
kill( d_pid, SIGHUP );
alarm(1);
int status;
waitpid( d_pid, &status, 0 ); 1 && DBG( "%s waitpid status=%d", __func__, status );
alarm(0);
d_exit_status = WEXITSTATUS( status ); 1 && DBG( "%s d_exit_status=%d", __func__, d_exit_status );
}
return d_exit_status;
}
ssize_t piped_forker::Read( void *dest, ssize_t sizeofDest ) {
if( d_fd == INVALID_fd ) {
return 0;
}
const auto rv( read( d_fd, dest, sizeofDest ) ); 0 && DBG( "%s-[%d] %ld", __func__, d_fd, rv );
if( rv <= 0 ) { ReapChild(); }
return rv;
}
bool piped_forker::ForkChildOk( const char *command ) { DBG( "%s+(from %d) '%s'", __func__, getpid(), command );
d_pid = INVALID_ProcessId;
int pipefds[2];
if( pipe( pipefds ) == -1 ) {
perror( "pipe" );
return false;
}
switch( (d_pid=fork()) ) {
case -1: perror( "fork" ); /* fail */
return false;
case 0: {signal( SIGPIPE, SIG_DFL ); /* child */ // FIXME: close other opened descriptor
close( pipefds[PIPE_RD] );
close( 0 );
const int nevdullfh( open("/dev/null", O_RDONLY) ); Assert( nevdullfh == 0 );
dup2( pipefds[PIPE_WR], 1 );
dup2( pipefds[PIPE_WR], 2 );
close( pipefds[PIPE_WR] );
setpgid(0, 0); // http://stackoverflow.com/questions/15692275/how-to-kill-a-process-tree-programmatically-on-linux-using-c
exit( system( command ) );
}return false; // keep compiler happy
default: close( pipefds[PIPE_WR] ); /* parent */
// fcntl( pipefds[PIPE_RD], F_SETFL, O_NONBLOCK );
d_fd = pipefds[PIPE_RD];
DBG( "%s-(from %d) fork parent; child=%d, d_fd=%d; '%s'", __func__, getpid(), d_pid, d_fd, command );
return true;
}
}
STATIC_FXN void prep_cmdline( PCChar pc, int cmdFlags, PCChar func__ ) {
0 && DBG( "%s: %s%s%s: %s"
, func__
, (cmdFlags & NO_ECHO_CMDLN) ? "@" : ""
, (cmdFlags & IGNORE_ERROR ) ? "-" : ""
, (cmdFlags & NOSHELL ) ? "^" : ""
, pc
);
}
STATIC_FXN PChar showTermReason( PChar dest, size_t sizeofDest, const int hProcessExitCode, const int unstartedJobCnt, const int failedJobsIgnored, double et ) {
enum { USER_BREAK = 99999999 }; // bugbug this MADE UP
if( 0 == hProcessExitCode ) {
FmtStr<30> ets( "in %.3f S", et );
if( failedJobsIgnored ) { snprintf( dest, sizeofDest, "--- processing successful, %d job-failure%s ignored %s ---", failedJobsIgnored, Add_s( failedJobsIgnored ), ets.c_str() ); }
else { snprintf( dest, sizeofDest, "--- processing successful %s ---", ets.c_str() ); }
}
else if( USER_BREAK == hProcessExitCode ) {
snprintf( dest, sizeofDest, "--- process TERMINATED with prejudice" );
}
else {
STATIC_CONST char hdr[] = "--- process FAILED, last exit code=0x";
if( unstartedJobCnt ) { snprintf( dest, sizeofDest, "%s%X, %d job%s unstarted ---", hdr, hProcessExitCode , unstartedJobCnt, Add_s( unstartedJobCnt ) ); }
else { snprintf( dest, sizeofDest, "%s%X ---" , hdr, hProcessExitCode ); }
}
return dest;
}
STATIC_FXN void PutLastLogLine( PFBUF d_pfLogBuf, stref s0 ) {
WhileHoldingGlobalVariableLock gvlock; // wait until we own the output resource
CapturePrevLineCountAllWindows( d_pfLogBuf, true );
d_pfLogBuf->PutLastLineRaw( s0 );
MoveCursorToEofAllWindows( d_pfLogBuf, true );
}
class TPipeReader {
piped_forker &d_piper;
ssize_t d_bytesInRawBuffer;
PChar d_pRawBuffer;
char d_rawBuffer[1024];
int RdChar();
enum { EMPTY = -1 };
public:
TPipeReader( piped_forker &piper ) : d_piper( piper ), d_bytesInRawBuffer( 0 ), d_pRawBuffer( d_rawBuffer ) {}
~TPipeReader() { }
bool GetlineEof( std::string &dest ); // printf "hi"
};
int TPipeReader::RdChar() {
if( d_bytesInRawBuffer == 0 ) {
d_bytesInRawBuffer = d_piper.Read( BSOB(d_rawBuffer) );
if( 0 == d_bytesInRawBuffer ) {
return EMPTY;
}
d_pRawBuffer = d_rawBuffer;
}
--d_bytesInRawBuffer;
const char rv( *d_pRawBuffer++ );
return rv;
}
bool TPipeReader::GetlineEof( std::string &dest ) { enum { SD=0 };
dest.clear();
while( 1 ) {
const auto lastCh( RdChar() ); SD && DBG(" %d", lastCh );
const auto atEof( lastCh == EMPTY );
if( !atEof ) {
dest.push_back( lastCh );
}
if( atEof || lastCh == 0x0A ) {
return atEof;
}
}
}
enum CP_PIPED_RC {
CP_PIPED_RC_OK = 0,
CP_PIPED_RC_EPIPE,
CP_PIPED_RC_ECREATEPROCESS,
CP_PIPED_RC_EGETEXITCODE,
};
STATIC_FXN CP_PIPED_RC CreateProcess_piped
( piped_forker &piper
, int *pd_hProcessExitCode
, PFBUF pfLogBuf
, PChar pS
, int cmdFlags
, std::string &sb
) {
if( !(cmdFlags & NO_ECHO_CMDLN) ) {
PutLastLogLine( pfLogBuf, pS );
}
// -ls -l && echo sleep 2 && sleep 2 && echo hello world
// -ls -l
// -ls -l && echo sleep 10 && sleep 10
if( !piper.ForkChildOk( pS ) ) {
char erbuf[265]; OsErrStr( BSOB(erbuf) );
ErrorDialogBeepf( "%s: piper.ForkChildOk '%s' FAILED: %s!!!", __func__, pS, erbuf );
return CP_PIPED_RC_ECREATEPROCESS;
}
TPipeReader pipeReader( piper );
while( 1 ) {
const auto atEof( pipeReader.GetlineEof( sb ) );
if( chomp( sb ) ) {
PutLastLogLine( pfLogBuf, sb );
}
if( atEof ) { // process has been reaped
const auto status( piper.Status() );
if( status && (cmdFlags & IGNORE_ERROR) ) {
PutLastLogLine( pfLogBuf, FmtStr<64>( "- process exit code=%d ignored", status ).c_str() );
}
return CP_PIPED_RC_OK;
}
}
}
int qx( std::string &dest, PCChar system_param ) {
dest.clear();
piped_forker piper;
if( !piper.ForkChildOk( system_param ) ) {
return -1;
}
while( true ) {
char buffer[1024];
const auto bc( piper.Read( BSOB(buffer) ) );
if( bc <= 0 ) {
return piper.Status();
}
dest.append( buffer, bc );
}
}
#include <sys/wait.h>
STATIC_FXN bool popen_rd_ok( std::string &dest, PCChar szcmdline ) {
dest.clear();
auto fp( popen( szcmdline, "r" ) );
if( fp != NULL ) {
char buf[8192];
while( fgets( buf, sizeof buf, fp ) != NULL ) {
dest.append( buf );
}
const auto status( pclose(fp) );
if( status == -1 ) { /* Error reported by pclose() */
}
else {
/* http://pubs.opengroup.org/onlinepubs/009695399/functions/wait.html
Use macros described under wait() to inspect `status' in order
to determine success/failure of command executed by popen()
*/
if( WIFEXITED(status) && 0 == WEXITSTATUS(status) ) {
return true;
}
}
}
return false;
}
STATIC_FXN bool cmd_available( PCChar cmdnm ) {
FmtStr<11+256+1> cmdline( "command -v %s" , cmdnm );
std::string dest;
const auto rv( popen_rd_ok( dest, cmdline.c_str() ) ); DBG( "%s: '%s' -> '%s'", __func__, cmdline.c_str(), dest.c_str() );
return rv;
}
STATIC_FXN bool xclip_read( std::string &dest ) {
STATIC_VAR std::string cli_fromxclip;
if( cli_fromxclip.empty() ) {
std::string emsg, em_xclip, em_iconv;
const auto xci( cmd_available( "xclip" ) );
const auto ici( cmd_available( "iconv" ) );
if( !xci ) {
dest = "xclip not in PATH; apt-get install xclip needed?";
return false; // fatal error
}
cli_fromxclip.append( "xclip -selection c -o" );
if( ici ) { // I believe iconv is a CORE package, so this is not expected. But check anyway.
cli_fromxclip.append( " | iconv -f UTF8 -t US-ASCII//TRANSLIT" );
}
}
if( popen_rd_ok( dest, cli_fromxclip.c_str() ) ) {
return true;
}
dest = "X clipboard read (xclip -selection c -o) failed?";
return false;
}
bool ARG::fromwinclip() {
std::string dest;
if( xclip_read( dest ) ) {
Clipboard_PutText_Multiline( dest );
Msg( "X clipboard -> <clipboard> ok" );
return true;
}
Msg( "%s", dest.c_str() );
return false;
}
void WinClipGetFirstLine( std::string &dest ) {
if( xclip_read( dest ) ) {
const auto eol( StrToNextOrEos( dest.c_str(), "\n" ) );
dest.resize( eol - dest.c_str() );
}
}
STATIC_FXN bool popen_wr_ok( PCChar szcmdline, stref sr ) {
auto fp( popen( szcmdline, "w" ) );
if( fp != NULL ) {
fwrite( sr.data(), sr.length(), 1, fp ); // ignore error here as it will show up when we pclose()
const auto status( pclose(fp) );
if( status == -1 ) { /* Error reported by pclose() */
}
else {
/* http://pubs.opengroup.org/onlinepubs/009695399/functions/wait.html
Use macros described under wait() to inspect `status' in order
to determine success/failure of command executed by popen()
*/
if( WIFEXITED(status) && 0 == WEXITSTATUS(status) ) {
return true;
}
}
}
return false;
}
#ifdef fn_toxclip
// NB! following are referenced by impl_towinclip.h
//
STATIC_CONST char cli_toxclip [] = "xclip -selection c";
typedef PChar hglbCopy_t;
STATIC_FXN bool ToWinClipMetaSingleLineXlat( std::string &stbuf ) {
return true;
}
STATIC_FXN PChar PrepClip( long size, hglbCopy_t &hglbCopy ) {
hglbCopy = static_cast<PChar>( malloc( size ) );
return hglbCopy;
}
STATIC_FXN PCChar WrToClipEMsg( hglbCopy_t hglbCopy ) {
const auto wrOk( popen_wr_ok( cli_toxclip, hglbCopy ) );
free( hglbCopy );
return wrOk ? nullptr : "xclip write failed" ;
}
STATIC_FXN size_t sizeof_eol() { return 1; }
STATIC_FXN void cat_eol( PChar &bufptr ) {
*bufptr++ = '\n';
}
STATIC_FXN PCChar DestNm() { return "X"; }
#include "impl_towinclip.h"
#endif
STATIC_FXN int system_detached( PCChar pFullCmdLn ) {
std::string cli( "</dev/null >/dev/null 2>/dev/null " );
cli.append( pFullCmdLn );
cli.append( " &" );
return system( cli.c_str() );
}
void StartShellExecuteProcess( PCChar pFullCmdLn, PCChar pExeFile ) {
std::string cli( "xdg-open '" );
cli.append( pFullCmdLn );
cli.append( "'" );
// int lunacy __attribute__((unused));
// lunacy = system( cli.c_str() );
system_detached( cli.c_str() );
}
//#################################################################################################################################
//#################################################################################################################################
//#################################################################################################################################
class InternalShellJobExecutor {
NO_COPYCTOR(InternalShellJobExecutor);
NO_ASGN_OPR(InternalShellJobExecutor);
STATIC_FXN void ChildProcessCtrlThread( InternalShellJobExecutor *pIsjx );
PFBUF d_pfLogBuf;
StringList *d_pSL;
const size_t d_numJobsRequested;
int d_Pid;
int d_ChildProcessExitCode;
std::mutex d_jobQueueMtx;
// Win32::ManualClrEvent d_AllJobsDone; BUGBUG
std::thread d_hThread; // should be LAST!!!
public:
InternalShellJobExecutor( PFBUF pfb, StringList *sl, bool fViewsActivelyTailOutput );
~InternalShellJobExecutor();
void GetJobStatus( size_t *pNumRequested, size_t *pNumNotStarted ) const {
*pNumRequested = d_numJobsRequested;
*pNumNotStarted = d_pSL->length();
}
private:
void ThreadFxnRunAllJobs();
int KillAllJobsInBkgndProcessQueue();
int DeleteAllEnqueuedJobs_locks();
};
InternalShellJobExecutor::InternalShellJobExecutor( PFBUF pfb, StringList *sl, bool fViewsActivelyTailOutput )
: d_pfLogBuf ( pfb )
, d_pSL ( sl )
, d_numJobsRequested ( sl->length() )
, d_Pid ( INVALID_ProcessId )
, d_ChildProcessExitCode ( 0 )
, d_hThread ( InternalShellJobExecutor::ChildProcessCtrlThread, this )
{
}
InternalShellJobExecutor::~InternalShellJobExecutor() {
KillAllJobsInBkgndProcessQueue();
Delete0( d_pSL );
}
void InternalShellJobExecutor::ThreadFxnRunAllJobs() { // RUNS ON ONE OR MORE TRANSIENT THREADS!!!
if( g_fMsgflush ) {
WhileHoldingGlobalVariableLock gvlock; // wait until we own the I/O
d_pfLogBuf->MakeEmpty();
}
auto failedJobsIgnored(0);
auto unstartedJobCnt(0);
PerfCounter pc;
std::string x2;
// Win32::AutoSignalEvent amce( d_AllJobsDone );
while( true ) { //**************** outerthreadloop ****************
StringListEl *pEl;
{
// std::scoped_lock LockTheJobQueue( d_jobQueueMtx ); // ##################### LockTheJobQueue ######################
d_Pid = INVALID_ProcessId;
DispNeedsRedrawStatLn(); // ???
if( !(pEl=d_pSL->remove_first()) ) { // ONLY EXIT FROM THREAD IS HERE!!!
linebuf buf;
PutLastLogLine( d_pfLogBuf, showTermReason( BSOB(buf), d_ChildProcessExitCode, unstartedJobCnt, failedJobsIgnored, pc.Capture() ) );
d_ChildProcessExitCode = 0;
// d_hThread = nullptr;
return; // ##################### LockTheJobQueue ######################
}
} // ##################### LockTheJobQueue ######################
auto cmdFlags(0);
auto pS( pEl->string + xlat_cmdline_flag_chars( pEl->string, &cmdFlags ) );
if( *pS ) {
prep_cmdline( pS, cmdFlags, __func__ );
piped_forker piper;
const auto cp_rc( CreateProcess_piped( piper, &d_ChildProcessExitCode, d_pfLogBuf, pS, cmdFlags, x2 ) );
if( CP_PIPED_RC_OK == cp_rc ) {
if( d_ChildProcessExitCode ) {
if( cmdFlags & IGNORE_ERROR ) { ++failedJobsIgnored; }
else { unstartedJobCnt = DeleteAllEnqueuedJobs_locks(); }
}
}
}
FreeStringListEl( pEl );
} //**************** outerthreadloop ****************
ConOut::Bell();
}
STATIC_CONST auto cpct_start_fmts = "%s::CPCT vvvvvvvvvvvvvvvvvv THREAD STARTS vvvvvvvvvvvvvvvvvv";
STATIC_CONST auto cpct_exit_fmts = "%s::CPCT ^^^^^^^^^^^^^^^^^^ THREAD EXITS ^^^^^^^^^^^^^^^^^^";
void InternalShellJobExecutor::ChildProcessCtrlThread( InternalShellJobExecutor *pIsjx ) {
0 && DBG( cpct_start_fmts, "ISJE" );
pIsjx->ThreadFxnRunAllJobs();
0 && DBG( cpct_exit_fmts , "ISJE" );
// equivalent to ExitThread( 0 );
}
bool StartInternalShellJob( StringList *sl, bool fAppend, PFBUF pFB ) {
if( pFB && !pFB->d_pInternalShellJobExecutor ) {
pFB->d_pInternalShellJobExecutor = new InternalShellJobExecutor( pFB, sl, true );
return true;
}
return false;
}
PFBUF StartInternalShellJob( StringList *sl, bool fAppend ) {
STATIC_VAR size_t s_nxt_shelljob_output_FBUF_num;
if( !fAppend ) {
NEXT_OUTBUF:
++s_nxt_shelljob_output_FBUF_num;
}
char fnm[30];
auto pFB( FBOP::FindOrAddFBuf( safeSprintf( BSOB(fnm), "<shell_output-%03" PR_SIZET ">", s_nxt_shelljob_output_FBUF_num ) ) );
if( pFB ) {
if( pFB->d_pInternalShellJobExecutor ) { goto NEXT_OUTBUF; } // don't want to append to an FBUF currently in use by a d_pInternalShellJobExecutor
pFB->PutFocusOn();
pFB->d_pInternalShellJobExecutor = new InternalShellJobExecutor( pFB, sl, true );
}
return pFB;
}
int InternalShellJobExecutor::DeleteAllEnqueuedJobs_locks() {
// std::scoped_lock LockTheJobQueue( d_jobQueueMtx );
auto &d_jobQHead = d_pSL->d_head;
const auto rmCnt( d_jobQHead.length() );
while( auto pEl = d_jobQHead.front() ) {
DLINK_REMOVE_FIRST( d_jobQHead, pEl, dlink );
Delete0( pEl );
}
return rmCnt;
}
int InternalShellJobExecutor::KillAllJobsInBkgndProcessQueue() {
DeleteAllEnqueuedJobs_locks();
if( INVALID_ProcessId != d_Pid
&& ConIO::Confirm( Sprintf2xBuf( "Kill background %s process (PID=%d)?", d_pfLogBuf->Name(), d_Pid ) )
&& INVALID_ProcessId != d_Pid
) {
kill( -d_Pid, SIGTERM );
sleep( 2 );
kill( -d_Pid, SIGKILL );
Msg( "killed pid=%d", d_Pid );
d_Pid = INVALID_ProcessId;
return 1;
}
return 1; // !IsThreadActive();
}
bool ARG::compile() {
switch( d_argType ) {
default: return BadArg();
case NOARG: {const auto fCompiling( IsCompileJobQueueThreadActive() );
Msg( "%scompile in progress", fCompiling ? "" : "no " );
return fCompiling;
}
case NULLARG: CompilePty_KillAllJobs();
return false;
case TEXTARG:{g_CurFBuf()->SyncWriteIfDirty_Wrote();
DispDoPendingRefreshesIfNotInMacro();
const auto cmdCnt( CompilePty_CmdsAsyncExec( StringList( d_textarg.pText ), true ) );
Msg( "Queued %d commands", cmdCnt );
return cmdCnt > 0;
}
}
}
STATIC_VAR bool g_fSystemShutdownOrLogoffRequested = false;
STATIC_FXN void IdleThread() {
1 && DBG( "*** %s STARTING***", __func__ );
while( true ) {
std::this_thread::sleep_for( std::chrono::milliseconds(100) ); // was 50 @ 20130101
WhileHoldingGlobalVariableLock gvlock;
if( g_fSystemShutdownOrLogoffRequested && EditorFilesystemNoneDirty() ) { // silently exit if no harm would be done
EditorExit( 0, true );
}
// DispDoPendingRefreshes() is here PRIMARILY for aux-thread related screen
// updates (when the screen changes NOT as a result of user edit activity,
// but from piped in output from a child process): when this is commented
// out, NORMAL edit-driven screen-redraw seems unaffected.
//
// UPDT: re-confirmed by accident! I accidentally turned off the main
// thread's call (CmdFromKbdForExec()'s) to
// GiveUpGlobalVariableLock(), and the only way I
// noticed is because child compile processes which wrote to
// <compile> ("alt+m,u,t" is simplest example) had no screen
// updates. Otherwise, there was ZERO noticable difference.
//
DispDoPendingRefreshes();
LuaIdleGC();
}
}
void DetachIdleThread() {
// IdleThread is quasi-related to JobQueues: it updates <compile> window when
// writer is a spawned process...
//
std::thread idle( IdleThread ); idle.detach();
}
const auto mainThreadId( std::this_thread::get_id() );
STIL void ASSERT_MAIN_THREAD() { Assert(mainThreadId == std::this_thread::get_id()); }
STIL void ASSERT_NOT_MAIN_THREAD() { Assert(mainThreadId != std::this_thread::get_id()); }
STATIC_VAR std::mutex s_GlobalVariableLock;
STIL void GiveUpGlobalVariableLock() { /* DBG( "rls baton" ); */ s_GlobalVariableLock.unlock(); }
STIL void WaitForGlobalVariableLock() { s_GlobalVariableLock.lock(); /* DBG( "got baton" ); */ }
WhileHoldingGlobalVariableLock::WhileHoldingGlobalVariableLock() { WaitForGlobalVariableLock(); }
WhileHoldingGlobalVariableLock::~WhileHoldingGlobalVariableLock() { GiveUpGlobalVariableLock(); }
void MainThreadGiveUpGlobalVariableLock() { ASSERT_MAIN_THREAD(); /* MainThreadPerfCounter::PauseAll() ; */ GiveUpGlobalVariableLock() ; }
void MainThreadWaitForGlobalVariableLock() { ASSERT_MAIN_THREAD(); WaitForGlobalVariableLock() ; /* MainThreadPerfCounter::ResumeAll() ; */ }