Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change threadDictionary to NSMutableDictionary #1762

Merged
merged 1 commit into from
Nov 29, 2018

Conversation

omochi
Copy link
Contributor

@omochi omochi commented Nov 13, 2018

This PR changes type of Thread.threadDictionary to NSMutableDictionary from Dictionary<String, Any>.
The reasons are below.

  1. Because apple's implementation for macOS/iOS is so.
    A current implementation is hard to write code which can be shared between Linux and iOS.
    I need to do for my project which has server side and client app.

  2. Key which has String type is hard to avoid conflict among some independent modules.
    Though I can do by giving prefix like domain,
    but this needs more runtime access cost to test equality of key in dictionary.
    With NSMutableDictionary, I can use ObjectIdentifier or UnsafeRawPointer of static key object.
    It is more safe from unexpected key conflict and cheap to test equality.

In original source, comment what about a implementation differs from Apple's is written.

/// - Note: this differs from the Darwin implementation in that the keys must be Strings

But the reason of this is not written.
So I worry about some intention in it.

And if some reason about not able to use NSMutableDictionary,
I think that Dictionary<AnyHashable, Any> is better for my 2nd reason.

@millenomi
Copy link
Contributor

… yeah, it expresses as a NSMutableDictionary on Darwin as well. Thanks.

@millenomi
Copy link
Contributor

@swift-ci please test and merge

@millenomi
Copy link
Contributor

@omochi can you investigate the test failures?

@omochi
Copy link
Contributor Author

omochi commented Nov 14, 2018

OK. I investigate.

@omochi
Copy link
Contributor Author

omochi commented Nov 14, 2018

CI says this.

12:21:49 Test Case 'TestNotificationCenter.test_observeOnPostingQueue' started at 2018-11-13 18:21:31.191
12:21:49 Could not cast value of type 'XCTest.WaiterManager<XCTest.XCTWaiter>' (0x561eaf88d1f8) to 'Foundation.NSObject' (0x7fc55d90a188).

I guess an error from this line.
https://github.com/apple/swift-corelibs-xctest/blob/ffa81a2067500c4641a1043732590ce89218ddc9/Sources/XCTest/Private/WaiterManager.swift#L41

I am trying to reproduce test failure on Linux environment now.
But I am stopped by another problem about build swift compiler.
I work on ubuntu16.04 on Docker on macOS.

[1/333] Linking CXX executable bin/clang-check
FAILED: : && /usr/bin/clang++   -Wno-unknown-warning-option -Werror=unguarded-availability-new -g -fPIC -fvisibility-inlines-hidden -Werror=date-time -Werror=unguarded-availability-new -std=c++11 -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -pedantic -Wno-long-long -Wcovered-switch-default -Wno-class-memaccess -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wstring-conversion -fdiagnostics-color -fno-common -Woverloaded-virtual -Wno-nested-anon-types -g  -Wl,-allow-shlib-undefined    -Wl,-rpath-link,/Users/omochi/work/swift-source/build/Ninja-DebugAssert/llvm-linux-x86_64/./lib tools/clang/tools/clang-check/CMakeFiles/clang-check.dir/ClangCheck.cpp.o  -o bin/clang-check  lib/libLLVMX86CodeGen.a lib/libLLVMX86AsmParser.a lib/libLLVMX86AsmPrinter.a lib/libLLVMX86Desc.a lib/libLLVMX86Disassembler.a lib/libLLVMX86Info.a lib/libLLVMX86Utils.a lib/libLLVMARMCodeGen.a lib/libLLVMARMAsmParser.a lib/libLLVMARMAsmPrinter.a lib/libLLVMARMDesc.a lib/libLLVMARMDisassembler.a lib/libLLVMARMInfo.a lib/libLLVMARMUtils.a lib/libLLVMAArch64CodeGen.a lib/libLLVMAArch64AsmParser.a lib/libLLVMAArch64AsmPrinter.a lib/libLLVMAArch64Desc.a lib/libLLVMAArch64Disassembler.a lib/libLLVMAArch64Info.a lib/libLLVMAArch64Utils.a lib/libLLVMPowerPCCodeGen.a lib/libLLVMPowerPCAsmParser.a lib/libLLVMPowerPCAsmPrinter.a lib/libLLVMPowerPCDesc.a lib/libLLVMPowerPCDisassembler.a lib/libLLVMPowerPCInfo.a lib/libLLVMSystemZCodeGen.a lib/libLLVMSystemZAsmParser.a lib/libLLVMSystemZAsmPrinter.a lib/libLLVMSystemZDesc.a lib/libLLVMSystemZDisassembler.a lib/libLLVMSystemZInfo.a lib/libLLVMMipsCodeGen.a lib/libLLVMMipsAsmParser.a lib/libLLVMMipsAsmPrinter.a lib/libLLVMMipsDesc.a lib/libLLVMMipsDisassembler.a lib/libLLVMMipsInfo.a lib/libLLVMOption.a lib/libLLVMSupport.a -lpthread lib/libclangAPINotes.a lib/libclangAST.a lib/libclangBasic.a lib/libclangDriver.a lib/libclangFrontend.a lib/libclangRewriteFrontend.a lib/libclangStaticAnalyzerFrontend.a lib/libclangTooling.a lib/libLLVMX86AsmPrinter.a lib/libLLVMX86Utils.a lib/libLLVMARMDesc.a lib/libLLVMARMAsmPrinter.a lib/libLLVMARMInfo.a lib/libLLVMARMUtils.a lib/libLLVMAArch64Desc.a lib/libLLVMAArch64AsmPrinter.a lib/libLLVMAArch64Info.a lib/libLLVMAArch64Utils.a lib/libLLVMPowerPCAsmPrinter.a lib/libLLVMSystemZDesc.a lib/libLLVMSystemZAsmPrinter.a lib/libLLVMSystemZInfo.a lib/libLLVMAsmPrinter.a lib/libLLVMGlobalISel.a lib/libLLVMSelectionDAG.a lib/libLLVMCodeGen.a lib/libLLVMScalarOpts.a lib/libLLVMAggressiveInstCombine.a lib/libLLVMInstCombine.a lib/libLLVMBitWriter.a lib/libLLVMTransformUtils.a lib/libLLVMTarget.a lib/libLLVMAnalysis.a lib/libLLVMObject.a lib/libLLVMMipsAsmPrinter.a lib/libLLVMMCDisassembler.a lib/libclangStaticAnalyzerCheckers.a lib/libclangStaticAnalyzerCore.a lib/libclangCrossTU.a lib/libclangToolingRefactor.a lib/libclangToolingRefactoring.a lib/libclangASTMatchers.a lib/libclangIndex.a lib/libclangFrontend.a lib/libclangDriver.a lib/libLLVMOption.a lib/libclangParse.a lib/libLLVMMCParser.a lib/libLLVMProfileData.a lib/libclangSerialization.a lib/libclangSema.a lib/libclangAPINotes.a lib/libLLVMBitReader.a lib/libclangEdit.a lib/libclangAnalysis.a lib/libclangFormat.a lib/libclangToolingInclusions.a lib/libclangToolingCore.a lib/libclangAST.a lib/libclangRewrite.a lib/libclangLex.a lib/libclangBasic.a lib/libLLVMCore.a lib/libLLVMMC.a lib/libLLVMBinaryFormat.a lib/libLLVMDebugInfoCodeView.a lib/libLLVMDebugInfoMSF.a lib/libLLVMSupport.a -lrt -ldl -ltinfo -lpthread -lm lib/libLLVMDemangle.a -Wl,-rpath,"\$ORIGIN/../lib" && :
clang: error: unable to execute command: Killed
clang: error: linker command failed due to signal (use -v to see invocation)
ninja: build stopped: subcommand failed.

I may need few days.

@spevans
Copy link
Collaborator

spevans commented Nov 14, 2018

@omochi You may be running out of RAM in your docker container. Try using at least 2GB and try running with --jobs=1 to try and reduce ram usage (although the build will be slower)

@omochi
Copy link
Contributor Author

omochi commented Nov 15, 2018

@spevans Thanks.
I tried with 8GB memory and 8GB swap then succeeded to advance.
I could avoid wired clang: error: unable to execute command: Killed error.

Now I stopped another error.

[34/78] Linking CXX shared library src/libdispatch.so
FAILED: : && /Users/omochi/work/swift-source/build/Ninja-DebugAssert/llvm-linux-x86_64/bin/clang++  -fPIC -g  -fuse-ld=gold -shared -Wl,-soname,libdispatch.so -o src/libdispatch.so src/swiftDispatch.o /Users/omochi/work/swift-source/build/Ninja-DebugAssert/swift-linux-x86_64/lib/swift/linux/x86_64/swiftrt.o src/CMakeFiles/dispatch.dir/allocator.c.o src/CMakeFiles/dispatch.dir/apply.c.o src/CMakeFiles/dispatch.dir/benchmark.c.o src/CMakeFiles/dispatch.dir/data.c.o src/CMakeFiles/dispatch.dir/init.c.o src/CMakeFiles/dispatch.dir/introspection.c.o src/CMakeFiles/dispatch.dir/io.c.o src/CMakeFiles/dispatch.dir/mach.c.o src/CMakeFiles/dispatch.dir/object.c.o src/CMakeFiles/dispatch.dir/once.c.o src/CMakeFiles/dispatch.dir/queue.c.o src/CMakeFiles/dispatch.dir/semaphore.c.o src/CMakeFiles/dispatch.dir/source.c.o src/CMakeFiles/dispatch.dir/time.c.o src/CMakeFiles/dispatch.dir/transform.c.o src/CMakeFiles/dispatch.dir/voucher.c.o src/CMakeFiles/dispatch.dir/shims.c.o src/CMakeFiles/dispatch.dir/event/event.c.o src/CMakeFiles/dispatch.dir/event/event_epoll.c.o src/CMakeFiles/dispatch.dir/event/event_kevent.c.o src/CMakeFiles/dispatch.dir/event/event_windows.c.o src/CMakeFiles/dispatch.dir/shims/lock.c.o src/CMakeFiles/dispatch.dir/event/workqueue.c.o src/CMakeFiles/dispatch.dir/block.cpp.o src/CMakeFiles/dispatch.dir/swift/DispatchStubs.cc.o  -lswiftSwiftOnoneSupport -lbsd /usr/lib/x86_64-linux-gnu/librt.so libBlocksRuntime.so -pthread -Wl,-rpath,/Users/omochi/work/swift-source/build/Ninja-DebugAssert/libdispatch-linux-x86_64: && cd /Users/omochi/work/swift-source/build/Ninja-DebugAssert/libdispatch-linux-x86_64/src && /usr/bin/cmake -E make_directory .libs && /usr/bin/cmake -E copy /Users/omochi/work/swift-source/build/Ninja-DebugAssert/libdispatch-linux-x86_64/src/libdispatch.so .libs
/usr/bin/ld.gold: error: cannot find -lswiftSwiftOnoneSupport

I try more.

@spevans
Copy link
Collaborator

spevans commented Nov 15, 2018

What command are you running to build swift?

@omochi
Copy link
Contributor Author

omochi commented Nov 19, 2018

Sorry for late.

I used this.

# utils/build-script --foundation --xctest --test 

@spevans
Copy link
Collaborator

spevans commented Nov 19, 2018

It looks like you are building the debug versions which take up quite a lot of space. You could try rebuilding add in -R to build a release version:

# utils/build-script -R --foundation --xctest --test 

@omochi
Copy link
Contributor Author

omochi commented Nov 19, 2018

I think debug build is better to work on CI failure.
But certainly it is hard to build and cycle development iteration.
I try release first, thanks.

@spevans
Copy link
Collaborator

spevans commented Nov 19, 2018

@omochi If you have access to macOS and Xcode you may find it easier to develop/debug using that, just make sure to download the latest Xcode snapshot.

@omochi
Copy link
Contributor Author

omochi commented Nov 20, 2018

In mac environment, my PR is no problem.
CI says that it has problem with in Linux environment.
So I use Ubuntu on Docker on macOS.

By the way, I succeeded build now !
I start to investigate issue around my PR.

@spevans
Copy link
Collaborator

spevans commented Nov 23, 2018

@swift-ci test

@omochi
Copy link
Contributor Author

omochi commented Nov 26, 2018

I report progress.

Issue is this essentially.
This code is crash on Linux.

import Foundation
class Cat {}
let a: Cat = Cat()
let array = NSMutableArray()
array.add(a as Any)
Could not cast value of type 'main.Cat' (0x7f180f3b0040) to 'Foundation.NSObject' (0x7f1801800a20).

@omochi
Copy link
Contributor Author

omochi commented Nov 27, 2018

I updated and fixed test failure.
To run this, this PR for xctest is need before.
apple/swift-corelibs-xctest#240

In my environment, there are other test failure about NSFileManager.
But I think it cause from my Docker environment and not related this PR.

@spevans
Copy link
Collaborator

spevans commented Nov 27, 2018

Please test with the following pull request apple/swift-corelibs-xctest#240
@swift-ci test

@spevans
Copy link
Collaborator

spevans commented Nov 27, 2018

@swift-ci test

@omochi
Copy link
Contributor Author

omochi commented Nov 29, 2018

@millenomi Please review again

@millenomi
Copy link
Contributor

That's a regression; any object should be storable in an array. I'll need to look into it.

@millenomi
Copy link
Contributor

@swift-ci please test and merge

@millenomi millenomi merged commit 1f06596 into apple:master Nov 29, 2018
@dduan
Copy link
Collaborator

dduan commented Apr 3, 2019

why was the access control level changed? This is a backward incompatible change.

@omochi
Copy link
Contributor Author

omochi commented Apr 3, 2019

Because I aligned it to Darwin's.
Sorry I forgot to reference about it in first post.

https://developer.apple.com/documentation/foundation/thread/1411433-threaddictionary

var threadDictionary: NSMutableDictionary { get }

@dduan
Copy link
Collaborator

dduan commented Apr 3, 2019

Yeah I guess the access control was wrong. But at least one open source library, RxSwift, depends on that bug. 😔

@omochi
Copy link
Contributor Author

omochi commented Apr 3, 2019

How does RxSwift on Darwin avoid problem?
I think that it can be solved problem by doing same as on Darwin.

@dduan
Copy link
Collaborator

dduan commented Apr 3, 2019

@omochi
Copy link
Contributor Author

omochi commented Apr 3, 2019

Ah, I understand. I read link and also Platform.Darwin.swift.

They needed to split implementation for previous incompatible implementation between NSMutableDictionary and var Dictionary.

As a fact, this my PR broke backward compatibility.
Sorry. But I think that this is right way.

@dduan
Copy link
Collaborator

dduan commented Apr 4, 2019

Another aspect of this: with NSMutableDictionary now a generic class such as final class C<T> {} can no longer be held as a value (runtime crash because the value is not an NSObject). This is only an issue on Linux but not macOS because, I think, Objective-C runtime things.

This is not an abstract example because RxSwift actually does this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants