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_setaffinity: Allow affinity param to have a length less than cpumask_size() #652

Merged
merged 2 commits into from
May 24, 2023

Conversation

squeek502
Copy link
Member

Instead of enforcing that the table length is >= cpumask_size(), any missing CPU numbers get assigned an affinity of false. This makes this API a bit more user-friendly, since users no longer have to initialize a table of length cpumask_size() before setting the particular affinities they actually want.

As an example, before this commit, you'd have to do something like:

local affinity = {}
for i=1,uv.cpumask_size() do
  affinity[i] = false
end
affinity[3] = true
affinity[5] = true

But after this commit, this table:

local affinity = { [3] = true, [5] = true }

will now be accepted and all the missing keys up to uv_cpumask_size() will be treated as false in the thread_setaffinity call.

…pumask_size()

Instead of enforcing that the table length is >= cpumask_size(), any missing CPU numbers get assigned an affinity of `false`. This makes this API a bit more user-friendly, since users no longer have to initialize a table of length `cpumask_size()` before setting the particular  affinities they actually want.

As an example, before this commit, you'd have to do something like:

    local affinity = {}
    for i=1,uv.cpumask_size() do
      affinity[i] = false
    end
    affinity[3] = true
    affinity[5] = true

But after this commit, this table:

    local affinity = { [3] = true, [5] = true }

will now be accepted and all the missing keys up to `uv_cpumask_size()` will be treated as `false` in the `thread_setaffinity` call.
Copy link
Member

@zhaozg zhaozg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just minor comments

@squeek502
Copy link
Member Author

squeek502 commented May 23, 2023

I can't see your comments

@zhaozg
Copy link
Member

zhaozg commented May 23, 2023

Mainly because lua_rawlen of a table(not array) will return 0.

From 415 line

  int min_mask_size = uv_cpumask_size();
  if (min_mask_size < 0) {
    return luv_error(L, min_mask_size);
  }
  int mask_size = lua_rawlen(L, 2);
  if (mask_size < min_mask_size) {
    mask_size = min_mask_size;
  }
  char* cpumask = malloc(mask_size);

can be simplify to

  int mask_size = uv_cpumask_size();
  if (mask_size < 0) {
    return luv_error(L, mask_size);
  }
  char* cpumask = malloc(mask_size);

src/thread.c Show resolved Hide resolved
@zhaozg
Copy link
Member

zhaozg commented May 24, 2023

LGTM.

@squeek502 squeek502 merged commit 16bbffd into luvit:master May 24, 2023
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

Successfully merging this pull request may close these issues.

2 participants