6 ms·
The macro should be called select, since that's what Go calls it. (Of course I know there is a POSIX function (not C) by that name; so what). This "choose {" "
by kazinator 8y ago
The macro should be called select, since that's what Go calls it. (Of course I know there is a POSIX function (not C) by that name; so what).
This "choose {" "end }" business is an awful way to design the macro. If you need nesting that involves a block, with prolog and epilog material on either or both ends, you just have to hide that into the macro:
selbegin;
out(ch, int, 42):
foo();
in(ch, int, i):
bar(i);
otherwise:
baz();
selend;
I don't understand the downvotes. I have 30 years of (continous!) C experience. This is the best practice for doing a macro like this.
This "end }" business has improper nesting, visually. The choose is outside of the braces, the end is within; it is scatter-brained. There is no error checking for a forgotten end.
I'm looking at the implementation and it's looking quite weird! choose simply expands into mill_choose_init__, and end expands into mill_choose_end__. The definitions of these seem as if they should pair together:
#define mill_choose_init__ \
{\
mill_choose_init_(MILL_HERE_);\
int mill_idx = -2;\
while(1) {\
if(mill_idx != -2) {\
if(0)
#define mill_choose_end__ \
break;\
}\
}\
mill_idx = mill_choose_wait_();\
}
But, oops, look at the obvious lack of balance in the braces.
mill_choose_init__ {
mill_choose_end__ }
we are putting a brace after the if (0) which balances the one after break, and the final missing brace that balances the one opened internally in mill_choose_init__.
There are best practices to design this sort of macrology that don't require the user put braces in weird places to balance the inconsistencies in macro expansions.
Speaking as a professional, I would not pass this in a code review.
That we have to use a flaky preprocessor in order to get a half decent syntax is already a significant regret. We must not compound the regret by allowing the macrology to be less than as good as it can be.
- frenchman99 8y agoAs someone unfamiliar with this library or concurrency in C for that matter, I do not understand your comment. The code examples on libmill.org look simple enough that I'd want to use it. What is it that you think is not right about it ?
- chrisseaton 8y agoI think their ‘should’ is just an idle opinion, not a technical comment as in saying it’s broken.
- kazinator 8y agoIt is an opinion, and it was obviously written in spare moments that can be identified as idle. But it is far from an idle opinion. I consider that a personal attack. Furthermore, my opinion here is, in fact a technical comment. Hidden open braces inside a macro expansion that are explicitly closed by ones written by the user, and vice versa, is actually a technical issue, not simply aesthetics. The two are not entirely separated.
- chrisseaton 8y agoWhy ‘should’ it be written as ‘select’? How is the current name incorrect? It’s just an opinion. The downvotes are probably because you’re expressing a naming bikeshed issue like your opinion is undeniably correct. ‘I would call the function ‘select’’ would be a reasonable comment.
- kazinator 8y agoIt doesn't match the Go syntax that it's trying to quite faithfully imitate. This is for a psychological reason, which I'm dispeling as a needless concern. It's fairly obvious to me that the author wanted that identifier; I'm just saying that the reasons against changing the name are weak.
- chrisseaton 8y agoI don’t know what you think ‘should’ means but it’s not ‘I think this way may be a better trade-off.’
- ori_b 8y agoAnd the Go syntax doesn't match the Alef syntax that it's trying to emulate. Go is clearly wrong here, and the correct name is 'alt'.
- microcolonel 8y agoIt's pretty ludicrous to alias select since it's the sort of thing you're likely to be calling in a concurrent program.
- kazinator 8y agoIt's not really the sort of thing you'd use in a modern concurrent program. There is no reason to use select on a system where you have poll. As of 2001, POSIX moved select into its own header, so you shouldn't be getting a declaration of it any more from <sys/time.h>. libmill provides some wrapping for polling mechanisms; you'd probably be going through that in libmill-based sources. The clash, if it happens, has very easy, localized workarounds. Note that the library has a macro called end, which is a very common identifier in programs. Why didn't the author consider that a problem, but switched Go's select to choose?
- legohead 8y agoselect is super well known in C, as I'm sure you're aware. I have over 20 years in it myself. sure, workarounds are easy, but that's not the point. your suggestion that it should be named the same as Go is a bit ridiculous. why is that even important? what is important is potentially confusing developers, and adding a new "select" into C would do just that. the only mistake is Go using the word select to begin with.
- kazinator 8y agoselect isn't in C; it's in POSIX. I worked in Windows development shops where nobody knew select; but they could write WaitForMultipleObjects code in their sleep. A strictly conforming ISO C program can use the select identifier however it wants, including as an external name for an object or function. > your suggestion that it should be named the same as Go is a bit ridiculous It's not any more ridiculous than wanting to have a library which imitates Go concurrency down to macros that imitate some of the syntax.
- xeeeeeeeeeeenu 8y ago
- pierrebai 8y agoI have a hard time wrapping my head aroun dthe fact that you think the brace placement is a problem while entertaining the suggestion that calling a macro "select" with the obvious complete breakage under unix would be fine. One is a minor esthetic problem, the other would be a nightmare to support and work around.
- kazinator 8y ago> suggestion that calling a macro "select" with the obvious complete breakage under unix would be fine. There are lots of API's in the world; you can't ponder about all of them when naming a macro. How do you know the next system vendor doesn't have a choose function? Or if not the next system vendor, then some third party library or module you'd like to use or whatever. POSIX doesn't reserve the select identifier for use as a macro, unless the header file is included which defines it. It is not "complete breakage", and there are workarounds in that situation, one possibility being: #undef select #define whatever mill_choose_init___ The breakage only happens for code that uses either the select macro or the select function, if the headers are included in the proper order: the system headers first, then libmill.h. I.e. this by itself doesn't cause a problem (except in the improble case that <sys/select.h> defines a select macro): #include <sys/select.h> // #undef select at worst! #include "libmill.h" // with #define select macro This does cause a problem, 100%: #include "libmill.h" // with #define select macro #include <sys/select.h> But you should never do that: do not include local headers before system or third-party headers, unless you're sure they don't define any macros. There is also no problem if the rest of the file uses the select macro. The only issue arises if the rest of the file wants to use the select function, and then we put in a workaround. You're not going to call the select function in hundreds of places, where you also need the libmill header. TL;DR: it's not anywhere near a "nightmare". Moreover, libmill defines a few macro with short names. Conceivably #define end could be a problem, as well as #define in(..) and #define out(...). For instance if we have a "range.h" which has this: struct range { int beg; int end; }; and we do this: #include "libmill.h" #include "range.h" then libmill's end macro will screw up the struct range declaration, not to mention any place where we use end as an identifier. It's not an uncommon local variable name, like for pointers pointing to the end of a memory range.
- Waterluvian 8y agoExperience doesn't always build upwards. Sometimes it's 30 years of learning to see the world a certain way that fits your view. Bad habits and unique domain expertise and everything in between.
- kazinator 8y agoDo you have evidence that my comment stems from years of developing a poor world view or entrenched bad coding habits?
- nothrabannosir 8y agoYou’ve judiciously used the length of your career to buttress various controversial claims throughout this thread. Don’t get me wrong; I love me a controversial opinion. Nothing quite like the sound of a crumbling institution. But your constant claim to authority is jarring, and to put the burden of proof on someone else here is a bit too much. He merely questioned the ironclad value of your opinion as gospel “just because you have a lot of experience”. He didn’t even say it’s definitely wrong , but that it’s not necessarily always a good argument. That is completely fair. You can’t go and ask OP now to prove that you somehow hold a mistaken belief. You want to make controversial claims? Great, please do. Love it. But you’ll have to back them up with more than just your say-so. Experience accounts for nothing in iconoclasm. Thankfully. (And I actually think the points you raise, themselves, are interesting, and great food for thought. For the record.)
- kazinator 8y agoI've carried all the burden of anything resembling proof in this entire thread, in the face of baseless claims that the world will fall apart if you have a #define select macro in a header file that simulates Go syntax! I've exhaustively explored pretty much the entire space of this. You can whip out and environment with a compiler and try those things I wrote about. There is literally nothing more to add.
- iririri 8y agoI'm pretty sure it's possible to get it to look like "choose { cases... } end;" instead, which is what I did in a mostly joke library I wrote to see how far I could go with the C preprocessor: chan_select(2) { chan_case_send(c, &foo, 0): do_stuff(); chan_case_recv(c, &bar, 1): do_other_stuff(bar); } chan_select_end; But the implementation (https://github.com/iriri/libcee/blob/master/include/cee/chan.h#L294 https://github.com/iriri/libcee/blob/master/include/cee/chan...) is both horrifying and not as efficient as just doing the sane thing: chan_case cases[] = { chan_case(c, CHAN_SEND, &foo), /* unfortunately still a macro but it's basically just a struct literal */ chan_case(c, CHAN_RECV, &bar), }; switch (chan_alt(cases, 2)) { case 0: do_stuff(); break; case 1: do_other_stuff(bar); }
- a1369209993 8y agoActually, I think you could pare it down to choose { cases... } using https://www.chiark.greenend.org.uk/~sgtatham/mp/ https://www.chiark.greenend.org.uk/~sgtatham/mp/ techniques.
- comex 8y agoI'm not a fan of your suggested approach because automatic reformatting will remove the indentation, whether done in an editor or with a tool like clang-format, etc. To be fair, automatic reformatting does have its limitations, and there are some cases where it's best to just disable it, such as in the definitions of some macros. However, I prefer to avoid requiring users of my macros to turn it off, if possible. In this case, I think you could make it work without any macro at the end, just a closing brace, without changing the rest of the syntax (including how the label-like constructs implicitly open a scope, which I think is a bad design but whatever). Something like this (for simplicity I've omitted backslashes and the concat calls needed to make label names unique): #define choose if (1) goto register_options; else while (1) if (1) { /* We get here once the registrations are done */ void *target = mill_choose_wait_(); goto *target; } else register_options: if (0) { #define in(ch, type, var) } else if (({goto register_N; did_register_N: 0;})) { type var; register_N: mill_choose_in_(ch, &var, &&picked_N); /* computed goto because why not */ goto did_register_N; picked_N
- deleted 8y ago[deleted]
- emilfihlman 8y ago>I have 30 years of (continous!) C experience. In here, nobody knows (and more importantly nobody cares) you are a dog.