Skip to content

Commit

Permalink
fix problem with attached(witouth detach) thread
Browse files Browse the repository at this point in the history
  • Loading branch information
kosyloa committed Mar 19, 2024
1 parent 16b7c94 commit 1b4d9aa
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
3 changes: 3 additions & 0 deletions DXFeedFramework/Native/Endpoint/NativeEndpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class NativeEndpoint {

private static let listenerCallback: dxfg_endpoint_state_change_listener_func = {_, oldState, newState, context in
if let context = context {
ThreadManager.insertPthread()
let currentThread = graal_get_current_thread(Isolate.shared.isolate.pointee)
print("change state \(currentThread) \(pthread_mach_thread_np(pthread_self()))")
let endpoint: AnyObject = bridge(ptr: context)
if let listener = endpoint as? WeakListener {
var old = (try? EnumUtil.valueOf(value: DXEndpointState.convert(oldState))) ?? .notConnected
Expand Down
18 changes: 17 additions & 1 deletion DXFeedFramework/Native/Graal/ThreadManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,27 @@ class ThreadManager {
private static let kThreadKey = "GraalThread"
private static let key = UnsafeMutablePointer<pthread_key_t>.allocate(capacity: 1)
static var str1: String = ""
private static var graalThreads = Set<String>()

private init() {
pthread_key_create(ThreadManager.key) { pointer in
pointer.withMemoryRebound(to: OpaquePointer.self, capacity: 1) { pointer1 in
print("deinit thread \(Thread.isMainThread) \(Thread.current) \(Thread.current.threadName) \(pthread_mach_thread_np(pthread_self())) \(pointer1)")

let currentThread = graal_get_current_thread(Isolate.shared.isolate.pointee)
// The call to this method has been removed.
// In some cases: an attachment to a java thread crashes when you try to use this thread (deinit in this java thread)
graal_detach_thread(pointer1.pointee)
print("pthread_key_create")
if !ThreadManager.containsPthread() {
graal_detach_thread(pointer1.pointee)
}
print("pthread_key_create1")
pointer.deallocate()
print("pthread_key_create2")
}
}
}

fileprivate func currentThread() -> OpaquePointer!{
defer {
objc_sync_exit(self)
Expand All @@ -61,6 +69,14 @@ class ThreadManager {
return threadPointer
}

static func insertPthread() {
graalThreads.insert("\(pthread_mach_thread_np(pthread_self()))")
}

static func containsPthread() -> Bool {
ThreadManager.graalThreads.contains("\(pthread_mach_thread_np(pthread_self()))")
}

}

internal extension Thread {
Expand Down
23 changes: 23 additions & 0 deletions DXFeedFrameworkTests/PublisherTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ final class PublisherTest: XCTestCase {

func testCreatePublisher() throws {
try execute()
wait(seconds: 2)
}

func execute() throws {
Expand All @@ -41,11 +42,20 @@ final class PublisherTest: XCTestCase {
.build()
let publisher = endpoint?.getPublisher()
let connectedExpectation = expectation(description: "Connected")
DispatchQueue.global(qos: .background).asyncAfter(deadline: .now() + 0.3) {
print("\(pthread_mach_thread_np(pthread_self()))")
print(Thread.current.threadName)
}
DispatchQueue.global(qos: .background).asyncAfter(deadline: .now() + 0.3) {
print("\(pthread_mach_thread_np(pthread_self()))")
print(Thread.current.threadName)
}
let stateListener: TestEndpoointStateListener? = TestEndpoointStateListener { listener in
listener.callback = { state in
if state == .connected {
connectedExpectation.fulfill()
DispatchQueue.global(qos: .background).asyncAfter(deadline: .now() + 0.3) {
print("\(pthread_mach_thread_np(pthread_self()))")
print(Thread.current.threadName)
try? publisher?.publish(events: [testQuote])
}
Expand Down Expand Up @@ -75,4 +85,17 @@ final class PublisherTest: XCTestCase {
print("\(error)")
}
}

func testDEtachThread() {
// create isolate in main thread
try? SystemProperty.setProperty("test", "test")

var thread: Thread? = Thread {
// touch graal in background
try? SystemProperty.setProperty("test", "test")
}
thread?.start()
thread = nil
wait(seconds: 2)
}
}
1 change: 1 addition & 0 deletions Samples/QuoteTableApp/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// var thread: Thread? = Thread {
// var endpoint = try? DXEndpoint.create()
// print("execute1 in new thread \(pthread_mach_thread_np(pthread_self()))")
// try? SystemProperty.setProperty("test", "test")
// try? SystemProperty.setProperty("test", "test")
Expand Down

0 comments on commit 1b4d9aa

Please sign in to comment.