cgen: format shared structs (#12062)

pull/12064/head
yuyi 2021-10-04 23:27:38 +08:00 committed by GitHub
parent 03269f9854
commit e94e08475d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 3 deletions

View File

@ -877,7 +877,10 @@ fn (mut g Gen) write_shareds() {
done_types << typ
sh_typ := '__shared__$base'
mtx_typ := 'sync__RwMutex'
g.shared_types.writeln('struct $sh_typ { $mtx_typ mtx; $base val; };')
g.shared_types.writeln('struct $sh_typ {')
g.shared_types.writeln('\t$mtx_typ mtx;')
g.shared_types.writeln('\t$base val;')
g.shared_types.writeln('};')
g.shared_functions.writeln('static inline voidptr __dup${sh_typ}(voidptr src, int sz) {')
g.shared_functions.writeln('\t$sh_typ* dest = memdup(src, sz);')
g.shared_functions.writeln('\tsync__RwMutex_init(&dest->mtx);')

View File

@ -23,14 +23,20 @@ const c_current_commit_hash_default = '
const c_concurrency_helpers = '
typedef struct __shared_map __shared_map;
struct __shared_map { sync__RwMutex mtx; map val; };
struct __shared_map {
sync__RwMutex mtx;
map val;
};
static inline voidptr __dup_shared_map(voidptr src, int sz) {
__shared_map* dest = memdup(src, sz);
sync__RwMutex_init(&dest->mtx);
return dest;
}
typedef struct __shared_array __shared_array;
struct __shared_array { sync__RwMutex mtx; array val; };
struct __shared_array {
sync__RwMutex mtx;
array val;
};
static inline voidptr __dup_shared_array(voidptr src, int sz) {
__shared_array* dest = memdup(src, sz);
sync__RwMutex_init(&dest->mtx);