Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thread local data and roots #191

Closed
intrigus opened this issue Nov 21, 2017 · 2 comments
Closed

Thread local data and roots #191

intrigus opened this issue Nov 21, 2017 · 2 comments

Comments

@intrigus
Copy link

intrigus commented Nov 21, 2017

Hello,
my code looks like this:

static __thread Env env;

struct Env {
    nongcmember nogc;
    gcmember gc; 	
};

struct gcmember {
	void* operator new(std::size_t sz) {
		return GC_MALLOC(sz);
	}
};

void main(){
env.gc = new gcmember();
// doo stuff
// after some time env.gc seems to get collected 
// and another object gets allocated at the position 
}

I'm initializing the Env struct in my main method.
After some time I allocate the gcmember and store it in env.gc.
Then my application runs for quite some time and env.gc seems to get collected.

Now I have found one solution:

GC_add_roots(&env, &env + 1);

this works well, but I'd like to remove this root after the thread exited and the thread local variables are cleaned up.
So I looked at GC_CALL GC_remove_roots but in the docs it says:
"May be unimplemented on some platforms."

/* May be unimplemented on some platforms. */

Is there an easier way to use thread locals as roots?

@ivmai
Copy link
Owner

ivmai commented Nov 21, 2017

Thread-local objects are not scanned.
Add env thread-local values to a global collection (e.g. hashtable or double-linked list).
Like:

static __thread Env env;
static Env *envList; // points to the first env object (of the double-linked list) or nullptr

struct Env {
nongcmember nogc;
gcmember gc;
Env *prev; // points to previous and next env objects
Env *next;
};

PS. It's better to ask such questions on Stackoverflow - https://stackoverflow.com/questions/tagged/boehm-gc

@ivmai ivmai closed this as completed Nov 21, 2017
@ivmai
Copy link
Owner

ivmai commented Nov 22, 2017

Or, a better way, I think, is to use GC_MALLOC_UNCOLLECTABLE:

struct Env {
nongcmember nogc;
gcmember gc;
};
static __thread Env *penv;
...
// init thread-local env:
penv = GC_NEW_UNCOLLECTABLE(Env);
...
// delete thread-local env:
GC_FREE(penv);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants