00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 module mango.sys.System;
00043
00044 private import std.thread;
00045
00046 private import mango.sys.OS,
00047 mango.sys.Epoch;
00048
00049
00050
00051
00052
00053
00054
00055 version (Win32)
00056 {
00057 static uint FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100;
00058 static uint FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200;
00059 static uint FORMAT_MESSAGE_FROM_STRING = 0x00000400;
00060 static uint FORMAT_MESSAGE_FROM_HMODULE = 0x00000800;
00061 static uint FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000;
00062 static uint FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x00002000;
00063 static uint FORMAT_MESSAGE_MAX_WIDTH_MASK = 0x000000FF;
00064
00065 WORD MAKELANGID(WORD p, WORD s) { return (((cast(WORD)s) << 10) | cast(WORD)p); }
00066 WORD PRIMARYLANGID(WORD lgid) { return (cast(WORD)lgid & 0x3ff); }
00067 WORD SUBLANGID(WORD lgid) { return (cast(WORD)lgid >> 10); }
00068
00069 alias HGLOBAL HLOCAL;
00070
00071 static uint LANG_NEUTRAL = 0x00;
00072 static uint SUBLANG_DEFAULT = 0x01;
00073
00074 extern (Windows)
00075 {
00076 DWORD FormatMessageA (DWORD dwFlags,
00077 LPCVOID lpSource,
00078 DWORD dwMessageId,
00079 DWORD dwLanguageId,
00080 LPTSTR lpBuffer,
00081 DWORD nSize,
00082 LPCVOID args
00083 );
00084
00085 HLOCAL LocalFree(HLOCAL hMem);
00086 int QueryPerformanceCounter (ulong *count);
00087 int QueryPerformanceFrequency (ulong *frequency);
00088
00089 VOID Sleep (DWORD millisecs);
00090 }
00091 }
00092 else
00093 version (Posix)
00094 {
00095 extern (C) char *strerror (int);
00096 extern (C) int strlen (char *);
00097 extern (C) int getErrno ();
00098 extern (C) void usleep(uint);
00099 }
00100 else
00101 static assert(0);
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111 struct System
00112 {
00113
00114
00115
00116
00117
00118
00119
00120 enum Interval {
00121 Microsec = 1,
00122 Millisec = 1000,
00123 Second = 1_000_000,
00124 Minute = 60_000_000
00125 };
00126
00127
00128
00129
00130
00131
00132
00133 final static ulong getMillisecs ()
00134 {
00135 return Epoch.utcMilli;
00136 }
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146 final static void sleep (uint interval = uint.max)
00147 {
00148 do {
00149 version (Posix)
00150 usleep (interval);
00151
00152 version (Win32)
00153 Sleep (interval / 1000);
00154 } while (interval == uint.max);
00155 }
00156
00157
00158
00159
00160
00161
00162
00163 version (Ares)
00164 typedef void delegate() ThreadDelegate;
00165 else
00166 typedef int delegate() ThreadDelegate;
00167
00168 final static Thread createThread (ThreadDelegate dg, bool start = false)
00169 {
00170 Thread t = new Thread (dg);
00171 if (start)
00172 t.start ();
00173 return t;
00174 }
00175
00176
00177
00178
00179
00180 final static char[] error ()
00181 {
00182 version (Win32)
00183 return error (GetLastError);
00184 else
00185 return error (getErrno);
00186 }
00187
00188
00189
00190
00191
00192 final static char[] error (uint errcode)
00193 {
00194 char[] text;
00195
00196 version (Win32)
00197 {
00198 DWORD r;
00199 LPVOID lpMsgBuf;
00200
00201 r = FormatMessageA (
00202 FORMAT_MESSAGE_ALLOCATE_BUFFER |
00203 FORMAT_MESSAGE_FROM_SYSTEM |
00204 FORMAT_MESSAGE_IGNORE_INSERTS,
00205 null,
00206 errcode,
00207 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
00208 cast(LPTSTR)&lpMsgBuf,
00209 0,
00210 null);
00211
00212
00213 if (r >= 2) r-= 2;
00214 text = (cast(char *)lpMsgBuf)[0..r].dup;
00215 LocalFree(cast(HLOCAL)lpMsgBuf);
00216 }
00217 else
00218 {
00219 uint r;
00220 char* pemsg;
00221
00222 pemsg = strerror(errcode);
00223 r = strlen(pemsg);
00224
00225
00226 if (pemsg[r-1] == '\n') r--;
00227 if (pemsg[r-1] == '\r') r--;
00228 text = pemsg[0..r].dup;
00229 }
00230
00231 return text;
00232 }
00233 }
00234