1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
#include "Selecter.h"
#include <iostream>
#include <signal.h>
#include <sys/poll.h>
using namespace std;
pthread_t Selecter::thread;
pthread_mutex_t Selecter::lock;
bool Selecter::exitPlease, Selecter::running;
void *Selecter::threadFunc(void *arg) {
cerr << "threadFunc start" << endl;
ServerSocket *socket = (ServerSocket *)arg;
struct pollfd pollData;
int retVal;
const int TIMEOUT = 1000;
pthread_mutex_lock(&lock);
running = true;
pthread_mutex_unlock(&lock);
pollData.fd = socket->getSocket();
pollData.events = (POLLIN | POLLPRI);
pollData.revents = 0;
while ((retVal = poll(&pollData, 1, TIMEOUT)) != -1) {
if (exitPlease) {
cerr << "threadFunc exitPlease" << endl;
running = false;
pthread_exit(0);
}
//0 = timeout
if (retVal == 0) {
// wtf
} else {
cerr << "threadFunc incoming" << endl;
socket->incoming();
}
}
cerr << "retval: " << retVal << endl;
if(retVal==-1) perror("threadfunc");
pthread_mutex_lock(&lock);
running = false;
pthread_mutex_unlock(&lock);
pthread_exit(0);
}
void Selecter::doit(ServerSocket &socket) {
exitPlease = false;
running = false;
pthread_mutex_init(&lock, NULL);
pthread_create(&thread, NULL, &threadFunc, (void *)&socket);
}
void Selecter::quit() {
cout << "Selecter::quit()" << endl;
pthread_mutex_lock(&lock);
if (!running) {
pthread_mutex_unlock(&lock);
return;
}
cout << "Selecter set quit" << endl;
exitPlease = true;
pthread_mutex_unlock(&lock);
}
|