Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3777c89

Browse files
committed
Demo of Simultaneous accesses
1 parentc99df0e commit3777c89

File tree

3 files changed

+162
-3
lines changed

3 files changed

+162
-3
lines changed

‎SwiftJava.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
BB5ACFDF1D35B09900E2386A /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB5ACFDE1D35B09900E2386A /* AppDelegate.swift */; };
1313
BB5ACFE11D35B09900E2386A /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = BB5ACFE01D35B09900E2386A /* Assets.xcassets */; };
1414
BB5ACFE41D35B09900E2386A /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = BB5ACFE21D35B09900E2386A /* MainMenu.xib */; };
15+
BB63C9E51F7970F00048D83B /* Fortify.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB63C9E41F7970EE0048D83B /* Fortify.swift */; };
1516
BB643B321F4873F7003E849E /* swift-android-gradle in Resources */ = {isa = PBXBuildFile; fileRef = BB643B311F4873F7003E849E /* swift-android-gradle */; };
1617
BB6651321F2E39F7004EFD5F /* swift-android-kotlin in Resources */ = {isa = PBXBuildFile; fileRef = BB6651301F2E39F6004EFD5F /* swift-android-kotlin */; };
1718
BB6651361F2E3F51004EFD5F /* android_toolchain in Resources */ = {isa = PBXBuildFile; fileRef = BB6651351F2E3F50004EFD5F /* android_toolchain */; };
@@ -226,6 +227,7 @@
226227
BB5ACFE01D35B09900E2386A /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
227228
BB5ACFE31D35B09900E2386A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/MainMenu.xib; sourceTree = "<group>"; };
228229
BB5ACFE51D35B09900E2386A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
230+
BB63C9E41F7970EE0048D83B /* Fortify.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Fortify.swift; sourceTree = "<group>"; };
229231
BB643B311F4873F7003E849E /* swift-android-gradle */ = {isa = PBXFileReference; lastKnownFileType = folder; path = "swift-android-gradle"; sourceTree = "<group>"; };
230232
BB6651301F2E39F6004EFD5F /* swift-android-kotlin */ = {isa = PBXFileReference; lastKnownFileType = folder; path = "swift-android-kotlin"; sourceTree = "<group>"; };
231233
BB6651351F2E3F50004EFD5F /* android_toolchain */ = {isa = PBXFileReference; lastKnownFileType = folder; path = android_toolchain; sourceTree = "<group>"; };
@@ -435,6 +437,7 @@
435437
BBC13DB71D48FDCE0035A039 /* org_swiftjava */ = {
436438
isa = PBXGroup;
437439
children = (
440+
BB63C9E41F7970EE0048D83B /* Fortify.swift */,
438441
CE4A251C1F355B0A003AF384 /* SwiftTest.java */,
439442
CE31850E1F368D44002046A6 /* SwiftHelloTypes_TextListener.swift */,
440443
CE4A25141F355902003AF384 /* SwiftHelloTest_TestResponderAdapter.swift */,
@@ -807,6 +810,7 @@
807810
BBC13DB91D48FDCE0035A039 /* org_swiftjava.swift in Sources */,
808811
CE4A25211F355CAC003AF384 /* SwiftHelloTest_TestListener.swift in Sources */,
809812
CE4A25191F355910003AF384 /* SwiftHelloTest_SwiftTestListener.swift in Sources */,
813+
BB63C9E51F7970F00048D83B /* Fortify.swift in Sources */,
810814
BBC13DC21D4900AA0035A039 /* SwiftTest.swift in Sources */,
811815
CE3185101F368D6B002046A6 /* SwiftHelloTypes_TextListener.swift in Sources */,
812816
CE4A251A1F355914003AF384 /* SwiftHelloTest_TestResponderAdapter.swift in Sources */,

‎org_swiftjava/Fortify.swift

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
//
2+
// Fortify.swift
3+
// Fortify
4+
//
5+
// Created by John Holdsworth on 19/09/2017.
6+
// Copyright © 2017 John Holdsworth. All rights reserved.
7+
//
8+
// Currently requires patched Swift toolchain from here:
9+
// http://johnholdsworth.com/swift-LOCAL-2017-09-20-a-osx.tar.gz
10+
//
11+
12+
import Foundation
13+
14+
openclassThreadLocal{
15+
publicrequiredinit(){
16+
}
17+
18+
publicclassfunc getThreadLocal<T:ThreadLocal>(ofClass:T.Type,
19+
keyVar:UnsafeMutablePointer<pthread_key_t>)->T{
20+
letneedsKey= keyVar.pointee==0
21+
if needsKey{
22+
letret=pthread_key_create(keyVar,{
23+
#if os(Linux)
24+
Unmanaged<ThreadLocal>.fromOpaque($0!).release()
25+
#else
26+
Unmanaged<ThreadLocal>.fromOpaque($0).release()
27+
#endif
28+
})
29+
if ret!=0{
30+
NSLog("Could not pthread_key_create: %s",strerror(ret))
31+
}
32+
}
33+
iflet existing=pthread_getspecific(keyVar.pointee){
34+
returnUnmanaged<T>.fromOpaque(existing).takeUnretainedValue()
35+
}
36+
else{
37+
letunmanaged=Unmanaged.passRetained(T())
38+
letret=pthread_setspecific(keyVar.pointee, unmanaged.toOpaque())
39+
if ret!=0{
40+
NSLog("Could not pthread_setspecific: %s",strerror(ret))
41+
}
42+
return unmanaged.takeUnretainedValue()
43+
}
44+
}
45+
}
46+
47+
@_silgen_name("setjmp")
48+
publicfunc setjump(_:UnsafeMutablePointer<jmp_buf>!)-> Int32
49+
50+
@_silgen_name("longjmp")
51+
publicfunc longjump(_:UnsafeMutablePointer<jmp_buf>!, _:Int32)-> Never
52+
53+
privatelet empty_buf=[UInt8](repeating:0, count: MemoryLayout<jmp_buf>.size)
54+
55+
openclass Fortify: ThreadLocal{
56+
57+
static privatevar pthreadKey: pthread_key_t=0
58+
59+
openclassvarthreadLocal:Fortify{
60+
returngetThreadLocal(ofClass:Fortify.self, keyVar:&pthreadKey)
61+
}
62+
63+
privatevarstack=[jmp_buf]()
64+
publicvarerror:Error?
65+
66+
// Required as Swift assumes it has control of the stack
67+
openclassfunc disableExclusivityChecking(){
68+
#if os(Android)
69+
letlibName="libswiftCore.so"
70+
#else
71+
letlibName:String?=nil
72+
#endif
73+
iflet stdlibHandle=dlopen(libName,Int32(RTLD_LAZY | RTLD_NOLOAD)),
74+
let disableExclusivity=dlsym(stdlibHandle,"_swift_disableExclusivityChecking"){
75+
disableExclusivity.assumingMemoryBound(to:Bool.self).pointee=true
76+
}
77+
else{
78+
NSLog("Could not disable exclusivity, failure likely...")
79+
}
80+
}
81+
82+
publicstaticletinstallHandlerOnce:Void={
83+
// _swift_stdlib_errorHandler = {
84+
// (prefix: StaticString, msg: String, file: StaticString,
85+
// line: UInt, flags: UInt32, config: Int32) in
86+
// escape(msg: msg, file: file, line: line)
87+
// }
88+
89+
disableExclusivityChecking()
90+
}()
91+
92+
openclassfunc exec<T>( block:()throws->T)throws->T{
93+
_= installHandlerOnce
94+
letlocal= threadLocal
95+
96+
empty_buf.withUnsafeBytes{
97+
letbuf_ptr= $0.baseAddress!.assumingMemoryBound(to: jmp_buf.self)
98+
local.stack.append(buf_ptr.pointee)
99+
}
100+
101+
defer{
102+
local.stack.removeLast()
103+
}
104+
105+
ifsetjump(&local.stack[local.stack.count-1])!=0{
106+
throw local.error??NSError(domain:"Error not available", code:-1, userInfo:nil)
107+
}
108+
109+
returntryblock()
110+
}
111+
112+
openclassfunc escape(msg:String, file:StaticString= #file, line:UInt= #line)->Never{
113+
escape(withError:NSError(domain: msg, code:-1, userInfo:[
114+
NSLocalizedDescriptionKey:"\(msg):\(file):\(line)",
115+
"msg": msg,"file": file,"line": line
116+
]))
117+
}
118+
119+
openclassfunc escape(withError error:Error)->Never{
120+
letlocal= threadLocal
121+
local.error= error
122+
123+
if local.stack.count==0{
124+
NSLog("escape without matching exec call:\(error)")
125+
#if !os(Linux)
126+
// pthread_exit(nil) just crashes
127+
varoldState:Int32=0
128+
pthread_setcancelstate(Int32(PTHREAD_CANCEL_ENABLE),&oldState)
129+
pthread_setcanceltype(Int32(PTHREAD_CANCEL_DEFERRED),&oldState)
130+
// pthread_cancel() never seems to be implemented
131+
letcancelled=pthread_cancel(pthread_self())
132+
if cancelled!=0{
133+
NSLog("pthread_cancel() failed: %s",strerror(cancelled))
134+
}
135+
sleep(1)
136+
#endif
137+
NSLog("cancel/exit not available/implemented or crashes, parking thread")
138+
Thread.sleep(until:Date.distantFuture)
139+
}
140+
141+
longjump(&local.stack[local.stack.count-1],1)
142+
NSLog("longjmp() failed, should not get here")
143+
}
144+
}

‎org_swiftjava/org_swiftjava.swift

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,21 @@ class org_swiftjava: XCTestCase {
2525
// Use XCTAssert and related functions to verify your tests produce the correct results.
2626

2727
// SwiftHelloTest_TestListenerLocal_.proxyClass()
28-
letinstance=SwiftHelloTest_TestResponderAdapter()
2928

30-
instance.loopback=SwiftTestListener()
31-
instance.loopback.setLoopback(loopback:SwiftHelloTest_TestResponderAdapter())
29+
// You'll get an assertion failure if
30+
// you comment the following line out.
31+
Fortify.disableExclusivityChecking()
32+
33+
letdest=SwiftTestListener()
34+
35+
letbounce2=SwiftHelloTest_TestResponderAdapter()
36+
bounce2.setLoopback(loopback: dest)
37+
38+
letbounce1=SwiftTestListener()
39+
bounce1.loopback= bounce2
40+
41+
letinstance=SwiftHelloTest_TestResponderAdapter()
42+
instance.setLoopback(loopback: bounce1)
3243

3344
iftrue{
3445
letreference:Bool=true

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp