aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/restricted/aws/aws-c-common/source/posix/condition_variable.c
blob: ca321c6bfadccb731d9aec9b461c07335e0f00b1 (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
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
/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

#include <aws/common/condition_variable.h>

#include <aws/common/clock.h>
#include <aws/common/mutex.h>

#include <errno.h>

static int process_error_code(int err) {
    switch (err) {
        case ENOMEM:
            return aws_raise_error(AWS_ERROR_OOM);
        case ETIMEDOUT:
            return aws_raise_error(AWS_ERROR_COND_VARIABLE_TIMED_OUT);
        default:
            return aws_raise_error(AWS_ERROR_COND_VARIABLE_ERROR_UNKNOWN);
    }
}

int aws_condition_variable_init(struct aws_condition_variable *condition_variable) {
    AWS_PRECONDITION(condition_variable);

    if (pthread_cond_init(&condition_variable->condition_handle, NULL)) {
        AWS_ZERO_STRUCT(*condition_variable);
        return aws_raise_error(AWS_ERROR_COND_VARIABLE_INIT_FAILED);
    }

    condition_variable->initialized = true;
    return AWS_OP_SUCCESS;
}

void aws_condition_variable_clean_up(struct aws_condition_variable *condition_variable) {
    AWS_PRECONDITION(condition_variable);

    if (condition_variable->initialized) {
        pthread_cond_destroy(&condition_variable->condition_handle);
    }

    AWS_ZERO_STRUCT(*condition_variable);
}

int aws_condition_variable_notify_one(struct aws_condition_variable *condition_variable) {
    AWS_PRECONDITION(condition_variable && condition_variable->initialized);

    int err_code = pthread_cond_signal(&condition_variable->condition_handle);

    if (err_code) {
        return process_error_code(err_code);
    }

    return AWS_OP_SUCCESS;
}

int aws_condition_variable_notify_all(struct aws_condition_variable *condition_variable) {
    AWS_PRECONDITION(condition_variable && condition_variable->initialized);

    int err_code = pthread_cond_broadcast(&condition_variable->condition_handle);

    if (err_code) {
        return process_error_code(err_code);
    }

    return AWS_OP_SUCCESS;
}

int aws_condition_variable_wait(struct aws_condition_variable *condition_variable, struct aws_mutex *mutex) {
    AWS_PRECONDITION(condition_variable && condition_variable->initialized);
    AWS_PRECONDITION(mutex && mutex->initialized);

    int err_code = pthread_cond_wait(&condition_variable->condition_handle, &mutex->mutex_handle);

    if (err_code) {
        return process_error_code(err_code);
    }

    return AWS_OP_SUCCESS;
}

int aws_condition_variable_wait_for(
    struct aws_condition_variable *condition_variable,
    struct aws_mutex *mutex,
    int64_t time_to_wait) {

    AWS_PRECONDITION(condition_variable && condition_variable->initialized);
    AWS_PRECONDITION(mutex && mutex->initialized);

    uint64_t current_sys_time = 0;
    if (aws_sys_clock_get_ticks(&current_sys_time)) {
        return AWS_OP_ERR;
    }

    time_to_wait += current_sys_time;

    struct timespec ts;
    uint64_t remainder = 0;
    ts.tv_sec =
        (time_t)aws_timestamp_convert((uint64_t)time_to_wait, AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_SECS, &remainder);
    ts.tv_nsec = (long)remainder;

    int err_code = pthread_cond_timedwait(&condition_variable->condition_handle, &mutex->mutex_handle, &ts);

    if (err_code) {
        return process_error_code(err_code);
    }

    return AWS_OP_SUCCESS;
}