Make stdin pollable on Windows

You can now play Super Mario Bros in CMD.EXE using Cosmopolitan! This is
thanks to a new worker thread that's spawned on Windows whenever any one
of poll(), select(), or ioctl(FIONREAD) is linked.
This commit is contained in:
Justine Tunney 2023-08-13 22:42:25 -07:00
parent ef6387ee5e
commit 9c0821def7
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
15 changed files with 280 additions and 58 deletions

View file

@ -16,10 +16,11 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/nt/process.h"
#include "libc/atomic.h"
#include "libc/intrin/atomic.h"
#include "libc/runtime/internal.h"
static textwindows char16_t *UintToChar16Array(char16_t p[21], uint64_t x) {
static dontasan textwindows char16_t *itoa16(char16_t p[21], uint64_t x) {
char t;
size_t a, b, i = 0;
do {
@ -36,14 +37,15 @@ static textwindows char16_t *UintToChar16Array(char16_t p[21], uint64_t x) {
return p + i;
}
textwindows char16_t *CreatePipeName(char16_t *a) {
static long x;
// This function is called very early by WinMain().
dontasan textwindows char16_t *__create_pipe_name(char16_t *a) {
char16_t *p = a;
const char *q = "\\\\?\\pipe\\cosmo\\";
static atomic_uint x;
while (*q) *p++ = *q++;
p = UintToChar16Array(p, GetCurrentProcessId());
p = itoa16(p, __pid);
*p++ = '-';
p = UintToChar16Array(p, (x += 1));
p = itoa16(p, atomic_fetch_add(&x, 1));
*p = 0;
return a;
}