I made Coroutines from scratch

  Рет қаралды 34,083

Tsoding Daily

Tsoding Daily

Күн бұрын

Пікірлер: 108
@FreezeBadGuy
@FreezeBadGuy 14 күн бұрын
"im not monetized in the first palce, so fuck that" bro is a coding chad.
@rubyciide5542
@rubyciide5542 13 күн бұрын
He is the only chad you can find on KZbin that programs 🫡. Rest are money hungry corporate slaves
@thissundae
@thissundae 13 күн бұрын
I often forgot he's a guy in the middle of Siberia his whole life
@FreezeBadGuy
@FreezeBadGuy 12 күн бұрын
@ yeah bro it makes sense hes a total bad ass about it. This guy will rule his own country one day.
@actualwafflesenjoyer
@actualwafflesenjoyer 2 күн бұрын
@@FreezeBadGuy citizenship only for c programmers
@nikkehtine
@nikkehtine 13 күн бұрын
Halfway through the explanation of coroutines he yielded his viewers to another stream. Great explanation Mister Zozin!
@bean_TM
@bean_TM 14 күн бұрын
chat in subtitles is a fantastic idea. chaotic good
@JohnDoeNobody
@JohnDoeNobody 14 күн бұрын
He’s always been doing this lol, this is just the first time he’s told anyone about it.
@Omena0
@Omena0 14 күн бұрын
​@JohnDoeNobodyhe has before
@HugoNobrega87
@HugoNobrega87 13 күн бұрын
​@JohnDoeNobodywell definitely not always, I think just a few months at this point
@dasten123
@dasten123 13 күн бұрын
why? Chat is fucking stupid
@igz5553
@igz5553 13 күн бұрын
Super interesting, not the best UX but original nonetheless
@ErikBongers
@ErikBongers 14 күн бұрын
This is a great tutorial explaining the architecture of co-routines. No half-baked analogies, but an under-the-hood get-your-hands-dirty demonstration. Thank you!
@seventhtenth
@seventhtenth 14 күн бұрын
"2 hours earlier" narrative gives a lot of hype. Thank you for sharing the streams
@c0d_0x16
@c0d_0x16 12 күн бұрын
I don't know if I'm just obsessed with your work, but really, you're the most versatile and knowledgeable programmer I've seen or heard of 😮. Your channel is a rear gem. Thank you for this amazing content.
@Spike.Spieko
@Spike.Spieko 14 күн бұрын
Ngl, starting the video with "What the f**k ...." is such an OP intro.
@playerguy2
@playerguy2 14 күн бұрын
For Tsoding (on the off chance you _do_ read this): alloca(), and see last sentence. Damn, I was just experimenting with implementing coroutines a week ago. Yes, the stack is the limiting factor. I ran into a bug where I implemented a generator with an out parameter and main() printf'd it out. Thing is: the output pointer in the generator function was offset by 0x4 away from the correct value after every the longjmp. Turns out, by jumping, the stack was effectively freed and printf() overwrote it. Returning from the func also segfaulted (I wonder why). The solution, which made me feel like a genius, a God amongst men, was to insert a call to alloca(16) right after the setjmp, before printf(). It would offset rsp by 2 pointers worth (return address and the out param), reserving the stack frame, protecting it from printf, and it worked! From there it's a case of using alloca(0) to measure the stack size, alloca() to protect, memcpy() to save and restore it, longjmp() to yield and resume.
@ToyosatomiminoMiko-z8t
@ToyosatomiminoMiko-z8t Күн бұрын
had to deal with coroutines recently in C and i pretty much just have a chunk of the coroutine struct dedicated to storing its own stack of about 8KB (and 2 jmpbufs for the caller/callee) if the callee's jmpbuf wasnt initialised(so if its the first time its been called), then i just poked at the stack pointer(and saved important information in static variables so that they'd still be accessible) and called the coroutine, which, when it yielded, longjmp'd back, returning us to our previous stack safely it was pretty bodgy (and i dont think i explained it quite well) but man did it feel great when i came up about that idea
@wahyuwardani3685
@wahyuwardani3685 8 күн бұрын
having video like this in youtube is insane, keep kicking ass dude!
@FryAndLeelaAndBender
@FryAndLeelaAndBender 14 күн бұрын
I must agree with Mr. Programmer here: at least for me, FASM (Flat Assembler) is like I think every assembler should be - simple to understand, elegant, easy to program code and with an easy command-line invocation - no make, no cmake, no nothing; everything in its purest form...
@brayn7504
@brayn7504 14 күн бұрын
I never requested a topic but I would love it if you explore Kotlins coroutines. Would be nice to see your take and view on it. Thank you Tsoding for the amazing work that you do. Kotlin's coroutines are even more concise and supports multi-threading as well so if possible, would be great to see what you think of it. It would definitely give you some new perspective on how to implement this and for others, better understanding of such a complex topic. Thank you.
@diadetediotedio6918
@diadetediotedio6918 14 күн бұрын
Kotlin coroutines are closer to futures than to the mill thing or go, they transform into state machines and are polled by a runtime. What I like on them is that you can control precisely where they will run, so if you just want to dispatch some in the local thread and keep it single threaded you just `runBlocking` and it usually just works
@my_online_logs
@my_online_logs 14 күн бұрын
any coroutines support multithreading bruh because its m:n mapped on os thread
@my_online_logs
@my_online_logs 14 күн бұрын
​@@diadetediotedio6918eh no bruh, kotlim coroutines is like golang, its stackfull. futures is stackless coroutines, rust and java are stackless coroutines. stackfull coroutines give the user easier to use but in very high load your performance will be lesser than stackless coroutines because each your coroutines need their own stack
@diadetediotedio6918
@diadetediotedio6918 13 күн бұрын
@ This is actually pretty much false. Kotlin coroutines are stackless coroutines, they only look more like stackful coroutines because design decisions of the language (such as not having some kind of 'await' keyword), but you still need to mark functions as 'async' (in kotlin terms, 'suspend') and it still works the same. You should remember that kotlin runs on the JVM, so they improvised the solution that could have interoped well with Java and cannot do something like Go did.
@naxaes7889
@naxaes7889 14 күн бұрын
How many here see a new Tsoding repository in the github feed and then are left in excitment of the new video that's about to drop?
@polskafan
@polskafan 14 күн бұрын
If go calls it goroutines, can we call it tsoroutines?
@MrMeltdown
@MrMeltdown 14 күн бұрын
Bro-routines. Bro-yield. Bro-go. Bro-run….
@seventhtenth
@seventhtenth 14 күн бұрын
@@MrMeltdowntsoro bhodhi
@machine.angel.777
@machine.angel.777 14 күн бұрын
1 minute ago is assembled
@kaporos
@kaporos 13 күн бұрын
About the relocation issue, you can change all of your mov rsi, toto with lea rsi, [toto] to use relative adressing and avoid the need for relocation
@hand-eye4517
@hand-eye4517 13 күн бұрын
I saw the video description , and said no way 😂 your nuts man . Hopefully I can become like you one day
@kahnfatman
@kahnfatman 14 күн бұрын
I thought I was crazy. But you are crazy!
@dastan331
@dastan331 14 күн бұрын
Using macros and void * in C, it is possible to implement almost all the features found in other languages. However, there is a risk of introducing a lot of errors, and of course, writing a lot of code. But that's real engineering!😅
@User948Z7Z-w7n
@User948Z7Z-w7n 14 күн бұрын
16 seconds ago is unbelievable
@marcocaspers3136
@marcocaspers3136 18 сағат бұрын
I don't know if it's been answered already, but, when "deleting" items by swapping in the last item with the current one that has to be deleted you change the order of the list. While it won't be an issue if there's only 2 items on the list, it can get interesting if there are more than 2 items on the list. It won't matter if there is no interdependency or if the order in which things happen don't matter, but where the order matters or there are interdependencies it's going to mess things up. If the goal is testing hypothesis then this is fine, but as a generic library function I wouldn't want the odd chance of this happening, or it should at least have been documented clearly that this is a side effect of the implementation that may or may not be wanted.
@polnio
@polnio 14 күн бұрын
I just saw on the Wikipedia page of GNU assembler that with a recent version of GNU assembler, you can tell him to use intel syntax instead of AT&T. I didn't tested though, but it seems more interesting than make the linking by hand
@JamesSjaalman
@JamesSjaalman 14 күн бұрын
BTW: coroutines are explained in Knuth#1, including code in (MIX) assembler. Suggested reading!
@joseoncrack
@joseoncrack 13 күн бұрын
I need to re-read that. I have the full collection.
@kasparzubarev
@kasparzubarev 12 күн бұрын
In microcontrollers, and in Linux kernel you have interrupts, that make the program switch context to handle the interrupt and continue after the interrupt is handled
@_bitties
@_bitties 14 күн бұрын
Best 1st minute intro
@multicoloredwiz
@multicoloredwiz 14 күн бұрын
This project is so cool. Loved the intro Gasbabbs btfo!!!!
@nousername9374
@nousername9374 12 күн бұрын
I admire you and your work
@bobby9568
@bobby9568 13 күн бұрын
From scratch is what we're here for!
@solanacean2812
@solanacean2812 Күн бұрын
I don't know much about fasm, but in fasmg/fasm 2 the built-in command 'struc' doesn't really define a structure, as the name suggests, but rather a labeled macroinstruction. Kinda like 'somestupidname db 0' defines the labeled form of instruction and 'db 0' the unlabeled one. To define a structure one has to include 'struct.inc' and use 'struct' macro: struct Context _rsp rq 1 _rbp rq 1 _rip rq 1 ends contexts db sizeof.Context * COROUTINES_CAPACITY dup ? assert Context._rsp = 0 assert Context._rbp = 8 assert Context._rip = 16
@hand-eye4517
@hand-eye4517 13 күн бұрын
I saw the video title , and said no way 😂 your nuts man . Hopefully I can become like you one day
@elgalas
@elgalas 12 күн бұрын
Epic, most epic stream ever. fasm, all hail fasm! All hail tsoding!
@asdfmonstrosity
@asdfmonstrosity 13 күн бұрын
Very cool! Afaik Lua implemented single threaded coroutines using pure c89. It is less convenient in the C api though, you need to pass continuation functions for 'the rest of the function', which might be inmediately run if no yield, or run elsewhere after a longjmp. Its pretty cool though. And the API from Lua code matches pretty similarly your code, coroutine.yield instead of coroutine_yield, etc.!
@olaff667
@olaff667 14 күн бұрын
you can use intel syntax with gnu assembler with as -msyntax=intel
@johangonzalez4894
@johangonzalez4894 13 күн бұрын
I'll just mention Duff's Device as it is (somehwat) related to the title, and i want to share a little piece of hacky C that may not be as widely known.
@rogo7330
@rogo7330 11 күн бұрын
I think you don't need special handlers to check file descriptors and stuff. You just need a way to talk back to the "main" execution context that yielded context will immediatly block/yield because it awaits file descriptor. It can be done just by passing memory between that will contain some information. That way "main" just need to understand on what couroutine should be synchronized (files, sockets, left disk space, system realtime, it does not matter), and then "main" will just decide to run coroutine again.
@blackhaze3856
@blackhaze3856 13 күн бұрын
long time without watching Tsoding channel. Where is the 'hello and welcome to yet another recreational programming session?' part? 😢
@nerdicon4822
@nerdicon4822 13 күн бұрын
why u push rbp into the stack and pop it at the end doesn't call and ret instruction do that automaticaly?
@chevychavanne4433
@chevychavanne4433 13 күн бұрын
Tsoding, you don't have to worry about the order of your static data in asm by using sections and segments. I'm pretty sure .bss section is where uninitialized variables go
@fredesch3158
@fredesch3158 12 күн бұрын
11:47 You don't understand tsoding! If a functional programmer actually got shit done, they'd be changing state, and everything they do need to be a pure function!
@luserdroog
@luserdroog 13 күн бұрын
Came for the codez. Stayed for the cussing.
@cryptonative
@cryptonative 12 күн бұрын
Came for the code stayed for the code
@JonathanZigler
@JonathanZigler 11 күн бұрын
Heck yeah
@deckard5pegasus673
@deckard5pegasus673 14 күн бұрын
My C code is filled with gotos. I never used gotos until I started programming Mac OSX carbon API back in 2003. And the Apple Carbon code had a really nice way of checking errors and cleaning up with gotos. Since then I always used it. The code is much more readable, and gets rid of nested if statements and many return points in the function For example: void someFunc(int param1) { int ret = error; if (some_proble) goto _ERROR; .... whatever code ..... ........ ret = ok; _ERROR: ...clean the function here... return ret; // only return point in the function. }
@monx
@monx 14 күн бұрын
13:15 Recreational programming manifesto 😂
@froop2393
@froop2393 13 күн бұрын
You should provide timestamps for each "fck". So i exactly know where the interesting parts of your vids are 🤣 btw: realy great content explainig and demonstrating the heart of coroutines in a live session
@MrMeltdown
@MrMeltdown 14 күн бұрын
No I don’t know what a co-routine is…you gonna tell me or what? Love your content.
@jermeekable
@jermeekable 13 күн бұрын
i mighta missed it but did he mention pinning?
@FryAndLeelaAndBender
@FryAndLeelaAndBender 13 күн бұрын
I love the part when Mr. Programmer says: "Fuck GNU assembler"... hahahaha 😂😂😂😂
@user-sq1oi9qp8w
@user-sq1oi9qp8w 14 күн бұрын
how did you integrate twitch live chat into cc?
@naranyala_dev
@naranyala_dev 12 күн бұрын
a CHAD programming session
@elgalas
@elgalas 13 күн бұрын
1:00:39 uxn mentioned
@PyguK
@PyguK 14 күн бұрын
How is it possible, that Mr Tsoding have 5000 views, but only about 10% likes? Are rly 90% of the viewers disliking the video and still watching it? I think its a great video with a lot to learn from, so thumbs up from my part. Keep Going, great project
@FryAndLeelaAndBender
@FryAndLeelaAndBender 13 күн бұрын
@@PyguK Probably many of the viewers use a smart TV to watch the videos and don't put a like because they forget to do that...
@FryAndLeelaAndBender
@FryAndLeelaAndBender 13 күн бұрын
@@PyguK Also, not giving a like isn't similar to dislike thr video. It's more like being indiferent about it...
@nel_tu_
@nel_tu_ 13 күн бұрын
💀
@igz5553
@igz5553 13 күн бұрын
That’s typical for any video, 10% likes generally
@PyguK
@PyguK 13 күн бұрын
@@igz5553 ah ok, didnt knew thats normal. Thanks 2 all 4 the replies.
@btarg1
@btarg1 13 күн бұрын
Please do some more game development streams! I'd love to see you make a game in C
@rustyname
@rustyname 14 күн бұрын
What exactly do you want from writing software anymore?
@elgalas
@elgalas 14 күн бұрын
Async/await in JS used to be polyfilled with generators
@rafalk5527
@rafalk5527 14 күн бұрын
C protothreads?
@AzharAli-n5c
@AzharAli-n5c 9 күн бұрын
great
@RiteshRanjan-x4k
@RiteshRanjan-x4k 13 күн бұрын
Program in DOSbox
@christophjasinski4804
@christophjasinski4804 7 күн бұрын
Go team watching to figure out why it took them so long 🤣
@Aziqfajar
@Aziqfajar 13 күн бұрын
8:08
@amitmalaker5445
@amitmalaker5445 14 күн бұрын
you have this similar example in the c9xme you know??? ikykyk...
@delq
@delq 14 күн бұрын
poggers
@btarg1
@btarg1 13 күн бұрын
C++ and c# already have equivalents out of the box, this is so entirely pointless yet I still find it cool for some reason
@infastin3795
@infastin3795 12 күн бұрын
The one in C++ is unusable crap
@elishahar2639
@elishahar2639 2 күн бұрын
hello i am posting from my son's account. he watched this and had a seizure when he saw goto, and is now in the hospitle. needless to say i am disliking and calling youtube.
@alejandroulisessanchezgame6924
@alejandroulisessanchezgame6924 14 күн бұрын
First
@_f0x604
@_f0x604 14 күн бұрын
You don't need to say wtf that often to look cool bro. Peace
@colonthree
@colonthree 14 күн бұрын
OwO
@cybermats2004
@cybermats2004 14 күн бұрын
asmr?
@simonfarre4907
@simonfarre4907 14 күн бұрын
Coroutines are horrible. Debugging coroutines are horrible. Thank god for real parallelism and multi threading and epoll. Btw, coroutines are functional programming ideas or a declarative programming paradigm. Use a thread pool and post tasks instead. Coroutines are just bad. Hot take.
@2mj0lk
@2mj0lk 10 күн бұрын
another genius here lmfao, humanity is doomed
@delicious_seabass
@delicious_seabass 14 күн бұрын
While this is cool and all and I wish C actually had real coroutines, rolling your own in assembly is probably a bad idea, if not for 'educational purposes'. For one thing, you'd have to implement this for each architecture, but more importantly debuggers will get confused about what is happening. If you want something like a coroutine, then just do a switch statement with an enum.
@diadetediotedio6918
@diadetediotedio6918 14 күн бұрын
> While this is cool and all and I wish C actually had real coroutines, rolling your own in assembly is probably a bad idea, if not for 'educational purposes'. > If you want something like a coroutine, then just do a switch statement with an enum Nah, this is pure bullshit.
@delicious_seabass
@delicious_seabass 14 күн бұрын
@@diadetediotedio6918 Good argument. Thanks for copy-pasting. I almost forgot what I wrote.
@diadetediotedio6918
@diadetediotedio6918 14 күн бұрын
@@delicious_seabass I think an argument would be a not enjoyable thing to do in this case, as you just came and felt the need to say this "should not be done" without nobody requesting that specific view. But I will actually respond to your points, so you may feel more inspired to develop your ideas. Your reasoning for why it is a bad idea revolves around the 2 stablished things, which I will mention and respond in order: > [For one thing, you'd have to implement this for each architecture] You don't need to if you are not planning to run for each architecture, but if you need to you just need to implement for each architecture, this is not a reason why it is a bad idea, it is just work. > [but more importantly debuggers will get confused about what is happening] Which says nothing about nothing, most debuggers of languages with any kind of async mechanism get lost on the tracks, even high level languages like C# have problems with that (with the debugger jumping around and getting into multiple spots every step you go), concurrency and parallelism are inherently hard to debug and that's why usually people rely more on logging for those kinds of systems. Either way, this is only a problem insofar you are concerned with that particularly, it don't make the whole thing a "bad idea". And lastly, about your "proposed solution", > [If you want something like a coroutine, then just do a switch statement with an enum.] This is nowhere near the convenience of coroutines, if the point is to do things manually then just spawn threads and pass things around, or do a manual futures library like he already did. This is a pointless alternative.
@delicious_seabass
@delicious_seabass 14 күн бұрын
​@@diadetediotedio6918 First of all, I don't need your permission or anyone else's to give my opinion. Comments are enabled for a reason. Secondly, I never said "should not be done". That's your terrible reading comprehension. I said "it's probably a bad idea", as in not definitively. I still believe people have brains and don't need to be coddled, so they can make up their own minds. What I was simply doing was pointing out some issues people may not have considered if they go down this route. The reason I mention the arch is because C is cross-platform and while people may target a specific arch at first, they also might change their mind and want to port things later. Now it's not a simple recompile for said target arch - now they gotta go add a new implementation for their custom coroutines because they use them everywhere, and its otherwise a rewrite of their C code. Also, there is the possibility the arch can change with new versions, so now you're on the hook for each update should it break things. If you don't have an issue with all that, then great! Knock yourself out. As for debugging, I don't know why you brought up async and concurrency. I'm talking about messing with regular control flow. If you do that, debuggers don't understand what happens when you randomly jump using assembly because they base their flow on the C code and not the assembly. This is why its difficult to debug things like fibers, for example. They usually continue running the program or run to the next breakpoint if it exists, missing anything before. If you don't have a problem with that, then cool. Have fun printf debugging all your coroutines. Lastly, it's kind of funny you mention convenience here, since debugging is not important to you. Also, I'm not sure why you think you need to spawn threads or use futures. Like what do you think a coroutine does exactly? It just saves the execution state so it can continue it later before jumping to a new location. You can do this manually with, like I said, a switch and an enum that gets incremented, and possibly some gotos if need be. Your code will be fully debuggable, easier to follow, and compileable to different platforms. Sounds pretty damn convenient to me! ... I need to stop arguing with randos who have anime profile pics.
@fernandobalieiro
@fernandobalieiro 14 күн бұрын
1 minute and 2 views, bro fell off
@doodocina
@doodocina 14 күн бұрын
first
I tried Ninja Build Tool and it changed me...
1:22:16
Tsoding Daily
Рет қаралды 16 М.
I switched from ASSEMBLY to C and it made my code better...
2:05:51
Tsoding Daily
Рет қаралды 20 М.
Making Smallest Possible Linux Distro (x64)
27:43
Nir Lichtman
Рет қаралды 124 М.
Tips for C Programming
34:41
Nic Barker
Рет қаралды 55 М.
Jeff Dean: AI will Reshape Chip Design - NeurIPS 2024
43:53
GradientSpills
Рет қаралды 5 М.
Ok I tried Tree-sitter! Here is what I think about it...
1:56:47
Tsoding Daily
Рет қаралды 66 М.
the new rsync exploit is sort of hilarious.
11:02
Low Level
Рет қаралды 197 М.
Rust & Zig Combined • Richard Feldman • GOTO 2023
45:34
GOTO Conferences
Рет қаралды 83 М.
Daily Coding (day 5) - Advent of Code problem 14
1:13:57
jacob stephens
Рет қаралды 7
"BEST C++ CODE ever written" // Code Review
27:38
The Cherno
Рет қаралды 126 М.
Leetcode Or Die
35:30
ThePrimeTime
Рет қаралды 201 М.
I made Futures from scratch
1:56:09
Tsoding Daily
Рет қаралды 38 М.