- Joined
- Nov 24, 2014
- Messages
- 3,162
- Trophies
- 2
- Age
- 29
- Location
- Bologna
- Website
- rinnegatamante.it
- XP
- 4,857
- Country
Is it normal that accept function blocks main thread even if it's called in a secondary thread?
Here's the code:
Here's the code:
Code:
// Globals
volatile bool accepted = false;
volatile bool initialized = false;
Handle update;
Handle acceptThread;
u32* threadStack;
volatile Socket* acceptResult = NULL;
// Secondary thread func
static void acceptAsync(void* arg){
Socket* my_socket = (Socket*)arg;
while(1) {
svcWaitSynchronization(update, U64_MAX);
svcClearEvent(update);
u32 bytesRead;
u32 control;
u32 sockClient = accept(my_socket->sock, (struct sockaddr*)NULL, NULL);
acceptResult = (Socket*)malloc(sizeof(Socket));
acceptResult->sock = sockClient;
acceptResult->serverSocket = false;
acceptResult->magic = 0xDEADDEAD;
svcExitThread();
}
}
// This is executed in main thread
static int lua_accept(lua_State *L)
{
int argc = lua_gettop(L);
if (argc != 1) return luaL_error(L, "wrong number of arguments");
Socket* my_socket = (Socket*)luaL_checkinteger(L, 1);
if (initialized){
if (acceptResult != NULL){
lua_pushinteger(L, (u32)acceptResult);
initialized = false;
return 1;
}else return 0;
}else{
svcCreateEvent(&update,0);
threadStack = (u32*)memalign(32, 8192);
svcSignalEvent(update);
Result ret = svcCreateThread(&acceptThread, acceptAsync, (u32)my_socket, &threadStack[2048], 0x18, 1);
initialized = true;
return 0;
}
}