Coverage Report

Created: 2025-08-26 07:06

/src/libxml2/include/private/threads.h
Line
Count
Source
1
#ifndef XML_THREADS_H_PRIVATE__
2
#define XML_THREADS_H_PRIVATE__
3
4
#include <libxml/threads.h>
5
6
#ifdef LIBXML_THREAD_ENABLED
7
  #ifdef _WIN32
8
    #define WIN32_LEAN_AND_MEAN
9
    #ifdef _WIN32_WINNT
10
      #undef _WIN32_WINNT
11
    #endif
12
    #define _WIN32_WINNT 0x0600
13
    #include <windows.h>
14
    #define HAVE_WIN32_THREADS
15
  #else
16
    #include <pthread.h>
17
    #define HAVE_POSIX_THREADS
18
  #endif
19
#endif
20
21
/*
22
 * xmlMutex are a simple mutual exception locks
23
 */
24
struct _xmlMutex {
25
#ifdef HAVE_POSIX_THREADS
26
    pthread_mutex_t lock;
27
#elif defined HAVE_WIN32_THREADS
28
    CRITICAL_SECTION cs;
29
#else
30
    int empty;
31
#endif
32
};
33
34
/*
35
 * xmlRMutex are reentrant mutual exception locks
36
 */
37
struct _xmlRMutex {
38
#ifdef HAVE_POSIX_THREADS
39
    pthread_mutex_t lock;
40
    unsigned int held;
41
    unsigned int waiters;
42
    pthread_t tid;
43
    pthread_cond_t cv;
44
#elif defined HAVE_WIN32_THREADS
45
    CRITICAL_SECTION cs;
46
#else
47
    int empty;
48
#endif
49
};
50
51
#if defined(LIBXML_THREAD_ENABLED) && \
52
    __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_ATOMICS__)
53
54
/** Atomic reference count */
55
typedef _Atomic size_t xmlRefCount;
56
57
/**
58
 * Initialize refcount.
59
 *
60
 * @param r  refcount
61
 */
62
static XML_INLINE void
63
18.0k
xmlRefCountInit(xmlRefCount *r) {
64
18.0k
    *r = 1;
65
18.0k
}
dict.c:xmlRefCountInit
Line
Count
Source
63
18.0k
xmlRefCountInit(xmlRefCount *r) {
64
18.0k
    *r = 1;
65
18.0k
}
Unexecuted instantiation: globals.c:xmlRefCountInit
Unexecuted instantiation: threads.c:xmlRefCountInit
Unexecuted instantiation: xmlmemory.c:xmlRefCountInit
Unexecuted instantiation: catalog.c:xmlRefCountInit
Unexecuted instantiation: relaxng.c:xmlRefCountInit
Unexecuted instantiation: xmlschemastypes.c:xmlRefCountInit
66
67
/**
68
 * Increase refcount.
69
 *
70
 * @param r  refcount
71
 */
72
static XML_INLINE void
73
4.08k
xmlRefCountInc(xmlRefCount *r) {
74
4.08k
    *r += 1;
75
4.08k
}
dict.c:xmlRefCountInc
Line
Count
Source
73
4.08k
xmlRefCountInc(xmlRefCount *r) {
74
4.08k
    *r += 1;
75
4.08k
}
Unexecuted instantiation: globals.c:xmlRefCountInc
Unexecuted instantiation: threads.c:xmlRefCountInc
Unexecuted instantiation: xmlmemory.c:xmlRefCountInc
Unexecuted instantiation: catalog.c:xmlRefCountInc
Unexecuted instantiation: relaxng.c:xmlRefCountInc
Unexecuted instantiation: xmlschemastypes.c:xmlRefCountInc
76
77
/**
78
 * Decrease refcount.
79
 *
80
 * @param r  refcount
81
 * @returns 0 if refcount reached zero, 1 otherwise
82
 */
83
static XML_INLINE int
84
22.1k
xmlRefCountDec(xmlRefCount *r) {
85
22.1k
    return --*r > 0;
86
22.1k
}
dict.c:xmlRefCountDec
Line
Count
Source
84
22.1k
xmlRefCountDec(xmlRefCount *r) {
85
22.1k
    return --*r > 0;
86
22.1k
}
Unexecuted instantiation: globals.c:xmlRefCountDec
Unexecuted instantiation: threads.c:xmlRefCountDec
Unexecuted instantiation: xmlmemory.c:xmlRefCountDec
Unexecuted instantiation: catalog.c:xmlRefCountDec
Unexecuted instantiation: relaxng.c:xmlRefCountDec
Unexecuted instantiation: xmlschemastypes.c:xmlRefCountDec
87
88
#elif defined(HAVE_POSIX_THREADS)
89
90
typedef struct {
91
    pthread_mutex_t mutex;
92
    size_t count;
93
} xmlRefCount;
94
95
static XML_INLINE void
96
xmlRefCountInit(xmlRefCount *r) {
97
    pthread_mutex_init(&r->mutex, NULL);
98
    r->count = 1;
99
}
100
101
static XML_INLINE void
102
xmlRefCountInc(xmlRefCount *r) {
103
    pthread_mutex_lock(&r->mutex);
104
    r->count += 1;
105
    pthread_mutex_unlock(&r->mutex);
106
}
107
108
static XML_INLINE int
109
xmlRefCountDec(xmlRefCount *r) {
110
    size_t val;
111
112
    pthread_mutex_lock(&r->mutex);
113
    val = --r->count;
114
    pthread_mutex_unlock(&r->mutex);
115
116
    if (val > 0)
117
        return 1;
118
119
    pthread_mutex_destroy(&r->mutex);
120
    return 0;
121
}
122
123
#elif defined(HAVE_WIN32_THREADS)
124
125
#ifdef _WIN64
126
127
typedef __int64 xmlRefCount;
128
129
static XML_INLINE void
130
xmlRefCountInit(xmlRefCount *r) {
131
    *r = 1;
132
}
133
134
static XML_INLINE void
135
xmlRefCountInc(xmlRefCount *r) {
136
    InterlockedIncrement64(r);
137
}
138
139
static XML_INLINE int
140
xmlRefCountDec(xmlRefCount *r) {
141
    return InterlockedDecrement64(r) > 0;
142
}
143
144
#else /* 32-bit */
145
146
typedef long xmlRefCount;
147
148
static XML_INLINE void
149
xmlRefCountInit(xmlRefCount *r) {
150
    *r = 1;
151
}
152
153
static XML_INLINE void
154
xmlRefCountInc(xmlRefCount *r) {
155
    InterlockedIncrement(r);
156
}
157
158
static XML_INLINE int
159
xmlRefCountDec(xmlRefCount *r) {
160
    return InterlockedDecrement(r) > 0;
161
}
162
163
#endif
164
165
#else /* no threads */
166
167
typedef size_t xmlRefCount;
168
169
static XML_INLINE void
170
xmlRefCountInit(xmlRefCount *r) {
171
    *r = 1;
172
}
173
174
static XML_INLINE void
175
xmlRefCountInc(xmlRefCount *r) {
176
    *r += 1;
177
}
178
179
static XML_INLINE int
180
xmlRefCountDec(xmlRefCount *r) {
181
    return --*r > 0;
182
}
183
184
#endif
185
186
XML_HIDDEN void
187
xmlInitMutex(xmlMutex *mutex);
188
XML_HIDDEN void
189
xmlCleanupMutex(xmlMutex *mutex);
190
191
XML_HIDDEN void
192
xmlInitRMutex(xmlRMutex *mutex);
193
XML_HIDDEN void
194
xmlCleanupRMutex(xmlRMutex *mutex);
195
196
#ifdef LIBXML_SCHEMAS_ENABLED
197
XML_HIDDEN void
198
xmlInitSchemasTypesInternal(void);
199
XML_HIDDEN void
200
xmlCleanupSchemasTypesInternal(void);
201
#endif
202
203
#ifdef LIBXML_RELAXNG_ENABLED
204
XML_HIDDEN void
205
xmlInitRelaxNGInternal(void);
206
XML_HIDDEN void
207
xmlCleanupRelaxNGInternal(void);
208
#endif
209
210
211
#endif /* XML_THREADS_H_PRIVATE__ */