Skip to content

Commit 562c89a

Browse files
make locking a compile-time setting
1 parent cb88af7 commit 562c89a

File tree

8 files changed

+56
-55
lines changed

8 files changed

+56
-55
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ option(CMARK_TESTS "Build cmark-gfm tests and enable testing" ON)
1818
option(CMARK_STATIC "Build static libcmark-gfm library" ON)
1919
option(CMARK_SHARED "Build shared libcmark-gfm library" ON)
2020
option(CMARK_LIB_FUZZER "Build libFuzzer fuzzing harness" OFF)
21+
option(CMARK_THREADING "Add locks around static accesses via pthreads" OFF)
2122

2223
add_subdirectory(src)
2324
add_subdirectory(extensions)

extensions/core-extensions.c

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
#include <pthread.h>
2-
#include <stdatomic.h>
3-
41
#include "cmark-gfm-core-extensions.h"
52
#include "autolink.h"
63
#include "mutex.h"
@@ -11,19 +8,17 @@
118
#include "registry.h"
129
#include "plugin.h"
1310

14-
static pthread_mutex_t extensions_lock;
15-
static atomic_int extensions_latch = 0;
11+
CMARK_DEFINE_LOCK(extensions);
1612

1713
static int core_extensions_registration(cmark_plugin *plugin) {
18-
initialize_mutex_once(&extensions_lock, &extensions_latch);
19-
pthread_mutex_lock(&extensions_lock);
14+
CMARK_INITIALIZE_AND_LOCK(extensions);
2015
cmark_plugin_register_syntax_extension(plugin, create_table_extension());
2116
cmark_plugin_register_syntax_extension(plugin,
2217
create_strikethrough_extension());
2318
cmark_plugin_register_syntax_extension(plugin, create_autolink_extension());
2419
cmark_plugin_register_syntax_extension(plugin, create_tagfilter_extension());
2520
cmark_plugin_register_syntax_extension(plugin, create_tasklist_extension());
26-
pthread_mutex_unlock(&extensions_lock);
21+
CMARK_UNLOCK(extensions);
2722
return 1;
2823
}
2924

@@ -32,11 +27,10 @@ static atomic_int registered_latch = 0;
3227
static _Atomic int registered = 0;
3328

3429
void cmark_gfm_core_extensions_ensure_registered(void) {
35-
initialize_mutex_once(&registered_lock, &registered_latch);
36-
pthread_mutex_lock(&registered_lock);
30+
CMARK_INITIALIZE_AND_LOCK(extensions);
3731
if (!registered) {
3832
cmark_register_plugin(core_extensions_registration);
3933
registered = 1;
4034
}
41-
pthread_mutex_unlock(&registered_lock);
35+
CMARK_UNLOCK(extensions);
4236
}

src/arena.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
#include <pthread.h>
2-
#include <stdatomic.h>
31
#include <stdlib.h>
42
#include <string.h>
53
#include <stdint.h>
64
#include "cmark-gfm.h"
75
#include "cmark-gfm-extension_api.h"
86
#include "mutex.h"
97

10-
static pthread_mutex_t arena_lock;
11-
static atomic_int arena_latch = 0;
8+
CMARK_DEFINE_LOCK(arena)
129

1310
static struct arena_chunk {
1411
size_t sz, used;
@@ -30,13 +27,12 @@ static struct arena_chunk *alloc_arena_chunk(size_t sz, struct arena_chunk *prev
3027
}
3128

3229
void cmark_arena_push(void) {
33-
initialize_mutex_once(&arena_lock, &arena_latch);
34-
pthread_mutex_lock(&arena_lock);
30+
CMARK_INITIALIZE_AND_LOCK(arena);
3531
if (!A)
3632
return;
3733
A->push_point = 1;
3834
A = alloc_arena_chunk(10240, A);
39-
pthread_mutex_unlock(&arena_lock);
35+
CMARK_UNLOCK(arena);
4036
}
4137

4238
int cmark_arena_pop(void) {
@@ -77,8 +73,7 @@ static void *arena_calloc(size_t nmem, size_t size) {
7773
const size_t align = sizeof(size_t) - 1;
7874
sz = (sz + align) & ~align;
7975

80-
// the arena lock will have already been set up by a previous call to init_arena
81-
pthread_mutex_lock(&arena_lock);
76+
CMARK_INITIALIZE_AND_LOCK(arena);
8277

8378
if (sz > A->sz) {
8479
A->prev = alloc_arena_chunk(sz, A->prev);
@@ -90,7 +85,7 @@ static void *arena_calloc(size_t nmem, size_t size) {
9085
void *ptr = (uint8_t *) A->ptr + A->used;
9186
A->used += sz;
9287

93-
pthread_mutex_unlock(&arena_lock);
88+
CMARK_UNLOCK(arena);
9489

9590
*((size_t *) ptr) = sz - sizeof(size_t);
9691
return (uint8_t *) ptr + sizeof(size_t);

src/config.h.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ extern "C" {
1717

1818
#cmakedefine HAVE___ATTRIBUTE__
1919

20+
#cmakedefine CMARK_THREADING
21+
2022
#ifdef HAVE___ATTRIBUTE__
2123
#define CMARK_ATTRIBUTE(list) __attribute__ (list)
2224
#else

src/include/cmark-gfm_config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ extern "C" {
1717

1818
#define HAVE___ATTRIBUTE__
1919

20+
#define CMARK_THREADING
21+
2022
#ifdef HAVE___ATTRIBUTE__
2123
#define CMARK_ATTRIBUTE(list) __attribute__ (list)
2224
#else

src/include/mutex.h

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#ifndef CMARK_MUTEX_H
22
#define CMARK_MUTEX_H
33

4+
#include "cmark-gfm_config.h"
5+
6+
#ifdef CMARK_THREADING
7+
48
#include <pthread.h>
59
#include <stdatomic.h>
610

7-
#include "cmark-gfm_config.h"
8-
911
static CMARK_INLINE void initialize_mutex_once(pthread_mutex_t *m, atomic_int *latch) {
1012
int expected = 0;
1113

@@ -14,4 +16,22 @@ static CMARK_INLINE void initialize_mutex_once(pthread_mutex_t *m, atomic_int *l
1416
}
1517
}
1618

17-
#endif
19+
#define CMARK_DEFINE_LOCK(NAME) \
20+
static pthread_mutex_t NAME##_lock; \
21+
static atomic_int NAME##_latch = 0;
22+
23+
#define CMARK_INITIALIZE_AND_LOCK(NAME) \
24+
initialize_mutex_once(&NAME##_lock, &NAME##_latch); \
25+
pthread_mutex_lock(&NAME##_lock);
26+
27+
#define CMARK_UNLOCK(NAME) pthread_mutex_unlock(&NAME##_lock);
28+
29+
#else // no threading support
30+
31+
#define CMARK_DEFINE_LOCK(NAME)
32+
#define CMARK_INITIALIZE_AND_LOCK(NAME)
33+
#define CMARK_UNLOCK(NAME)
34+
35+
#endif // CMARK_THREADING
36+
37+
#endif // CMARK_MUTEX_H

src/inlines.c

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#include <stdlib.h>
22
#include <string.h>
33
#include <stdio.h>
4-
#include <pthread.h>
5-
#include <stdatomic.h>
64

75
#include "cmark_ctype.h"
86
#include "cmark-gfm_config.h"
@@ -69,8 +67,7 @@ typedef struct subject{
6967
// Extensions may populate this.
7068
static int8_t SKIP_CHARS[256];
7169

72-
pthread_mutex_t chars_lock;
73-
static atomic_int chars_latch = 0;
70+
CMARK_DEFINE_LOCK(chars);
7471

7572
static CMARK_INLINE bool S_is_line_end_char(char c) {
7673
return (c == '\n' || c == '\r');
@@ -411,8 +408,7 @@ static int scan_delims(subject *subj, unsigned char c, bool *can_open,
411408
} else {
412409
before_char_pos = subj->pos - 1;
413410

414-
initialize_mutex_once(&chars_lock, &chars_latch);
415-
pthread_mutex_lock(&chars_lock);
411+
CMARK_INITIALIZE_AND_LOCK(chars);
416412

417413
// walk back to the beginning of the UTF_8 sequence:
418414
while ((peek_at(subj, before_char_pos) >> 6 == 2 || SKIP_CHARS[peek_at(subj, before_char_pos)]) && before_char_pos > 0) {
@@ -424,7 +420,7 @@ static int scan_delims(subject *subj, unsigned char c, bool *can_open,
424420
before_char = 10;
425421
}
426422

427-
pthread_mutex_unlock(&chars_lock);
423+
CMARK_UNLOCK(chars);
428424
}
429425

430426
if (c == '\'' || c == '"') {
@@ -442,8 +438,7 @@ static int scan_delims(subject *subj, unsigned char c, bool *can_open,
442438
} else {
443439
after_char_pos = subj->pos;
444440

445-
initialize_mutex_once(&chars_lock, &chars_latch);
446-
pthread_mutex_lock(&chars_lock);
441+
CMARK_INITIALIZE_AND_LOCK(chars);
447442

448443
while (SKIP_CHARS[peek_at(subj, after_char_pos)] && after_char_pos < subj->input.len) {
449444
after_char_pos += 1;
@@ -454,7 +449,7 @@ static int scan_delims(subject *subj, unsigned char c, bool *can_open,
454449
after_char = 10;
455450
}
456451

457-
pthread_mutex_unlock(&chars_lock);
452+
CMARK_UNLOCK(chars);
458453
}
459454

460455
left_flanking = numdelims > 0 && !cmark_utf8proc_is_space(after_char) &&
@@ -1382,21 +1377,19 @@ static bufsize_t subject_find_special_char(subject *subj, int options) {
13821377
}
13831378

13841379
void cmark_inlines_add_special_character(unsigned char c, bool emphasis) {
1385-
initialize_mutex_once(&chars_lock, &chars_latch);
1386-
pthread_mutex_lock(&chars_lock);
1380+
CMARK_INITIALIZE_AND_LOCK(chars);
13871381
SPECIAL_CHARS[c] = 1;
13881382
if (emphasis)
13891383
SKIP_CHARS[c] = 1;
1390-
pthread_mutex_unlock(&chars_lock);
1384+
CMARK_UNLOCK(chars);
13911385
}
13921386

13931387
void cmark_inlines_remove_special_character(unsigned char c, bool emphasis) {
1394-
initialize_mutex_once(&chars_lock, &chars_latch);
1395-
pthread_mutex_lock(&chars_lock);
1388+
CMARK_INITIALIZE_AND_LOCK(chars);
13961389
SPECIAL_CHARS[c] = 0;
13971390
if (emphasis)
13981391
SKIP_CHARS[c] = 0;
1399-
pthread_mutex_unlock(&chars_lock);
1392+
CMARK_UNLOCK(chars);
14001393
}
14011394

14021395
static cmark_node *try_extensions(cmark_parser *parser,

src/registry.c

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#include <stdatomic.h>
21
#include <stdint.h>
32
#include <stdlib.h>
43
#include <string.h>
@@ -14,8 +13,7 @@ extern cmark_mem CMARK_DEFAULT_MEM_ALLOCATOR;
1413

1514
static cmark_llist *syntax_extensions = NULL;
1615

17-
static pthread_mutex_t extensions_lock;
18-
static atomic_int extensions_latch = 0;
16+
CMARK_DEFINE_LOCK(extensions);
1917

2018
void cmark_register_plugin(cmark_plugin_init_func reg_fn) {
2119
cmark_plugin *plugin = cmark_plugin_new();
@@ -28,22 +26,20 @@ void cmark_register_plugin(cmark_plugin_init_func reg_fn) {
2826
cmark_llist *syntax_extensions_list = cmark_plugin_steal_syntax_extensions(plugin),
2927
*it;
3028

31-
initialize_mutex_once(&extensions_lock, &extensions_latch);
32-
pthread_mutex_lock(&extensions_lock);
29+
CMARK_INITIALIZE_AND_LOCK(extensions);
3330

3431
for (it = syntax_extensions_list; it; it = it->next) {
3532
syntax_extensions = cmark_llist_append(&CMARK_DEFAULT_MEM_ALLOCATOR, syntax_extensions, it->data);
3633
}
3734

38-
pthread_mutex_unlock(&extensions_lock);
35+
CMARK_UNLOCK(extensions);
3936

4037
cmark_llist_free(&CMARK_DEFAULT_MEM_ALLOCATOR, syntax_extensions_list);
4138
cmark_plugin_free(plugin);
4239
}
4340

4441
void cmark_release_plugins(void) {
45-
initialize_mutex_once(&extensions_lock, &extensions_latch);
46-
pthread_mutex_lock(&extensions_lock);
42+
CMARK_INITIALIZE_AND_LOCK(extensions);
4743

4844
if (syntax_extensions) {
4945
cmark_llist_free_full(
@@ -53,30 +49,28 @@ void cmark_release_plugins(void) {
5349
syntax_extensions = NULL;
5450
}
5551

56-
pthread_mutex_unlock(&extensions_lock);
52+
CMARK_UNLOCK(extensions);
5753
}
5854

5955
cmark_llist *cmark_list_syntax_extensions(cmark_mem *mem) {
6056
cmark_llist *it;
6157
cmark_llist *res = NULL;
6258

63-
initialize_mutex_once(&extensions_lock, &extensions_latch);
64-
pthread_mutex_lock(&extensions_lock);
59+
CMARK_INITIALIZE_AND_LOCK(extensions);
6560

6661
for (it = syntax_extensions; it; it = it->next) {
6762
res = cmark_llist_append(mem, res, it->data);
6863
}
6964

70-
pthread_mutex_unlock(&extensions_lock);
65+
CMARK_UNLOCK(extensions);
7166
return res;
7267
}
7368

7469
cmark_syntax_extension *cmark_find_syntax_extension(const char *name) {
7570
cmark_llist *tmp;
7671
cmark_syntax_extension *res = NULL;
7772

78-
initialize_mutex_once(&extensions_lock, &extensions_latch);
79-
pthread_mutex_lock(&extensions_lock);
73+
CMARK_INITIALIZE_AND_LOCK(extensions);
8074

8175
for (tmp = syntax_extensions; tmp; tmp = tmp->next) {
8276
cmark_syntax_extension *ext = (cmark_syntax_extension *) tmp->data;
@@ -86,6 +80,6 @@ cmark_syntax_extension *cmark_find_syntax_extension(const char *name) {
8680
}
8781
}
8882

89-
pthread_mutex_unlock(&extensions_lock);
83+
CMARK_UNLOCK(extensions);
9084
return res;
9185
}

0 commit comments

Comments
 (0)