Make terminal ui binaries work well everywhere

Here's some screenshots of an emulator tui program that was compiled on
Linux, then scp'd it to Windows, Mac, and FreeBSD.

https://justine.storage.googleapis.com/blinkenlights-cmdexe.png
https://justine.storage.googleapis.com/blinkenlights-imac.png
https://justine.storage.googleapis.com/blinkenlights-freebsd.png
https://justine.storage.googleapis.com/blinkenlights-lisp.png

How is this even possible that we have a nontrivial ui binary that just
works on Mac, Windows, Linux, and BSD? Surely a first ever achievement.

Fixed many bugs. Bootstrapped John McCarthy's metacircular evaluator on
bare metal in half the size of Altair BASIC (about 2.5kb) and ran it in
emulator for fun and profit.
This commit is contained in:
Justine Tunney 2020-10-10 21:18:53 -07:00
parent 680daf1210
commit 9e3e985ae5
276 changed files with 7026 additions and 3790 deletions

View file

@ -1,2 +1,15 @@
.include "o/libc/nt/codegen.inc"
.imp KernelBase,__imp_CancelIo,CancelIo,101
.text.windows
CancelIo:
push %rbp
mov %rsp,%rbp
.profilable
mov %rdi,%rcx
sub $32,%rsp
call *__imp_CancelIo(%rip)
leave
ret
.endfn CancelIo,globl
.previous

View file

@ -1,2 +1,12 @@
.include "o/libc/nt/codegen.inc"
.imp KernelBase,__imp_CancelIoEx,CancelIoEx,102
.text.windows
CancelIoEx:
push %rbp
mov %rsp,%rbp
.profilable
mov __imp_CancelIoEx(%rip),%rax
jmp __sysv2nt
.endfn CancelIoEx,globl
.previous

View file

@ -1,2 +1,15 @@
.include "o/libc/nt/codegen.inc"
.imp KernelBase,__imp_CancelSynchronousIo,CancelSynchronousIo,103
.text.windows
CancelSynchronousIo:
push %rbp
mov %rsp,%rbp
.profilable
mov %rdi,%rcx
sub $32,%rsp
call *__imp_CancelSynchronousIo(%rip)
leave
ret
.endfn CancelSynchronousIo,globl
.previous

View file

@ -1,2 +1,12 @@
.include "o/libc/nt/codegen.inc"
.imp KernelBase,__imp_CreateIoCompletionPort,CreateIoCompletionPort,195
.text.windows
CreateIoCompletionPort:
push %rbp
mov %rsp,%rbp
.profilable
mov __imp_CreateIoCompletionPort(%rip),%rax
jmp __sysv2nt
.endfn CreateIoCompletionPort,globl
.previous

View file

@ -1,2 +1,12 @@
.include "o/libc/nt/codegen.inc"
.imp KernelBase,__imp_CreateThread,CreateThread,224
.text.windows
CreateThread:
push %rbp
mov %rsp,%rbp
.profilable
mov __imp_CreateThread(%rip),%rax
jmp __sysv2nt6
.endfn CreateThread,globl
.previous

View file

@ -1,2 +1,12 @@
.include "o/libc/nt/codegen.inc"
.imp KernelBase,__imp_CreateWaitableTimerW,CreateWaitableTimerW,234
.text.windows
CreateWaitableTimer:
push %rbp
mov %rsp,%rbp
.profilable
mov __imp_CreateWaitableTimerW(%rip),%rax
jmp __sysv2nt
.endfn CreateWaitableTimer,globl
.previous

View file

@ -1,2 +1,12 @@
.include "o/libc/nt/codegen.inc"
.imp KernelBase,__imp_GetQueuedCompletionStatus,GetQueuedCompletionStatus,695
.text.windows
GetQueuedCompletionStatus:
push %rbp
mov %rsp,%rbp
.profilable
mov __imp_GetQueuedCompletionStatus(%rip),%rax
jmp __sysv2nt6
.endfn GetQueuedCompletionStatus,globl
.previous

View file

@ -1,2 +1,12 @@
.include "o/libc/nt/codegen.inc"
.imp KernelBase,__imp_GetQueuedCompletionStatusEx,GetQueuedCompletionStatusEx,696
.text.windows
GetQueuedCompletionStatusEx:
push %rbp
mov %rsp,%rbp
.profilable
mov __imp_GetQueuedCompletionStatusEx(%rip),%rax
jmp __sysv2nt6
.endfn GetQueuedCompletionStatusEx,globl
.previous

View file

@ -1,2 +1,12 @@
.include "o/libc/nt/codegen.inc"
.imp KernelBase,__imp_PostQueuedCompletionStatus,PostQueuedCompletionStatus,1221
.text.windows
PostQueuedCompletionStatus:
push %rbp
mov %rsp,%rbp
.profilable
mov __imp_PostQueuedCompletionStatus(%rip),%rax
jmp __sysv2nt
.endfn PostQueuedCompletionStatus,globl
.previous

View file

@ -1,2 +1,12 @@
.include "o/libc/nt/codegen.inc"
.imp KernelBase,__imp_ReadFileEx,ReadFileEx,1312
.text.windows
ReadFileEx:
push %rbp
mov %rsp,%rbp
.profilable
mov __imp_ReadFileEx(%rip),%rax
jmp __sysv2nt6
.endfn ReadFileEx,globl
.previous

View file

@ -1,2 +1,12 @@
.include "o/libc/nt/codegen.inc"
.imp KernelBase,__imp_SetWaitableTimer,SetWaitableTimer,1583
.text.windows
SetWaitableTimer:
push %rbp
mov %rsp,%rbp
.profilable
mov __imp_SetWaitableTimer(%rip),%rax
jmp __sysv2nt6
.endfn SetWaitableTimer,globl
.previous

View file

@ -1,2 +1,12 @@
.include "o/libc/nt/codegen.inc"
.imp KernelBase,__imp_WriteFileEx,WriteFileEx,1824
.text.windows
WriteFileEx:
push %rbp
mov %rsp,%rbp
.profilable
mov __imp_WriteFileEx(%rip),%rax
jmp __sysv2nt6
.endfn WriteFileEx,globl
.previous

View file

@ -121,10 +121,10 @@ bool32 CreateDirectory(const char16_t *lpPathName,
struct NtSecurityAttributes *lpSecurityAttributes);
bool32 RemoveDirectory(const char16_t *lpPathName);
int32_t DuplicateHandle(int64_t hSourceProcessHandle, int64_t hSourceHandle,
int64_t hTargetProcessHandle, int64_t *lpTargetHandle,
uint32_t dwDesiredAccess, bool32 bInheritHandle,
uint32_t dwOptions);
bool32 DuplicateHandle(int64_t hSourceProcessHandle, int64_t hSourceHandle,
int64_t hTargetProcessHandle, int64_t *lpTargetHandle,
uint32_t dwDesiredAccess, bool32 bInheritHandle,
uint32_t dwOptions);
bool32 GetHandleInformation(int64_t hObject, uint32_t *out_lpdwFlags);
bool32 SetHandleInformation(int64_t hObject, uint32_t dwMask, uint32_t dwFlags);
@ -244,6 +244,7 @@ bool32 ReadFileScatter(
uint32_t nNumberOfBytesToReadThatsMultipleOfFileVolumeSectorSize,
uint32_t *lpReserved, struct NtOverlapped *inout_lpOverlapped)
paramsnonnull();
bool32 WriteFileGather(int64_t hFileOpenedWithOverlappedAndNoBuffering,
const union NtFileSegmentElement aSegmentArray[],
uint32_t nNumberOfBytesToWrite, uint32_t *lpReserved,

79
libc/nt/iocp.h Normal file
View file

@ -0,0 +1,79 @@
#ifndef COSMOPOLITAN_LIBC_NT_IOCP_H_
#define COSMOPOLITAN_LIBC_NT_IOCP_H_
#if 0
/* ░░░░
cosmopolitan § new technology » i/o completion ports
*/
#endif
#include "libc/nt/struct/overlapped.h"
#include "libc/nt/struct/overlappedentry.h"
#define kNtFileSkipCompletionPortOnSuccess 1
#define kNtFileSkipSetEventOnHandle 2
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
typedef void (*NtOverlappedCompletionRoutine)(
uint32_t dwErrorCode, uint32_t dwNumberOfBytesTransfered,
struct NtOverlapped *inout_lpOverlapped);
int64_t CreateIoCompletionPort(int64_t FileHandleOrNeg1,
int64_t opt_ExistingCompletionPortOrZero,
void *StatePointer,
uint32_t NumberOfConcurrentThreads);
bool32 GetQueuedCompletionStatus(int64_t CompletionPort,
uint32_t *lpNumberOfBytesTransferred,
void *StatePointerPointer,
struct NtOverlapped **lpOverlapped,
uint32_t dwMilliseconds);
bool32 GetQueuedCompletionStatusEx(
int64_t CompletionPort,
struct NtOverlappedEntry *out_lpCompletionPortEntries, uint32_t ulCount,
uint32_t *out_ulNumEntriesRemoved, uint32_t dwMilliseconds,
bool32 fAlertable);
bool32 PostQueuedCompletionStatus(int64_t CompletionPort,
uint32_t dwNumberOfBytesTransferred,
uint32_t *dwCompletionKey,
struct NtOverlapped *opt_lpOverlapped);
bool32 SetFileCompletionNotificationModes(int64_t FileHandle,
unsigned char Flags);
bool32 ReadFileEx(int64_t hFile, void *lpBuffer, uint32_t nNumberOfBytesToRead,
uint32_t *lpNumberOfBytesRead,
struct NtOverlapped *opt_lpOverlapped,
NtOverlappedCompletionRoutine lpCompletionRoutine);
bool32 WriteFileEx(int64_t hFile, const void *lpBuffer,
uint32_t nNumberOfBytesToWrite,
struct NtOverlapped *lpOverlapped,
NtOverlappedCompletionRoutine lpCompletionRoutine);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_NT_IOCP_H_ */

View file

@ -28,17 +28,17 @@
#endif
/* CreateNamedPipe:dwOpenMode */
#define kNtPipeAccessInbound 0x00000001
#define kNtPipeAccessInbound 0x00000001
#define kNtPipeAccessOutbound 0x00000002
#define kNtPipeAccessDuplex 0x00000003
#define kNtPipeAccessDuplex 0x00000003
/* CreateNamedPipe::dwPipeMode */
#define kNtPipeWait 0x00000000
#define kNtPipeNowait 0x00000001
#define kNtPipeReadmodeByte 0x00000000
#define kNtPipeReadmodeMessage 0x00000002
#define kNtPipeTypeByte 0x00000000
#define kNtPipeTypeMessage 0x00000004
#define kNtPipeWait 0x00000000
#define kNtPipeNowait 0x00000001
#define kNtPipeReadmodeByte 0x00000000
#define kNtPipeReadmodeMessage 0x00000002
#define kNtPipeTypeByte 0x00000000
#define kNtPipeTypeMessage 0x00000004
#define kNtPipeAcceptRemoteClients 0x00000000
#define kNtPipeRejectRemoteClients 0x00000008
@ -55,28 +55,34 @@ COSMOPOLITAN_C_START_
struct NtOverlapped;
struct NtSecurityAttributes;
int CreatePipe(int64_t *out_hReadPipe, int64_t *out_hWritePipe,
const struct NtSecurityAttributes *opt_lpPipeAttributes,
uint32_t nSize) paramsnonnull((1, 2));
void *CreateNamedPipe(
bool32 CreatePipe(int64_t *out_hReadPipe, int64_t *out_hWritePipe,
const struct NtSecurityAttributes *opt_lpPipeAttributes,
uint32_t nSize) paramsnonnull((1, 2));
int64_t CreateNamedPipe(
const char16_t *lpName, uint32_t dwOpenMode, uint32_t dwPipeMode,
uint32_t nMaxInstances, uint32_t nOutBufferSize, uint32_t nInBufferSize,
uint32_t nDefaultTimeOut,
const struct NtSecurityAttributes *opt_lpSecurityAttributes)
paramsnonnull((1));
bool32 ConnectNamedPipe(int64_t *hNamedPipe, struct NtOverlapped *lpOverlapped);
bool32 CallNamedPipe(const char16_t *lpNamedPipeName, void *lpInBuffer,
uint32_t nInBufferSize, void *lpOutBuffer,
uint32_t nOutBufferSize, uint32_t *lpBytesRead,
uint32_t nTimeOut);
bool32 ConnectNamedPipe(int64_t *hNamedPipe, struct NtOverlapped *lpOverlapped);
bool32 WaitNamedPipe(const char16_t *lpNamedPipeName, uint32_t nTimeOut);
bool32 DisconnectNamedPipe(int64_t *hNamedPipe);
bool32 SetNamedPipeHandleState(int64_t *hNamedPipe, uint32_t *lpMode,
uint32_t *lpMaxCollectionCount,
uint32_t *lpCollectDataTimeout);
bool32 PeekNamedPipe(int64_t *hNamedPipe, void *lpBuffer, uint32_t nBufferSize,
uint32_t *lpBytesRead, uint32_t *lpTotalBytesAvail,
uint32_t *lpBytesLeftThisMessage);
bool32 TransactNamedPipe(int64_t *hNamedPipe, void *lpInBuffer,
uint32_t nInBufferSize, void *lpOutBuffer,
uint32_t nOutBufferSize, uint32_t *lpBytesRead,

View file

@ -1,2 +1,12 @@
.include "o/libc/nt/codegen.inc"
.imp kernel32,__imp_SetFileCompletionNotificationModes,SetFileCompletionNotificationModes,1318
.text.windows
SetFileCompletionNotificationModes:
push %rbp
mov %rsp,%rbp
.profilable
mov __imp_SetFileCompletionNotificationModes(%rip),%rax
jmp __sysv2nt
.endfn SetFileCompletionNotificationModes,globl
.previous

View file

@ -341,11 +341,11 @@ imp 'CallWindowProc' CallWindowProcW user32 1537
imp 'CallbackMayRunLong' CallbackMayRunLong KernelBase 100
imp 'CancelDC' CancelDC gdi32 1038
imp 'CancelDeviceWakeupRequest' CancelDeviceWakeupRequest kernel32 113
imp 'CancelIo' CancelIo KernelBase 101
imp 'CancelIoEx' CancelIoEx KernelBase 102
imp 'CancelIo' CancelIo KernelBase 101 1
imp 'CancelIoEx' CancelIoEx KernelBase 102 2
imp 'CancelOverlappedAccess' CancelOverlappedAccess advapi32 1091
imp 'CancelShutdown' CancelShutdown user32 1538
imp 'CancelSynchronousIo' CancelSynchronousIo KernelBase 103
imp 'CancelSynchronousIo' CancelSynchronousIo KernelBase 103 1
imp 'CancelTimerQueueTimer' CancelTimerQueueTimer kernel32 118
imp 'CancelWaitableTimer' CancelWaitableTimer KernelBase 105
imp 'CascadeChildWindows' CascadeChildWindows user32 1539
@ -625,7 +625,7 @@ imp 'CreateIcon' CreateIcon user32 1615
imp 'CreateIconFromResource' CreateIconFromResource user32 1616
imp 'CreateIconFromResourceEx' CreateIconFromResourceEx user32 1617
imp 'CreateIconIndirect' CreateIconIndirect user32 1618
imp 'CreateIoCompletionPort' CreateIoCompletionPort KernelBase 195
imp 'CreateIoCompletionPort' CreateIoCompletionPort KernelBase 195 4
imp 'CreateJobObjectA' CreateJobObjectA kernel32 209
imp 'CreateJobObject' CreateJobObjectW kernel32 210
imp 'CreateJobSet' CreateJobSet kernel32 211
@ -702,7 +702,7 @@ imp 'CreateSymbolicLinkTransactedA' CreateSymbolicLinkTransactedA kernel32
imp 'CreateSymbolicLinkTransacted' CreateSymbolicLinkTransactedW kernel32 238
imp 'CreateSystemThreads' CreateSystemThreads user32 1623
imp 'CreateTapePartition' CreateTapePartition kernel32 240
imp 'CreateThread' CreateThread KernelBase 224
imp 'CreateThread' CreateThread KernelBase 224 6
imp 'CreateThreadpool' CreateThreadpool KernelBase 225
imp 'CreateThreadpoolCleanupGroup' CreateThreadpoolCleanupGroup KernelBase 226
imp 'CreateThreadpoolIo' CreateThreadpoolIo KernelBase 227
@ -717,7 +717,7 @@ imp 'CreateUmsThreadContext' CreateUmsThreadContext kernel32 252
imp 'CreateWaitableTimerA' CreateWaitableTimerA kernel32 253
imp 'CreateWaitableTimerExA' CreateWaitableTimerExA kernel32 254
imp 'CreateWaitableTimerEx' CreateWaitableTimerExW KernelBase 233
imp 'CreateWaitableTimer' CreateWaitableTimerW KernelBase 234
imp 'CreateWaitableTimer' CreateWaitableTimerW KernelBase 234 3
imp 'CreateWellKnownSid' CreateWellKnownSid KernelBase 235
imp 'CreateWindowEx' CreateWindowExW user32 1625 12
imp 'CreateWindowExA' CreateWindowExA user32 1624 12
@ -2516,8 +2516,8 @@ imp 'GetPtrCalDataArray' GetPtrCalDataArray KernelBase 692
imp 'GetPublisherCacheFolder' GetPublisherCacheFolder KernelBase 693
imp 'GetPublisherRootFolder' GetPublisherRootFolder KernelBase 694
imp 'GetQueueStatus' GetQueueStatus user32 1936
imp 'GetQueuedCompletionStatus' GetQueuedCompletionStatus KernelBase 695
imp 'GetQueuedCompletionStatusEx' GetQueuedCompletionStatusEx KernelBase 696
imp 'GetQueuedCompletionStatus' GetQueuedCompletionStatus KernelBase 695 5
imp 'GetQueuedCompletionStatusEx' GetQueuedCompletionStatusEx KernelBase 696 6
imp 'GetROP2' GetROP2 gdi32 1695
imp 'GetRandomRgn' GetRandomRgn gdi32 1696
imp 'GetRasterizerCaps' GetRasterizerCaps gdi32 1697
@ -4320,7 +4320,7 @@ imp 'PolylineTo' PolylineTo gdi32 1791
imp 'PoolPerAppKeyStateInternal' PoolPerAppKeyStateInternal KernelBase 1220
imp 'PostMessageA' PostMessageA user32 2204
imp 'PostMessage' PostMessageW user32 2205
imp 'PostQueuedCompletionStatus' PostQueuedCompletionStatus KernelBase 1221
imp 'PostQueuedCompletionStatus' PostQueuedCompletionStatus KernelBase 1221 4
imp 'PostQuitMessage' PostQuitMessage user32 2206 1
imp 'PostThreadMessageA' PostThreadMessageA user32 2207
imp 'PostThreadMessage' PostThreadMessageW user32 2208
@ -4517,7 +4517,7 @@ imp 'ReadEncryptedFileRaw' ReadEncryptedFileRaw advapi32 1601
imp 'ReadEventLogA' ReadEventLogA advapi32 1602
imp 'ReadEventLog' ReadEventLogW advapi32 1603
imp 'ReadFile' ReadFile KernelBase 1311 5
imp 'ReadFileEx' ReadFileEx KernelBase 1312
imp 'ReadFileEx' ReadFileEx KernelBase 1312 5
imp 'ReadFileScatter' ReadFileScatter KernelBase 1313 5
imp 'ReadProcessMemory' ReadProcessMemory KernelBase 1314
imp 'ReadStateAtomValue' ReadStateAtomValue KernelBase 1315
@ -6225,7 +6225,7 @@ imp 'SetFileAttributesA' SetFileAttributesA KernelBase 1507 2
imp 'SetFileAttributesTransactedA' SetFileAttributesTransactedA kernel32 1314
imp 'SetFileAttributesTransacted' SetFileAttributesTransactedW kernel32 1315
imp 'SetFileBandwidthReservation' SetFileBandwidthReservation kernel32 1317
imp 'SetFileCompletionNotificationModes' SetFileCompletionNotificationModes kernel32 1318
imp 'SetFileCompletionNotificationModes' SetFileCompletionNotificationModes kernel32 1318 2
imp 'SetFileInformationByHandle' SetFileInformationByHandle KernelBase 1509
imp 'SetFileIoOverlappedRange' SetFileIoOverlappedRange KernelBase 1510
imp 'SetFilePointer' SetFilePointer KernelBase 1511 4
@ -6425,7 +6425,7 @@ imp 'SetVolumeLabel' SetVolumeLabelW kernel32 1401
imp 'SetVolumeMountPointA' SetVolumeMountPointA kernel32 1402
imp 'SetVolumeMountPoint' SetVolumeMountPointW kernel32 1403
imp 'SetVolumeMountPointWStub' SetVolumeMountPointWStub kernel32 1404
imp 'SetWaitableTimer' SetWaitableTimer KernelBase 1583
imp 'SetWaitableTimer' SetWaitableTimer KernelBase 1583 6
imp 'SetWaitableTimerEx' SetWaitableTimerEx KernelBase 1584
imp 'SetWinEventHook' SetWinEventHook user32 2388
imp 'SetWinMetaFileBits' SetWinMetaFileBits gdi32 1917
@ -7188,7 +7188,7 @@ imp 'WriteConsoleOutputCharacter' WriteConsoleOutputCharacterW KernelBase
imp 'WriteConsoleOutputCharacterA' WriteConsoleOutputCharacterA KernelBase 1819 5
imp 'WriteEncryptedFileRaw' WriteEncryptedFileRaw advapi32 1857
imp 'WriteFile' WriteFile KernelBase 1823 5
imp 'WriteFileEx' WriteFileEx KernelBase 1824
imp 'WriteFileEx' WriteFileEx KernelBase 1824 5
imp 'WriteFileGather' WriteFileGather KernelBase 1825 5
imp 'WritePrivateProfileSectionA' WritePrivateProfileSectionA kernel32 1565
imp 'WritePrivateProfileSection' WritePrivateProfileSectionW kernel32 1566

View file

@ -35,7 +35,7 @@ COSMOPOLITAN_C_START_
each release. Quoth MSDN */
#endif
NtStatus NtDelayExecution(bool32 alertable, int64_t *hectonanoseconds);
NtStatus NtDelayExecution(bool32 alertable, int64_t *AbsCobolOrNegRelHectoNano);
#if ShouldUseMsabiAttribute()
#include "libc/nt/nt/thunk/time.inc"

15
libc/nt/struct/guid.h Normal file
View file

@ -0,0 +1,15 @@
#ifndef COSMOPOLITAN_LIBC_NT_STRUCT_GUID_H_
#define COSMOPOLITAN_LIBC_NT_STRUCT_GUID_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
struct NtGuid {
uint32_t Data1;
uint16_t Data2;
uint16_t Data3;
uint8_t Data4[8];
};
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_NT_STRUCT_GUID_H_ */

View file

@ -0,0 +1,16 @@
#ifndef COSMOPOLITAN_LIBC_NT_STRUCT_OVERLAPPEDENTRY_H_
#define COSMOPOLITAN_LIBC_NT_STRUCT_OVERLAPPEDENTRY_H_
#include "libc/nt/struct/overlapped.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
struct NtOverlappedEntry {
uint32_t *lpCompletionKey;
struct NtOverlapped *lpOverlapped;
uint32_t *Internal;
uint32_t dwNumberOfBytesTransferred;
};
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_NT_STRUCT_OVERLAPPEDENTRY_H_ */

View file

@ -1,6 +1,10 @@
#ifndef COSMOPOLITAN_LIBC_NT_SYNCHRONIZATION_H_
#define COSMOPOLITAN_LIBC_NT_SYNCHRONIZATION_H_
#include "libc/nt/struct/criticalsection.h"
#include "libc/nt/struct/filetime.h"
#include "libc/nt/struct/linkedlist.h"
#include "libc/nt/struct/securityattributes.h"
#include "libc/nt/struct/systemtime.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
#if 0
@ -30,34 +34,19 @@ COSMOPOLITAN_C_START_
*/
#endif
struct NtCriticalSection;
struct NtFileTime;
struct NtSystemTime;
typedef void (*NtTimerapcroutine)(void *lpArgToCompletionRoutine,
uint32_t dwTimerLowValue,
uint32_t dwTimerHighValue);
void Sleep(uint32_t dwMilliseconds);
uint32_t SleepEx(uint32_t dwMilliseconds, bool32 bAlertable);
void GetSystemTime(struct NtSystemTime *lpSystemTime);
bool32 SystemTimeToFileTime(const struct NtSystemTime *lpSystemTime,
struct NtFileTime *lpFileTime);
void GetSystemTimeAsFileTime(
struct NtFileTime *out_lpSystemTimeAsFileTime); /* win8+ */
void GetSystemTimePreciseAsFileTime(
struct NtFileTime *out_lpSystemTimeAsFileTime); /* win8+ */
void InitializeCriticalSection(struct NtCriticalSection *lpCriticalSection);
void EnterCriticalSection(struct NtCriticalSection *lpCriticalSection);
void LeaveCriticalSection(struct NtCriticalSection *lpCriticalSection);
int32_t InitializeCriticalSectionAndSpinCount(
struct NtCriticalSection *lpCriticalSection, uint32_t dwSpinCount);
uint32_t SetCriticalSectionSpinCount(
struct NtCriticalSection *lpCriticalSection, uint32_t dwSpinCount);
int32_t TryEnterCriticalSection(struct NtCriticalSection *lpCriticalSection);
void DeleteCriticalSection(struct NtCriticalSection *lpCriticalSection);
int32_t SetEvent(int64_t hEvent);
int32_t ResetEvent(int64_t hEvent);
int32_t PulseEvent(int64_t hEvent);
int32_t ReleaseSemaphore(int64_t hSemaphore, int32_t lReleaseCount,
int *lpPreviousCount);
int32_t ReleaseMutex(int64_t hMutex);
void GetSystemTimeAsFileTime(struct NtFileTime *); // win8+
void GetSystemTimePreciseAsFileTime(struct NtFileTime *); // win8+
uint32_t WaitForSingleObject(int64_t hHandle, uint32_t dwMilliseconds);
uint32_t WaitForMultipleObjects(uint32_t nCount, const void **lpHandles,
bool32 bWaitAll, uint32_t dwMilliseconds);
@ -67,6 +56,30 @@ uint32_t WaitForMultipleObjectsEx(unsigned int nCount, const void **lpHandles,
bool32 bWaitAll, uint32_t dwMilliseconds,
bool32 bAlertable);
int64_t CreateWaitableTimer(struct NtSecurityAttributes *lpTimerAttributes,
bool32 bManualReset, const char16_t *lpTimerName);
bool32 SetWaitableTimer(int64_t hTimer, const int64_t *lpDueTimeAsFtOrNegRela,
int32_t opt_lPeriodMs, NtTimerapcroutine opt_callback,
void *lpArgToCallback, bool32 fUnsleepSystem);
int32_t SetEvent(int64_t hEvent);
int32_t ResetEvent(int64_t hEvent);
int32_t PulseEvent(int64_t hEvent);
int32_t ReleaseMutex(int64_t hMutex);
int32_t ReleaseSemaphore(int64_t hSemaphore, int32_t lReleaseCount,
int *lpPreviousCount);
void InitializeCriticalSection(struct NtCriticalSection *lpCriticalSection);
void EnterCriticalSection(struct NtCriticalSection *lpCriticalSection);
void LeaveCriticalSection(struct NtCriticalSection *lpCriticalSection);
int32_t TryEnterCriticalSection(struct NtCriticalSection *lpCriticalSection);
void DeleteCriticalSection(struct NtCriticalSection *lpCriticalSection);
int32_t InitializeCriticalSectionAndSpinCount(
struct NtCriticalSection *lpCriticalSection, uint32_t dwSpinCount);
uint32_t SetCriticalSectionSpinCount(
struct NtCriticalSection *lpCriticalSection, uint32_t dwSpinCount);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_NT_SYNCHRONIZATION_H_ */

View file

@ -1,6 +1,8 @@
#ifndef COSMOPOLITAN_LIBC_NT_THREADS_H_
#define COSMOPOLITAN_LIBC_NT_THREADS_H_
#include "libc/nt/enum/threadaccess.h"
#include "libc/nt/struct/overlapped.h"
#include "libc/nt/struct/securityattributes.h"
#include "libc/nt/thunk/msabi.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
@ -31,6 +33,13 @@ COSMOPOLITAN_C_START_
*/
#endif
typedef uint32_t (*NtThreadStartRoutine)(void *lpParameter);
int64_t CreateThread(struct NtSecurityAttributes *lpThreadAttributes,
size_t dwStackSize, NtThreadStartRoutine lpStartAddress,
void *lpParameter, uint32_t dwCreationFlags,
uint32_t *opt_lpThreadId);
void ExitThread(uint32_t dwExitCode) noreturn;
int64_t GetCurrentThread(void);
uint32_t GetCurrentThreadId(void);
@ -47,6 +56,10 @@ bool32 SetThreadPriorityBoost(int64_t hThread, bool32 bDisablePriorityBoost);
bool32 GetThreadPriorityBoost(int64_t hThread, bool32 *pDisablePriorityBoost);
bool32 GetThreadIOPendingFlag(int64_t hThread, bool32 *lpIOIsPending);
bool32 CancelSynchronousIo(int64_t hThread);
bool32 CancelIo(int64_t hFile);
bool32 CancelIoEx(int64_t hFile, struct NtOverlapped opt_lpOverlapped);
#if ShouldUseMsabiAttribute()
#include "libc/nt/thunk/thread.inc"
#endif /* ShouldUseMsabiAttribute() */

View file

@ -9,8 +9,9 @@
* generate code that calls MS ABI functions directly, without needing
* to jump through the assembly thunks.
*/
#if __GNUC__ * 100 + __GNUC_MINOR__ >= 408 || \
(__has_attribute(__ms_abi__) || defined(__llvm__))
#if !defined(__STRICT_ANSI__) && \
(__GNUC__ * 100 + __GNUC_MINOR__ >= 408 || \
(__has_attribute(__ms_abi__) || defined(__llvm__)))
#define __msabi __attribute__((__ms_abi__))
#endif
@ -18,7 +19,7 @@
* Returns true if header should provide MS-ABI overrides.
*/
#ifndef ShouldUseMsabiAttribute
#if defined(__msabi) && defined(NDEBUG) && !defined(__STRICT_ANSI__)
#if defined(__msabi) && defined(NDEBUG) && !defined(__PG__)
#define ShouldUseMsabiAttribute() 1
#else
#define ShouldUseMsabiAttribute() 0

View file

@ -1,10 +1,9 @@
#ifndef COSMOPOLITAN_LIBC_NT_WINSOCK_H_
#define COSMOPOLITAN_LIBC_NT_WINSOCK_H_
#include "libc/nt/struct/guid.h"
#include "libc/nt/struct/overlapped.h"
#include "libc/nt/struct/pollfd.h"
#include "libc/sock/sock.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
#if 0
/* ░▓█████████████████████████████████████████████▓▒
@ -46,9 +45,8 @@ COSMOPOLITAN_C_START_
*/
#endif
#define kNtSioSocketCloseNotify 0x9800000Du
#define kNtSioUdpConnreset 0x9800000Cu
#define kNtSioUdpNetreset 0x9800000F
#define kNtWsaFlagOverlapped 0x01
#define kNtWsaFlagNoHandleInherit 0x80
#define kNtTfDisconnect 0x01
#define kNtTfReuseSocket 0x02
@ -57,6 +55,67 @@ COSMOPOLITAN_C_START_
#define kNtTfUseSystemThread 0x10
#define kNtTfUseKernelApc 0x20
#define kNtSoConnectTime 0x700C
#define kNtSoUpdateAcceptContext 0x700B
#define kNtSoUpdateConnectContext 0x7010
#define kNtSioAddressListChange 0x28000017u
#define kNtSioAddressListQuery 0x48000016u
#define kNtSioAddressListSort 0xC8000019u
#define kNtSioAssociateHandle 0x88000001u
#define kNtSioEnableCircularQueueing 0x28000002u
#define kNtSioFindRoute 0x48000003u
#define kNtSioFlush 0x28000004u
#define kNtSioGetBroadcastAddress 0x48000005u
#define kNtSioGetExtensionFunctionPointer 0xC8000006u
#define kNtSioGetGroupQos 0xC8000008u
#define kNtSioGetQos 0xC8000007u
#define kNtSioMulticastScope 0x8800000Au
#define kNtSioMultipointLoopback 0x88000009u
#define kNtSioQueryRssProcessorInfo 0x48000025u
#define kNtSioQueryTargetPnpHandle 0x48000018u
#define kNtSioReserved1 0x8800001Au
#define kNtSioReserved2 0x88000021u
#define kNtSioRoutingInterfaceChange 0x88000015u
#define kNtSioRoutingInterfaceQuery 0xC8000014u
#define kNtSioSetGroupQos 0x8800000Cu
#define kNtSioSetQos 0x8800000Bu
#define kNtSioSocketCloseNotify 0x9800000Du
#define kNtSioTranslateHandle 0xC800000Du
#define kNtSioUdpConnreset 0x9800000Cu
#define kNtSioUdpNetreset 0x9800000Fu
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
#define kNtWsaidAcceptex \
{ \
0xB5367DF1, 0xCBAC, 0x11CF, { \
0x95, 0xCA, 0x00, 0x80, 0x5F, 0x48, 0xA1, 0x92 \
} \
}
#define kNtWsaidConnectex \
{ \
0x25A207B9, 0xDDF3, 0x4660, { \
0x8E, 0xE9, 0x76, 0xE5, 0x8C, 0x74, 0x06, 0x3E \
} \
}
#define kNtWsaidDisconnectex \
{ \
0x7FDA2E11, 0x8630, 0x436F, { \
0xA0, 0x31, 0xF5, 0x36, 0xA6, 0xEE, 0xC1, 0x57 \
} \
}
#define kNtWsaidTransmitfile \
{ \
0xB5367DF0, 0xCBAC, 0x11CF, { \
0x95, 0xCA, 0x00, 0x80, 0x5F, 0x48, 0xA1, 0x92 \
} \
}
enum NtWsaEComparator { COMP_EQUAL, COMP_NOTLESS };
enum NtWsaCompletionType {
@ -96,13 +155,6 @@ struct NtWsaData {
char szSystemStatus[129];
};
struct NtGuid {
uint32_t Data1;
uint16_t Data2;
uint16_t Data3;
uint8_t Data4[8];
};
struct NtSocketAddress {
struct sockaddr *lpSockaddr;
int32_t iSockaddrLength;
@ -512,6 +564,14 @@ void GetAcceptExSockaddrs(
struct sockaddr **out_RemoteSockaddr /*[*RemoteSockaddrLength]*/,
int *out_RemoteSockaddrLength);
bool32 ConnectEx(int64_t s, const struct sockaddr *name, int namelen,
const void *opt_lpSendBuffer, uint32_t dwSendDataLength,
uint32_t *out_lpdwBytesSent,
struct NtOverlapped *inout_lpOverlapped);
bool32 DisconnectEx(int64_t s, struct NtOverlapped *inout_opt_lpOverlapped,
uint32_t dwFlags, uint32_t dwReserved);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_NT_WINSOCK_H_ */