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

lunarmodules / luacheck / 21044623957

15 Jan 2026 08:02PM UTC coverage: 97.014% (-0.03%) from 97.042%
21044623957

push

github

web-flow
Merge da3776733 into 6fc4af915

5 of 7 new or added lines in 1 file covered. (71.43%)

5 existing lines in 4 files now uncovered.

6303 of 6497 relevant lines covered (97.01%)

19269.74 hits per line

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

80.0
/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✔
16
   return multithreading
×
17
end
18

19
local cpu_number_detection_commands = {}
512✔
20

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

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

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

36
      if output then
512✔
37
         local cpu_number = tonumber(utils.strip(output))
512✔
38

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

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

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

56
      if arg == nil then
×
57
         return results
×
58
      end
59

60
      results[key] = func(arg)
×
61
   end
62
end
63

64
local function protected_worker_task(...)
65
   return true, utils.try(worker_task, ...)
×
66
end
67

68
local worker_gen = lanes.gen("*", protected_worker_task)
512✔
69

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

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

79
   local workers = {}
156✔
80
   local linda = lanes.linda()
156✔
81

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

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

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

94
   local results = {}
156✔
95

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

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

112
      if ok then
388✔
113
         utils.update(results, worker_results)
388✔
114
      else
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