Completely threaded...and should be thread safe!
This commit is contained in:
parent
e78bcfbd3e
commit
415f87a910
@ -14,7 +14,8 @@
|
||||
extern struct conf cf;
|
||||
short difflog = 0;
|
||||
extern pthread_mutex_t cfmx;
|
||||
|
||||
extern pthread_t networkth;
|
||||
extern pthread_t modemth;
|
||||
void usage(argv)
|
||||
const char* argv;
|
||||
{
|
||||
@ -51,8 +52,8 @@ int main(argc, argv)
|
||||
cf.loglevels = LL_ALL;
|
||||
pthread_mutex_unlock(&cfmx);
|
||||
initialize();
|
||||
network();
|
||||
shutd();
|
||||
pthread_create(&networkth, NULL, network, NULL);
|
||||
pthread_create(&modemth, NULL, modem_io, NULL);
|
||||
pthread_exit(NULL);
|
||||
return 0;
|
||||
}
|
||||
|
@ -53,6 +53,7 @@ int modemfd;
|
||||
unsigned int cou = 0;
|
||||
char buffer[512];
|
||||
short doing_cid = 0;
|
||||
pthread_t modemth;
|
||||
pthread_mutex_t modemmx = PTHREAD_MUTEX_INITIALIZER;
|
||||
pthread_mutex_t buffermx = PTHREAD_MUTEX_INITIALIZER;
|
||||
void stmod(const char* str)
|
||||
@ -167,3 +168,42 @@ void modem_hread(char* cbuf)
|
||||
}
|
||||
pthread_mutex_unlock(&buffermx);
|
||||
}
|
||||
void *modem_io(k)
|
||||
void* k;
|
||||
{
|
||||
fd_set fds;
|
||||
struct timeval tv;
|
||||
char cbuf[1];
|
||||
k = 0;
|
||||
*cbuf = '\0'; cbuf[1] = '\0';
|
||||
pthread_mutex_lock(&modemmx);
|
||||
for(;;) {
|
||||
FD_ZERO(&fds);
|
||||
FD_SET(modemfd, &fds);
|
||||
tv.tv_sec = 2; /* tunable */
|
||||
tv.tv_usec = 0;
|
||||
switch(select(modemfd + 1, &fds, NULL, NULL, &tv)) {
|
||||
case -1:
|
||||
lprintf(error, "select on modem: %s", strerror(errno));
|
||||
pthread_mutex_unlock(&modemmx);
|
||||
pthread_exit(NULL);
|
||||
break;
|
||||
case 0:
|
||||
pthread_mutex_unlock(&modemmx);
|
||||
sleep(1);
|
||||
pthread_mutex_lock(&modemmx);
|
||||
break;
|
||||
default:
|
||||
{
|
||||
if(FD_ISSET(modemfd, &fds) != 0) {
|
||||
read(modemfd, cbuf, 1);
|
||||
modem_hread(cbuf);
|
||||
*cbuf = '\0';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&modemmx);
|
||||
pthread_exit(NULL);
|
||||
return 0;
|
||||
}
|
||||
|
@ -47,11 +47,9 @@
|
||||
|
||||
#include <phoned.h>
|
||||
|
||||
extern int modemfd;
|
||||
extern FILE* modem;
|
||||
extern pthread_mutex_t modemmx;
|
||||
extern pthread_mutex_t buffermx;
|
||||
|
||||
pthread_t networkth;
|
||||
void *handclient(k)
|
||||
void* k;
|
||||
{
|
||||
@ -82,15 +80,18 @@ void *handclient(k)
|
||||
return 0;
|
||||
}
|
||||
|
||||
void network(void) /* name is misleading because we also do modem IO here */
|
||||
void *network(b)
|
||||
void* b;
|
||||
{
|
||||
int s, /* us,*/ sn;
|
||||
fd_set fds;
|
||||
int sin_size, ilen;
|
||||
pthread_t thr;
|
||||
char cbuf[1];
|
||||
void* is;
|
||||
cbuf[0] = '\0'; cbuf[1] = '\0';
|
||||
struct sockaddr_un it;
|
||||
is = b;
|
||||
if((s = socket(AF_LOCAL, SOCK_STREAM, 0)) == -1) {
|
||||
perror("socket");
|
||||
exit(-1);
|
||||
@ -110,7 +111,6 @@ void network(void) /* name is misleading because we also do modem IO here */
|
||||
for(;;) {
|
||||
FD_ZERO(&fds);
|
||||
FD_SET(s, &fds);
|
||||
FD_SET(modemfd, &fds);
|
||||
switch(select(s + 1, &fds, NULL, NULL, NULL)) {
|
||||
case -1:
|
||||
perror("select");
|
||||
@ -135,12 +135,6 @@ void network(void) /* name is misleading because we also do modem IO here */
|
||||
,ilen);
|
||||
|
||||
}
|
||||
if(FD_ISSET(modemfd, &fds) != 0)
|
||||
{
|
||||
read(modemfd, cbuf, 1);
|
||||
modem_hread(cbuf);
|
||||
*cbuf = '\0';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user