Plan 9 from Bell Labs’s /usr/web/sources/contrib/steve/root/sys/src/c++/lib/task/_main.C

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


/*
 * special version for the task library
 * so we can get the proper stack base
 */
#include <stdlib.h>

extern "C" {
	extern void __dtors();
	extern void __main();
}
typedef void (*PFV)();
typedef struct Link Link;
struct Link{
	Link	*next;
	PFV	ctor;
	PFV	dtor;
};

static struct{
	Link	*link;
	long	nuxi;
} __head = { 0, 0xdeadbeef };
extern PFV _ctors[];
extern PFV _dtors[];
void __task__init();

void __main()
{
	__task__init();
	atexit(__dtors);

	if(__head.link){
		Link *last, *it, *next;
		last = 0;
		it = __head.link;
		while(it){
			if(it->ctor)
				(*it->ctor)();
			next = it->next;
			it->next = last;
			last = it;
			it = next;
		}
		__head.link = last;
		return;
	}
	for(PFV* pf=_ctors; *pf; pf++)
		(**pf)();
}

void
__dtors()
{
	static int done;
	if(done)
		return;
	done = 1;	// once only
	if(__head.link){
		Link *it;
		for(it = __head.link; it; it = it->next)
			if(it->dtor)
				(*it->dtor)();
		return;
	}
	PFV* pf = _dtors;
	while(*pf)
		pf++;
	while(_dtors < pf)
		(**--pf)();
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.