Commit de2ec1ba authored by DailyShana's avatar DailyShana Committed by GitHub

use standard thread library (#2221)

* use standard thread library
* use standard sleep function
parent 4d893d52
......@@ -65,11 +65,11 @@ inline int myswprintf(wchar_t(&buf)[N], const wchar_t* fmt, TR... args) {
#include <stdlib.h>
#include <memory.h>
#include <time.h>
#include <thread>
#include "bufferio.h"
#include "myfilesystem.h"
#include "mymutex.h"
#include "mysignal.h"
#include "mythread.h"
#include "../ocgcore/ocgapi.h"
#include "../ocgcore/common.h"
......
......@@ -65,7 +65,7 @@ bool DuelClient::StartClient(unsigned int ip, unsigned short port, bool create_g
event* resp_event = event_new(client_base, 0, EV_TIMEOUT, ConnectTimeout, 0);
event_add(resp_event, &timeout);
}
Thread::NewThread(ClientThread, 0);
std::thread(ClientThread).detach();
return true;
}
void DuelClient::ConnectTimeout(evutil_socket_t fd, short events, void* arg) {
......@@ -225,7 +225,7 @@ void DuelClient::ClientEvent(bufferevent *bev, short events, void *ctx) {
event_base_loopexit(client_base, 0);
}
}
int DuelClient::ClientThread(void* param) {
int DuelClient::ClientThread() {
event_base_dispatch(client_base);
bufferevent_free(client_bev);
event_base_free(client_base);
......@@ -3884,7 +3884,7 @@ void DuelClient::BeginRefreshHost() {
timeval timeout = {3, 0};
resp_event = event_new(broadev, reply, EV_TIMEOUT | EV_READ | EV_PERSIST, BroadcastReply, broadev);
event_add(resp_event, &timeout);
Thread::NewThread(RefreshThread, broadev);
std::thread(RefreshThread, broadev).detach();
//send request
SOCKADDR_IN local;
local.sin_family = AF_INET;
......@@ -3913,8 +3913,7 @@ void DuelClient::BeginRefreshHost() {
closesocket(sSend);
}
}
int DuelClient::RefreshThread(void * arg) {
event_base* broadev = (event_base*)arg;
int DuelClient::RefreshThread(event_base* broadev) {
event_base_dispatch(broadev);
evutil_socket_t fd;
event_get_assignment(resp_event, 0, &fd, 0, 0, 0);
......
......@@ -43,7 +43,7 @@ public:
static void StopClient(bool is_exiting = false);
static void ClientRead(bufferevent* bev, void* ctx);
static void ClientEvent(bufferevent *bev, short events, void *ctx);
static int ClientThread(void* param);
static int ClientThread();
static void HandleSTOCPacketLan(char* data, unsigned int len);
static int ClientAnalyze(char* msg, unsigned int len);
static void SwapField();
......@@ -80,7 +80,7 @@ protected:
public:
static std::vector<HostPacket> hosts;
static void BeginRefreshHost();
static int RefreshThread(void* arg);
static int RefreshThread(event_base* broadev);
static void BroadcastReply(evutil_socket_t fd, short events, void* arg);
};
......
......@@ -842,11 +842,7 @@ void Game::MainLoop() {
fps++;
cur_time = timer->getTime();
if(cur_time < fps * 17 - 20)
#ifdef _WIN32
Sleep(20);
#else
usleep(20000);
#endif
std::this_thread::sleep_for(std::chrono::milliseconds(20));
if(cur_time >= 1000) {
myswprintf(cap, L"YGOPro FPS: %d", fps);
device->setWindowCaption(cap);
......@@ -861,11 +857,7 @@ void Game::MainLoop() {
DuelClient::StopClient(true);
if(dInfo.isSingleMode)
SingleMode::StopPlay(true);
#ifdef _WIN32
Sleep(500);
#else
usleep(500000);
#endif
std::this_thread::sleep_for(std::chrono::milliseconds(500));
SaveConfig();
// device->drop();
}
......
#ifndef THREAD_H
#define THREAD_H
#ifdef _WIN32
#include <windows.h>
class Thread {
public:
static void NewThread(int (*thread_func)(void*), void* param) {
CreateThread(0, 0, (LPTHREAD_START_ROUTINE)thread_func, param, 0, 0);
}
};
#else // _WIN32
#include <pthread.h>
class Thread {
public:
static void NewThread(int (*thread_func)(void*), void* param) {
pthread_t thread;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(&thread, &attr, (void * (*)(void *))thread_func, param);
}
};
#endif // _WIN32
#endif // THREAD_H
......@@ -33,7 +33,7 @@ bool NetServer::StartServer(unsigned short port) {
return false;
}
evconnlistener_set_error_cb(listener, ServerAcceptError);
Thread::NewThread(ServerThread, net_evbase);
std::thread(ServerThread).detach();
return true;
}
bool NetServer::StartBroadcast() {
......@@ -136,7 +136,7 @@ void NetServer::ServerEchoEvent(bufferevent* bev, short events, void* ctx) {
else DisconnectPlayer(dp);
}
}
int NetServer::ServerThread(void* param) {
int NetServer::ServerThread() {
event_base_dispatch(net_evbase);
for(auto bit = users.begin(); bit != users.end(); ++bit) {
bufferevent_disable(bit->first, EV_READ);
......
......@@ -33,7 +33,7 @@ public:
static void ServerAcceptError(evconnlistener *listener, void* ctx);
static void ServerEchoRead(bufferevent* bev, void* ctx);
static void ServerEchoEvent(bufferevent* bev, short events, void* ctx);
static int ServerThread(void* param);
static int ServerThread();
static void DisconnectPlayer(DuelPlayer* dp);
static void HandleCTOSPacket(DuelPlayer* dp, char* data, unsigned int len);
static void SendPacketToPlayer(DuelPlayer* dp, unsigned char proto) {
......
......@@ -24,7 +24,7 @@ bool ReplayMode::StartReplay(int skipturn) {
skip_turn = skipturn;
if(skip_turn < 0)
skip_turn = 0;
Thread::NewThread(ReplayThread, 0);
std::thread(ReplayThread).detach();
return true;
}
void ReplayMode::StopReplay(bool is_exiting) {
......@@ -56,7 +56,7 @@ bool ReplayMode::ReadReplayResponse() {
set_responseb(pduel, resp);
return result;
}
int ReplayMode::ReplayThread(void* param) {
int ReplayMode::ReplayThread() {
const ReplayHeader& rh = cur_replay.pheader;
mainGame->dInfo.isFirst = true;
mainGame->dInfo.isTag = !!(rh.flag & REPLAY_TAG);
......
......@@ -32,7 +32,7 @@ public:
static void SwapField();
static void Pause(bool is_pause, bool is_step);
static bool ReadReplayResponse();
static int ReplayThread(void* param);
static int ReplayThread();
static bool StartDuel();
static void EndDuel();
static void Restart(bool refresh);
......
......@@ -12,7 +12,7 @@ bool SingleMode::is_continuing = false;
Replay SingleMode::last_replay;
bool SingleMode::StartPlay() {
Thread::NewThread(SinglePlayThread, 0);
std::thread(SinglePlayThread).detach();
return true;
}
void SingleMode::StopPlay(bool is_exiting) {
......@@ -28,7 +28,7 @@ void SingleMode::SetResponse(unsigned char* resp, unsigned int len) {
last_replay.WriteData(resp, len);
set_responseb(pduel, resp);
}
int SingleMode::SinglePlayThread(void* param) {
int SingleMode::SinglePlayThread() {
const int start_lp = 8000;
const int start_hand = 5;
const int draw_count = 1;
......
......@@ -15,7 +15,7 @@ public:
static bool StartPlay();
static void StopPlay(bool is_exiting = false);
static void SetResponse(unsigned char* resp, unsigned int len);
static int SinglePlayThread(void* param);
static int SinglePlayThread();
static bool SinglePlayAnalyze(char* msg, unsigned int len);
static void SinglePlayRefresh(int flag = 0xf81fff);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment