File tree Expand file tree Collapse file tree 2 files changed +28
-13
lines changed Expand file tree Collapse file tree 2 files changed +28
-13
lines changed Original file line number Diff line number Diff line change 8
8
#include "registry.h"
9
9
#include "plugin.h"
10
10
11
- CMARK_DEFINE_LOCK (extensions );
12
-
13
11
static int core_extensions_registration (cmark_plugin * plugin ) {
14
- CMARK_INITIALIZE_AND_LOCK (extensions );
15
12
cmark_plugin_register_syntax_extension (plugin , create_table_extension ());
16
13
cmark_plugin_register_syntax_extension (plugin ,
17
14
create_strikethrough_extension ());
18
15
cmark_plugin_register_syntax_extension (plugin , create_autolink_extension ());
19
16
cmark_plugin_register_syntax_extension (plugin , create_tagfilter_extension ());
20
17
cmark_plugin_register_syntax_extension (plugin , create_tasklist_extension ());
21
- CMARK_UNLOCK (extensions );
22
18
return 1 ;
23
19
}
24
20
25
- pthread_mutex_t registered_lock ;
26
- static atomic_int registered_latch = 0 ;
27
- static _Atomic int registered = 0 ;
21
+ CMARK_DEFINE_LATCH (registered );
28
22
29
23
void cmark_gfm_core_extensions_ensure_registered (void ) {
30
- CMARK_INITIALIZE_AND_LOCK (extensions );
31
- if (!registered ) {
24
+ if (CMARK_CHECK_LATCH (registered )) {
32
25
cmark_register_plugin (core_extensions_registration );
33
- registered = 1 ;
34
26
}
35
- CMARK_UNLOCK (extensions );
36
27
}
Original file line number Diff line number Diff line change 8
8
#include <pthread.h>
9
9
#include <stdatomic.h>
10
10
11
- static CMARK_INLINE void initialize_mutex_once ( pthread_mutex_t * m , atomic_int * latch ) {
11
+ static CMARK_INLINE bool check_latch ( atomic_int * latch ) {
12
12
int expected = 0 ;
13
-
14
13
if (atomic_compare_exchange_strong (latch , & expected , 1 )) {
14
+ return true;
15
+ } else {
16
+ return false;
17
+ }
18
+ }
19
+
20
+ static CMARK_INLINE void initialize_mutex_once (pthread_mutex_t * m , atomic_int * latch ) {
21
+ if (check_latch (latch )) {
15
22
pthread_mutex_init (m , NULL );
16
23
}
17
24
}
@@ -26,12 +33,29 @@ pthread_mutex_lock(&NAME##_lock);
26
33
27
34
#define CMARK_UNLOCK (NAME ) pthread_mutex_unlock(&NAME##_lock);
28
35
36
+ #define CMARK_DEFINE_LATCH (NAME ) static atomic_int NAME = 0;
37
+
38
+ #define CMARK_CHECK_LATCH (NAME ) check_latch(&NAME)
39
+
29
40
#else // no threading support
30
41
42
+ static CMARK_INLINE bool check_latch (int * latch ) {
43
+ if (!* latch ) {
44
+ * latch = 1 ;
45
+ return true;
46
+ } else {
47
+ return false;
48
+ }
49
+ }
50
+
31
51
#define CMARK_DEFINE_LOCK (NAME )
32
52
#define CMARK_INITIALIZE_AND_LOCK (NAME )
33
53
#define CMARK_UNLOCK (NAME )
34
54
55
+ #define CMARK_DEFINE_LATCH static int NAME = 0;
56
+
57
+ #define CMARK_CHECK_LATCH check_latch(&NAME)
58
+
35
59
#endif // CMARK_THREADING
36
60
37
61
#endif // CMARK_MUTEX_H
You can’t perform that action at this time.
0 commit comments