example-greenthreads icon indicating copy to clipboard operation
example-greenthreads copied to clipboard

Remove global variable RUNTIME.

Open MashPlant opened this issue 4 years ago • 4 comments

More detailed modifications are recorded in CHANGELOG.md. My English may be poor, so if you are confused with my explanation, just ask me directly.

MashPlant avatar Jan 16 '20 05:01 MashPlant

First of all. This looks like an impressive improvement. Some cool techniques here I haven't seen before. Using global_asm! was a smart move.

Have you tried running the program under Valgrind? Regarding the wasted space on the stack I think I added some space at the bottom because I got an issue reporting some errors in Valgrind using offsets of -16 and -8 but I might be mistaken.

I'll get back to you on this one once I've had some time to go through everything thoroughly.

cfsamson avatar Jan 16 '20 20:01 cfsamson

I tried to compile your code on my mac and it seems giving this error? 🤔 Have you tested your code on mac os or?

error: linking with `cc` failed: exit code: 1
  |
  = note: Undefined symbols for architecture x86_64:
            "_switch", referenced from:
                green_threads::Runtime::do_yield::h6963f530cc3ab8ee in green_threads-77913a0e134e42f4.nexreiqecebn77q.rcgu.o
          ld: symbol(s) not found for architecture x86_64
          clang: error: linker command failed with exit code 1 (use -v to see invocation)

liufuyang avatar Jan 19 '20 17:01 liufuyang

What I'll most likely do here is that I'll merge this into a separate branch, and point readers to it in the start of the book.

It requires a substantial rewrite of the Windows branch as well in addition to rewrite (and proofread) quite a bit of the text in the book if I merge this to master (and I also think the added complexity might make the example less accessible to readers which also argues for keeping it in a separate branch).

I'll still have to run the code on macos and through Valgrind though. I can't see any obvious reason this should work on Linux and not macos as reported by @liufuyang.

cfsamson avatar Jan 20 '20 09:01 cfsamson

Ok, so I have had some time to have a look at this now. It works on Linux, but there is a problem on macos, with the following error:

= note: Undefined symbols for architecture x86_64:
            "_switch", referenced from:
                green_threads::Runtime::do_yield::h2c1879a601ea63e4 in green_threads-3e6b868f1b554120.3xumwfqgwyz6otwr.rcgu.o
          ld: symbol(s) not found for architecture x86_64
          clang: error: linker command failed with exit code 1 (use -v to see invocation)

Turns out that there are some subtle differences in the ABI, one is that symbols are prefixed with a leading underscore for function names on macos. Changing the name of the function defined in the assembly to _switch solved the linker error but resulted in a segfault at runtime.

I'd love for this to work on both macos and linux (it's ok to skip windows for this), but I don't have time to debug this rignt now.

I'll consider referencing this in the book anyways stating the limitations just because I think it used some interesting techniques that.

cfsamson avatar Jan 28 '20 22:01 cfsamson