+ 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;
+ }
+
+ /* 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 *elm, *head = &rec->slist;
+
+ for (elm = slist_first(head); elm; elm = slist_first(head)) {
+ block_rec = container_of(elm, struct block_rec, slist);
+ lightrec_cancel_block_rec(rec, block_rec);