<commit_before>/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#ifndef LOCKS_H
#define LOCKS_H 1

#include <stdexcept>
#include <iostream>
#include <sstream>

#include "common.hh"
#include "mutex.hh"
#include "syncobject.hh"

class LockHolder {
public:
    /**
     * Acquire the lock in the given mutex.
     */
    LockHolder(Mutex &m) : mutex(m), locked(false) {
        lock();
    }

    /**
     * Release the lock.
     */
    ~LockHolder() {
        unlock();
    }

    void lock() {
        mutex.acquire();
        locked = true;
    }

    void unlock() {
        if (locked) {
            mutex.release();
            locked = false;
        }
    }

private:
    Mutex &mutex;
    bool locked;

    DISALLOW_COPY_AND_ASSIGN(LockHolder);
};

class MultiLockHolder {
public:

    MultiLockHolder(Mutex *m, size_t n) : mutexes(m),
                                          locked(NULL),
                                          n_locks(n) {
        locked = new bool[n];
        lock();
    }

    ~MultiLockHolder() {
        unlock();
        delete[] locked;
    }

    void lock() {
        for (size_t i = 0; i < n_locks; i++) {
            mutexes[i].acquire();
            locked[i] = true;
        }
    }

    void unlock() {
        for (size_t i = 0; i < n_locks; i++) {
            if (locked[i]) {
                mutexes[i].release();
            }
            locked[i] = false;
        }
    }

private:
    Mutex  *mutexes;
    bool   *locked;
    size_t  n_locks;

    DISALLOW_COPY_AND_ASSIGN(MultiLockHolder);
};

#endif /* LOCKS_H */
<commit_msg>Fix up locked assignment in LockHolder.<commit_after>/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#ifndef LOCKS_H
#define LOCKS_H 1

#include <stdexcept>
#include <iostream>
#include <sstream>

#include "common.hh"
#include "mutex.hh"
#include "syncobject.hh"

class LockHolder {
public:
    /**
     * Acquire the lock in the given mutex.
     */
    LockHolder(Mutex &m) : mutex(m), locked(false) {
        lock();
    }

    /**
     * Release the lock.
     */
    ~LockHolder() {
        unlock();
    }

    void lock() {
        mutex.acquire();
        locked = true;
    }

    void unlock() {
        if (locked) {
            locked = false;
            mutex.release();
        }
    }

private:
    Mutex &mutex;
    bool locked;

    DISALLOW_COPY_AND_ASSIGN(LockHolder);
};

class MultiLockHolder {
public:

    MultiLockHolder(Mutex *m, size_t n) : mutexes(m),
                                          locked(NULL),
                                          n_locks(n) {
        locked = new bool[n];
        lock();
    }

    ~MultiLockHolder() {
        unlock();
        delete[] locked;
    }

    void lock() {
        for (size_t i = 0; i < n_locks; i++) {
            mutexes[i].acquire();
            locked[i] = true;
        }
    }

    void unlock() {
        for (size_t i = 0; i < n_locks; i++) {
            if (locked[i]) {
                locked[i] = false;
                mutexes[i].release();
            }
        }
    }

private:
    Mutex  *mutexes;
    bool   *locked;
    size_t  n_locks;

    DISALLOW_COPY_AND_ASSIGN(MultiLockHolder);
};

#endif /* LOCKS_H */

{'fraction_non_alphanumeric': 0.1392988929889299, 'fraction_numerical': 0.003075030750307503, 'mean_word_length': 1.5594020456333595, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3029744', 'n_tokens_mistral': 1114, 'n_tokens_neox': 1066, 'n_words': 241}