summaryrefslogtreecommitdiff
path: root/client/threads.cpp
blob: a9b8ea50022132de21731bd6f6babd61a8d458a3 (plain)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
   Copyright (C) 2009 Red Hat, Inc.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/

#include "common.h"
#include "threads.h"
#include "utils.h"
#include "debug.h"
#ifdef WIN32
#include <sys/timeb.h>
#endif

Thread::Thread(thread_main_t thread_main, void* opaque)
{
    int r = pthread_create(&_thread, NULL, thread_main, opaque);
    if (r) {
        THROW("failed %d", r);
    }
}

void Thread::join()
{
    pthread_join(_thread, NULL);
}

static inline void rel_time(struct timespec& time, uint64_t delta_nano)
{
#ifdef WIN32
    struct _timeb now;
    _ftime_s(&now);
    time.tv_sec = (long)now.time;
    time.tv_nsec = now.millitm * 1000 * 1000;
#else
    clock_gettime(CLOCK_MONOTONIC, &time);
#endif
    delta_nano += (uint64_t)time.tv_sec * 1000 * 1000 * 1000;
    delta_nano += time.tv_nsec;
    time.tv_sec = long(delta_nano / (1000 * 1000 * 1000));
    time.tv_nsec = long(delta_nano % (1000 * 1000 * 1000));
}

void Lock::timed_lock(uint64_t timout_nano)
{
    struct timespec time;
    int r;

    rel_time(time, timout_nano);
    if ((r = pthread_mutex_timedlock(_mutex.get(), &time))) {
        _locked = false;
        if (r != ETIMEDOUT) {
            THROW("failed %d", r);
        }
        return;
    }
    _locked = true;
}

Condition::Condition()
{
#ifdef WIN32
    pthread_cond_init(&_condition, NULL);
#else
    pthread_condattr_t attr;
    pthread_condattr_init(&attr);
    int r;
    if ((r = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC))) {
        THROW("set clock failed %d", r);
    }
    pthread_cond_init(&_condition, &attr);
    pthread_condattr_destroy(&attr);
#endif
}

bool Condition::timed_wait(Lock& lock, uint64_t nano)
{
    struct timespec time;
    rel_time(time, nano);
    int r = pthread_cond_timedwait(&_condition, lock.get(), &time);
    if (r) {
        if (r != ETIMEDOUT) {
            THROW("failed %d", r);
        }
        return false;
    }
    return true;
}

Mutex::Mutex(Type type)
{
    pthread_mutexattr_t attr;

    pthread_mutexattr_init(&attr);
    if (type == NORMAL) {
        pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
    } else if (type == RECURSIVE) {
        pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
    } else {
        THROW("invalid type %d", type);
    }

    int r;
    if ((r = pthread_mutex_init(&_mutex, &attr))) {
        THROW("int failed %d", r);
    }
    pthread_mutexattr_destroy(&attr);
}

Mutex::~Mutex()
{
    pthread_mutex_destroy(&_mutex);
}