1
+ #include <pthread.h>
2
+ #include <stdatomic.h>
1
3
#include <stdlib.h>
2
4
#include <string.h>
3
5
#include <stdint.h>
4
6
#include "cmark-gfm.h"
5
7
#include "cmark-gfm-extension_api.h"
8
+ #include "mutex.h"
9
+
10
+ static pthread_mutex_t arena_lock ;
11
+ static atomic_int arena_latch = 0 ;
6
12
7
13
static struct arena_chunk {
8
14
size_t sz , used ;
@@ -24,10 +30,13 @@ static struct arena_chunk *alloc_arena_chunk(size_t sz, struct arena_chunk *prev
24
30
}
25
31
26
32
void cmark_arena_push (void ) {
33
+ initialize_mutex_once (& arena_lock , & arena_latch );
34
+ pthread_mutex_lock (& arena_lock );
27
35
if (!A )
28
36
return ;
29
37
A -> push_point = 1 ;
30
38
A = alloc_arena_chunk (10240 , A );
39
+ pthread_mutex_unlock (& arena_lock );
31
40
}
32
41
33
42
int cmark_arena_pop (void ) {
@@ -68,6 +77,9 @@ static void *arena_calloc(size_t nmem, size_t size) {
68
77
const size_t align = sizeof (size_t ) - 1 ;
69
78
sz = (sz + align ) & ~align ;
70
79
80
+ // the arena lock will have already been set up by a previous call to init_arena
81
+ pthread_mutex_lock (& arena_lock );
82
+
71
83
if (sz > A -> sz ) {
72
84
A -> prev = alloc_arena_chunk (sz , A -> prev );
73
85
return (uint8_t * ) A -> prev -> ptr + sizeof (size_t );
@@ -77,6 +89,9 @@ static void *arena_calloc(size_t nmem, size_t size) {
77
89
}
78
90
void * ptr = (uint8_t * ) A -> ptr + A -> used ;
79
91
A -> used += sz ;
92
+
93
+ pthread_mutex_unlock (& arena_lock );
94
+
80
95
* ((size_t * ) ptr ) = sz - sizeof (size_t );
81
96
return (uint8_t * ) ptr + sizeof (size_t );
82
97
}
0 commit comments