-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread_handle.c
More file actions
101 lines (96 loc) · 2.76 KB
/
thread_handle.c
File metadata and controls
101 lines (96 loc) · 2.76 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
88
89
90
91
92
93
94
95
96
97
98
99
100
#include"global.h"
extern int global_port; //from server_handle.c
extern int max_slave_server; //freom server_handle.c
static int active_ports=0; //Keep count on number of ports that are active active
/*=========================================================================================
Function responsible for handling threads
=========================================================================================*/
// fuction responsible for pushing thread in stack and deactivate them
void push_thread(struct threadID * temp_thread)
{
pthread_mutex_lock(&mutex_variable);
if(thread_header==NULL)
{
thread_header=temp_thread;
thread_header->next=NULL;
}
else
{
temp_thread->next=thread_header;
thread_header=temp_thread;
}
temp_thread->status=0; // to make thread stop running till it get call
++active_ports;
pthread_mutex_unlock(&mutex_variable);
}
//function responsible for creation of thread- to be called once
int intialize_thread() // return 1 on success else 0.
{
int i;
struct threadID *new_thread=NULL;
for(i=0;i<max_slave_server;i++)
{
new_thread=malloc(sizeof(struct threadID));// allocating space to new node
if(new_thread==NULL)
{
printf("\nMemory overflow");
return 0;
}
else
{
new_thread->client.port=global_port+1+i; // allocating port number to newly created thread
if(sem_init(&(new_thread->binary_sem),0,0)==0)
new_thread->status=0;
pthread_create(&(new_thread->ID),NULL,handle,new_thread);
// opening port for accepting connections
if(common_connect_server(&host_ipaddress,&new_thread->client.server_socket,new_thread->client.port,(const char *)NULL)==0)
{
printf("\n Not able to open connection");
return 1;
}
printf("\n Able to open new port\n");
push_thread(new_thread); // pushing newly created thread into stack
}
}
return 1;
}
// function for activating thread
int activate_thread()
{
struct threadID * temp_thread;
pthread_mutex_lock(&mutex_variable);
if(thread_header==NULL)
{
pthread_mutex_unlock(&mutex_variable);
return 0;
}
else
{ printf("\n switching");//TODO
temp_thread=thread_header;
thread_header=thread_header->next;
}
active_ports--;
pthread_mutex_unlock(&mutex_variable);
printf("\n Unlocking");
sem_post(&(temp_thread->binary_sem));
}
void cleanup_thread()
{
struct threadID *temp_thread;
pthread_mutex_lock(&mutex_variable);
while(thread_header!=NULL)
{
temp_thread=thread_header;
thread_header=thread_header->next;
SDLNet_TCP_Close(temp_thread->client.server_socket);
pthread_cancel(temp_thread->ID); //stoping thread
sem_destroy(&(temp_thread->binary_sem));
free(temp_thread);
}
if(thread_header==NULL)
printf("\n Great every thing is cleaned");
}
int get_active_threads()
{
return active_ports;
}