Article View: comp.lang.c++
Article #1015Re: A better experimental::scope_exit
From: Scott Lurndal
Date: Mon, 23 Nov 2026 17:35
Date: Mon, 23 Nov 2026 17:35
135 lines
5738 bytes
5738 bytes
boltar@caprica.universe writes:
>On Mon, 23 Nov 2026 16:37:32 GMT
>scott@slp53.sl.home (Scott Lurndal) gabbled:
>>Bonita Montero <Bonita.Montero@gmail.com> writes:
>>>Am 23.11.2026 um 13:26 schrieb David Brown:
>>>
>>>> template<typename T>
>>>> struct scope_exit {
>>>> T f;
>>>> scope_exit(T f) : f(f) { }
>>>> ~scope_exit() { f(); }
>>>> };
>>>
>>>You can't cascade multiple exits with that. This means if you have
>>>several manipulations on a data structure and you want to revert them
>>>if an exception occurs the destructor revers the changes so far. But
>>>if nothing happens in that sense you simply call .disable() on the
>>>last node in the chain and all dependent nodes are also disabled.
>>>Just as a commit in a databse.
>>
>>KISS. This is from a 1992-ish port of Unix to run as
>>an actor for the Chorus microkernel.
>>
>>//
>>// Destructor: undo what fork has done upon failure.
>>//
>>tForkFailure::~tForkFailure()
>
>Why is fork failure a specific class? Thats a bit wierd.
>
Because it is used as a cleanup task to simplify the
Fork functionality, to wit:
/
// int tProc::Fork(KnCap* PActUI, int Caller, tLwp *CrLwp,
// int IsVFork, boolean_t ForkAll)
// Perform all the fork time operations for the invoking object.
//
// Calling/Exit State:
// On entry, the LWPs in the parent process other than the forking LWP
// are at a barrier. Further, the process reader/writer lock of the
// parent process has been acquired (in the read mode) to keep /proc
// at bay. The invoking object is fully initialized from the parent
// process. All the embedded synchronization objects have been initialized.
//
// Remarks:
// The only asynchronous activity in the parent is the result of
// message handlers executing in the parent.
int
tProc::Fork(KnCap* PActUI, int Caller, tLwp *CrLwp, int IsVFork, boolean_t ForkAll)
{
int Error = 0;
int nlwp; // total number of active lwps in the child
int total_lwp; // total number of lwps in the child (active + zombies)
ASSERT((Caller == FORK_SYSCALL) || (Caller == FORK_BOOTPROC));
KnCap *ChildCap = Actor.GetCap();
// Hand crafted processes do not have a ParentP.
tProc *ParentP = CrLwp->MyProc();
// Initialise the global Ops event that is used for global Ops
// synchronisation
EVENT_INIT(&GlobalEvt);
//
// Only Single threaded processes can vfork().
// Currently, we don't support vfork.
if (IsVFork && (!(ParentP->LwpList.SingleThreaded()))) {
return EINVAL;
}
if (ForkAll) {
nlwp = ParentP->LwpList.GetCountLwp();
total_lwp = ParentP->LwpList.GetTotalLwp();
} else {
nlwp = 1;
total_lwp = 1;
}
//
// Check to see if we will be exceeding the quota.
// XXXKYS: Check the interface for picking up quota tokens.
// Would prefer an interface that accepted the number of
// tokens needed - proc + lwps.
// NOTE: The number of LWP tokens picked up will be equal to the
// total number of LWPs in the child (including zombie LWPs).
if (UidQuota.Fork(ProcCred->GetUid(), ProcCred->GetEuid(), total_lwp)) {
return EAGAIN;
}
>>> After this point, any resources allocated to the newly created
>>> process must be deallocated if the fork() fails (e.g. due to
>>> insufficient resources,
tForkFailure ff(this, CrLwp, ChildCap, nlwp, total_lwp);
//
// Perform fork time operations on the credentials.
// Basically, apply the necessary holds.
// NOTE: The UID state we have inherited corresponds to the
// credential state we have inherited (at the process level).
// We will synch up all the LWPs in the child to the process
// level cred state.
ProcCred->CrHold(nlwp + 1);
//
// Perform the fork time operations on the resource limits.
// Refer to the note under the credential operation above.
ProcRlim->rlHold(nlwp + 1);
...
//
// Write an accounting record
//
ParentP->Acct.Fork(); // Count another child
if (SystemAccounting.isEnabled( ACCT_FORK )) {
EventFork rec( ParentP, this );
rec.Write();
}
return ff.ret = 0; // OK
} // tProc::Fork
Technical Details
Message-ID:
<SP_MS.74146$aM44.48644@fx46.iad>Path:
mbox-mo1.pugleaf.net!srv.newsdeef.eu!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!feeder.usenetexpress.com!tr3.iad1.usenetexpress.com!news-out.netnews.com!netnews.com!s1-4.netnews.com!peer05.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx46.iad.POSTED!not-for-mailReferences:
<11dsk17$8ske$2@raubtier-asyl.eternal-september.org> <11e1bd7$1rqma$1@dont-email.me> <11e1gtl$1u1jq$1@raubtier-asyl.eternal-september.org> <gZZMS.79729$Mef7.47357@fx15.iad> <11e1qla$227b2$1@dont-email.me>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!feeder.usenetexpress.com!tr3.iad1.usenetexpress.com!news-out.netnews.com!netnews.com!s1-4.netnews.com!peer05.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx46.iad.POSTED!not-for-mail X-newsreader: xrn 9.03-beta-14-64bit Sender: scott@dragon.sl.home (Scott Lurndal) From: scott@slp53.sl.home (Scott Lurndal) Reply-To: slp53@pacbell.net Subject: Re: A better experimental::scope_exit Newsgroups: comp.lang.c++ References: <11dsk17$8ske$2@raubtier-asyl.eternal-september.org> <11e1bd7$1rqma$1@dont-email.me> <11e1gtl$1u1jq$1@raubtier-asyl.eternal-september.org> <gZZMS.79729$Mef7.47357@fx15.iad> <11e1qla$227b2$1@dont-email.me> Lines: 135 Message-ID: <SP_MS.74146$aM44.48644@fx46.iad> X-Complaints-To: abuse@usenetserver.com NNTP-Posting-Date: Mon, 23 Nov 2026 17:35:46 UTC Organization: UsenetServer - www.usenetserver.com Date: Mon, 23 Nov 2026 17:35:46 GMT X-Received-Bytes: 5422