So for the first time in six years today somebody came in here and complained about the music, so clearly it's time to quit Five minutes a day dude Chill out. All right No, maybe if you guys all scream really loud, I'll come in here again All right. Anyway, we're just we're disturbing the geology department. You know the rocks don't make a lot of noise Apparently they don't like rock music Jokes are folks go on and on. Okay, so let's have class today Let's make my release your point of work. All right quick assignment one checkpoint It should be almost done at this point. The deadline is Friday at five. Remember, there's no late credit for this Get what you can out of the 50 points and move on assignment two is the first part of assignment two is to do a week later So there's just no time to like Realize that you should have started two weeks ago, right? All right. Anyway, you've seen this before Yeah, that's where we are. So we have upstowers today tomorrow Friday Please come in and get help if you need it Also, please how many people have submitted something to test 161 or At least run the verify step to make sure things are set up. Okay. Okay. Do that You got to do that. We're not gonna We're not gonna accept late submissions because you couldn't get things set up properly. That's on you to do Okay so today we're going to talk about locks and spin locks and Other simple synchronization primitives. Hopefully you guys have some comfort level with these now because you've been using them We'll go through a example that's new to you that you haven't had to solve before and at some point Maybe on Friday we'll discuss a little bit about choosing the right primitive now Again, this is something you guys are gaining some experience with so both the stoplight and the whale mating problem can be solved Using several primitives But if you've and actually if you've already finished one of those problems and you're kind of bored and you don't know What to do for the next couple days Try solving it with a different primitive try going back Redoing it. I mean save your code create a branch or something and get so you don't lose the solution you have But try try doing it with something else and see how it works you usually what you'll find is that there's one primitive that works really well and Leads to a clean solution that's very satisfying and there's another one that just starts to get gross and you feel sad and unhappy about So it's not always true sometimes there's several primitives that can work well in a particular situation But usually there's a good good choice All right, so the NLS class we started to talk about critical section So a critical section is something that we use to bring some control to this wild world of concurrency that we've created and The idea of critical section is only one thread can be executed inside the critical section at a time and On uniprocessor systems It was possible to implement a critical sections by just making sure that the thread that was inside the critical section Didn't stop running until it left and the reason for that is because there's no other cores on the system Nothing else can be happening ergo. Nobody else is running inside the critical section But that solution is clearly broken on almost every interesting computer today because every interesting computer today has at least two cores If not more Well, I shouldn't say that I mean I guess if you're interested in Arduino's I mean, but there's probably like it's in there Like a multicore Arduino Okay, there you go. Well, okay fine any interesting computer. It has at least two cores Again so so this is this is just sort of a historical interest I'm not gonna talk about this for a little bit behind So in general what we need is we need a mechanism so that other threads Will stop before they get inside the critical section and wait in some way or another And we'll talk about two different ways that we can get threads to wait both of which are Familiar to you has anyone looked carefully at the code for the spin lock that we gave you obviously that's a working primitive You're just so in curious. Come on read the code. It's so interesting So I would encourage you to do this the spin locks we gave you work obviously But you might want to get a sense of how they're how they're implemented Achieving synchronization on multicore Systems requires some type of hardware assist in most cases usually the hardware itself is going to provide you some help The it's actually kind of interesting the MIPS instruction set that your compiler tool chain generates and that sys 161 supports is At this point in time a very strange amalgam of old features that were part of the MIPS R 3000 which is what was I'm sorry if you guys are like getting nervous up here. I'm gonna like walk into your lap Okay, I'm gonna move so I have more room to wander around So at this point that instruction set is the mixture of old stuff that was from the R 3000 era And then a couple of new instructions that David added that are designed to support synchronization Those instructions did not exist in the MIPS R 3000 instruction set. Why not? Yeah, that was like I'm the 80s there There just wasn't a problem they needed to solve and so you have this like very vintage instruction set that has a couple of special Instructions that have been just ported in from the future in order to make the system work on multiple cores And the guy idea here is that these special hardware instructions give a little bit of a boost to the Software that's using them and make it easier to write the low-level primitives that provide Atomicity across multiple cores think about I've got one thread of one cord that's running and suddenly it needs to Communicate something with the threads on the other cores doing this safely and doing this efficiently you can require a little bit of hardware Help so that's what we do There's a couple of varieties of these there's probably new varieties that are out there today There's something called a test and set So when I'm talking about instructions now, I'm talking about processor instructions These are part of the architectural instruction set. These are not like see things And if you look at the code for spin locks, I think at some point there's some inline assembly there Where David is actually using the new atomic instructions that he introduced into the the MIPS R 3000 variant that OS 161 supports So test and set works by it writes a value into a particular memory location and returns the old value Now the reason that these instructions are special is that they're guaranteed to work across multiple cores So if let's let's say that there's a memory location and The value of the memory location is zero When I start and at the same time Four cores hit it with the test and set and the value that they're setting is one So what should happen? Four cores existing memory location holds value zero. I try to test and set it to one What should happen? Yeah Exactly one of them and only one now which one doesn't matter right? It's one of the four that's doing something at the same time gets back Zero the previous value and sets it to one and then all three of the other ones get back a one So you can start to see how this is something that I can use to implement other synchronization problems All right. Yeah, so this is just pseudo C code for for this operation Also something called compare and swap same thing a little bit different Compare the contents of a memory location to a value that's passed in if they're the same then set it to a new value And here's some C like code So I give you a comparison Say and so I could say if it zero set it to one And so if it's one it won't be set Right because because it didn't meet the first condition Again keep in mind This is atomic with respect to other cores that are trying to execute the same hardware instruction So for these happen at the same time you can work out what should what should go on one one person We'll get the old value back and everybody else will get a new value or or the compare and swap will fail Yeah, okay, and so this is a this is a cute variant of it that uses two instructions There's something called load link store conditional load link returns the value of a memory address and then Later on I have this store operation that will succeed only if that memory address hasn't been changed since I did the first load link So again these are just examples and you don't have to understand all these perfectly tested set is by far the easiest one to understand Because it's a simple, but the other ones lend themselves to different programming paradigms that that may make it a little bit easier to implement the things We want right this is actually an hour remember this is here because this is what test 160 This is what system 161 uses. This is what the MIPS are 3,000 architecture supports So this is code from OS 161 somewhere Has anyone ever included in line assembly code in their C code before? Ah, there we go You can do it. Here it is. Here's here's one of the possible syntaxes And here these are the two instructions here that are actually doing the work So this is the load link and this is the store conditional All right any any questions about these underlying hardware permits. Yeah, yeah So Between you guys did this for what for 341 I have no idea Okay, but keep in mind this instruction is from the future, right these were introduced so at some point Like three or four years ago David said I want to add multi-core support to OS 161 Because I want you guys to be able to do assignments with multiple cores, which is really cool Again that MIPS as far as I understand the basic MIPS are 3,000 instructions that didn't really has had support for these instructions And so David sort of had free reign to modify this 161 to support the instruction He wanted to use and this is the one he chose or the combination of them, right? So my point being simply I have no idea if this this has any basis in the official MIPS are 3,000 instructions that are whatever it was based on because that that instruction set was not necessarily a multi-core from Okay So a lot of processors provide some variant of one of these and one of these instructions And what we do is we use that as a starting point to build Simple synchronization primitives, and then we can use them to build more complicated synchronization primitives and And on other equivalents you can use this using other So essentially if you have one of these you can implement some of the others So if I have a test inside I can use that to create an atomic compare as well Okay, so going back to the bank example that we had before Let's try to use it a bear a test inside on this is never what you would actually do because remember the goal of these Atomic hardware instructions is to use them to build synchronization libraries on top of So you would never use these instructions on their own unless this was sort of a special case But let's try to modify our example to use a test and set so remember test and set Test the value of it returns the old value of a variable such it to a new value Okay, so Remember what what three lines of the critical section here that I'm trying to establish What three lines of code represent the critical section? It's the first three and you could think of this being the part of the code where the balance is inconsistent My local copy of the balance is inconsistent with the global copy once I've synchronized the two I'm done, but those three lines of code are the place where I'm making a modification that I can't get interrupted in the middle of And so what I can do is like an add a test and set at the top. I set it to one So that other threads know that I'm inside the critical section and then at the bottom I'm going to set it to zero so that other threads know that I'm finished Does this work? No, I'm just using the values of flag Right you can think about of the value here as a boolean indicating whether or not there's a thread inside this critical section So so does this correctly mark whether that there's a thread inside the critical section? Yeah, so it's a to one and then clears it. That's fine. What does it not do? Yeah, if two threads come in here the second one's just going to blow right past the test and set and keep going All right, so I need some way to wait At the top. How would I do that using a test and set? It's possible Yeah, so I can do this If a test and set is equal to one. So what does that mean if the test and set returns one? Someone's inside the critical section. So what I need to do wait Do I have any useful way to wait? here Yeah, so what I essentially end up having to do here is write a while loop that repeatedly test the condition Until it returns zero and and remember the hardware is helping me here So the hardware is guaranteeing that as you know The the point is that I will that while loop will return as soon as somebody else sets it to zero Now when I'm busy waiting it doesn't matter as much because even if the two interleaved a little bit differently I'd be okay, but in other other cases. It's important particularly when I'm when I'm waiting using sleep So this will work What's wrong with it? Okay, yeah, so so there's there's it. Well, there's a couple problems that are wrong with it What's what's the first thing that could happen here, right? What what's wrong with that top loop? What is it? What is it accomplishing? Yeah, it's doing it's doing nothing, right? And so yeah, so what you would have here is something like this, right? Now now there's two cases here, right? Oh, yeah I don't know whatever. It's a meme Oh Heath Ledger he was very good. He was good. He was good at that role Yeah, so this is you know the problems of the project waste cycles Okay, not now. There's there's two cases here in one case It's like really bad although it will probably still work and in the other case. It's just marginally bad So what's the really really bad case here? Remember the processor the the operate system is swapping threads back and forth between a smaller number of processors to create the illusion of concurrency So what's the worst thing that can happen here? Yeah Well, yeah, that's terrible, right? Yeah, so so and that's not that's a really great point And this is really really true when you guys are doing like look if a thread crashes in your kernel It's going to panic, right? That's probably what's going to happen But so on some level if the kernel is panicking and rebooting who cares that there's a lock that's held, right? It won't be held when I reboot, but you're right Whenever you use synchronization primitives in real code You have to be very careful to make sure that you handle errors inside of them properly so that the lock doesn't get stuck That's a good point. Yeah, what's something else? That's a good point. Yeah, if my if my like bi-weekly deposit gets gets held up by all the Amazon purchases I'm making right then at some point I'm gonna have a problem, right? Yeah, but but yeah, so let's assume that this is fair, right? Let's assume that once I'm in line and I start waiting. There's no real rhyme or reason to who gets the lock next. Yeah Yeah, imagine I have one core Okay, or imagine for some reason all the threads that are running this code are running on one core So one thread gets into that loop at the top What is it waiting for? It's waiting for some other thread to run and drop the lock get out of the way so it can get in But if it's sitting there on that core, what's not happening that other threads not running, right? So I am actively impeding the thing that I'm waiting for I'm waiting for something and I'm making that thing take longer, right bad, okay? So that's the worst case where for some reason either I only have one core which again I told you never happens which is true or For some reason this this thread now in general this thread is in the way the longer I'm waiting useful works not getting done now There is one case in which this is okay. So what am I hope is going to happen? Let's say I have two cores When I get into this while one loop, what do I hope is happening? The other core is running the thread that's inside the critical section So if I'm spinning on thread on CPU one and CPU two is running this other thread then he's still making progress Sorry, I always use mail pronouns when I'm referring to threads, right? Just think about that because they're dumb. They just do one thing at a time and they run sequentially until they die, right? So that's why That's why I'll use mail pronouns for the threads, okay? So that the thread is running on the other core and it's just going to run until it dies Until it finishes and then the other thread is going to make progress. So that's what I hope is happening, right? Has anyone run into this on on their when doing their OS 161 code? You may have gotten an error message about spin locks deadlock on a spin lock So the spin lock implementation we gave you actually enforces the fact that the spin lock is held by core zero and another thread on That core tries to acquire it. It'll panic and You know, it's not necessarily in every case a bug I could just spin on that core until the seat the OS decided to run something else and then it would make progress But the semantics of spin locks are that you're not supposed to hold them for very long And so if I am I'm sleeping if I get descheduled and that ends up being a problem, okay? So problems with this approach busy waiting. I'm wasting cycles And and I can actually slow down the threads that are trying to do the thing that I'm waiting for. This is not good all right We've gone to the point I mean you guys been using locks, right? And the the real Reason to use locks as a synchronization primitive is when you can associate them with one or multiple critical sections the typical use of a lock is to guard some piece of shared state So that the people that are accessing that shared state the threads that are accessing that shared state can do so safely It's pretty much the primary purpose of a lock Now I don't want to confuse you it's possible to have multiple critical sections that are associated with the same Structure let's say I have some sort of shared data structure I that might have one method that adds an item to it I might have another method that withdraws an item from it I might have a method that looks up an item imagine like the implementation of a python Dictionary for example right that has to be implemented that there's actual code that implements that I know it's magic Right, but like when you do like dictionary whatever there's actual C code or C plus plus probably That has to run to implement that and and it has to work synchrony It probably has some locking that it does to make sure that this works Okay Because there's there's operations that it's doing on this structure that need to all be done together So that the structure state doesn't get corrupt The way is a lock pretty simple you guys are have started to do this Acquire it when I enter the critical section drop it behind Now what we've described today is something called a spin lock and the idea here is that there's two ways to wait Why one is I wait actively by continually checking the condition that I'm waiting for The other way is passively where I go to sleep and I have an arrangement with the Thread that's using the resource or inside the critical section that as soon as it's done. It's going to wake me up So those are my two options Yeah, so it's a spin lock Normally spin locks are not used on their own to solve synchronization problems now It's possible actually in some later assignments that you may want to use spin locks in a few places and we'll talk about why Well by the time we're done Hopefully I'll give you a little bit of a guide about when to use spin locks and when to use a traditional Sleeping lock that you guys have implemented. All right But they're commonly used I mean you you have seen them used now in both the semaphores and your lock and limitation as a way of building more complicated synchronization purpose All right, so but let's go back and let's solve this in a different way. So now let's create this lock object and What happens if I call a lock a choir while another thread is inside the critical section? It has to wait somehow right so I have to wait and if I wait actively I'm spinning if I wait passively I'm sleeping Okay, so I think I've said a lot of this so I can I can wait actively by continuing to hold the CPU and Continuing to check this condition over and over again. So this is busy waiting or active waiting or I can have some sort of Semantics where I arrange to sleep get out of the way let the other thread that's inside the critical section make progress and It then needs to be able to wake me up when it's finished so that I have my chance to go through the critical section myself Now There are some cases where spinning is actually the right approach So let's let's think about it Why would I never want to spin on a single core system? We just talked about this Because I'm slowing down the thing I'm waiting for on a single core system There is no way that spinning can ever be useful because whatever's inside the critical section needs to run It needs to do work, and if I'm using the CPU then it can't so this is never the right thing to do on a single core system If the critical section is short and if I'm completely a hundred percent certain that I'm not going to sleep So for example if the critical section involves doing something like I owe where my thread is going to have to wait for some Really slow hardware device like a disk to do some stuff then by definition the critical section is not short anymore It's really long So the critical section being short means I cannot do any sleeping Now we haven't talked about context switches yet because we sort of swap things around a little bit We're doing synchronization first, but when we talk about threading there is work That is required to stop remember the operating system is producing this illusion of concurrency by rapidly shuffling threads back and forth Onto the CPU and off the CPU that is not done Magically there is work that has to be done. I have to do some housekeeping every time I move a thread onto the CPU or move a thread off of the CPU and So if the critical section is short, so here's an example let's say that CPU, you know, this is an example of two threads running on two different CPUs for some reason they're both called thread one that are Waiting for a resource. So thread one acquired the resource first. It's inside the critical section CPU to ran a thread That tried to acquire that resource and decided to sleep so it said okay, I'm gonna go to sleep and The what what happens is that thread stops running and there's all this work I have to do to save the state of that thread so that I can restart it safely again and in the meantime Thread CPU want this thread ones done, right? I mean in this case imagine the critical section is only a couple of instructions So by the time CPU to wakes up the amount of work that I had to do to put it to sleep and wake it up Again is actually larger than the amount of work It would have had to do to just wait actively for a couple more cycles. Does this make sense? So in one case, I'm wasting cycles by waiting actively in the other case I'm wasting cycles by going to sleep and being woken up again because these are not free operations And so if the critical section is really short then on average I actually win by waiting actively because I waste less CPU cycles I also get the resource faster, right? If I had waited actively I would have had the resource right here But by the time I get to sleep and wake up again all this time has gone by Conversely if the critical section is long involves lots of operations could ever potentially involve Going to sleep waiting for IO waiting for some sort of slow resource then it's always better to use a Sleep lock so here's an example again sort of the converse of the other one I have a long critical section the thread on CPU 2 is waiting actively and in this case at some point And it doesn't have to be very long It becomes better to go to sleep yield the CPU let somebody else do useful work and then arrange for the first thread to wake me up late Again, this is always true whenever there's any sleeping involved And that's why there are those checks in your current system So that if you hold a spin lock and do something that goes to sleep or calls yield The next attempt to grab that spin lock will will panic Questions about this at this point I Like this I feel like you guys understand all this stuff. I actually don't even have to give these lectures You just this is like I May not be physically able to continue giving these lectures if I can run into that thing all right Okay The what you guys have seen And what you guys have implemented is is pretty pretty common functionality particularly the weight channel So the notion of a weight channel is pretty powerful And you guys may not understand how general purpose it is the weight channels that you guys are using are not used Just in the context of the locks and the condition variables that you're using them for they used all over the place in the kernel and and the idea is pretty simple essentially it allows a thread to weight and Give the system an identifier that identifier is typically like a memory address that has some special significance So for example, it could be the memory address of the lock structure that I'm waiting for If I was waiting on IO it could be the memory address of the IO queue that the operate system is using internally or something like this It's just a like a it's just a name But of course it's a machine name so it's in hex and it has lots of ones and zeros in it And and this essentially allows me to wait for something to happen where that something is you know Denomified is that a word? I'm trying to use words that are outside my comfort zone now So anyway, that is named by some sort of key And then later I can wake up One or multiple threads that we're waiting on that When something happens and you can imagine again that this is a very general purpose primitive It allows me to wake up and put the sleep threads that are waiting on locks Condition variables IO You guys will use this later when you do assignment 3 to wait for particular Memory copies to be done to various parts of memory so that a process can restart whatever But this is that this is the basic operation of a weight channel And I just want to point out a lot of this stuff actually this is true for all of these things So has anyone ever used these primitives in user space? Does anyone every no one has ever used a lock in a user program? Anyway, we need better system courses. Yeah, so you can do all this in use of it I just want to make sure people understand these are not this is stuff that we're good We're teaching you because you're gonna need it in your kernel But this is something that you can use when you implement like a web server This is something you use when you implement any piece of multi-threaded code every good piece of Software today, sorry every good programming language and every good sort of framework has these types of functions, right? If you program in Python, there are Python locks mutex's condition variables If you program and go there are the equivalent sometimes the names are a little bit different go calls locks mutex's There are different terminology, but the functionality is essentially the same same with weighting all this stuff I can do it user level as well and use it to manage user level concurrency We're just happening to be used in it to manage kernel level concurrency in this particular class All right questions about locks Yeah Yeah, so the way channel happens to be implemented in a FIFO queue. I think currently, you know, it's 161 That's not guaranteed by the interface. So that's that's very important so and this is a good example of the hazards of Making assumptions about how things work So if you look at the way channel interface all wait wake one says is it wakes up one threat It does not say which that it wakes up It doesn't say the thread that was waiting the longest or whatever. It just says it wakes up a thread So if I went and changed the wake one implementation to wake up a random thread your code had better still work Now I will make an I will make a sad Admission here, which is that we our solution for reader writer locks violates that that interface contract So we make assumptions about the lower-level code and I did that and I'm okay with that I'm a grown-up now and I can make these sort of trade-offs But it would break if I changed the implementation of wake one, but that's a good point the interface doesn't say something specifically Users of the interface are not supposed to assume it, right? It makes it possible for the underlying implementation of the interface to change, right? It's a good question. Yeah Well, keep in mind spinning spinning the little bit spin locks and sleep locks Well, I guess that's true I mean, I guess I can implement a lock that allows me to wait for it either Through spinning or through sleeping, right? Whether or not I would use that to do prioritization. I don't know, right? Necessarily let me see where I am in time because I want to mention Okay, we'll come we'll come back to that when we talk about the schedule any priorities Yeah, other questions. Yeah Yep We don't want to stay away That's a good good question. So why is it safe to use spin locks inside the semaphores inside your locks? Yeah Well, okay So the way channel is going to unlock the spin lock before you go to sleep, right? But but again come back to the critical section arguments, right? There is a critical section inside of your lock implementation Okay, there's a set of things that have to happen atomically Specifically, there's a condition you have to check and then you have to go to sleep if you don't hold a spin lock there You can try this. I mean if you're done with your locks do the following That's been locked that you're grabbing move it inside of the check, right? Now the weight channel will break if you don't hold the spin lock before you call the weight channel But it's easy to fix that right just instead of grabbing the spin lock checking the condition and then going to sleep Check the condition then grab the spin lock and go to sleep And what you'll find is there's a race condition there, right? And so that spin lock is creating a very small critical section inside your lock and the fact that it's small Is what's important and obviously if you can't implement a sleep lock without needing another sleep lock You're in trouble, right? Like yeah, like I'm gonna call myself recursively and it would just be bad Yeah, but that's so this is an example of how we're using a spin lock to build these higher level points. Yeah question Yes. Yeah, that's true Yeah, it's yeah, it's busy waiting on the way to getting onto a sleep cue potentially Yeah, yeah, but absolutely. There's there's a little you're right There can be a little bit of busy waiting In the process of of checking the condition that that determines whether or not I can acquire a lock or not Yeah, right So remember I'm busy waiting to make sure that the condition doesn't change between when I check it and go to sleep Right, so if one thread grabs a lock and then goes off and computes at 100,000 digits of pi Other threads that try to acquire that lock will do a small amount of busy waiting and then they'll be on the way Q right, but you're right That's a great point. It does require a little bit of busy waiting to get on to that wake you, right? Yeah, but other questions. Yeah No, okay, so so let me make a distinction here And this is important for you guys to understand as programmers and something that we should have taught you four years ago Okay, when you guys use other pieces of code, right? So you when you guys are implementing your Pregnization primitives you're using the weight channel. Okay, we gave you the weight channel now look if you want to it's your Code you can make the weight channel do this, right? You can say that part of the weight channel interface is that it is going to use a FIFO queue But the description of the interface that you're using does not say anything else other about Does not say anything about wake one other than it wakes up one threat. So it gives you no guarantees in a more normal case you're using an external library that somebody else wrote right and So you have two options You can take that library and you can make it your own and start maintaining it and make it do exactly what you want But that's usually not what you want to do You want them to maintain the library because that's what they that's what they're doing That's how you collaborate on things But if the library maintainer says it wakes up one thread and you're assuming that it wakes up threads in FIFO order Then some day you may start failing all of your tests, right? Because the library maintainer decided it was more efficient to store the threads in like a heap or something like that And it stopped it stopped behaving the same way, right? So this is not about the fact that we can't guarantee that the threads come out in FIFO order the interface doesn't guarantee All right, but this is really important to understand when you guys read interface descriptions if the interface says acts That's it. It's all you get Actually just as a side anecdote so when people are you guys I always have to ask this question Are you guys old enough to remember back when Microsoft was getting all this trouble for anti-trust violations? You guys maybe we're in diapers. I guess okay, some of you are old enough Yeah, so at some point people are like and it's kind of funny now because Microsoft seems like a little behind the times now But back back then people were like really scared Microsoft's dominating the market and and one of the things people were concerned about was this idea that If you use Windows if you use the Windows API you don't have access to the source code So you call some part of the Windows API and you don't know how it's implemented Except if you're so let's say you're building Internet Explorer wait, so let's say you're building a Chrome Right, so you get this big book. That's like that thick that says Windows API documentation Okay, and then you read it and it says okay this function does this but the problem with the Windows API is after 20 30 years It's got like 18 functions that do any one thing you want to do okay, so it's like read a file Okay, take your pick off the menu. There's 15 different functions that do that in slightly different ways So if you're chrome you scratch your head a little bit You're like okay, I'm gonna pick that one will hope hopefully it works right if you work on Internet Explorer You pick up the phone and you call the guy who maintains that code and you're like which is the fastest like which is the best Which is the one that you're gonna keep maintaining which is the one that's gonna disappear in the next version of Windows And he's like oh, yeah, I'll check that out for you and whatever so this was one of the concerns people had about Collusion between Microsoft writing software and Microsoft writing their operating system was that the operating system? the software that Microsoft wrote can make assumptions about the way that the operating system worked that other people couldn't and You can imagine it going even further the guy who's developing Internet Explorer might be like it would be great If there was this system called it did X right Why don't I call up the guy who works in a team and be like by the way It would be really helpful to us It would really improve our performance if you would do this one thing. Oh, okay, you know, or if you're at Chrome You're like no way, you know just You don't you don't have that opportunity So this was one of the concerns back in the day about this sort of collusion with that Microsoft understood the internals of their interface everybody else had to try to use the interfaces written and wasn't able to make these sort of assumptions Interesting historical artifact That's another question I don't know how to answer that question you would have think with all the advantages they had All right, it's like, ah, but this is the thing. I mean it was like Anyway, like Imagine how imagine how bad it could have been right if they couldn't have made all those some Anyway, okay, let's Yeah, well, it's still alive I guess two people so he's an explorer other than my other than my other than my father who I've been begging for years Please stop I Will migrate all of your bookmarks to Chrome and then you can stop calling me every day because you're Alright, let's let's let's forge onward today. Okay locks are designed to protect critical sections It really is The number one reason to use a lock and if you think about the lock semantics that you guys of the locks you guys have designed You'll you'll see this Locks are not a communication or signaling mechanism.  They're designed to make sure that a bunch of things can access one piece of shared state And do so safely and that's really and they are good at that. That's what they're for, right? That's their core purpose. They are not good for other things Now to some degree lock release is kind of a signal because what lock release is doing is signaling the other threads that are Waiting that they can come in now. Okay, but this is a very very limited form of signal Okay, so let's talk about other types of conditions That threads that are working together on a problem might want to notify each other about and we're going to do this buffer buffer example so for example, I might have one thread that puts data into a buffer for another thread to process and thread the first thread wants a way to notify the other thread that there's data waiting for it Okay, this happens a lot right happens a lot in programs that you guys use where there are multiple threads Working together to do things, you know one thread takes the input from the from the mouse click Figures out where you clicked adjust some state and then call some other thread to re-render the UI or something like that Your child is exited You know, how do I notify a process that one of the sub processes are created as exited? So in contrast to locks which are really entirely a correctness primitive condition variables are a signaling mechanism condition variables are allowed to let threads Signal each other when certain things happen and wait for things Probably the most confusing thing about condition variables is the condition. Well, it's a variable part, right? Well, maybe it's both parts actually condition and variable because there's no condition or variable in the condition variables, right?  Where is the variable? Where are the condition in the variable? Outside the condition very yeah So condition variables are usually used as part of some conditional expression Frequently inside a while loop and with some global variable that multiple threads are accessing And the condition variable you can think of as a way of one thread notifying other threads Speaking of noise Geology department's moving rocks around again Okay, so you can think of the condition variable is allowing one thread to notify other threads about interesting changes to the variable Right. There's some change to the variable that I want to alert other threads to because it's going to cause them to do something something interesting and This condition usually represents some change to shared state right a condition on a you know a variable has changed and Some condition that you're waiting for may have changed as well All right, how much time do we have left? Oh five minutes. This is awesome.  I'm not done All right, so condition variables can can convey information about the world and this the more information But it's really any information right all locks allow me to tell all locks allow threads to do is tell each other that They're done using a critical section. That's it condition variable the information that I convey could be a lot richer All right, so let's go through an example with a shared buffer. This is sort of a classic condition variable Example you can find code for this online whatever So here's here's what I want to do. I have a buffer that stores Information some sort of object and I have a producer that puts things into the buffer and I have a consumer that takes things out of The buffer All right, why would I want to do this? It seems dumb. Why not just let the producer thread like process the data itself? Why would I ever use a queue like this? Does anyone come up with like a realistic example? Again, it doesn't seem dumb right did I concoct this just for the purposes of this Example so that we could use a condition variable I mean an example where it might make sense to do this sort of separation of work Yeah, okay, that's it's not bad. Yeah pipe pipes like I want the first process to keep doing what it's doing I don't know how I have to wait What else? Well any sort of website, right? So so frequently on websites when you guys click something or click submit There's all sorts of stuff that has to happen like when you buy Things on Amazon when you hit buy now It's not like it completes the whole transaction while you're sitting there, right? It could spin until the the package arrived on your doorstep. It's like No, oh, it's delivered right now, okay? Clearly. I mean obviously there's parts of the latency I want to hide in those parts. I don't care But the point is I want to do just enough work so that I kind of know that things are kind of okay And then I want to draw things I want to redraw the display and finish the transaction in the background There's transaction processing going on. There's emails that get generated blah blah blah right and those can be done in the background by separate threads So the thread that's actually rendering the UI can return and give you a useful information This is like a common way of reducing the latency of an interactive application by farming stuff out to background tasks Right, I get a background task to send the email so that I can return immediately and show you a nice nice confirmation page Okay so Why are so you might think can wire condition variables as synchronization mechanisms just may talk about this I think we'll start with this example on Friday One of the things that's really important when I use CV signal CV weight And CV broadcast is making sure that the condition doesn't change between the time that I notify Other other threads and the time that the threads check the condition So this is very similar to the interior of the lock that we talked about before where there can be a small race So if I so for example if I check that the buffer is full and the buffer is still full And then I go to sleep, but I don't guard that carefully I can end up sleeping despite the fact that there is space in the buffer and for this reason condition variables of The the type that we use like to this is exactly the same this example, right? so if I don't guard this buffer state carefully if The other thread gets switched in put some things in the buffer I'm already on my way to the sleep queue and so now I have a I have a condition I don't want which is I have a consumer that's sleeping despite the fact that there's data in the buffer And so for this reason I use condition variables with a lock. I don't know how this Maybe I took that example All right, so let's stop here on Friday. We'll do the producer consumer buffer example And we'll talk about deadlock. I'll see you then good luck with assignment one


The FBI visited the home of a Trump supporter in California. According to these posts, the agents wanted to discuss a person that the homeowner had met online. However, when the agents noticed that the interaction was being filmed, they immediately stopped talking and left the premises. This incident has sparked some online discussions and raised questions about the FBI's actions. Is Sacramento FBI? What? What is it? Looking to talk to David? Or what? Uh, hoping he can help us with some of what he met online. Alright. Yeah, I'm just gonna record. Yeah. Um, what is it? What is it, uh, concerning? I don't tell you if you turn it off.  Okay, the person we're here to talk about... I can't say it on camera. But it's not about you. Okay, well then I can't. I'm not gonna speak. You're not gonna say it on camera. What are you even up to? I need your business. You're refusing interview? I'm not gonna say it on camera.


Hello everyone, and thank you for being here today really toughing out the whole day Congrats, and thank you for being here and holding up. So this is not a mistake, right? Eclipse Foundation and we're talking about hardware Well, you will understand why in a moment, but before that, let me introduce myself So I'm Frédéric de Viens of Fred. I manage IoT and Edge computing programs at the Eclipse Foundation And I've been around for a while doing various things and you can find me on social media if you are so inclined Also, you know if you like what you hear today You can have the asynchronous version of this in a chapter of this book and many other things about Eclipse IoT components as well So if you want to annoy a significant author or one of your kids, you know, this is the perfect gift for them All right, so today our presentation is really focused on three things First we'll take a step back and make the case for open source in hardware plus software, you know this specific combination Then I will introduce you to open hardware group the actual creators of the Core 5 MCU and Well to the Eclipse Foundation as well since we are working together to build this comprehensive risk 5 based Hardware plus software ecosystem, and then I will describe what the ecosystem is All right So the starting point for this is really the fact that if you want to build a successful ecosystem You need to be open in everything that you do and a good example for this is bread The secret of breadmaking we we don't know who invented bread, right, but the secret So to speak of breadmaking has been around for a long time and wherever part of the world that you visit Maybe a bit except Asia, but even in Asia you will find local varieties of bread And what's great is that wherever you go? Then of course you can try new varieties and anyone willing to start a breadmaking Company or just do bread at home can do so freely, right because it's been an open secret That's not a good example So I mean we are in a place where beer is important So it's the same with beer, right wherever I go in the world I can't find local beers that they are interesting and all of that Anyway, we could take many other examples But all of those things show that when you have this kind of open secret which is really shared then you can build Successful ecosystems on the top and those ecosystems are much more valuable Let's say then if the secret of beer had been you know guarded secret Possessed by five companies and you only have five Different beers in the world, right and this doesn't preclude openness This openness doesn't preclude commercial success because of course the secret of breadmaking is open But now the specifics of my favorite beers I don't know. I wouldn't be able to replicate necessarily exactly the same thing. So you can still do Well successful commercialization So open source at its core really is about the fact that we have the source code made it really available But whoever wrote it they hold the copyright and it can be redistributed modified including for commercial purposes But the third point is really important vendor neutral governance You know is also a key thing in the sense that well, sometimes there are single vendor open source projects and Well, it goes as well as you can guess, right? I mean In any case certainly that's the whole difference Let's say to take a Google example not that I hate Google anything But that's the difference between Kubernetes, which is vendor neutral governance versus Golang where Google decides whatever goes in the language And there's a big difference between the two dynamics there And of course, I'm not neutral there in the sense that I represent an organization that does vendor neutral governance But we feel at the Eclipse Foundation that true open source is the one where there's a level playing field for everyone All right, and open source is becoming of course important for many organizations So every year we run an IoT and Edge developer survey and an IoT and Edge commercial adoption survey And we ask people hey is open source is strategic for you So in the latest edition edition of the commercial adoption survey we found out that for 75 or 74 percent of organizations open source is in our brainer. They consume it. They use it and many of them Are even you know looking purely at open source solutions This would have been unthinkable in the 80s when I was just a kid So this shows how much the open source movement really transformed the dynamic of software over time and Of course the main advantage of open source Is not just the fact that it's free the cost But rather the customization and the fact that you have more control over what we do and this is Critically important in terms of IoT and Edge or even purely embedded projects and why is that well Let's say that when you think about embedded IoT Edge computing those Projects are not run of the mill IT projects And if you're used more to the IT side of things like I've been for a long time Then of course having the OT mindset that characterizes embedded is a different game, right? Because essentially when you have a factory you don't rip and replace the machines every two years or every five years you are making big capital investments in things that will be there for a long time and This means that the solution has different characteristics than the typical IT solution or even the typical cloud service like you know This is Google slides behind the scenes. So of course I wanted to be up and everything But I won't complain if I have to reload the page if I want to get new features Whereas if we're talking about industrial automation, of course then You know things need to be more stable because stopping for just an afternoon could be 10 million euros lost of production value So different mindset so the lifespan the Teoreogeneity the constraints that the equipment have to face it of course the fact that when we talk about IOT the network is an Inerrant component of the solution all of those things make embedded IoT and Edge really different and open source helps with all of those aspects If you have a longer lifespan, then of course having the source code for whatever you have helps you maintain it over the long term Or pay someone to maintain it The iterogeneity is of course Well very frequent in IOT embedded an edge because you will pick whatever microcontroller fits the job So you will have arm base risk 5 maybe if you are older even MIPS or author more exotic things lying around and You don't want those platforms to change too much because of course changing the platform means that you will need to rewrite a chunk of code and everything That's on those microcontrollers that has been debugged and battle-hardened in the real world You don't want to touch and you want to focus on creating new value with new products, of course The constraints whether they are physical and all of that once again having the code is helpful and the connectivity Well, that's the same if you deal with Especially radio technologies that have open components that are not controlled by a single supplier for the cheap set or the modulation and things like that You have more choice and then it's easier of course to integrate into whatever you are building and this brings us to open hardware and core 5 so is strictly risk 5 has been an open source instruction set and We are not necessarily all chip designers. I'm not a chip designer. So of course Having an open source instruction set is great for people that are deeply knowledgeable about chips But that's about it. And the fact is the resulting Core or the resulting processor that you build can be closed and many many many risk 5 solutions in the market are closed so essentially realizing the power of open source in the domain in the domain of Software they are where people in the risk 5 ecosystems that said hey Let's go further and build together open source processor cores and microcontrollers That everyone will be able to work with to make chips off and to integrate into whatever products they want And this is what happens. So essentially open hardware group has been created. I think in 2018 2019 by Rick O'Connor a former executive director of the risk 5 foundation So a chip maker by trade and they have incorporated in Canada and the goal is really to build the full The full ecosystem of what you need not only to have a chip but everything around so the IP for the processor cores software tool chains emulation platforms verification test everything and everything built on GitHub in the open Okay, so you can literally download the system very log for the cores I will tell you about modify them run them on FPGA is whatever. It's really up to you It's truly truly open source and core 5 is the first family of cores that open hardware group put together as their initial effort and Still going strong. So there are plenty of members in open hardware group Okay, so you see the strategic ones at the top of the slide and plenty of other commercial Organizations there, but if you are from academia, there are plenty of universities and research centers Doing work around this technology as well And of course, there's a broader family of friends around the eclipse foundation is proud to work with open hardware group To facilitate and enable this ecosystem and for whatever reason they are indicating whatever bank they are doing business with I never understood that one, but you know, they provided me the slides. So I'm not complaining All right, so what's in there in the core 5 family right now? And of course this evolves over time So maybe next year there will be other things but for the time being there are three main cores in the family so cv2 32 bit two-stage ultra low power and really The idea there is to have the smallest thing possible for the simplest applications Cv4 32 bit four stage. So a family with of course a bit more complexity there Implementing the pulp extensions. So if you're not familiar with pulp That's for parallel ultra low power So essentially the idea is that you have hardware based accelerators in your core for specific instructions of the risks 5 ISA or and this of course reduces power consumption every time that you have hardware acceleration anyway, so it's got specific extensions for DSPs as well And so that's a fairly fairly capable core and this is the one that we have in the core 5 MCU or at least one of the cores from the cv4 family and then there's the cva6 core Which is of course a bit bigger more complex and is able to run full operating systems as compared to just real-time OSes like like Zephyr or Zephyr or Friartos, let's say and it can support Linux in this case So really we have a family that spans everything that's you know from the gateway all the way down to your very very constrained device and Really the use cases for the cores are are really obvious I mean for ultra constrained device you would pick cv2 or cv4 for edge computing And IOT gateways then maybe the cv4 cv6 depending on how much power or whether you are operating on battery power Versus DC power or something like that and of course for full IOT platform Then the cv6 family is the way to go and there is even a vector core processor that you can add to it to accelerate whatever vector mathematics does in fact and By themselves open hardware group They have a software task group that really has a very broad scope in bringing everything you need in software to support The core 5 family of course and this of course Includes the compiler to chains and the processor and platform models even an IDE An hardware abstraction layer benchmarking facilities and demo application. So all of that are things that they work on and normally most if not all of the work that they do at least on the compiler to chains and operating systems are Proposed or pushed upstream so they are not kept in into anything special now They could be little exceptions here and there as you know, this is about negotiations I guess with the current committers, but in any case they are working as closely to upstream as possible For the various things that they are touching Now the eclipse foundation I Will need you all to stand up and Place your heart your hand on your heart and repeat after me I solemnly pledge to tell off all of my friends Especially if they work in tech that eclipse foundation is more than just the ID Okay, don't do it, but please say it We've been doing IOT Well since I mean 2011 so that's 12 years maybe a bit more at this point in time We've been doing edge since 2019 so going on five years and yet people still associate us with the clumsy Yet still widely used IDE from 2004. So, okay, we've got a marketing problem there But in any case, yeah, we take IOT and edge really seriously at the eclipse foundation. So of course a few metrics there I mean four hundred and eighteen projects and the idea are just you know 10 or 20 out of those maybe if we are generous So eclipse is much more than just development tools and IOT and edge computing is one of our four strategic Peeters at this point in time. So of course we have things you don't care about because this is embedded OSS summit So, you know cloud native Java you don't care But still the fact is this is a thriving open ecosystem Which is more relevant now that it's been you know taken out of the clutches of the previous commercial owner that I want name but anyway and Of course, we are having a big success story in automotive as well So if you are interested in automotive grade Linux, that's great That's great technology But we are building in the eclipse software defined vehicle working group a full suite of tools that enable you to really build whatever components go at the software level into a car all Of that in the open. So please have a look if you are more on the automotive side of things And of course development tools are still relevant to us But you know if you work with the Arduino ID or the arm embed ID you've been using eclipse technologies without even knowing it Wow, and those particular ideas are not built on the traditional Java based desktop ID But on eclipse on eclipse Taya, which is essentially Visual studio code done right if I can simplify things like that in the sense that it's not tied to any specific vendor and Connects to open vsx and open and vendor neutral repository for the extensions Okay, so you've been using eclipse and you didn't even know it, but it's not your father's eclipse. So that's great All right So we have an IOT working group with plenty of organizations and our strategic members in the space our Bosch Rotech and Red Hat And we have lots of friends And we use this indeed and the little reference architecture to position things in our ecosystem and the thing is of course we don't have I would say that the full blown operating system to call our own But we've got something in the space called on hero Which is using the Linux and Zephyr kernels and adds value on the top. So there was a talk on this Earlier today, but anyway If you missed it, you can watch the recording It's really interesting technology in any case I'm putting those logos there not for you to squint and try to figure out where those things fit I've seen you squint But rather to impress on you the extensiveness of the toolkit This is just half of the logos and I'm just positioning them to impress on you that you know there's plenty for you to explore there and use and of course not all of that is directly related to whatever Goes on the microcontroller, but many of those things do or at least could go there And we work with open hardware group really to build this comprehensive stack Where essentially they provide the hardware the base tool chains and even what they call the core 5 IDE that we'll see a bit later and we have our IoT building blocks on the top And I won't pretend this is a fully integrated stack that you can use and install You know single installer and everything is done in five minutes. No, that's not a point We are working on those building blocks so that you can build whatever makes sense for you using them Right, so this is not pre-integrated and pre-tested the goal is not to rivalize with any of our Members in the commercial space, of course The goal is to enable innovation because well if you use if you don't use our building blocks You're very reinventing the wheel and you are literally wasting time instead of providing value to your customers All right, so now let's dig a bit More into what makes the core 5m cu and what is there inside? So first we start with the CV to treat you a 40p core gosh Formerly known as the risky core if you are familiar with that particular space So there's been previous work done at ETH Zurich and In other places that essentially were previous incarnation of this course. So this is proven in mature at this point but in any case What's found in there is really? Well Nothing special if you look at it from a pure chip design perspective The true value of this is that it does whatever you need it to do But it's completely flexible because you've got the source code for it Okay, so you can modify it synthesize to FPGA test your modifications and then you go and Make a custom chip out of that given the recent supply chain constraints and all of that having a Design like this which is completely open is certainly attractive now. I won't pretend that chip making is easy It's not but the fact is that we tremendously lowered the point of entry by having all of that built into the open okay Now the full MCU of course adds lots of value around the base processor core So the target frequency at this point in time is rough roughly more or less 400 megahertz It can run three RTOS, but people have been working on Zephyr and other Alternative real-time operating systems for the platform. It's got an embedded FPGA Okay, so you can leverage that for another level of flexibility in there it's got a full complement of interfaces and right now we are in the last very last miles of getting those chips made with global funders and You know, I will tell you about the full blown evaluation board a bit later in the talk, but really What's in there once again? There there's nothing tremendously innovative in this microcontroller Except from the fact that you can add whatever you want to hit if you wish so and have the skills Okay, and if you don't have the skills you can find them as we're in the market So this flexibility is really what makes this stack Different and the fact that the community does all of its work in the open So if you want to submit a bug report for this You can you go on Github and anyone can contribute and voice their opinions So those are the organizations that are more deeply involved with this project and of course Nicholas Foundation is one of them and Really if you want to get started right away The easiest way is to buy one of those indeed and the FPGA boards So both the Nexus 7 and the Genesis 2 are supported However, the Genesis 2 is more expensive and really overkill for just the microcontroller So, you know, you would use that to run Linux on the top of the CVSX core. Okay, and they you know There's nothing that prevents you to deploy the micro the microcontroller to it but the cheaper A7 is good enough if what you target is really the core 5 MCU and Don't forget the little debug probe. It's really it's a it's a debug probe which works with open OCD and greatly facilitates of course the debugging of anything that you would deploy on this and You can download and use the core 5 IDE free of charge as well It includes all of the tool chains pre-configured and ready to run examples for the FPGAs and with documentation And getting started guides and yes, I know it's built on the traditional desktop IDE So maybe you won't like it But it does a job and people are already working on VS code support and utter, you know Other IDE initiatives in the ecosystem as well So if you don't like that one, maybe there will be in the future something else that maybe you like But stop complaining. I mean this is good enough as a starting point at least And we have a tremendous dark mode now. So it's so trendy and This is the actual death kid that you will be able to buy I I was hoping to have this today, but Global fondries are taking their sweet time and every time that our friends at open hardware come back with their Later synthesize for the chip. They find new little things to fix Anyway, so they still play that ping-pong game at this point in time But by the end of the year we are expecting to have the chips and the boards So if you are interested and want to stay in sync, I put on the slide the link to the campaign so you can pre-order at least Register your interest toward the one if you are so interested But really the death kit will be really really nice because of course you have Not only the the core, but the full the full blown MCU You will have the onboard debug probe in there from Ashling. Okay, so pre-integrated you You won't risk losing mine like I'm doing all the time USB-C for terminal in onboard debug access a micro boost on board socket and an expansion adder so there's already an ecosystem of hardware components that you can work with there as well and There's a built-in temperature sensor because otherwise, you know, it would be hard to do demos at shows. I guess so in any case you know, it's a fairly complete board and As I mentioned, you can register your interest by using the link on the slide So at this point That's it for now I'm happy to take any questions you may have but don't go too deep on the chip side of things, please I'm not the chip designer. I pee to try to answer any question And as a reminder, you can find me for the time being on Twitter otherwise on mastodon So thank you so much. And now if you have any questions Did I scare you? Yes A few hundred dollars so a bit less in euros, I guess so the goal is really for this to be affordable and and You know spread the skill set and awareness of the platform not to make any money as I mentioned open hardware is a non-profit So really they try to enable the community with the board and it's the same with The FPGA is so the Nexus. There's a student edition if you can qualify for that So it's it's already fairly affordable But if you are a student or know a student that can buy one for you, then it's even cheaper Other questions, so okay, so if you don't have questions, I We have one online All right or five At this point in time. I don't think at death kit for okay, so the question was well, okay, you read it. Sorry I don't Let me restart reset reboot. All right. So at this point in time. I don't think they have Projects for a development board with the CV a six core the one which is able to run Linux and free BSD and You know beef your operating system so to speak But if there's demand who knows I mean their focus is really is the on the microcontroller right now because they feel at open Hardware that there's the place where they will make the largest impact However, I think that Intel and AMD and the other players and even the typical suppliers of Gateway capable or gateway class controllers or System on a chip need a bit more of competition to truly in a variant and lower the prices a bit So of course if you a six could lead to that So if there's an interest let them know at open hardware I can put you in touch if you are interested in a deaf board for the CV a six and if they have enough feedback coming from the wider Community, maybe they could do it but to my knowledge and Of course, I keep in sync with them, but not every day. Let's say so there's no such deaf board planned for the time being So if there are no other questions in the room, I have a question for you then since you are attending So overall raise your hand if you feel this is interesting and could really Transform a bit, you know the market for microcontrollers Okay, okay good and for the others, that's fine. You will be convinced five years from now So for those not seeing that of course about maybe half of the room raised their hand You know, which I consider a very successful outcome for you know Maybe the first time you were exposed to this and for the others, you know, we can chat In any case, thank you so much once again