http://bugs.winehq.org/show_bug.cgi?id=28413
--- Comment #16 from Jörg Höhle hoehle@users.sourceforge.net 2011-11-11 06:28:30 CST --- There's a decades old debate whether small ints like UNIX file descriptors or FILE* pointers are good design. Unlike pointers, small ints are easy to check for validity but can be reused inadvertently, exactly like my 0x8000 example.
Raymond Chen describes that MS mixes both approaches and uses the upper 16 bits to store a unique number, while the lower 16 bits are still an array index. http://blogs.msdn.com/b/oldnewthing/archive/2007/07/17/3903614.aspx
The late waveOutReset(0x8000) from thread 1 would be returned INVALHANDLE and no more disturb the new handle 0xABCD8000 from thread 2.
Wine should do something like that too (e.g. via a counter or random number generator). In WINMM_GetDeviceFromHWAVE if(junk != 0x1) becomes (junk && junk != device->cookie) return NULL; For completeness, upper 16 bits = 0000 must be a pass-through for 16bit apps.
It is currently very unfortunate that handle numbers may be reused immediately in Wine. Think about how UNIX systems avoid reusing process numbers all too soon. It's the same issue.