Sleep Tight

        Submitter: Muli Ben-Yehuda

        Language: C

        Date:        2003/07/04

Suppose there is a platform, call it A, that has a

void msleep(unsigned int milliseconds); 
facility, which is, of course, useful, but not standard. You need to port code that uses it to another platform, B, that does not have msleep(3) but does of course have the POSIX sleep(3),
void sleep(unsigned int seconds); 

- what do you do?

Apparently, there's a line of thought amongst programmers who do not understand what portability means that simply wrapping up any system service in a MACRO is enough: printf() becomes PRINTF(), sleep() becomes SLEEP(), etc., etc., ad nauseam. The fallacy here, of course, is that they assume that the name might change, but the interface won't, which is dead wrong. Name changes are the least of your worries. It's interface changes that you should be concerned about, and your "portability layer" must present a common, unified interface, not just "the interface of whichever platform this code ran on first."

The result of this line of thought is code like

#ifdef _PLATFORM_A_
#define SLEEP(x) msleep((x))
#elif defined(_PLATFORM_B_)
#define SLEEP(x) sleep((x))
#endif

The programmer is happy, since the portability layer "works" and supports both platform A and platform B without changing a single line of code, thanks to the SLEEP() macro. The code will sleep a thousand times more than it should on platform B, but that's an implementation detail, obviously.