+static unsigned int get_processors_count(void)
+{
+ 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 slist_elm * lightrec_get_first_elm(struct slist_elm *head)
+{
+ struct block_rec *block_rec;
+ 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)
+ return elm;
+ }
+
+ return NULL;
+}
+
+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;
+ }
+
+ /* Block is not yet being processed - remove it from the list */
+ slist_remove(&rec->slist, &block_rec->slist);
+ lightrec_free(rec->state, MEM_FOR_LIGHTREC,
+ sizeof(*block_rec), block_rec);
+
+ return true;
+}
+
+static void lightrec_cancel_list(struct recompiler *rec)
+{
+ struct block_rec *block_rec;
+ struct slist_elm *next;
+
+ while (!!(next = lightrec_get_first_elm(&rec->slist))) {
+ block_rec = container_of(next, struct block_rec, slist);
+
+ lightrec_cancel_block_rec(rec, block_rec);
+ }
+
+ pthread_cond_broadcast(&rec->cond2);
+}
+
+static void lightrec_flush_code_buffer(struct lightrec_state *state, void *d)
+{
+ struct recompiler *rec = d;
+
+ pthread_mutex_lock(&rec->mutex);
+
+ if (rec->must_flush) {
+ lightrec_remove_outdated_blocks(state->block_cache, NULL);
+ rec->must_flush = false;
+ }
+
+ pthread_mutex_unlock(&rec->mutex);
+}
+
+static void lightrec_compile_list(struct recompiler *rec,
+ struct recompiler_thd *thd)