aboutsummaryrefslogtreecommitdiffstats
path: root/vespamalloc/src/vespamalloc/malloc/common.cpp
blob: d14a03176302469f41ebedf2b4ddbb0a144e6a6e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespamalloc/malloc/common.h>
#include <pthread.h>

namespace vespamalloc {

uint32_t Mutex::_threadCount = 0;
bool     Mutex::_stopRecursion = true;

void Mutex::lock()
{
    if (_use) {
        pthread_mutex_lock(&_mutex);
    }
}
void Mutex::unlock()
{
    if (_use) {
        pthread_mutex_unlock(&_mutex);
    }
}

void Mutex::quit()
{
    if (_use) {
        _use = false;
        pthread_mutex_destroy(&_mutex);
    }
}

void Mutex::init() {
    if (!_use && ! _stopRecursion) {
        pthread_mutex_init(&_mutex, NULL);
        _use = true;
    }
}

Guard::Guard(Mutex & m) :
     _mutex(&m)
{
    MallocRecurseOnSuspend(false);
    _mutex->lock();
    MallocRecurseOnSuspend(true);
}


}

extern "C" void MallocRecurseOnSuspend(bool recurse)
{
    (void) recurse;
}