2
2
#include <string.h>
3
3
#include <stdio.h>
4
4
#include <pthread.h>
5
+ #include <stdatomic.h>
5
6
6
7
#include "cmark_ctype.h"
7
8
#include "cmark-gfm_config.h"
14
15
#include "scanners.h"
15
16
#include "inlines.h"
16
17
#include "syntax_extension.h"
18
+ #include "mutex.h"
17
19
18
20
static const char * EMDASH = "\xE2\x80\x94" ;
19
21
static const char * ENDASH = "\xE2\x80\x93" ;
@@ -65,7 +67,10 @@ typedef struct subject{
65
67
} subject ;
66
68
67
69
// 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 ;
69
74
70
75
static CMARK_INLINE bool S_is_line_end_char (char c ) {
71
76
return (c == '\n' || c == '\r' );
@@ -405,6 +410,10 @@ static int scan_delims(subject *subj, unsigned char c, bool *can_open,
405
410
before_char = 10 ;
406
411
} else {
407
412
before_char_pos = subj -> pos - 1 ;
413
+
414
+ initialize_mutex_once (& chars_lock , & chars_latch );
415
+ pthread_mutex_lock (& chars_lock );
416
+
408
417
// walk back to the beginning of the UTF_8 sequence:
409
418
while ((peek_at (subj , before_char_pos ) >> 6 == 2 || SKIP_CHARS [peek_at (subj , before_char_pos )]) && before_char_pos > 0 ) {
410
419
before_char_pos -= 1 ;
@@ -414,6 +423,8 @@ static int scan_delims(subject *subj, unsigned char c, bool *can_open,
414
423
if (len == -1 || (before_char < 256 && SKIP_CHARS [(unsigned char ) before_char ])) {
415
424
before_char = 10 ;
416
425
}
426
+
427
+ pthread_mutex_unlock (& chars_lock );
417
428
}
418
429
419
430
if (c == '\'' || c == '"' ) {
@@ -430,14 +441,20 @@ static int scan_delims(subject *subj, unsigned char c, bool *can_open,
430
441
after_char = 10 ;
431
442
} else {
432
443
after_char_pos = subj -> pos ;
444
+
445
+ initialize_mutex_once (& chars_lock , & chars_latch );
446
+ pthread_mutex_lock (& chars_lock );
447
+
433
448
while (SKIP_CHARS [peek_at (subj , after_char_pos )] && after_char_pos < subj -> input .len ) {
434
449
after_char_pos += 1 ;
435
450
}
436
451
len = cmark_utf8proc_iterate (subj -> input .data + after_char_pos ,
437
452
subj -> input .len - after_char_pos , & after_char );
438
453
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 );
441
458
}
442
459
443
460
left_flanking = numdelims > 0 && !cmark_utf8proc_is_space (after_char ) &&
@@ -1350,8 +1367,6 @@ static char SMART_PUNCT_CHARS[] = {
1350
1367
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
1351
1368
};
1352
1369
1353
- pthread_mutex_t chars_lock ;
1354
-
1355
1370
static bufsize_t subject_find_special_char (subject * subj , int options ) {
1356
1371
bufsize_t n = subj -> pos + 1 ;
1357
1372
@@ -1367,6 +1382,7 @@ static bufsize_t subject_find_special_char(subject *subj, int options) {
1367
1382
}
1368
1383
1369
1384
void cmark_inlines_add_special_character (unsigned char c , bool emphasis ) {
1385
+ initialize_mutex_once (& chars_lock , & chars_latch );
1370
1386
pthread_mutex_lock (& chars_lock );
1371
1387
SPECIAL_CHARS [c ] = 1 ;
1372
1388
if (emphasis )
@@ -1375,6 +1391,7 @@ void cmark_inlines_add_special_character(unsigned char c, bool emphasis) {
1375
1391
}
1376
1392
1377
1393
void cmark_inlines_remove_special_character (unsigned char c , bool emphasis ) {
1394
+ initialize_mutex_once (& chars_lock , & chars_latch );
1378
1395
pthread_mutex_lock (& chars_lock );
1379
1396
SPECIAL_CHARS [c ] = 0 ;
1380
1397
if (emphasis )
0 commit comments