| 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 |
|
| 6 |
#include "azure_c_shared_utility/condition.h" |
| 7 |
#include "windows.h" |
| 8 |
#include "azure_c_shared_utility/xlogging.h" |
| 9 |
#include "azure_c_shared_utility/gballoc.h" |
| 10 |
|
| 11 |
MU_DEFINE_ENUM_STRINGS(COND_RESULT, COND_RESULT_VALUES); |
| 12 |
|
| 13 |
typedef struct CONDITION_TAG |
| 14 |
{ |
| 15 |
volatile LONG waiting_thread_count; |
| 16 |
HANDLE event_handle; |
| 17 |
} |
| 18 |
CONDITION; |
| 19 |
|
| 20 |
COND_HANDLE Condition_Init(void) |
| 21 |
{ |
| 22 |
// Codes_SRS_CONDITION_18_002: [ Condition_Init shall create and return a CONDITION_HANDLE ] |
| 23 |
CONDITION* cond = (CONDITION*)malloc(sizeof(CONDITION)); |
| 24 |
|
| 25 |
// Codes_SRS_CONDITION_18_008: [ Condition_Init shall return NULL if it fails to allocate the CONDITION_HANDLE ] |
| 26 |
if (cond != NULL) |
| 27 |
{ |
| 28 |
(void)memset(cond, 0, sizeof(CONDITION)); |
| 29 |
cond->event_handle = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 30 |
|
| 31 |
if (cond->event_handle == NULL) |
| 32 |
{ |
| 33 |
LogError("CreateEvent failed with error %d", GetLastError()); |
| 34 |
free(cond); |
| 35 |
cond = NULL; |
| 36 |
} |
| 37 |
else |
| 38 |
{ |
| 39 |
/* Needed to emulate pthread_signal as we only signal the event when there are waiting threads */ |
| 40 |
cond->waiting_thread_count = 0; |
| 41 |
} |
| 42 |
} |
| 43 |
else |
| 44 |
{ |
| 45 |
LogError("Failed to allocate condition handle"); |
| 46 |
} |
| 47 |
|
| 48 |
return (COND_HANDLE)cond; |
| 49 |
} |
| 50 |
|
| 51 |
COND_RESULT Condition_Post(COND_HANDLE handle) |
| 52 |
{ |
| 53 |
COND_RESULT result; |
| 54 |
if (handle == NULL) |
| 55 |
{ |
| 56 |
LogError("Null argument handle passed to Condition_Post"); |
| 57 |
|
| 58 |
// Codes_SRS_CONDITION_18_001: [ Condition_Post shall return COND_INVALID_ARG if handle is NULL ] |
| 59 |
result = COND_INVALID_ARG; |
| 60 |
} |
| 61 |
else |
| 62 |
{ |
| 63 |
CONDITION* cond = (CONDITION*)handle; |
| 64 |
|
| 65 |
/* Emulate pthreads signalling, by only unblocking *one* waiting thread if there is one waiting */ |
| 66 |
if (cond->waiting_thread_count == 0 || SetEvent(cond->event_handle)) |
| 67 |
{ |
| 68 |
// Codes_SRS_CONDITION_18_003: [ Condition_Post shall return COND_OK if it succcessfully posts the condition ] |
| 69 |
result = COND_OK; |
| 70 |
} |
| 71 |
else |
| 72 |
{ |
| 73 |
LogError("Failed SetEvent call with error %d", GetLastError()); |
| 74 |
|
| 75 |
result = COND_ERROR; |
| 76 |
} |
| 77 |
} |
| 78 |
return result; |
| 79 |
} |
| 80 |
|
| 81 |
COND_RESULT Condition_Wait(COND_HANDLE handle, LOCK_HANDLE lock, int timeout_milliseconds) |
| 82 |
{ |
| 83 |
COND_RESULT result; |
| 84 |
|
| 85 |
// Codes_SRS_CONDITION_18_004: [ Condition_Wait shall return COND_INVALID_ARG if handle is NULL ] |
| 86 |
// Codes_SRS_CONDITION_18_005: [ Condition_Wait shall return COND_INVALID_ARG if lock is NULL and timeout_milliseconds is 0 ] |
| 87 |
// Codes_SRS_CONDITION_18_006: [ Condition_Wait shall return COND_INVALID_ARG if lock is NULL and timeout_milliseconds is not 0 ] |
| 88 |
if (handle == NULL || lock == NULL) |
| 89 |
{ |
| 90 |
result = COND_INVALID_ARG; |
| 91 |
} |
| 92 |
else |
| 93 |
{ |
| 94 |
CONDITION* cond = (CONDITION*)handle; |
| 95 |
|
| 96 |
/* Increment the waiting thread count, unlock the lock and wait */ |
| 97 |
cond->waiting_thread_count++; |
| 98 |
|
| 99 |
if (Unlock(lock) == LOCK_OK) |
| 100 |
{ |
| 101 |
DWORD wait_result; |
| 102 |
|
| 103 |
// Codes_SRS_CONDITION_18_013: [ Condition_Wait shall accept relative timeouts ] |
| 104 |
wait_result = WaitForSingleObject(cond->event_handle, timeout_milliseconds == 0 ? INFINITE : timeout_milliseconds); |
| 105 |
|
| 106 |
/* If we unlocked ok, it means the lock handle is valid, lock must succeed since it wraps EnterCriticalSection */ |
| 107 |
(void)Lock(lock); |
| 108 |
|
| 109 |
if (wait_result != WAIT_OBJECT_0 && wait_result != WAIT_TIMEOUT) |
| 110 |
{ |
| 111 |
LogError("Failed wait, wait returned with %x", wait_result); |
| 112 |
|
| 113 |
/* cond might be freed at this point, just return error and do not touch condition */ |
| 114 |
result = COND_ERROR; |
| 115 |
} |
| 116 |
else |
| 117 |
{ |
| 118 |
/* To handle the chance of a race condition reset the event again when there are no more waiting threads */ |
| 119 |
if (--cond->waiting_thread_count == 0) |
| 120 |
{ |
| 121 |
(void)ResetEvent(cond->event_handle); |
| 122 |
} |
| 123 |
|
| 124 |
if (wait_result == WAIT_TIMEOUT) |
| 125 |
{ |
| 126 |
// Codes_SRS_CONDITION_18_011: [ Condition_Wait shall return COND_TIMEOUT if the condition is NOT triggered and timeout_milliseconds is not 0 ] |
| 127 |
result = COND_TIMEOUT; |
| 128 |
} |
| 129 |
else |
| 130 |
{ |
| 131 |
// Codes_SRS_CONDITION_18_012: [ Condition_Wait shall return COND_OK if the condition is triggered and timeout_milliseconds is not 0 ] |
| 132 |
result = COND_OK; |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
else |
| 137 |
{ |
| 138 |
cond->waiting_thread_count--; |
| 139 |
LogError("Invalid lock passed which failed to unlock"); |
| 140 |
result = COND_ERROR; |
| 141 |
} |
| 142 |
} |
| 143 |
return result; |
| 144 |
} |
| 145 |
|
| 146 |
void Condition_Deinit(COND_HANDLE handle) |
| 147 |
{ |
| 148 |
// Codes_SRS_CONDITION_18_007: [ Condition_Deinit will not fail if handle is NULL ] |
| 149 |
// Codes_SRS_CONDITION_18_009: [ Condition_Deinit will deallocate handle if it is not NULL |
| 150 |
if (handle != NULL) |
| 151 |
{ |
| 152 |
CONDITION* cond = (CONDITION*)handle; |
| 153 |
|
| 154 |
(void)CloseHandle(cond->event_handle); |
| 155 |
cond->event_handle = INVALID_HANDLE_VALUE; |
| 156 |
|
| 157 |
free(cond); |
| 158 |
} |
| 159 |
} |
| 160 |
|