Skip to content

Commit e2fcd53

Browse files
committed
[PlaygroundLogger] Use a wrapper static inline C function to get and set PGLThreadIsLogging.
This is to insulate from any issues which might arise because Swift may or may not handle `__thread` variables correctly.
1 parent 58f43da commit e2fcd53

File tree

3 files changed

+35
-25
lines changed

3 files changed

+35
-25
lines changed

PlaygroundLogger/PlaygroundLogger/LegacySupport/LegacyEntrypoints.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ fileprivate func legacySendDataStub(_: NSData) -> Void {
3636

3737
@_silgen_name("playground_log_hidden")
3838
public func legacyLog<T>(instance: T, name: String, id: Int, startLine: Int, endLine: Int, startColumn: Int, endColumn: Int) -> AnyObject? {
39-
guard !PGLThreadIsLogging else { return nil }
40-
PGLThreadIsLogging = true
41-
defer { PGLThreadIsLogging = false }
39+
guard !PGLGetThreadIsLogging() else { return nil }
40+
PGLSetThreadIsLogging(true)
41+
defer { PGLSetThreadIsLogging(false) }
4242

4343
let packet = LogPacket(describingResult: instance, named: name, withPolicy: .default, startLine: startLine, endLine: endLine, startColumn: startColumn, endColumn: endColumn)
4444

@@ -73,9 +73,9 @@ public func legacyLog<T>(instance: T, name: String, id: Int, startLine: Int, end
7373

7474
@_silgen_name ("playground_log_scope_entry")
7575
public func legacyLogScopeEntry(startLine: Int, endLine: Int, startColumn: Int, endColumn: Int) -> AnyObject? {
76-
guard !PGLThreadIsLogging else { return nil }
77-
PGLThreadIsLogging = true
78-
defer { PGLThreadIsLogging = false }
76+
guard !PGLGetThreadIsLogging() else { return nil }
77+
PGLSetThreadIsLogging(true)
78+
defer { PGLSetThreadIsLogging(false) }
7979

8080
let packet = LogPacket(scopeEntryWithStartLine: startLine, endLine: endLine, startColumn: startColumn, endColumn: endColumn)
8181

@@ -87,9 +87,9 @@ public func legacyLogScopeEntry(startLine: Int, endLine: Int, startColumn: Int,
8787

8888
@_silgen_name ("playground_log_scope_exit")
8989
public func legacyLogScopeExit(startLine: Int, endLine: Int, startColumn: Int, endColumn: Int) -> AnyObject? {
90-
guard !PGLThreadIsLogging else { return nil }
91-
PGLThreadIsLogging = true
92-
defer { PGLThreadIsLogging = false }
90+
guard !PGLGetThreadIsLogging() else { return nil }
91+
PGLSetThreadIsLogging(true)
92+
defer { PGLSetThreadIsLogging(false) }
9393

9494
let packet = LogPacket(scopeExitWithStartLine: startLine, endLine: endLine, startColumn: startColumn, endColumn: endColumn)
9595

@@ -101,9 +101,9 @@ public func legacyLogScopeExit(startLine: Int, endLine: Int, startColumn: Int, e
101101

102102
@_silgen_name ("playground_log_postprint")
103103
public func legacyLogPostPrint(startLine: Int, endLine: Int, startColumn: Int, endColumn: Int) -> AnyObject? {
104-
guard !PGLThreadIsLogging else { return nil }
105-
PGLThreadIsLogging = true
106-
defer { PGLThreadIsLogging = false }
104+
guard !PGLGetThreadIsLogging() else { return nil }
105+
PGLSetThreadIsLogging(true)
106+
defer { PGLSetThreadIsLogging(false) }
107107

108108
let printedString = Thread.current.threadDictionary[printedStringThreadDictionaryKey] as! String? ?? ""
109109

PlaygroundLogger/PlaygroundLogger/LoggerEntrypoints.swift

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ func logResult(_ result: Any,
1919
endLine: Int,
2020
startColumn: Int,
2121
endColumn: Int) {
22-
guard !PGLThreadIsLogging else { return }
23-
PGLThreadIsLogging = true
24-
defer { PGLThreadIsLogging = false }
22+
guard !PGLGetThreadIsLogging() else { return }
23+
PGLSetThreadIsLogging(true)
24+
defer { PGLSetThreadIsLogging(false) }
2525

2626
let packet = LogPacket(describingResult: result, named: name, withPolicy: .default, startLine: startLine, endLine: endLine, startColumn: startColumn, endColumn: endColumn)
2727

@@ -58,9 +58,9 @@ func logScopeEntry(startLine: Int,
5858
endLine: Int,
5959
startColumn: Int,
6060
endColumn: Int) {
61-
guard !PGLThreadIsLogging else { return }
62-
PGLThreadIsLogging = true
63-
defer { PGLThreadIsLogging = false }
61+
guard !PGLGetThreadIsLogging() else { return }
62+
PGLSetThreadIsLogging(true)
63+
defer { PGLSetThreadIsLogging(false) }
6464

6565
let packet = LogPacket(scopeEntryWithStartLine: startLine, endLine: endLine, startColumn: startColumn, endColumn: endColumn)
6666

@@ -74,9 +74,9 @@ func logScopeExit(startLine: Int,
7474
endLine: Int,
7575
startColumn: Int,
7676
endColumn: Int) {
77-
guard !PGLThreadIsLogging else { return }
78-
PGLThreadIsLogging = true
79-
defer { PGLThreadIsLogging = false }
77+
guard !PGLGetThreadIsLogging() else { return }
78+
PGLSetThreadIsLogging(true)
79+
defer { PGLSetThreadIsLogging(false) }
8080

8181
let packet = LogPacket(scopeExitWithStartLine: startLine, endLine: endLine, startColumn: startColumn, endColumn: endColumn)
8282

@@ -90,7 +90,7 @@ let printedStringThreadDictionaryKey: NSString = "org.swift.PlaygroundLogger.pri
9090

9191
func printHook(string: String) {
9292
// Don't store the printed string if we're already logging elsewhere in this thread.
93-
guard !PGLThreadIsLogging else { return }
93+
guard !PGLGetThreadIsLogging() else { return }
9494

9595
Thread.current.threadDictionary[printedStringThreadDictionaryKey] = string as NSString
9696
}
@@ -99,9 +99,9 @@ func logPostPrint(startLine: Int,
9999
endLine: Int,
100100
startColumn: Int,
101101
endColumn: Int) {
102-
guard !PGLThreadIsLogging else { return }
103-
PGLThreadIsLogging = true
104-
defer { PGLThreadIsLogging = false }
102+
guard !PGLGetThreadIsLogging() else { return }
103+
PGLSetThreadIsLogging(true)
104+
defer { PGLSetThreadIsLogging(false) }
105105

106106
guard let printedString = Thread.current.threadDictionary[printedStringThreadDictionaryKey] as! String? else {
107107
return

PlaygroundLogger/PlaygroundLogger/Utilities/PGLThreadIsLogging.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,14 @@ __BEGIN_DECLS
2424
/// themselves logged.
2525
extern __thread bool PGLThreadIsLogging;
2626

27+
/// A helper function to get the value of `PGLThreadIsLogging` safely from Swift.
28+
static inline bool PGLGetThreadIsLogging(void) {
29+
return PGLThreadIsLogging;
30+
}
31+
32+
/// A helper function to set the value of `PGLThreadIsLogging` safely from Swift.
33+
static inline void PGLSetThreadIsLogging(bool threadIsLogging) {
34+
PGLThreadIsLogging = threadIsLogging;
35+
}
36+
2737
__END_DECLS

0 commit comments

Comments
 (0)