summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/cmetrics/src/cmt_atomic_msvc.c
blob: 65bfd2f80db0deff907e5d00cc19ab3d5542e29c (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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*  CMetrics
 *  ========
 *  Copyright 2021-2022 The CMetrics Authors
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#include <cmetrics/cmt_atomic.h>
#include <windows.h>

/* This allows cmt_atomic_initialize to be automatically called 
 * as soon as the program starts if enabled.
 */

#ifndef _WIN64
CRITICAL_SECTION atomic_operation_lock;
static int       atomic_operation_system_initialized = 0;

int cmt_atomic_initialize()
{
    if (0 == atomic_operation_system_initialized) {
        InitializeCriticalSection(&atomic_operation_lock);

        atomic_operation_system_initialized = 1;
    }

    return 0;
}

int cmt_atomic_compare_exchange(uint64_t *storage, 
                                uint64_t old_value, uint64_t new_value)
{
    uint64_t result;

    if (0 == atomic_operation_system_initialized) {
        printf("CMT ATOMIC : Atomic operation backend not initalized\n");
        exit(1);
    }

    EnterCriticalSection(&atomic_operation_lock);

    if (*storage == old_value) {
        *storage = new_value;

        result = 1;
    }
    else {
        result = 0;
    }

    LeaveCriticalSection(&atomic_operation_lock);

    return result;
}

void cmt_atomic_store(uint64_t *storage, uint64_t new_value)
{
    if (0 == atomic_operation_system_initialized) {
        printf("CMT ATOMIC : Atomic operation backend not initalized\n");
        exit(1);
    }

    EnterCriticalSection(&atomic_operation_lock);

    *storage = new_value;
    
    LeaveCriticalSection(&atomic_operation_lock);
}

uint64_t cmt_atomic_load(uint64_t *storage)
{
    uint64_t result;

    if (0 == atomic_operation_system_initialized) {
        printf("CMT ATOMIC : Atomic operation backend not initalized\n");
        exit(1);
    }

    EnterCriticalSection(&atomic_operation_lock);

    result = *storage;
    
    LeaveCriticalSection(&atomic_operation_lock);

    return result;
}

#else /* _WIN64 */

int cmt_atomic_initialize()
{
    return 0;
}

int cmt_atomic_compare_exchange(uint64_t *storage, 
                                       uint64_t old_value, uint64_t new_value)
{
    uint64_t result;

    result = _InterlockedCompareExchange64(storage, new_value, old_value);

    if (result != old_value) {
        return 0;
    }

    return 1;
}

void cmt_atomic_store(uint64_t *storage, uint64_t new_value)
{
    _InterlockedExchange64(storage, new_value);
}

uint64_t cmt_atomic_load(uint64_t *storage)
{
    return _InterlockedOr64(storage, 0);
}

#endif