fix rare bug

This commit is contained in:
red-prig 2022-09-05 16:09:58 +03:00
parent bf89edfd43
commit 0959c31248
3 changed files with 36 additions and 18 deletions

View File

@ -13,6 +13,8 @@ uses
LFQueue,
bittype,
sys_crt,
sys_types,
sys_kernel,
ps4_libSceVideoOut,
@ -398,6 +400,9 @@ var
work_do:Boolean;
begin
Result:=0;
sys_crt_init;
repeat
work_do:=False;

View File

@ -6,6 +6,7 @@ interface
uses
windows,
sys_pthread,
spinlock;
Procedure sys_crt_init;
@ -17,6 +18,10 @@ uses
var
StdOutLock:Pointer=nil;
StdOutColor:Word;
const
StdErrColor=FOREGROUND_RED;
function GetConsoleTextAttribute(hConsoleOutput:HANDLE;var wAttributes:WORD):WINBOOL;
var
@ -49,22 +54,18 @@ Begin
end;
Procedure CrtErrWrite(var t:TextRec);
const
new=FOREGROUND_RED;
var
n:DWORD;
old:WORD;
Begin
if (t.BufPos=0) then exit;
n:=0;
old:=t._private;
_sig_lock(SL_NOINTRRUP);
spin_lock(StdOutLock);
SetConsoleTextAttribute(t.Handle,new);
SetConsoleTextAttribute(t.Handle,StdErrColor);
WriteConsole(t.Handle,t.Bufptr,t.BufPos,@n,nil);
SetConsoleTextAttribute(t.Handle,old);
SetConsoleTextAttribute(t.Handle,StdOutColor);
spin_unlock(StdOutLock);
_sig_unlock(SL_NOINTRRUP);
@ -87,17 +88,11 @@ Begin
end;
Procedure CrtOpenErr(Var F:TextRec);
var
old:WORD;
Begin
TextRec(F).Handle:=GetStdHandle(STD_ERROR_HANDLE);
TextRec(F).InOutFunc:=@CrtErrWrite;
TextRec(F).FlushFunc:=@CrtErrWrite;
TextRec(F).CloseFunc:=@CrtClose;
old:=7;
GetConsoleTextAttribute(TextRec(F).Handle,old);
TextRec(F)._private:=old;
end;
procedure AssignCrt(var F:Text;cb:codepointer);
@ -108,6 +103,8 @@ end;
Procedure sys_crt_init;
begin
tcb_thread:=nil; //need zero tcb
AssignCrt(Output,@CrtOpenOut);
Rewrite(Output);
@ -121,5 +118,9 @@ begin
Rewrite(StdErr);
end;
initialization
StdOutColor:=7;
GetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),StdOutColor);
end.

View File

@ -73,7 +73,9 @@ function __sigprocmask(how:Integer;_set,oldset:p_sigset_t):Integer;
function __sigaction(signum:Integer;act,oldact:p_sigaction_t):Integer;
procedure _sig_lock(flags:integer=0);
procedure __sig_lock(pt:Pointer;flags:integer=0);
procedure _sig_unlock(flags:integer=0);
procedure __sig_unlock(pt:Pointer;flags:integer=0);
function _pthread_kill(t:Pointer;sig:Integer):Integer;
@ -319,7 +321,7 @@ function __sigprocmask(how:Integer;_set,oldset:p_sigset_t):Integer;
var
t:pthread;
begin
t:=_get_curthread;
t:=tcb_thread;
if (t=nil) then Exit(EINVAL);
if (_set=nil) then
@ -426,6 +428,11 @@ const
function __sig_self_interrupt(t:pthread):Integer; forward;
procedure _sig_lock(flags:integer=0);
begin
__sig_lock(tcb_thread,flags);
end;
procedure __sig_lock(pt:Pointer;flags:integer=0);
label
tryagain;
var
@ -433,8 +440,8 @@ var
i:Integer;
pc:QWORD;
begin
t:=_get_curthread;
if (t=nil) then Exit;
if (pt=nil) then Exit;
t:=pt;
if ((flags and SL_ALERTABLE)<>0) then
begin
@ -471,6 +478,11 @@ begin
end;
procedure _sig_unlock(flags:integer=0);
begin
__sig_unlock(tcb_thread,flags);
end;
procedure __sig_unlock(pt:Pointer;flags:integer=0);
label
tryagain;
var
@ -479,8 +491,8 @@ var
Alertable:Boolean;
pc:QWORD;
begin
t:=_get_curthread;
if (t=nil) then Exit;
if (pt=nil) then Exit;
t:=pt;
i:=load_acq_rel(t^.sig._lock);
Alertable:=((t^.sig._flag and ALERTABLE_FLAG)<>0);
@ -908,7 +920,7 @@ begin
Result:=sigqueue_add(@pthread(t)^.sig,@sinfo);
if (Result<>0) then Exit;
if (t=_get_curthread) then
if (t=tcb_thread) then
begin
_sig_lock;
Result:=__sig_self_interrupt(t);