1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-03-09 15:50:02 +00:00

rm redundant getpid(2) syscalls (re: 9de65210)

Now that we have ${.sh.pid} a.k.a. shgd->current_pid, which is
updated using getpid() whenever forking a new process, there is no
need for anything else to ever call getpid(); we can use the stored
value instead. There were a lot of these syscalls kicking around,
some of them in performance-sensitive places.

The following lists only changes *other* than changing getpid() to
shgd->currentpid.

src/cmd/ksh93/include/defs.h:
- Comments: clarify what shgd->{pid,ppid,current_pid} are for.

src/cmd/ksh93/sh/main.c,
src/cmd/ksh93/sh/init.c:
- On reinit for a new script, update shgd->{pid,ppid,current_pid}
  in the sh_reinit() function itself instead of calling sh_reinit()
  from sh_main() and then updating those immediately after that
  call. It just makes more sense this way. Nothing else ever calls
  sh_reinit() so there are no side effects.

src/cmd/ksh93/sh/xec.c: _sh_fork():
- Update shgd->current_pid in the child early, so that the rest of
  the function can use it instead of calling getpid() again.
- Remove reassignment of SH_PIDNOD->nvalue.lp value pointer to
  shgd->current_pid (which makes ${.sh.pid} work in the shell).
  It's constant and was already set on init.
This commit is contained in:
Martijn Dekker 2020-09-23 04:19:02 +02:00
parent ce68e1be37
commit 843b546c1a
13 changed files with 43 additions and 44 deletions

View file

@ -1636,7 +1636,7 @@ int sh_exec(register const Shnode_t *t, int flags)
{
shp->trapnote &= ~SH_SIGIGNORE;
if(shp->exitval == (SH_EXITSIG|SIGINT))
kill(getpid(),SIGINT);
kill(shgd->current_pid,SIGINT);
}
}
if(type&FAMP)
@ -2887,8 +2887,9 @@ pid_t _sh_fork(Shell_t *shp,register pid_t parent,int flags,int *jobid)
#if !_std_malloc
vmtrace(-1);
#endif
shp->outpipepid = ((flags&FPOU)?getpid():0);
/* This is the child process */
shgd->current_pid = getpid(); /* ${.sh.pid} */
shp->outpipepid = ((flags&FPOU)?shgd->current_pid:0);
if(shp->trapnote&SH_SIGTERM)
sh_exit(SH_EXITSIG|SIGTERM);
shp->gd->nforks=0;
@ -2898,7 +2899,7 @@ pid_t _sh_fork(Shell_t *shp,register pid_t parent,int flags,int *jobid)
sh_offstate(SH_MONITOR);
if(sh_isstate(SH_MONITOR))
{
parent = getpid();
parent = shgd->current_pid;
if(postid==0)
job.curpgid = parent;
while(setpgid(0,job.curpgid)<0 && job.curpgid!=parent)
@ -2925,9 +2926,6 @@ pid_t _sh_fork(Shell_t *shp,register pid_t parent,int flags,int *jobid)
sh_onstate(SH_NOLOG);
if (shp->fn_reset)
shp->fn_depth = shp->fn_reset = 0;
/* Set ${.sh.pid} to the child process ID */
shp->gd->current_pid = getpid();
SH_PIDNOD->nvalue.lp = &shp->gd->current_pid;
#if SHOPT_ACCT
sh_accsusp();
#endif /* SHOPT_ACCT */
@ -2944,7 +2942,7 @@ pid_t _sh_fork(Shell_t *shp,register pid_t parent,int flags,int *jobid)
sig = shp->savesig;
shp->savesig = 0;
if(sig>0)
kill(getpid(),sig);
kill(shgd->current_pid,sig);
sh_sigcheck(shp);
usepipe=0;
return(0);
@ -2965,7 +2963,7 @@ pid_t sh_fork(Shell_t *shp,int flags, int *jobid)
sig = shp->savesig;
shp->savesig = 0;
if(sig>0)
kill(getpid(),sig);
kill(shgd->current_pid,sig);
job_fork(parent);
return(parent);
}
@ -3177,7 +3175,7 @@ int sh_funscope(int argn, char *argv[],int(*fun)(void*),void *arg,int execflg)
if(jmpval)
r=shp->exitval;
if(r>SH_EXITSIG && ((r&SH_EXITMASK)==SIGINT || ((r&SH_EXITMASK)==SIGQUIT)))
kill(getpid(),r&SH_EXITMASK);
kill(shgd->current_pid,r&SH_EXITMASK);
if(jmpval > SH_JMPFUN)
{
sh_chktrap(shp);