{"action":"created","issue":{"url":"https://api.github.com/repos/robovm/robovm/issues/651","labels_url":"https://api.github.com/repos/robovm/robovm/issues/651/labels{/name}","comments_url":"https://api.github.com/repos/robovm/robovm/issues/651/comments","events_url":"https://api.github.com/repos/robovm/robovm/issues/651/events","html_url":"https://github.com/robovm/robovm/issues/651","id":52368844,"number":651,"title":"Refactor attaching/detaching of native threads","user":{"login":"ntherning","id":135765,"avatar_url":"https://avatars.githubusercontent.com/u/135765?v=3","gravatar_id":"","url":"https://api.github.com/users/ntherning","html_url":"https://github.com/ntherning","followers_url":"https://api.github.com/users/ntherning/followers","following_url":"https://api.github.com/users/ntherning/following{/other_user}","gists_url":"https://api.github.com/users/ntherning/gists{/gist_id}","starred_url":"https://api.github.com/users/ntherning/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ntherning/subscriptions","organizations_url":"https://api.github.com/users/ntherning/orgs","repos_url":"https://api.github.com/users/ntherning/repos","events_url":"https://api.github.com/users/ntherning/events{/privacy}","received_events_url":"https://api.github.com/users/ntherning/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/robovm/robovm/labels/enhancement","name":"enhancement","color":"84b6eb"}],"state":"open","locked":false,"assignee":null,"milestone":{"url":"https://api.github.com/repos/robovm/robovm/milestones/5","labels_url":"https://api.github.com/repos/robovm/robovm/milestones/5/labels","id":912076,"number":5,"title":"1.0","description":"","creator":{"login":"ntherning","id":135765,"avatar_url":"https://avatars.githubusercontent.com/u/135765?v=3","gravatar_id":"","url":"https://api.github.com/users/ntherning","html_url":"https://github.com/ntherning","followers_url":"https://api.github.com/users/ntherning/followers","following_url":"https://api.github.com/users/ntherning/following{/other_user}","gists_url":"https://api.github.com/users/ntherning/gists{/gist_id}","starred_url":"https://api.github.com/users/ntherning/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ntherning/subscriptions","organizations_url":"https://api.github.com/users/ntherning/orgs","repos_url":"https://api.github.com/users/ntherning/repos","events_url":"https://api.github.com/users/ntherning/events{/privacy}","received_events_url":"https://api.github.com/users/ntherning/received_events","type":"User","site_admin":false},"open_issues":69,"closed_issues":17,"state":"open","created_at":"2014-12-22T10:16:06Z","updated_at":"2015-01-05T08:14:12Z","due_on":"2015-01-31T23:00:00Z","closed_at":null},"comments":3,"created_at":"2014-12-18T14:24:22Z","updated_at":"2015-01-05T14:36:57Z","closed_at":null,"body":"When using APIs like `CoreMotion` a single thread gets attached/detached a lot. On every attach we need to do a lot of work, e.g. allocating Env, Thread and JavaThread structs and calling a constructor. Maybe we can be smarter? One idea is to let detached threads live on for a short period of time before we discard them so that we can reuse the same Java Thread object and Env should the same thread be attached within that time period. We need a way to detect a Thread which is detached, how long it's been detached and we need a reaper thread which kills threads which have been detached for a certain amount of time."},"comment":{"url":"https://api.github.com/repos/robovm/robovm/issues/comments/68715330","html_url":"https://github.com/robovm/robovm/issues/651#issuecomment-68715330","issue_url":"https://api.github.com/repos/robovm/robovm/issues/651","id":68715330,"user":{"login":"ntherning","id":135765,"avatar_url":"https://avatars.githubusercontent.com/u/135765?v=3","gravatar_id":"","url":"https://api.github.com/users/ntherning","html_url":"https://github.com/ntherning","followers_url":"https://api.github.com/users/ntherning/followers","following_url":"https://api.github.com/users/ntherning/following{/other_user}","gists_url":"https://api.github.com/users/ntherning/gists{/gist_id}","starred_url":"https://api.github.com/users/ntherning/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ntherning/subscriptions","organizations_url":"https://api.github.com/users/ntherning/orgs","repos_url":"https://api.github.com/users/ntherning/repos","events_url":"https://api.github.com/users/ntherning/events{/privacy}","received_events_url":"https://api.github.com/users/ntherning/received_events","type":"User","site_admin":false},"created_at":"2015-01-05T14:36:57Z","updated_at":"2015-01-05T14:36:57Z","body":"After giving this even more thought I don't think it's a good idea to add a flag to `@Callback`, `@Block`, etc annotations. That puts the control into the hands of the binding developer but if anyone should have any control over this I think it should be the user of the binding.\r\n\r\nI'm leaning towards changing the way `@Callback` methods attach/detach instead. They only attach if not already attached and never detach. We still implement the deconstructor which detaches when the Env TLS is cleared on thread termination. In most cases the thread will already have been attached anyway, e.g. the main thread. So what will happen is that a native thread which calls into a `@Callback` will not be detached until it terminates.\r\n\r\nThis approach can obviously leak some memory since some of our structures (Env, Thread, JavaThread, etc) may hold on to some data until the thread dies but I don't think it's that much actually and the situation where a callback is called only once and then the thread lives on for a long time should be fairly uncommon. The most common case is that the callback is called on the main thread anyway. An app which creates a lot of threads and keep them alive will run into other problems quite quickly. This could be the approach which will lead to the least support."}}