Depends on which country you're in. In the US, it's called a "living will", in the UK an "advance directive". Whatever the name, it's a way to declare your wishes in a legal document, in the event you become unable to communicate for medical reasons (like being in a coma). Again, depending on your legal jurisdiction, you can either have a lawyer make one for you, or might be able to get the forms online and do it yourself. They're commonly obtained by elderly people as a standard part of estate/end-of-life planning, but that's not their only use. I have one, for the exact same reason you seem to be asking about the topic.
Limitations also depend on what legal jurisdiction you live in. In my jurisdiction, there's a serious restriction on refusing life saving medical care - you basically have to be declared brain dead by two doctors - and a living will may not be legally binding - in my case, my next of kin has my medical power of attorney, which can be used to override my living will and keep me alive indefinitely. <sigh>
But, it's worth looking into. Maybe where you live there's a more right-to-die friendly legal environment.