by kazinator · 7y ago · 0 comments
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.
One is a minor esthetic problem, the other would be a nightmare to support and work around.
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.Holy shit.
Standard practice in C is to prefix your functions with their 'namespace' to avoid collisions. In this case, I don't even like that they used "choose" instead of "mill_choose" or "mill_select".
But redefining select() is a whole other level of inconsiderate.
Did you notice the #define end, which is a common name for struct members and local variables. Redefining select is certainly not a whole other level of inconsiderate in that light.
The caveat is that whoever uses this should include it last, after any other headers, and then deal with the macro clashes: don't use "choose" or "end" in their code and so on.
One way to mitigate this is to have these macros in a separate header, say "libmill-macros.h", so that the rest of the API is available without them.
I don't know what's happened to HN these days. The sheer number of kids who have come out of the woodwork to flame this guy in different ways on different threads -- all with the same level of ignorance on a really simple topic -- is nothing short of deflating.
If someone someday makes a name that collides with yours, that's a problem for that "someday". Avoiding today's problem today seems pretty valuable.
POSIX systems in fact go out of their way to support conforming ISO C programs, which are permitted to contain things like:
void select(int x) // not in any ISO C reserved namespace!
{
selection = x;
}
In the case of the GNU C library how it works is that select is a weak symbol aliasing for __select. If anything in the library needs to use select, it calls __select, so as not to be derailed by a user program that redefines the weak symbol.Issues with macros are less severe compared to clashes in actual external linkage. No weak symbol support needed in shared libs; just something has to be #undef-ed here, or an order of headers rearranged there.
Note that the code already has some multiplexing gadgetry there, switchable between Linux's epoll, BSD's kqueue and the portable poll (see epoll.inc, kqueue.inc and poll.inc).
> To software written in C, the syntax or naming choices of other languages are really quite irrelevant.
However, at least one person out there thinks that the syntax and naming choices of Go are relevant to software written in C, so they wrote this thing called "libmill".
Oh wait ... what were we talking about?
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...) 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);
} choose { cases... }
using https://www.chiark.greenend.org.uk/~sgtatham/mp/ techniques.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_Nlibmill 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?
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.
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.
Are you aware that you're making that complaint in a discussion on Libmill: Go-Style Concurrency in C?
It's one thing to skip the submission to jump to the comment section. It's an entirely different thing to start mindlessly criticizing constructive criticism presented by fellow users while being entirely oblivious to both the subject and the context. That's just noise, and contributes zero to the discussion. In fact, it takes an awful lot from it.
It's a very common source of hidden bugs thanks to FD_SETSIZE.
If you want to select on file descriptor 1000, the bitmask has to include zeros for descriptors 0 through 999. Those get copied to the kernel and iterated over dutifully.
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.)
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.
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.
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.
In here, nobody knows (and more importantly nobody cares) you are a dog.