• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lunarmodules / luacheck / 23272392073

18 Mar 2026 11:37PM UTC coverage: 97.029%. Remained the same
23272392073

push

github

web-flow
Merge 0c417df82 into eea104d82

5 of 6 new or added lines in 1 file covered. (83.33%)

8 existing lines in 1 file now uncovered.

6304 of 6497 relevant lines covered (97.03%)

19846.27 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.36
/src/luacheck/multithreading.lua
1
-- luacheck: push compat
2
local unpack = table.unpack or unpack
512✔
3
-- luacheck: pop
4

5
local utils = require "luacheck.utils"
512✔
6

7
local multithreading = {}
512✔
8

9
local lanes_ok, lanes = pcall(require, "lanes")
512✔
10
lanes_ok = lanes_ok and pcall(lanes.configure)
512✔
11
multithreading.has_lanes = lanes_ok
512✔
12
multithreading.lanes = lanes
512✔
13
multithreading.default_jobs = 1
512✔
14

15
if not lanes_ok then
512✔
UNCOV
16
   return multithreading
×
17
end
18

19
local lanes_major = tonumber(lanes.ABOUT.version:match("^(%d+)"))
512✔
20

21
local cpu_number_detection_commands = {}
512✔
22

23
if utils.is_windows then
512✔
UNCOV
24
   cpu_number_detection_commands[1] = "echo %NUMBER_OF_PROCESSORS%"
×
25
else
26
   cpu_number_detection_commands[1] = "getconf _NPROCESSORS_ONLN 2>&1"
512✔
27
   cpu_number_detection_commands[2] = "sysctl -n hw.ncpu 2>&1"
512✔
28
   cpu_number_detection_commands[3] = "psrinfo -p 2>&1"
512✔
29
end
30

31
for _, command in ipairs(cpu_number_detection_commands) do
512✔
32
   local handler = io.popen(command)
512✔
33

34
   if handler then
512✔
35
      local output = handler:read("*a")
512✔
36
      handler:close()
512✔
37

38
      if output then
512✔
39
         local cpu_number = tonumber(utils.strip(output))
512✔
40

41
         if cpu_number then
512✔
42
            multithreading.default_jobs = math.floor(math.max(cpu_number, 1))
512✔
43
            break
256✔
44
         end
45
      end
46
   end
47
end
48

49
-- Reads pairs {key, arg} from given linda slot until it gets nil as arg.
50
-- Returns table with pairs [key] = func(arg).
51
local function worker_task(linda, input_slot, func)
UNCOV
52
   local results = {}
×
53

54
   while true do
UNCOV
55
      local _, pair = linda:receive(nil, input_slot)
×
UNCOV
56
      local key, arg = pair[1], pair[2]
×
57

58
      if arg == nil then
×
UNCOV
59
         return results
×
60
      end
61

62
      results[key] = func(arg)
×
63
   end
64
end
65

66
local function protected_worker_task(...)
UNCOV
67
   return true, utils.try(worker_task, ...)
×
68
end
69

70
local worker_gen = lanes.gen("*", protected_worker_task)
512✔
71

72
-- Maps func over array, performing at most jobs calls in parallel.
73
function multithreading.pmap(func, array, jobs)
512✔
74
   jobs = jobs or multithreading.default_jobs
468✔
75
   jobs = math.min(jobs, #array)
468✔
76

77
   if jobs < 2 then
468✔
78
      return utils.map(func, array)
312✔
79
   end
80

81
   local workers = {}
156✔
82
   local linda = lanes.linda()
156✔
83

84
   for i = 1, jobs do
544✔
85
      workers[i] = worker_gen(linda, 0, func)
388✔
86
   end
87

88
   for i, item in ipairs(array) do
860✔
89
      linda:send(nil, 0, {i, item})
704✔
90
   end
91

92
   for _ = 1, jobs do
544✔
93
      linda:send(nil, 0, {})
388✔
94
   end
95

96
   local results = {}
156✔
97

98
   for _, worker in ipairs(workers) do
544✔
99
      local join_results = {worker:join()}
388✔
100

101
      -- Manage both new and old lane:join() API formats.
102
      -- See https://github.com/LuaLanes/lanes/commit/bfdc7a92c4e3e99522abb6d90ef2cbb021f36fc8
103
      local ok, worker_results, _
104
      if  lanes_major >= 4 then
388✔
105
         -- New API: {true, _, ok, worker_results}
NEW
106
         _, _, ok, worker_results = unpack(join_results)
×
107
      else
108
         -- Old API: {true, ok, worker_results}
109
         _, ok, worker_results = unpack(join_results)
388✔
110
      end
111

112
      if ok then
388✔
113
         utils.update(results, worker_results)
388✔
114
      else
UNCOV
115
         error(worker_results, 0)
×
116
      end
117
   end
118

119
   return results
156✔
120
end
121

122
return multithreading
512✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc