Skip to content

Commit 496832d

Browse files
make standard node flag initialization thread-safe
1 parent 5a3db92 commit 496832d

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/node.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
#include <string.h>
33

44
#include "cmark-gfm_config.h"
5+
#include "mutex.h"
56
#include "node.h"
67
#include "syntax_extension.h"
78

9+
CMARK_DEFINE_LOCK(shift)
10+
811
static void S_node_unlink(cmark_node *node);
912

1013
#define NODE_MEM(node) cmark_node_mem(node)
@@ -14,6 +17,8 @@ cmark_node__internal_flags CMARK_NODE__LAST_LINE_BLANK;
1417
cmark_node__internal_flags CMARK_NODE__LAST_LINE_CHECKED;
1518

1619
void cmark_register_node_flag(cmark_node__internal_flags *flags) {
20+
CMARK_INITIALIZE_AND_LOCK(shift);
21+
1722
static uint8_t shift = 0;
1823

1924
// flags should be a pointer to a global variable and this function
@@ -31,16 +36,20 @@ void cmark_register_node_flag(cmark_node__internal_flags *flags) {
3136

3237
*flags = (cmark_node__internal_flags)1 << shift;
3338
shift++;
39+
40+
CMARK_UNLOCK(shift);
41+
}
42+
43+
CMARK_DEFINE_ONCE(initialized);
44+
45+
static void initialize_standard_flags(void) {
46+
cmark_register_node_flag(&CMARK_NODE__OPEN);
47+
cmark_register_node_flag(&CMARK_NODE__LAST_LINE_BLANK);
48+
cmark_register_node_flag(&CMARK_NODE__LAST_LINE_CHECKED);
3449
}
3550

3651
void cmark_init_standard_node_flags() {
37-
static int initialized = 0;
38-
if (!initialized) {
39-
initialized = 1;
40-
cmark_register_node_flag(&CMARK_NODE__OPEN);
41-
cmark_register_node_flag(&CMARK_NODE__LAST_LINE_BLANK);
42-
cmark_register_node_flag(&CMARK_NODE__LAST_LINE_CHECKED);
43-
}
52+
CMARK_RUN_ONCE(initialized, initialize_standard_flags);
4453
}
4554

4655
bool cmark_node_can_contain_type(cmark_node *node, cmark_node_type child_type) {

0 commit comments

Comments
 (0)