+ unsigned int nb = 1;
+
+#if defined(PTW32_VERSION)
+ nb = pthread_num_processors_np();
+#elif defined(__APPLE__) || defined(__FreeBSD__)
+ int count;
+ size_t size = sizeof(count);
+
+ nb = sysctlbyname("hw.ncpu", &count, &size, NULL, 0) ? 1 : count;
+#elif defined(_SC_NPROCESSORS_ONLN)
+ nb = sysconf(_SC_NPROCESSORS_ONLN);
+#endif
+
+ return nb < 1 ? 1 : nb;
+}
+
+static struct block_rec * lightrec_get_best_elm(struct slist_elm *head)
+{
+ struct block_rec *block_rec, *best = NULL;
+ struct slist_elm *elm;
+
+ for (elm = slist_first(head); elm; elm = elm->next) {
+ block_rec = container_of(elm, struct block_rec, slist);
+
+ if (!block_rec->compiling
+ && (!best || block_rec->requests > best->requests))
+ best = block_rec;
+ }
+
+ return best;
+}
+
+static bool lightrec_cancel_block_rec(struct recompiler *rec,
+ struct block_rec *block_rec)
+{
+ if (block_rec->compiling) {
+ /* Block is being recompiled - wait for
+ * completion */
+ pthread_cond_wait(&rec->cond2, &rec->mutex);
+
+ /* We can't guarantee the signal was for us.
+ * Since block_rec may have been removed while
+ * we were waiting on the condition, we cannot
+ * check block_rec->compiling again. The best
+ * thing is just to restart the function. */
+ return false;