Skip to content

Commit d62fa12

Browse files
add mutex initializer in new header
1 parent 39ae2a8 commit d62fa12

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

extensions/core-extensions.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
#include <pthread.h>
2+
#include <stdatomic.h>
23

34
#include "cmark-gfm-core-extensions.h"
45
#include "autolink.h"
6+
#include "mutex.h"
57
#include "strikethrough.h"
68
#include "table.h"
79
#include "tagfilter.h"
810
#include "tasklist.h"
911
#include "registry.h"
1012
#include "plugin.h"
1113

12-
pthread_mutex_t extensions_lock;
14+
static pthread_mutex_t extensions_lock;
15+
static atomic_int extensions_latch = 0;
1316

1417
static int core_extensions_registration(cmark_plugin *plugin) {
18+
initialize_mutex_once(&extensions_lock, &extensions_latch);
1519
pthread_mutex_lock(&extensions_lock);
1620
cmark_plugin_register_syntax_extension(plugin, create_table_extension());
1721
cmark_plugin_register_syntax_extension(plugin,
@@ -24,9 +28,11 @@ static int core_extensions_registration(cmark_plugin *plugin) {
2428
}
2529

2630
pthread_mutex_t registered_lock;
31+
static atomic_int registered_latch = 0;
2732
static _Atomic int registered = 0;
2833

2934
void cmark_gfm_core_extensions_ensure_registered(void) {
35+
initialize_mutex_once(&registered_lock, &registered_latch);
3036
pthread_mutex_lock(&registered_lock);
3137
if (!registered) {
3238
cmark_register_plugin(core_extensions_registration);

src/include/mutex.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef CMARK_MUTEX_H
2+
#define CMARK_MUTEX_H
3+
4+
#include <pthread.h>
5+
#include <stdatomic.h>
6+
7+
#include "cmark-gfm_config.h"
8+
9+
static CMARK_INLINE void initialize_mutex_once(pthread_mutex_t *m, atomic_int *latch) {
10+
int expected = 0;
11+
12+
if (atomic_compare_exchange_strong(latch, &expected, 1)) {
13+
pthread_mutex_init(m, NULL);
14+
}
15+
}
16+
17+
#endif

src/inlines.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <string.h>
33
#include <stdio.h>
44
#include <pthread.h>
5+
#include <stdatomic.h>
56

67
#include "cmark_ctype.h"
78
#include "cmark-gfm_config.h"
@@ -14,6 +15,7 @@
1415
#include "scanners.h"
1516
#include "inlines.h"
1617
#include "syntax_extension.h"
18+
#include "mutex.h"
1719

1820
static const char *EMDASH = "\xE2\x80\x94";
1921
static const char *ENDASH = "\xE2\x80\x93";
@@ -65,7 +67,10 @@ typedef struct subject{
6567
} subject;
6668

6769
// Extensions may populate this.
68-
static _Atomic int8_t SKIP_CHARS[256];
70+
static int8_t SKIP_CHARS[256];
71+
72+
pthread_mutex_t chars_lock;
73+
static atomic_int chars_latch = 0;
6974

7075
static CMARK_INLINE bool S_is_line_end_char(char c) {
7176
return (c == '\n' || c == '\r');
@@ -405,6 +410,10 @@ static int scan_delims(subject *subj, unsigned char c, bool *can_open,
405410
before_char = 10;
406411
} else {
407412
before_char_pos = subj->pos - 1;
413+
414+
initialize_mutex_once(&chars_lock, &chars_latch);
415+
pthread_mutex_lock(&chars_lock);
416+
408417
// walk back to the beginning of the UTF_8 sequence:
409418
while ((peek_at(subj, before_char_pos) >> 6 == 2 || SKIP_CHARS[peek_at(subj, before_char_pos)]) && before_char_pos > 0) {
410419
before_char_pos -= 1;
@@ -414,6 +423,8 @@ static int scan_delims(subject *subj, unsigned char c, bool *can_open,
414423
if (len == -1 || (before_char < 256 && SKIP_CHARS[(unsigned char) before_char])) {
415424
before_char = 10;
416425
}
426+
427+
pthread_mutex_unlock(&chars_lock);
417428
}
418429

419430
if (c == '\'' || c == '"') {
@@ -430,14 +441,20 @@ static int scan_delims(subject *subj, unsigned char c, bool *can_open,
430441
after_char = 10;
431442
} else {
432443
after_char_pos = subj->pos;
444+
445+
initialize_mutex_once(&chars_lock, &chars_latch);
446+
pthread_mutex_lock(&chars_lock);
447+
433448
while (SKIP_CHARS[peek_at(subj, after_char_pos)] && after_char_pos < subj->input.len) {
434449
after_char_pos += 1;
435450
}
436451
len = cmark_utf8proc_iterate(subj->input.data + after_char_pos,
437452
subj->input.len - after_char_pos, &after_char);
438453
if (len == -1 || (after_char < 256 && SKIP_CHARS[(unsigned char) after_char])) {
439-
after_char = 10;
440-
}
454+
after_char = 10;
455+
}
456+
457+
pthread_mutex_unlock(&chars_lock);
441458
}
442459

443460
left_flanking = numdelims > 0 && !cmark_utf8proc_is_space(after_char) &&
@@ -1350,8 +1367,6 @@ static char SMART_PUNCT_CHARS[] = {
13501367
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
13511368
};
13521369

1353-
pthread_mutex_t chars_lock;
1354-
13551370
static bufsize_t subject_find_special_char(subject *subj, int options) {
13561371
bufsize_t n = subj->pos + 1;
13571372

@@ -1367,6 +1382,7 @@ static bufsize_t subject_find_special_char(subject *subj, int options) {
13671382
}
13681383

13691384
void cmark_inlines_add_special_character(unsigned char c, bool emphasis) {
1385+
initialize_mutex_once(&chars_lock, &chars_latch);
13701386
pthread_mutex_lock(&chars_lock);
13711387
SPECIAL_CHARS[c] = 1;
13721388
if (emphasis)
@@ -1375,6 +1391,7 @@ void cmark_inlines_add_special_character(unsigned char c, bool emphasis) {
13751391
}
13761392

13771393
void cmark_inlines_remove_special_character(unsigned char c, bool emphasis) {
1394+
initialize_mutex_once(&chars_lock, &chars_latch);
13781395
pthread_mutex_lock(&chars_lock);
13791396
SPECIAL_CHARS[c] = 0;
13801397
if (emphasis)

0 commit comments

Comments
 (0)