aboutsummaryrefslogtreecommitdiff
path: root/src/stable/condition.cpp
blob: 06d069b93d5b0f2b35c327233fee52ebf4e47785 (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
/*
 * Copyright (C) 2007-2019 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#include "bu/condition.h"

#include <sys/time.h>

Bu::Condition::Condition()
{
    pthread_cond_init( &cond, NULL );
}

Bu::Condition::~Condition()
{
    pthread_cond_destroy( &cond );
}

int Bu::Condition::wait()
{
    return pthread_cond_wait( &cond, &mutex );
}

int Bu::Condition::wait( int nSec, int nUSec )
{
    struct timeval now;
    struct timespec timeout;
    struct timezone tz;
    
    gettimeofday( &now, &tz );
    timeout.tv_sec = now.tv_sec + nSec + ((now.tv_usec + nUSec)/1000000);
    timeout.tv_nsec = ((now.tv_usec + nUSec)%1000000)*1000;

    return pthread_cond_timedwait( &cond, &mutex, &timeout );
}

int Bu::Condition::signal()
{
    return pthread_cond_signal( &cond );
}

int Bu::Condition::broadcast()
{
    return pthread_cond_broadcast( &cond );
}