Skip to content

Instantly share code, notes, and snippets.

@sonots
Created January 4, 2018 17:11
Show Gist options
  • Save sonots/f374647a22aed850062a7bf20d1382d9 to your computer and use it in GitHub Desktop.
Save sonots/f374647a22aed850062a7bf20d1382d9 to your computer and use it in GitHub Desktop.

rb_thread_call_without_gvl を使う

例えば普通に書くと

nvrtcResult status = nvrtcCreateProgram(&_prog, _src, _name, _numHeaders, _headers, _includeNames);

となる C 関数の呼び出しを、GVLを外して呼び出すには、

struct nvrtcCreateProgramParam {
    nvrtcProgram *prog;
    const char* src;
    const char *name;
    int numHeaders;
    const char** headers;
    const char** includeNames;
};

static void*
nvrtcCreateProgram_without_gvl_cb(void *param)
{
    struct nvrtcCreateProgramParam *p = param;
    nvrtcResult status;
    status = nvrtcCreateProgram(p->prog, p->src, p->name, p->numHeaders, p->headers, p->includeNames);
    return (void *)status;
}

struct nvrtcCreateProgramParam param = {&_prog, _src, _name, _numHeaders, _headers, _includeNames};
status = (nvrtcResult)rb_thread_call_without_gvl(nvrtcCreateProgram_without_gvl_cb, &param, NULL, NULL);

こんなかんじになる。struct と callback 関数を作る必要があるので非常にめんどい。

@sonots
Copy link
Author

sonots commented Apr 19, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment