| 1 |
// Copyright (c) Microsoft. All rights reserved. |
| 2 |
// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 |
|
| 4 |
#include <stdlib.h> |
| 5 |
#include "azure_c_shared_utility/lock.h" |
| 6 |
#include "azure_c_shared_utility/condition.h" |
| 7 |
#include "azure_c_shared_utility/xlogging.h" |
| 8 |
#include <errno.h> |
| 9 |
#include <pthread.h> |
| 10 |
#include "azure_c_shared_utility/gballoc.h" |
| 11 |
#include "time.h" |
| 12 |
#include "linux_time.h" |
| 13 |
|
| 14 |
MU_DEFINE_ENUM_STRINGS(COND_RESULT, COND_RESULT_VALUES); |
| 15 |
|
| 16 |
pthread_cond_t* create_cond(void) |
| 17 |
{ |
| 18 |
pthread_cond_t * cond = (pthread_cond_t*)malloc(sizeof(pthread_cond_t)); |
| 19 |
if (cond != NULL) |
| 20 |
{ |
| 21 |
#ifdef __MACH__ |
| 22 |
pthread_cond_init(cond, NULL); |
| 23 |
#else |
| 24 |
pthread_condattr_t cattr; |
| 25 |
pthread_condattr_init(&cattr); |
| 26 |
pthread_condattr_setclock(&cattr, time_basis); |
| 27 |
pthread_cond_init(cond, &cattr); |
| 28 |
pthread_condattr_destroy(&cattr); |
| 29 |
#endif |
| 30 |
} |
| 31 |
|
| 32 |
return cond; |
| 33 |
} |
| 34 |
|
| 35 |
COND_HANDLE Condition_Init(void) |
| 36 |
{ |
| 37 |
set_time_basis(); |
| 38 |
|
| 39 |
// Codes_SRS_CONDITION_18_002: [ Condition_Init shall create and return a CONDITION_HANDLE ] |
| 40 |
// Codes_SRS_CONDITION_18_008: [ Condition_Init shall return NULL if it fails to allocate the CONDITION_HANDLE ] |
| 41 |
return create_cond(); |
| 42 |
} |
| 43 |
|
| 44 |
COND_RESULT Condition_Post(COND_HANDLE handle) |
| 45 |
{ |
| 46 |
COND_RESULT result; |
| 47 |
// Codes_SRS_CONDITION_18_001: [ Condition_Post shall return COND_INVALID_ARG if handle is NULL ] |
| 48 |
if (handle == NULL) |
| 49 |
{ |
| 50 |
result = COND_INVALID_ARG; |
| 51 |
} |
| 52 |
else |
| 53 |
{ |
| 54 |
// Codes_SRS_CONDITION_18_003: [ Condition_Post shall return COND_OK if it succcessfully posts the condition ] |
| 55 |
if (pthread_cond_signal((pthread_cond_t*)handle) == 0) |
| 56 |
{ |
| 57 |
result = COND_OK; |
| 58 |
} |
| 59 |
else |
| 60 |
{ |
| 61 |
result = COND_ERROR; |
| 62 |
} |
| 63 |
} |
| 64 |
return result; |
| 65 |
} |
| 66 |
|
| 67 |
COND_RESULT Condition_Wait(COND_HANDLE handle, LOCK_HANDLE lock, int timeout_milliseconds) |
| 68 |
{ |
| 69 |
COND_RESULT result; |
| 70 |
// Codes_SRS_CONDITION_18_004: [ Condition_Wait shall return COND_INVALID_ARG if handle is NULL ] |
| 71 |
// Codes_SRS_CONDITION_18_005: [ Condition_Wait shall return COND_INVALID_ARG if lock is NULL and timeout_milliseconds is 0 ] |
| 72 |
// Codes_SRS_CONDITION_18_006: [ Condition_Wait shall return COND_INVALID_ARG if lock is NULL and timeout_milliseconds is not 0 ] |
| 73 |
if (handle == NULL || lock == NULL) |
| 74 |
{ |
| 75 |
result = COND_INVALID_ARG; |
| 76 |
} |
| 77 |
else |
| 78 |
{ |
| 79 |
if (timeout_milliseconds > 0) |
| 80 |
{ |
| 81 |
// Codes_SRS_CONDITION_18_013: [ Condition_Wait shall accept relative timeouts ] |
| 82 |
struct timespec tm; |
| 83 |
if (get_time_ns(&tm) != 0) |
| 84 |
{ |
| 85 |
LogError("Failed to get the current time"); |
| 86 |
return COND_ERROR; |
| 87 |
} |
| 88 |
|
| 89 |
tm.tv_nsec += (timeout_milliseconds % MILLISECONDS_IN_1_SECOND) * NANOSECONDS_IN_1_MILLISECOND; |
| 90 |
tm.tv_sec += timeout_milliseconds / MILLISECONDS_IN_1_SECOND; |
| 91 |
// handle overflow in tv_nsec |
| 92 |
tm.tv_sec+= tm.tv_nsec / NANOSECONDS_IN_1_SECOND; |
| 93 |
tm.tv_nsec %= NANOSECONDS_IN_1_SECOND; |
| 94 |
int wait_result = pthread_cond_timedwait((pthread_cond_t *)handle, (pthread_mutex_t *)lock, &tm); |
| 95 |
if (wait_result == ETIMEDOUT) |
| 96 |
{ |
| 97 |
// Codes_SRS_CONDITION_18_011: [ Condition_Wait shall return COND_TIMEOUT if the condition is NOT triggered and timeout_milliseconds is not 0 ] |
| 98 |
return COND_TIMEOUT; |
| 99 |
} |
| 100 |
else if (wait_result == 0) |
| 101 |
{ |
| 102 |
// Codes_SRS_CONDITION_18_012: [ Condition_Wait shall return COND_OK if the condition is triggered and timeout_milliseconds is not 0 ] |
| 103 |
result = COND_OK; |
| 104 |
} |
| 105 |
else |
| 106 |
{ |
| 107 |
LogError("Failed to pthread_cond_timedwait"); |
| 108 |
return COND_ERROR; |
| 109 |
} |
| 110 |
} |
| 111 |
else |
| 112 |
{ |
| 113 |
if (pthread_cond_wait((pthread_cond_t*)handle, (pthread_mutex_t *)lock) != 0) |
| 114 |
{ |
| 115 |
LogError("Failed to pthread_cond_wait"); |
| 116 |
return COND_ERROR; |
| 117 |
} |
| 118 |
else |
| 119 |
{ |
| 120 |
// Codes_SRS_CONDITION_18_010: [ Condition_Wait shall return COND_OK if the condition is triggered and timeout_milliseconds is 0 ] |
| 121 |
result = COND_OK; |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
return result; |
| 126 |
} |
| 127 |
|
| 128 |
void Condition_Deinit(COND_HANDLE handle) |
| 129 |
{ |
| 130 |
// Codes_SRS_CONDITION_18_007: [ Condition_Deinit will not fail if handle is NULL ] |
| 131 |
if (handle != NULL) |
| 132 |
{ |
| 133 |
// Codes_SRS_CONDITION_18_009: [ Condition_Deinit will deallocate handle if it is not NULL |
| 134 |
pthread_cond_t* cond = (pthread_cond_t*)handle; |
| 135 |
pthread_cond_destroy(cond); |
| 136 |
free(cond); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
|