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.Epoch;
00047
00048
00049
00050
00051
00052
00053
00054 version (Win32)
00055 {
00056 private import std.c.windows.windows;
00057
00058 static uint FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100;
00059 static uint FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200;
00060 static uint FORMAT_MESSAGE_FROM_STRING = 0x00000400;
00061 static uint FORMAT_MESSAGE_FROM_HMODULE = 0x00000800;
00062 static uint FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000;
00063 static uint FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x00002000;
00064 static uint FORMAT_MESSAGE_MAX_WIDTH_MASK = 0x000000FF;
00065
00066 WORD MAKELANGID(WORD p, WORD s) { return (((cast(WORD)s) << 10) | cast(WORD)p); }
00067 WORD PRIMARYLANGID(WORD lgid) { return (cast(WORD)lgid & 0x3ff); }
00068 WORD SUBLANGID(WORD lgid) { return (cast(WORD)lgid >> 10); }
00069
00070 alias HGLOBAL HLOCAL;
00071
00072 static uint LANG_NEUTRAL = 0x00;
00073 static uint SUBLANG_DEFAULT = 0x01;
00074
00075 extern (Windows)
00076 {
00077 DWORD FormatMessageA (DWORD dwFlags,
00078 LPCVOID lpSource,
00079 DWORD dwMessageId,
00080 DWORD dwLanguageId,
00081 LPTSTR lpBuffer,
00082 DWORD nSize,
00083 LPCVOID args
00084 );
00085
00086 HLOCAL LocalFree(HLOCAL hMem);
00087 int QueryPerformanceCounter (ulong *count);
00088 int QueryPerformanceFrequency (ulong *frequency);
00089
00090 VOID Sleep (DWORD millisecs);
00091 }
00092 }
00093
00094 version (Posix)
00095 {
00096 version (linux)
00097 private import std.c.linux.linux;
00098
00099 else version (darwin)
00100 private import std.c.darwin.darwin;
00101
00102 else static assert (0);
00103
00104 extern (C) char *strerror (int);
00105 extern (C) int strlen (char *);
00106 extern (C) int getErrno ();
00107 extern (C) void usleep(uint);
00108 }
00109
00110
00111
00112
00113
00114
00115
00116 static this()
00117 {
00118 version(Win32){}
00119 else
00120 version (Posix){}
00121 else
00122 throw new Exception ("Unsupported O/S environment (version is not Win32 or Posix).");
00123 }
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134 struct System
00135 {
00136
00137
00138
00139
00140
00141
00142
00143 enum Interval {
00144 Microsec = 1,
00145 Millisec = 1000,
00146 Second = 1_000_000,
00147 Minute = 60_000_000
00148 };
00149
00150
00151
00152
00153
00154
00155
00156 final static ulong getMillisecs ()
00157 {
00158 return Epoch.utcTime;
00159 }
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169 final static void sleep (uint interval = uint.max)
00170 {
00171 do {
00172 version (Posix)
00173 usleep (interval);
00174
00175 version (Win32)
00176 Sleep (interval / 1000);
00177 } while (interval == uint.max);
00178 }
00179
00180
00181
00182
00183
00184
00185
00186 version (Ares)
00187 typedef void delegate() ThreadDelegate;
00188 else
00189 typedef int delegate() ThreadDelegate;
00190
00191 final static Thread createThread (ThreadDelegate dg, bool start = false)
00192 {
00193 Thread t = new Thread (dg);
00194 if (start)
00195 t.start ();
00196 return t;
00197 }
00198
00199
00200
00201
00202
00203 final static char[] error ()
00204 {
00205 version (Win32)
00206 return error (GetLastError);
00207 else
00208 return error (getErrno);
00209 }
00210
00211
00212
00213
00214
00215 final static char[] error (uint errcode)
00216 {
00217 char[] text;
00218
00219 version (Win32)
00220 {
00221 DWORD r;
00222 LPVOID lpMsgBuf;
00223
00224 r = FormatMessageA (
00225 FORMAT_MESSAGE_ALLOCATE_BUFFER |
00226 FORMAT_MESSAGE_FROM_SYSTEM |
00227 FORMAT_MESSAGE_IGNORE_INSERTS,
00228 null,
00229 errcode,
00230 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
00231 cast(LPTSTR)&lpMsgBuf,
00232 0,
00233 null);
00234
00235
00236 if (r >= 2) r-= 2;
00237 text = (cast(char *)lpMsgBuf)[0..r].dup;
00238 LocalFree(cast(HLOCAL)lpMsgBuf);
00239 }
00240 else
00241 {
00242 uint r;
00243 char* pemsg;
00244
00245 pemsg = strerror(errcode);
00246 r = strlen(pemsg);
00247
00248
00249 if (pemsg[r-1] == '\n') r--;
00250 if (pemsg[r-1] == '\r') r--;
00251 text = pemsg[0..r].dup;
00252 }
00253
00254 return text;
00255 }
00256 }
00257