| 159 | } |
| 160 | |
| 161 | void Fetch::multiMainThread() |
| 162 | { |
| 163 | int trash; |
| 164 | while (multiThreadInfo) |
| 165 | { |
| 166 | CURLMcode mc; |
| 167 | |
| 168 | __lock_acquire(multiHandleMutex); |
| 169 | mc = curl_multi_perform(multiHandle, &trash); |
| 170 | __lock_release(multiHandleMutex); |
| 171 | if (mc == CURLM_OK) |
| 172 | { |
| 173 | int numFDs; |
| 174 | __lock_acquire(multiHandleMutex); |
| 175 | mc = curl_multi_wait(multiHandle, nullptr, 0, 1, &numFDs); |
| 176 | __lock_release(multiHandleMutex); |
| 177 | if (numFDs == 0) |
| 178 | { |
| 179 | static constexpr timespec sleepTime = {0, 1000000}; |
| 180 | nanosleep(&sleepTime, nullptr); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | __lock_acquire(multiHandleMutex); |
| 185 | auto msg = curl_multi_info_read(multiHandle, &trash); |
| 186 | while (msg != nullptr) |
| 187 | { |
| 188 | // Find the done handle |
| 189 | __lock_acquire(fetchesMutex); |
| 190 | auto it = |
| 191 | std::find_if(fetches.begin(), fetches.end(), [&msg](const MultiFetchRecord& record) |
| 192 | { return record.fetch->curl.get() == msg->easy_handle; }); |
| 193 | // And delete it |
| 194 | if (it != fetches.end()) |
| 195 | { |
| 196 | if (it->onFinish) |
| 197 | { |
| 198 | it->onFinish(msg->data.result, it->fetch); |
| 199 | } |
| 200 | curl_multi_remove_handle(multiHandle, it->fetch->curl.get()); |
| 201 | fetches.erase(it); |
| 202 | } |
| 203 | __lock_release(fetchesMutex); |
| 204 | |
| 205 | msg = curl_multi_info_read(multiHandle, &trash); |
| 206 | } |
| 207 | __lock_release(multiHandleMutex); |
| 208 | |
| 209 | // Terrible things have happened, but I don't know what to do |
| 210 | } |
| 211 | |
| 212 | // multiThreadInfo = true; |
| 213 | } |
| 214 | |
| 215 | Result Fetch::initMulti() |
| 216 | { |