forked from zbackup/zbackup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mt.cc
83 lines (66 loc) · 1.37 KB
/
mt.cc
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
// Copyright (c) 2012-2014 Konstantin Isakov <[email protected]> and ZBackup contributors, see CONTRIBUTORS
// Part of ZBackup. Licensed under GNU GPLv2 or later + OpenSSL, see LICENSE
#include "mt.hh"
#include <unistd.h>
#include "check.hh"
Mutex::Mutex()
{
pthread_mutex_init( &mutex, 0 );
}
void Mutex::lock()
{
pthread_mutex_lock( &mutex );
}
void Mutex::unlock()
{
pthread_mutex_unlock( &mutex );
}
Mutex::~Mutex()
{
pthread_mutex_destroy( &mutex );
}
Condition::Condition()
{
pthread_cond_init( &cond, 0 );
}
void Condition::signal()
{
pthread_cond_signal( &cond );
}
void Condition::broadcast()
{
pthread_cond_broadcast( &cond );
}
void Condition::wait( Mutex & m )
{
pthread_cond_wait( &cond, &m.mutex );
}
Condition::~Condition()
{
pthread_cond_destroy( &cond );
}
void * Thread::__thread_routine( void * param )
{
return ( (Thread *)param ) -> threadFunction();
}
void Thread::start()
{
CHECK( pthread_create( &thread, 0, &__thread_routine, this ) == 0,
"pthread_create() failed" );
}
void Thread::detach()
{
CHECK( pthread_detach( thread ) == 0, "pthread_detach() failed" );
}
void * Thread::join()
{
void * ret;
pthread_join( thread, &ret );
return ret;
}
size_t getNumberOfCpus()
{
long result = sysconf( _SC_NPROCESSORS_ONLN );
// Handle -1 and also sanitize the 0 value which wouldn't make sense
return result < 1 ? 1 : result;
}