-
Notifications
You must be signed in to change notification settings - Fork 204
/
lock_win32.c
96 lines (84 loc) · 2.57 KB
/
lock_win32.c
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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#include <stdlib.h>
#include <windows.h>
#include "azure_c_shared_utility/lock.h"
#include "azure_c_shared_utility/gballoc.h"
#include "azure_c_shared_utility/xlogging.h"
#include "azure_macro_utils/macro_utils.h"
LOCK_HANDLE Lock_Init(void)
{
/* Codes_SRS_LOCK_10_002: [Lock_Init on success shall return a valid lock handle which should be a non NULL value] */
/* Codes_SRS_LOCK_10_003: [Lock_Init on error shall return NULL ] */
SRWLOCK* result = malloc(sizeof(SRWLOCK));
if (result == NULL)
{
LogError("Allocate SRWLOCK failed.");
}
else
{
InitializeSRWLock(result);
}
return (LOCK_HANDLE)result;
}
LOCK_RESULT Lock_Deinit(LOCK_HANDLE handle)
{
LOCK_RESULT result;
if (handle == NULL)
{
/* Codes_SRS_LOCK_10_007: [Lock_Deinit on NULL handle passed returns LOCK_ERROR] */
LogError("Invalid argument; handle is NULL.");
result = LOCK_ERROR;
}
else
{
/* Codes_SRS_LOCK_10_012: [Lock_Deinit frees the memory pointed by handle] */
free(handle);
result = LOCK_OK;
}
return result;
}
LOCK_RESULT Lock(LOCK_HANDLE handle)
{
LOCK_RESULT result;
if (handle == NULL)
{
/* Codes_SRS_LOCK_10_007: [Lock on NULL handle passed returns LOCK_ERROR] */
LogError("Invalid argument; handle is NULL.");
result = LOCK_ERROR;
}
else
{
AcquireSRWLockExclusive((SRWLOCK*)handle);
/* Codes_SRS_LOCK_10_005: [Lock on success shall return LOCK_OK] */
result = LOCK_OK;
// Cannot fail
/* Codes_SRS_LOCK_10_006: [Lock on error shall return LOCK_ERROR] */
}
return result;
}
LOCK_RESULT Unlock(LOCK_HANDLE handle)
{
LOCK_RESULT result;
if (handle == NULL)
{
/* Codes_SRS_LOCK_10_007: [Unlock on NULL handle passed returns LOCK_ERROR] */
LogError("Invalid argument; handle is NULL.");
result = LOCK_ERROR;
}
else
{
#ifdef _MSC_VER
#pragma warning(disable:26110) // Warning C26110: Caller failing to hold lock 'handle' before calling function 'ReleaseSRWLockExclusive'.
#endif
ReleaseSRWLockExclusive((SRWLOCK*)handle);
#ifdef _MSC_VER
#pragma warning (default:26110)
#endif
/* Codes_SRS_LOCK_10_009: [Unlock on success shall return LOCK_OK] */
result = LOCK_OK;
// Cannot fail
/* Codes_SRS_LOCK_10_010: [Unlock on error shall return LOCK_ERROR] */
}
return result;
}