Coverage Report

Created: 2023-03-26 07:38

/src/yara/libyara/threading.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
Copyright (c) 2016. The YARA Authors. All Rights Reserved.
3
4
Redistribution and use in source and binary forms, with or without modification,
5
are permitted provided that the following conditions are met:
6
7
1. Redistributions of source code must retain the above copyright notice, this
8
list of conditions and the following disclaimer.
9
10
2. Redistributions in binary form must reproduce the above copyright notice,
11
this list of conditions and the following disclaimer in the documentation and/or
12
other materials provided with the distribution.
13
14
3. Neither the name of the copyright holder nor the names of its contributors
15
may be used to endorse or promote products derived from this software without
16
specific prior written permission.
17
18
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
22
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
*/
29
30
#include <yara/error.h>
31
#include <yara/threading.h>
32
33
#if defined(_WIN32) || defined(__CYGWIN__)
34
35
YR_THREAD_ID yr_current_thread_id(void)
36
{
37
  return GetCurrentThreadId();
38
}
39
40
41
int yr_mutex_create(YR_MUTEX* mutex)
42
{
43
  *mutex = CreateMutex(NULL, FALSE, NULL);
44
45
  if (*mutex == NULL)
46
    return ERROR_INTERNAL_FATAL_ERROR;
47
48
  return ERROR_SUCCESS;
49
}
50
51
52
int yr_mutex_destroy(YR_MUTEX* mutex)
53
{
54
  if (CloseHandle(*mutex) == FALSE)
55
    return ERROR_INTERNAL_FATAL_ERROR;
56
57
  return ERROR_SUCCESS;
58
}
59
60
61
int yr_mutex_lock(YR_MUTEX* mutex)
62
{
63
  if (WaitForSingleObject(*mutex, INFINITE) == WAIT_FAILED)
64
    return ERROR_INTERNAL_FATAL_ERROR;
65
66
  return ERROR_SUCCESS;
67
}
68
69
70
int yr_mutex_unlock(YR_MUTEX* mutex)
71
{
72
  if (ReleaseMutex(*mutex) == FALSE)
73
    return ERROR_INTERNAL_FATAL_ERROR;
74
75
  return ERROR_SUCCESS;
76
}
77
78
79
int yr_thread_storage_create(YR_THREAD_STORAGE_KEY* storage)
80
{
81
  *storage = TlsAlloc();
82
83
  if (*storage == TLS_OUT_OF_INDEXES)
84
    return ERROR_INTERNAL_FATAL_ERROR;
85
86
  return ERROR_SUCCESS;
87
}
88
89
90
int yr_thread_storage_destroy(YR_THREAD_STORAGE_KEY* storage)
91
{
92
  if (TlsFree(*storage) == FALSE)
93
    return ERROR_INTERNAL_FATAL_ERROR;
94
95
  return ERROR_SUCCESS;
96
}
97
98
99
int yr_thread_storage_set_value(YR_THREAD_STORAGE_KEY* storage, void* value)
100
{
101
  if (TlsSetValue(*storage, value) == FALSE)
102
    return ERROR_INTERNAL_FATAL_ERROR;
103
104
  return ERROR_SUCCESS;
105
}
106
107
108
void* yr_thread_storage_get_value(YR_THREAD_STORAGE_KEY* storage)
109
{
110
  return TlsGetValue(*storage);
111
}
112
113
114
#else  // POSIX implementation
115
116
117
YR_THREAD_ID yr_current_thread_id(void)
118
0
{
119
0
  return pthread_self();
120
0
}
121
122
123
int yr_mutex_create(YR_MUTEX* mutex)
124
0
{
125
0
  if (pthread_mutex_init(mutex, NULL) != 0)
126
0
    return ERROR_INTERNAL_FATAL_ERROR;
127
128
0
  return ERROR_SUCCESS;
129
0
}
130
131
132
int yr_mutex_destroy(YR_MUTEX* mutex)
133
0
{
134
0
  if (pthread_mutex_destroy(mutex) != 0)
135
0
    return ERROR_INTERNAL_FATAL_ERROR;
136
137
0
  return ERROR_SUCCESS;
138
0
}
139
140
141
int yr_mutex_lock(YR_MUTEX* mutex)
142
0
{
143
0
  if (pthread_mutex_lock(mutex) != 0)
144
0
    return ERROR_INTERNAL_FATAL_ERROR;
145
146
0
  return ERROR_SUCCESS;
147
0
}
148
149
150
int yr_mutex_unlock(YR_MUTEX* mutex)
151
0
{
152
0
  if (pthread_mutex_unlock(mutex) != 0)
153
0
    return ERROR_INTERNAL_FATAL_ERROR;
154
155
0
  return ERROR_SUCCESS;
156
0
}
157
158
159
int yr_thread_storage_create(YR_THREAD_STORAGE_KEY* storage)
160
24
{
161
24
  if (pthread_key_create(storage, NULL) != 0)
162
0
    return ERROR_INTERNAL_FATAL_ERROR;
163
164
24
  return ERROR_SUCCESS;
165
24
}
166
167
168
int yr_thread_storage_destroy(YR_THREAD_STORAGE_KEY* storage)
169
0
{
170
0
  if (pthread_key_delete(*storage) != 0)
171
0
    return ERROR_INTERNAL_FATAL_ERROR;
172
173
0
  return ERROR_SUCCESS;
174
0
}
175
176
177
int yr_thread_storage_set_value(YR_THREAD_STORAGE_KEY* storage, void* value)
178
23.1k
{
179
23.1k
  if (pthread_setspecific(*storage, value) != 0)
180
0
    return ERROR_INTERNAL_FATAL_ERROR;
181
182
23.1k
  return ERROR_SUCCESS;
183
23.1k
}
184
185
186
void* yr_thread_storage_get_value(YR_THREAD_STORAGE_KEY* storage)
187
0
{
188
0
  return pthread_getspecific(*storage);
189
0
}
190
191
#endif