Skip to content

Commit cd6c5cf

Browse files
committed
Mutex C impl
1 parent 16cfac8 commit cd6c5cf

File tree

8 files changed

+131
-73
lines changed

8 files changed

+131
-73
lines changed

Sources/_SwiftSyntaxCShims/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
set(target ${SWIFTSYNTAX_TARGET_NAMESPACE}_SwiftSyntaxCShims)
22
add_library(${target} STATIC
3-
PlatformMutex.cpp
3+
PlatformMutex.c
44
)
55
target_include_directories(${target} PUBLIC "include")
66
set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS ${target})
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "PlatformMutex.h"
14+
#include <stdlib.h>
15+
16+
#if defined(__APPLE__)
17+
#include <os/lock.h>
18+
19+
PlatformMutex swiftsyntax_platform_mutex_create() {
20+
PlatformMutex m;
21+
m.opaque = malloc(sizeof(os_unfair_lock));
22+
*(os_unfair_lock_t)m.opaque = OS_UNFAIR_LOCK_INIT;
23+
return m;
24+
}
25+
26+
void swiftsyntax_platform_mutex_lock(PlatformMutex m) {
27+
os_unfair_lock_lock(m.opaque);
28+
}
29+
30+
void swiftsyntax_platform_mutex_unlock(PlatformMutex m) {
31+
os_unfair_lock_unlock(m.opaque);
32+
}
33+
34+
void swiftsyntax_platform_mutex_destroy(PlatformMutex m) {
35+
free(m.opaque);
36+
}
37+
38+
#elif defined(_WIN32)
39+
#include <synchapi.h>
40+
41+
PlatformMutex swiftsyntax_platform_mutex_create() {
42+
PlatformMutex m;
43+
m.opaque = malloc(sizeof(SRWLOCK));
44+
InitializeSRWLock(m.opaque);
45+
return m;
46+
}
47+
48+
void swiftsyntax_platform_mutex_lock(PlatformMutex m) {
49+
AcquireSRWLockExclusive(m.opaque);
50+
}
51+
52+
void swiftsyntax_platform_mutex_unlock(PlatformMutex m) {
53+
ReleaseSRWLockExclusive(m.opaque);
54+
}
55+
56+
void swiftsyntax_platform_mutex_destroy(PlatformMutex m) {
57+
free(m.opaque);
58+
}
59+
60+
#elif __has_include(<pthread.h>)
61+
#include <pthread.h>
62+
63+
PlatformMutex swiftsyntax_platform_mutex_create() {
64+
PlatformMutex m;
65+
m.opaque = malloc(sizeof(pthread_mutex_t));
66+
pthread_mutex_init(m.opaque, 0);
67+
return m;
68+
}
69+
70+
void swiftsyntax_platform_mutex_lock(PlatformMutex m) {
71+
pthread_mutex_lock(m.opaque);
72+
}
73+
74+
void swiftsyntax_platform_mutex_unlock(PlatformMutex m) {
75+
pthread_mutex_unlock(m.opaque);
76+
}
77+
78+
void swiftsyntax_platform_mutex_destroy(PlatformMutex m) {
79+
pthread_mutex_destroy(m.opaque);
80+
free(m.opaque);
81+
}
82+
83+
#else
84+
#warning "platfrom mutex implementation is not available. Assuming single thread"
85+
86+
PlatformMutex swiftsyntax_platform_mutex_create() {
87+
PlatformMutex m;
88+
m.opaque = 0;
89+
return m;
90+
}
91+
92+
void swiftsyntax_platform_mutex_lock(PlatformMutex m) {}
93+
void swiftsyntax_platform_mutex_unlock(PlatformMutex m) {}
94+
void swiftsyntax_platform_mutex_destroy(PlatformMutex m) {}
95+
96+
#endif

Sources/_SwiftSyntaxCShims/PlatformMutex.cpp

Lines changed: 0 additions & 57 deletions
This file was deleted.

Sources/_SwiftSyntaxCShims/include/PlatformMutex.h

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,22 @@
1313
#ifndef SWIFTSYNTAX_PLATFORMMUTEX_H
1414
#define SWIFTSYNTAX_PLATFORMMUTEX_H
1515

16-
#ifdef __cplusplus
17-
extern "C" {
18-
#endif
16+
#include "_bridging.h"
1917

2018
typedef struct PlatformMutex {
2119
void *opaque;
2220
} PlatformMutex;
2321

24-
__attribute__((swift_name("PlatformMutex.create()")))
22+
SWIFT_NAME_S("PlatformMutex.create()")
2523
PlatformMutex swiftsyntax_platform_mutex_create(void);
2624

27-
__attribute__((swift_name("PlatformMutex.lock(self:)")))
25+
SWIFT_NAME_S("PlatformMutex.lock(self:)")
2826
void swiftsyntax_platform_mutex_lock(PlatformMutex m);
2927

30-
__attribute__((swift_name("PlatformMutex.unlock(self:)")))
28+
SWIFT_NAME_S("PlatformMutex.unlock(self:)")
3129
void swiftsyntax_platform_mutex_unlock(PlatformMutex m);
3230

33-
__attribute__((swift_name("PlatformMutex.destroy(self:)")))
31+
SWIFT_NAME_S("PlatformMutex.destroy(self:)")
3432
void swiftsyntax_platform_mutex_destroy(PlatformMutex m);
3533

36-
#ifdef __cplusplus
37-
} // extern "C"
38-
#endif
39-
4034
#endif // SWIFTSYNTAX_PLATFORMMUTEX_H
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef SWIFTSYNTAX_BRIDGING_H
14+
#define SWIFTSYNTAX_BRIDGING_H
15+
16+
#if __has_attribute(swift_name)
17+
#define SWIFT_NAME_S(NAME) __attribute__((swift_name(NAME)))
18+
#else
19+
#define SWIFT_NAME_S(NAME)
20+
#endif
21+
22+
#endif // SWIFTSYNTAX_BRIDGING_H

Sources/_SwiftSyntaxCShims/include/module.modulemap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@ module _SwiftSyntaxCShims {
44
header "PlatformMutex.h"
55
header "swiftsyntax_errno.h"
66
header "swiftsyntax_stdio.h"
7+
8+
textual header "_bridging.h"
9+
710
export *
811
}

Sources/_SwiftSyntaxCShims/include/swiftsyntax_errno.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
#include <errno.h>
1717

18-
__attribute__((swift_name("getter:_errno()")))
18+
SWIFT_NAME_S("getter:_errno()")
1919
static inline int swiftsyntax_errno(void) {
2020
return errno;
2121
}

Sources/_SwiftSyntaxCShims/include/swiftsyntax_stdio.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@
1515

1616
#include <stdio.h>
1717

18-
__attribute__((swift_name("getter:_stdout()")))
18+
SWIFT_NAME_S("getter:_stdout()")
1919
static inline FILE *swiftsyntax_stdout(void) {
2020
return stdout;
2121
}
2222

23-
__attribute__((swift_name("getter:_stdin()")))
23+
SWIFT_NAME_S("getter:_stdin()")
2424
static inline FILE *swiftsyntax_stdin(void) {
2525
return stdin;
2626
}
2727

28-
__attribute__((swift_name("getter:_stderr()")))
28+
SWIFT_NAME_S("getter:_stderr()")
2929
static inline FILE *swiftsyntax_stderr(void) {
3030
return stderr;
3131
}

0 commit comments

Comments
 (0)