Array<String> Matches = {};
char          Prompt[256];
std::mutex    SearchThreadArrayMutex;
int64_t       SearchThreadStopSearching;

WORK_FUNCTION(SearchForMatchesWork) {
    if (Prompt[0] == 0) return;

    int64_t search_thread_stop_searching = SearchThreadStopSearching;

    SearchThreadArrayMutex.lock();
    Matches.clear();
    SearchThreadArrayMutex.unlock();

    String  buffer = XGetArenaString();
    String  find   = Prompt;
    int64_t index  = 0;
    while (Seek(buffer, find, &index, SeekFlag_IgnoreCase)) {
        String found = {buffer.data + index, find.len};
        if (search_thread_stop_searching != SearchThreadStopSearching) break;
        Matches.bounded_add(found);
        buffer = buffer.skip(index + find.len);
    }
}

void StartSearchingForMatches() {
    SearchThreadStopSearching += 1;
    PushWork(&MainWorkQueue, NULL, SearchForMatchesWork);
}

Array<String> LockSearchResults() {
    SearchThreadArrayMutex.lock();
    Array<String> copy = Matches;
    return copy;
}

void UnlockSearchResults() {
    SearchThreadArrayMutex.unlock();
}

void InitSearch() {
    Matches.reserve(100000);
}