File: lockable.cc

package info (click to toggle)
apt-cacher-ng 0.7.27-1~bpo70+1
  • links: PTS
  • area: main
  • in suites: wheezy-backports
  • size: 1,740 kB
  • sloc: cpp: 13,987; sh: 519; perl: 442; ansic: 414; makefile: 77
file content (96 lines) | stat: -rw-r--r-- 2,919 bytes parent folder | download
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

#define LOCAL_DEBUG
#include "debug.h"

#include <config.h>
#include "lockable.h"
#include <inttypes.h>
#include <errno.h>
#include <pthread.h>
#include <sys/time.h>

//#include <iostream>
//using namespace MYSTD;

lockable::lockable() { pthread_mutex_init(&__mutex, NULL); /*MYSTD::cout << "inited lock:" <<&__mutex<<endl;*/}
lockable::~lockable() { pthread_mutex_destroy(&__mutex); }
void lockable::lock() { /*cout << "locking: " << &__mutex<<endl;*/ pthread_mutex_lock(&__mutex); }
void lockable::unlock() { /*cout << "releasing: " << &__mutex<<endl;*/ pthread_mutex_unlock(&__mutex); }
bool lockable::tryLock() { return EBUSY!=pthread_mutex_trylock(&__mutex); }

condition::condition() : lockable() { pthread_cond_init(&__cond, NULL); }
condition::~condition() { pthread_cond_destroy(&__cond); }
void condition::wait() { 
    //ldbg("waiting on " << (uint64_t) &__cond); 
    pthread_cond_wait(&__cond, &__mutex); 
}

bool condition::wait_until(time_t nUTCsecs, long msec)
{
	if(nUTCsecs < 0 || msec < 0)
		return false; // looks suspicious, report timeout, immediately
	if(msec>=1000)
	{
		nUTCsecs += (msec/1000);
		msec %=1000;
	}
	struct timespec timeout = {nUTCsecs, msec*1000};
	return ETIMEDOUT == pthread_cond_timedwait(&__cond, &__mutex, &timeout);
}

#if 0
bool condition::wait(time_t sec, long msec)
{
	if(sec<0 || msec<0)
		return false; // looks suspicious, report timeout, immediately

    struct timeval now;
    struct timespec timeout;
    gettimeofday(&now, NULL);

    timeout.tv_sec = now.tv_sec + sec;
    timeout.tv_nsec = now.tv_usec * 1000 + msec;
    if(timeout.tv_nsec>=1000000)
    {
    	timeout.tv_sec += (timeout.tv_nsec / 1000000);
    	timeout.tv_nsec %= 1000000;
	}

    // make sure to not cause harm if it ever underflows
    if(timeout.tv_sec < 0)
    	timeout.tv_sec = MAX_VAL(time_t);

#ifdef _DARWIN_C_SOURCE
    // From: Andrew Sharpe <andrew.sharpe79@gmail.com>
    // Date: Sat, 29 Dec 2012 19:21:11 +1000
    // (slightly modified)

    // kludge to work around what looks like wrapping issues -
	// I'd like to know the implementation of pthread_cond_timedwait
	// to get a definitive solution to this, as tracing through the
	// headers on OSX lead me to believe that timespec.tv_sec is of
	// type __darwin_time_t and that can handle values up to
	// 9223372036854775807 (on this machine), however this
	// implementation breaks with any value greater than
	// 2147483647 (std::numeric_limits<int>::max())
	if (timeout.tv_sec > MAX_VAL(int))
		timeout.tv_sec = MAX_VAL(int);
#endif

	return ETIMEDOUT != pthread_cond_timedwait(&__cond, &__mutex, &timeout);
}
#endif

void condition::notifyAll() { 
    //ldbg("broadcast " << (uint64_t) &__cond);
    pthread_cond_broadcast(&__cond);
}

void condition::notify() { 
    //ldbg("signal " << (uint64_t) &__cond);
    pthread_cond_signal(&__cond); 
}



//void condition::notifyWithLocking() { lock(); notify(); unlock();}