1
+ #include <stdatomic.h>
1
2
#include <stdint.h>
2
3
#include <stdlib.h>
3
4
#include <string.h>
4
5
5
6
#include "cmark-gfm_config.h"
6
7
#include "cmark-gfm.h"
8
+ #include "mutex.h"
7
9
#include "syntax_extension.h"
8
10
#include "registry.h"
9
11
#include "plugin.h"
@@ -12,6 +14,9 @@ extern cmark_mem CMARK_DEFAULT_MEM_ALLOCATOR;
12
14
13
15
static cmark_llist * syntax_extensions = NULL ;
14
16
17
+ static pthread_mutex_t extensions_lock ;
18
+ static atomic_int extensions_latch = 0 ;
19
+
15
20
void cmark_register_plugin (cmark_plugin_init_func reg_fn ) {
16
21
cmark_plugin * plugin = cmark_plugin_new ();
17
22
@@ -23,41 +28,64 @@ void cmark_register_plugin(cmark_plugin_init_func reg_fn) {
23
28
cmark_llist * syntax_extensions_list = cmark_plugin_steal_syntax_extensions (plugin ),
24
29
* it ;
25
30
31
+ initialize_mutex_once (& extensions_lock , & extensions_latch );
32
+ pthread_mutex_lock (& extensions_lock );
33
+
26
34
for (it = syntax_extensions_list ; it ; it = it -> next ) {
27
35
syntax_extensions = cmark_llist_append (& CMARK_DEFAULT_MEM_ALLOCATOR , syntax_extensions , it -> data );
28
36
}
37
+
38
+ pthread_mutex_unlock (& extensions_lock );
29
39
30
40
cmark_llist_free (& CMARK_DEFAULT_MEM_ALLOCATOR , syntax_extensions_list );
31
41
cmark_plugin_free (plugin );
32
42
}
33
43
34
44
void cmark_release_plugins (void ) {
45
+ initialize_mutex_once (& extensions_lock , & extensions_latch );
46
+ pthread_mutex_lock (& extensions_lock );
47
+
35
48
if (syntax_extensions ) {
36
49
cmark_llist_free_full (
37
50
& CMARK_DEFAULT_MEM_ALLOCATOR ,
38
51
syntax_extensions ,
39
52
(cmark_free_func ) cmark_syntax_extension_free );
40
53
syntax_extensions = NULL ;
41
54
}
55
+
56
+ pthread_mutex_unlock (& extensions_lock );
42
57
}
43
58
44
59
cmark_llist * cmark_list_syntax_extensions (cmark_mem * mem ) {
45
60
cmark_llist * it ;
46
61
cmark_llist * res = NULL ;
47
62
63
+ initialize_mutex_once (& extensions_lock , & extensions_latch );
64
+ pthread_mutex_lock (& extensions_lock );
65
+
48
66
for (it = syntax_extensions ; it ; it = it -> next ) {
49
67
res = cmark_llist_append (mem , res , it -> data );
50
68
}
69
+
70
+ pthread_mutex_unlock (& extensions_lock );
51
71
return res ;
52
72
}
53
73
54
74
cmark_syntax_extension * cmark_find_syntax_extension (const char * name ) {
55
75
cmark_llist * tmp ;
76
+ cmark_syntax_extension * res = NULL ;
56
77
78
+ initialize_mutex_once (& extensions_lock , & extensions_latch );
79
+ pthread_mutex_lock (& extensions_lock );
80
+
57
81
for (tmp = syntax_extensions ; tmp ; tmp = tmp -> next ) {
58
82
cmark_syntax_extension * ext = (cmark_syntax_extension * ) tmp -> data ;
59
- if (!strcmp (ext -> name , name ))
60
- return ext ;
83
+ if (!strcmp (ext -> name , name )) {
84
+ res = ext ;
85
+ break ;
86
+ }
61
87
}
62
- return NULL ;
88
+
89
+ pthread_mutex_unlock (& extensions_lock );
90
+ return res ;
63
91
}
0 commit comments