<commit_before>/* This file is part of Strigi Desktop Search
 *
 * Copyright (C) 2006 <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "strigi_thread.h"

// thread safe smart pointer
template <class T>
class TSSPtr {
private:
    struct P {
        StrigiMutex lock;
        T* p;
        int count;
    };
    P* p;
public:
    TSSPtr(T* ptr = 0) {
        if (ptr) {
            p = new P();
            p->p = ptr;
            p->count = 1;
        } else {
            p = 0;
        }
    }
    TSSPtr(const TSSPtr& t) {
        set(t);
    }
    ~TSSPtr() {
        deref();
    }
    void operator=(const TSSPtr& t) {
        if (t.p == p) return;
        deref();
        set(t);
    }
    T* operator->() const {
        return (p) ?p->p :0;
    }
private:
    void deref() {
        if (p) {
            STRIGI_MUTEX_LOCK(&p->lock.lock);
            int c = --(p->count);
            STRIGI_MUTEX_UNLOCK(&p->lock.lock);
            if (c == 0) {
                delete p->p;
                delete p;
            }
            p = 0;
        }
    }
    void set(const TSSPtr& t) {
        if (t.p) {
            STRIGI_MUTEX_LOCK(&t.p->lock.lock);
            t.p->count++;
            STRIGI_MUTEX_UNLOCK(&t.p->lock.lock);
            p = t.p;
        } else {
            p = 0;
        }
    }
};<commit_msg>port the used of mutexes to the new internal mutex api<commit_after>/* This file is part of Strigi Desktop Search
 *
 * Copyright (C) 2006 <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "strigi_thread.h"

// thread safe smart pointer
template <class T>
class TSSPtr {
private:
    struct P {
        StrigiMutex lock;
        T* p;
        int count;
    };
    P* p;
public:
    TSSPtr(T* ptr = 0) {
        if (ptr) {
            p = new P();
            p->p = ptr;
            p->count = 1;
        } else {
            p = 0;
        }
    }
    TSSPtr(const TSSPtr& t) {
        set(t);
    }
    ~TSSPtr() {
        deref();
    }
    void operator=(const TSSPtr& t) {
        if (t.p == p) return;
        deref();
        set(t);
    }
    T* operator->() const {
        return (p) ?p->p :0;
    }
private:
    void deref() {
        if (p) {
            p->lock.lock();
            int c = --(p->count);
            p->lock.unlock();
            if (c == 0) {
                delete p->p;
                delete p;
            }
            p = 0;
        }
    }
    void set(const TSSPtr& t) {
        if (t.p) {
            t.p->lock.lock();
            t.p->count++;
            t.p->lock.unlock();
            p = t.p;
        } else {
            p = 0;
        }
    }
};
{'fraction_non_alphanumeric': 0.1124303368063969, 'fraction_numerical': 0.011146110976496244, 'mean_word_length': 1.6892508143322476, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5602567', 'n_tokens_mistral': 1310, 'n_tokens_neox': 1267, 'n_words': 466}