This question already has answers here:
How do I create a global, mutable singleton?
(7 answers)
How can you make a safe static singleton in Rust?
(3 answers)
Closed 2 years ago.
I'm trying to port a Go language chess engine ( https://github.com/easychessanimations/gobbit ) to Rust ( https://github.com/easychessanimations/rustengine ). Problem with Go is that it produces a WASM executable which is bloated in size because it contains the whole Go runtime ( > 2 MB ). Also the native executable could be a bit faster. So I decided to give Rust a try.
Chess engines need a lot of tables that are initialized in the beginning and serve practically as constants throughout the whole lifetime of the program.
Already with initializing attack bitboards I'm having problems.
I define an attack table:
/// AttackTable type records an attack bitboard for every square of a chess board
pub type AttackTable = [Bitboard; BOARD_AREA];
/// EMPTY_ATTACK_TABLE defines an empty attack table, useful for initializing attack tables
pub const EMPTY_ATTACK_TABLE: AttackTable = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];
Then I have global variables of this type:
/// KNIGHT_ATTACK is the attack table of knight
pub static mut KNIGHT_ATTACK: AttackTable = EMPTY_ATTACK_TABLE;
/// BISHOP_ATTACK is the attack table of bishop
pub static mut BISHOP_ATTACK: AttackTable = EMPTY_ATTACK_TABLE;
...
Already at initialization I have to do this in an unsafe block:
/// initializes attack tables, must be called before using the module
pub fn init_attack_tables() {
let mut sq: Square = 0;
loop {
if sq < BOARD_AREA {
unsafe {
KNIGHT_ATTACK[sq] = jump_attack_8(sq, KNIGHT_DELTAS, 0);
BISHOP_ATTACK[sq] = sliding_attack_4(sq, BISHOP_DELTAS, 0);
...
}
sq += 1;
} else {
break;
}
}
}
All later accesses to these tables also have to be in unsafe blocks, so I must be doing something wrong.
However I need these tables, they have to be globals and should be accessed easily and quickly. I don't know what to do.
I'm trying to hook IDirect3DDevice9::Present or ::EndScene and render my own overlay (which, for now, is just a simple rectangle) on top of everything else in a D3D9 application, but my overlay seems to be appearing and disappearing quite randomly. The drawing code I'm currently using is:
typedef struct CUSTOMVERTEX {
float x, y, z, rwh;
DWORD color;
};
#define CUSTOMFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)
void draw() {
// the positions are just for testing purposes, so they don't really make sense
CUSTOMVERTEX vertices[] = {
{ 0, 0, 0, 1.0f, D3DCOLOR_XRGB(255, 255, 255) },
{ 0, cursor_pos.y+500, 0, 1.0f, D3DCOLOR_XRGB(127, 255, 255) },
{ cursor_pos.x, cursor_pos.y, 0, 1.0f, D3DCOLOR_XRGB(255,255, 255) },
{ cursor_pos.x, 600, 0, 1.0f, D3DCOLOR_XRGB(127, 0, 0) }
};
if (vBuffer == 0) return;
VOID* pVoid;
vBuffer->Lock(0, 0, &pVoid, 0);
memcpy(pVoid, vertices, sizeof(vertices));
vBuffer->Unlock();
d3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
d3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
D3DMATRIX orthographicMatrix;
D3DMATRIX identityMatrix;
// MAKE_D3DMATRIX should be equivalent to D3DXMATRIX constructor
D3DMATRIX viewMatrix = MAKE_D3DMATRIX(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
((float)-(get_window_width()/2)), ((float)-(get_window_height()/ 2)), 0, 1
);
// MAKE_ORTHO_LH is equivalent to D3DMatrixOrthoLH
MAKE_ORTHO_LH(&orthographicMatrix, (FLOAT)get_window_width(), (FLOAT)get_window_height(), -1.0, 1.0);
// and this to D3DMatrixIdentity
MAKE_IDENTITY(&identityMatrix); // and this to D3DMatrixIdentity
d3dDevice->SetTransform(D3DTS_PROJECTION, &orthographicMatrix);
d3dDevice->SetTransform(D3DTS_WORLD, &identityMatrix);
d3dDevice->SetTransform(D3DTS_VIEW, &viewMatrix);
d3dDevice->SetRenderState(D3DRS_ZENABLE, false);
d3dDevice->SetFVF(CUSTOMFVF);
d3dDevice->SetStreamSource(0, vBuffer, 0, sizeof(CUSTOMVERTEX));
d3dDevice->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);
d3dDevice->SetRenderState(D3DRS_ZENABLE, true);
}
I can't seem to figure out why this would cause the overlay to pop in and out of existence at seemingly random points in time.. What am I missing?
I found a foolproof method to render my stuff directly to the backbuffer (on CPU):
IDirect3DSwapChain9 *sc;
if (FAILED(d3dDevice->GetSwapChain(0, &sc))) {
PRINT("GetSwapChain failed\n");
return;
}
IDirect3DSurface9 *s;
if (FAILED(sc->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &s))) {
PRINT("GetBackBuffer failed\n");
return;
}
D3DLOCKED_RECT r;
if (FAILED(s->LockRect(&r, NULL, D3DLOCK_DONOTWAIT))) {
PRINT("LockRect failed\n");
return;
}
// here, find out pixel format and manipulate
// pixel buffer through r->pBits, using r->Pitch accordingly
s->UnlockRect();
s->Release();
sc->Release();
Will most probably incur a performance penalty, but in my application this doesn't really make a difference. A more optimal way would be to harness the GPU's hwacceled rendering capabilities, but I currently lack the D3D knowledge to do so.
I want to capture web page by pywebkitgtk. I have installed the X org server on that machine, started the X server with some exceptions, but the X server is running.
Issue is I'm not able to read any data from the X unix socket, which is PF_FILE type with path=#"/tmp/.X11-unix/X0", recvfrom is throwing EAGAIN (Resource temporarily unavailable) error, polling against that socket constantly time out without any coming data, below is the trace log, in which we can find successfully created unix socket with fd 5, connected unix socket, succeed so far, but failed to read anything from that unix socket.
developer#kernel: /data/play/null> strace python ../pywebkitgtk-1.1.8/demos/browser.py
socket(PF_FILE, SOCK_STREAM|SOCK_CLOEXEC, 0) = 5
connect(5, {sa_family=AF_FILE, path=#"/tmp/.X11-unix/X0"}, 20) = 0
getpeername(5, {sa_family=AF_FILE, path=#"/tmp/.X11-unix/X0"}, [20]) = 0
...........................
recvfrom(5, "\1\3\304\1\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 4096, 0, NULL, NULL) = 32
recvfrom(5, 0x25ff6c4, 4096, 0, 0, 0) = -1 EAGAIN (Resource temporarily unavailable)
recvfrom(5, 0x25ff6c4, 4096, 0, 0, 0) = -1 EAGAIN (Resource temporarily unavailable)
poll([{fd=5, events=POLLIN|POLLOUT}], 1, -1) = 1 ([{fd=5, revents=POLLOUT}])
writev(5, [{"\220\10\7\0\0\1G\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0 \1", 28}, {NULL, 0}, {"", 0}], 3) = 28
poll([{fd=5, events=POLLIN}], 1, -1) = 1 ([{fd=5, revents=POLLIN}])
recvfrom(5, "\1\3\305\1X\4\0\0\0\0\10\377G\0\0\31\31\0104\1\370\0\0\0\0\0\0\0\0\0\0\10"..., 4096, 0, NULL, NULL) = 4096
recvfrom(5, "\216\377\10\20\0\0\0\0\1\1\1\0\33\377\10\20\0\0\0\0\1\1\1\0_\377\10\20\0\0\0\0"..., 384, 0, NULL, NULL) = 384
recvfrom(5, 0x25ff6c4, 4096, 0, 0, 0) = -1 EAGAIN (Resource temporarily unavailable)
recvfrom(5, 0x25ff6c4, 4096, 0, 0, 0) = -1 EAGAIN (Resource temporarily unavailable)
poll([{fd=5, events=POLLIN|POLLOUT}], 1, -1) = 1 ([{fd=5, revents=POLLOUT}])
writev(5, [{"\220\21\3\0\3\0G\0\0\30\0\0", 12}, {NULL, 0}, {"", 0}], 3) = 12
poll([{fd=5, events=POLLIN}], 1, -1) = 1 ([{fd=5, revents=POLLIN}])
recvfrom(5, "\1\3\306\1\16\0\0\0\0\30\0\0\10\377\31\1\377\37\10\370\0\0\0\0\0\0\0\0\0\0\0\0"..., 4096, 0, NULL, NULL) = 88
recvfrom(5, 0x25ff6c4, 4096, 0, 0, 0) = -1 EAGAIN (Resource temporarily unavailable)
recvfrom(5, 0x25ff6c4, 4096, 0, 0, 0) = -1 EAGAIN (Resource temporarily unavailable)
poll([{fd=5, events=POLLIN|POLLOUT}], 1, -1) = 1 ([{fd=5, revents=POLLOUT}])
writev(5, [{"\20\0\3\0\4\0G\0Meta", 12}, {NULL, 0}, {"", 0}], 3) = 12
poll([{fd=5, events=POLLIN}], 1, -1) = 1 ([{fd=5, revents=POLLIN}])
recvfrom(5, "\1\0\307\1\0\0\0\0\206\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 4096, 0, NULL, NULL) = 32
recvfrom(5, 0x25ff6c4, 4096, 0, 0, 0) = -1 EAGAIN (Resource temporarily unavailable)
recvfrom(5, 0x25ff6c4, 4096, 0, 0, 0) = -1 EAGAIN (Resource temporarily unavailable)
poll([{fd=5, events=POLLIN|POLLOUT}], 1, -1) = 1 ([{fd=5, revents=POLLOUT}])
writev(5, [{"\20\0\4\0\5\0G\0Super\0\0\0", 16}, {NULL, 0}, {"", 0}], 3) = 16
poll([{fd=5, events=POLLIN}], 1, -1) = 1 ([{fd=5, revents=POLLIN}])
recvfrom(5, "\1\0\310\1\0\0\0\0\207\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 4096, 0, NULL, NULL) = 32
recvfrom(5, 0x25ff6c4, 4096, 0, 0, 0) = -1 EAGAIN (Resource temporarily unavailable)
recvfrom(5, 0x25ff6c4, 4096, 0, 0, 0) = -1 EAGAIN (Resource temporarily unavailable)
poll([{fd=5, events=POLLIN|POLLOUT}], 1, -1) = 1 ([{fd=5, revents=POLLOUT}])
writev(5, [{"\20\0\4\0\5\0G\0Hyper\0\0\0", 16}, {NULL, 0}, {"", 0}], 3) = 16
poll([{fd=5, events=POLLIN}], 1, -1) = 1 ([{fd=5, revents=POLLIN}])
recvfrom(5, "\1\0\311\1\0\0\0\0\210\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 4096, 0, NULL, NULL) = 32
recvfrom(5, 0x25ff6c4, 4096, 0, 0, 0) = -1 EAGAIN (Resource temporarily unavailable)
recvfrom(5, 0x25ff6c4, 4096, 0, 0, 0) = -1 EAGAIN (Resource temporarily unavailable)
poll([{fd=5, events=POLLIN|POLLOUT}], 1, -1) = 1 ([{fd=5, revents=POLLOUT}])
writev(5, [{";\3\5\0\16\0 \1\0\0\0\0\3\0\3\0\316\2\22\0008\0\5\0\16\0 \1\0000\0\0"..., 16384}, {NULL, 0}, {"", 0}], 3) = 16384
recvfrom(5, 0x25ff6c4, 4096, 0, 0, 0) = -1 EAGAIN (Resource temporarily unavailable)
poll([{fd=5, events=POLLIN|POLLOUT}], 1, -1) = 1 ([{fd=5, revents=POLLOUT}])
writev(5, [{"6\3\2\0f\0 \1", 8}, {NULL, 0}, {"", 0}], 3) = 8
recvfrom(5, 0x25ff6c4, 4096, 0, 0, 0) = -1 EAGAIN (Resource temporarily unavailable)
recvfrom(5, 0x25ff6c4, 4096, 0, 0, 0) = -1 EAGAIN (Resource temporarily unavailable)
recvfrom(5, 0x25ff6c4, 4096, 0, 0, 0) = -1 EAGAIN (Resource temporarily unavailable)
poll([{fd=6, events=POLLIN}, {fd=5, events=POLLIN}], 2, 0) = 0 (Timeout)
recvfrom(5, 0x25ff6c4, 4096, 0, 0, 0) = -1 EAGAIN (Resource temporarily unavailable)
poll([{fd=6, events=POLLIN}, {fd=5, events=POLLIN}], 2, 0) = 0 (Timeout)
poll([{fd=5, events=POLLIN|POLLOUT}], 1, -1) = 1 ([{fd=5, revents=POLLOUT}])
writev(5, [{"5\30\4\0\200\0 \1\3\0 \1\316\2\22\0\223\4\5\0\201\0 \1\200\0 \1)\0\0\0"..., 296}, {NULL, 0}, {"", 0}], 3) = 296
recvfrom(5, 0x25ff6c4, 4096, 0, 0, 0) = -1 EAGAIN (Resource temporarily unavailable)
recvfrom(5, 0x25ff6c4, 4096, 0, 0, 0) = -1 EAGAIN (Resource temporarily unavailable)
poll([{fd=6, events=POLLIN}, {fd=5, events=POLLIN}], 2, 497) = 0 (Timeout)
poll([{fd=5, events=POLLIN|POLLOUT}], 1, -1) = 1 ([{fd=5, revents=POLLOUT}])
writev(5, [{"(\30\4\0\3\0 \1\1\1\0\0\0\0\0\0", 16}, {NULL, 0}, {"", 0}], 3) = 16
poll([{fd=5, events=POLLIN}], 1, -1) = 1 ([{fd=5, revents=POLLIN}])
recvfrom(5, "\1\1#\2\0\0\0\0\250\3\300\0o\0N\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 4096, 0, NULL, NULL) = 32
recvfrom(5, 0x25ff6c4, 4096, 0, 0, 0) = -1 EAGAIN (Resource temporarily unavailable)
recvfrom(5, 0x25ff6c4, 4096, 0, 0, 0) = -1 EAGAIN (Resource temporarily unavailable)
recvfrom(5, 0x25ff6c4, 4096, 0, 0, 0) = -1 EAGAIN (Resource temporarily unavailable)
poll([{fd=6, events=POLLIN}, {fd=5, events=POLLIN}], 2, 4498) = 0 (Timeout)
futex(0x7f2d8af5ea9c, FUTEX_WAKE_OP_PRIVATE, 1, 1, 0x7f2d8af5ea98, {FUTEX_OP_SET, 0, FUTEX_OP_CMP_GT, 1}) = 1
recvfrom(5, 0x25ff6c4, 4096, 0, 0, 0) = -1 EAGAIN (Resource temporarily unavailable)
Somewhere in the .... part the socket was marked as non-blocking, so getting EAGAIN (which is the same errno(3) value as EWOULDBLOCK, btw) is totally normal - it means there's no data to consume at that particular moment.
Then your trace does show that you do get data from the socket every time after poll(2) with revents=POLLIN, so I don't see anything wrong here. Maybe just the fact that the unsuccessful read is always repeated twice, but that's not a real issue, just inefficiency (or maybe the other way around - some very clever trick).
Have you sent X11 protocol requests on the Unix socket that you’re expecting replies from? After the initial X11 protocol handshake, you won’t get anything from the X server until you request it - either via a request that returns an immediate response or one that registers you for later events.
The X11 protocol is complex enough that trying to write your own code to connect to it is a far larger task than anyone should do - you should use the libxcb or libX11 libraries to handle the protocol for you, and a higher level toolkit such as GTK or Qt to handle most of the widgets and user interface.