-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread_functions.c
More file actions
91 lines (61 loc) · 1.42 KB
/
thread_functions.c
File metadata and controls
91 lines (61 loc) · 1.42 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "basic.h"
#include "common.h"
#include "timer_functions.h"
#include "packet_functions.h"
#include "window_operations.h"
void cond_init(pthread_cond_t * cond)
{
if(pthread_cond_init(cond,NULL) != 0)
err_exit("cond init\n");
}
void mutex_init(pthread_mutex_t* mtx)
{
if(pthread_mutex_init(mtx,NULL) != 0)
err_exit("mutex init");
}
void mutex_lock(pthread_mutex_t* mtx)
{
if(pthread_mutex_lock(mtx) != 0)
err_exit("mutex lock");
}
void mutex_unlock(pthread_mutex_t* mtx)
{
if(pthread_mutex_unlock(mtx) != 0)
err_exit("mutex unlock");
}
void wait_cond(pthread_cond_t* cond,pthread_mutex_t* mtx)
{
if(pthread_cond_wait(cond,mtx)!= 0)
err_exit("cond wait");
}
void send_signal(pthread_cond_t* cond)
{
if(pthread_cond_signal(cond) != 0)
err_exit("cond wait");
}
void* thread_job(void* arg)
{
struct thread_data* td = (struct thread_data*) arg;
Window* w;
int sockfd;
struct sockaddr_in servaddr;
w = td->w;
sockfd = td->sockfd;
servaddr = td->servaddr;
struct timespec time_check = {0,7000000};
for(;;){
nanosleep(&time_check,NULL);
check_window(w,w->E,sockfd,servaddr); /*control expired packets*/
if(w->end == 1)
break;
}
return NULL;
}
void start_thread(struct thread_data td,struct sockaddr_in servaddr,int sockfd,Window* w)
{
td.servaddr = servaddr;
td.sockfd = sockfd;
td.w = w;
if(pthread_create(&(td.tid),NULL,thread_job,&td) != 0)
err_exit("pthread create");
}