static size_t uart16550_write(struct file *filp, const char *user_buffer, size_t size, loff_t *offset) { struct task_struct **task_user_push_data = ftask_user_push_data(filp->private_data); struct kfifo* data_from_user = fdata_from_user(filp->private_data); int i; for(i = 0; i<size; i++){ wait_for_queue(task_user_push_data,can_write,data_from_user); kfifo_put(data_from_user,charToU8(user_buffer[i])); } return size; }
static size_t uart16550_read(struct file *filp, const char *buffer, size_t size, loff_t *offset) { struct task_struct **task_user_get_data = ftask_user_get_data(filp->private_data); struct kfifo *data_from_device = fdata_from_device (filp->private_data); uint8_t u8; int i; char* pos = buffer; for(i =0;i<size;i++) { wait_for_queue(task_user_get_data,can_read,data_from_device); kfifo_get(data_from_device,&u8); put_user(u8ToChar(u8),pos+i); } return size; }
int thread_main(server_decl_t * srv) { ci_connection_t con; char clientname[CI_MAXHOSTNAMELEN + 1]; int ret, request_status = CI_NO_STATUS; int keepalive_reqs; //*********************** thread_signals(0); //************************* srv->srv_id = getpid(); //Setting my pid ... for (;;) { /* If we must shutdown IMEDIATELLY it is time to leave the server else if we are going to shutdown GRACEFULLY we are going to die only if there are not any accepted connections */ if (child_data->to_be_killed == IMMEDIATELY) { srv->running = 0; return 1; } if ((ret = get_from_queue(con_queue, &con)) == 0) { if (child_data->to_be_killed) { srv->running = 0; return 1; } ret = wait_for_queue(con_queue); continue; } if (ret < 0) { //An error has occured ci_debug_printf(1, "Fatal Error!!! Error getting a connection from connections queue!!!\n"); break; } ci_thread_mutex_lock(&counters_mtx); /*Update counters as soon as possible */ (child_data->freeservers)--; (child_data->usedservers)++; ci_thread_mutex_unlock(&counters_mtx); ci_netio_init(con.fd); ret = 1; if (srv->current_req == NULL) srv->current_req = newrequest(&con); else ret = recycle_request(srv->current_req, &con); if (srv->current_req == NULL || ret == 0) { ci_sockaddr_t_to_host(&(con.claddr), clientname, CI_MAXHOSTNAMELEN); ci_debug_printf(1, "Request from %s denied...\n", clientname); hard_close_connection((&con)); goto end_of_main_loop_thread; /*The request rejected. Log an error and continue ... */ } keepalive_reqs = 0; do { if (MAX_KEEPALIVE_REQUESTS > 0 && keepalive_reqs >= MAX_KEEPALIVE_REQUESTS) srv->current_req->keepalive = 0; /*do not keep alive connection */ if (child_data->to_be_killed) /*We are going to die do not keep-alive */ srv->current_req->keepalive = 0; if ((request_status = process_request(srv->current_req)) == CI_NO_STATUS) { ci_debug_printf(5, "Process request timeout or interrupted....\n"); ci_request_reset(srv->current_req); break; } srv->served_requests++; srv->served_requests_no_reallocation++; keepalive_reqs++; /*Increase served requests. I dont like this. The delay is small but I don't like... */ ci_thread_mutex_lock(&counters_mtx); (child_data->requests)++; ci_thread_mutex_unlock(&counters_mtx); log_access(srv->current_req, request_status); // break; //No keep-alive ...... if (child_data->to_be_killed == IMMEDIATELY) break; //Just exiting the keep-alive loop /*if we are going to term gracefully we will try to keep our promice for keepalived request.... */ if (child_data->to_be_killed == GRACEFULLY && srv->current_req->keepalive == 0) break; ci_debug_printf(8, "Keep-alive:%d\n", srv->current_req->keepalive); if (srv->current_req->keepalive && keepalive_request(srv->current_req)) { ci_debug_printf(8, "Server %d going to serve new request from client (keep-alive) \n", srv->srv_id); } else break; } while (1); if (srv->current_req) { if (request_status != CI_OK || child_data->to_be_killed) { hard_close_connection(srv->current_req->connection); } else { close_connection(srv->current_req->connection); } } if (srv->served_requests_no_reallocation > MAX_REQUESTS_BEFORE_REALLOCATE_MEM) { ci_debug_printf(5, "Max requests reached, reallocate memory and buffers .....\n"); ci_request_destroy(srv->current_req); srv->current_req = NULL; srv->served_requests_no_reallocation = 0; } end_of_main_loop_thread: ci_thread_mutex_lock(&counters_mtx); (child_data->freeservers)++; (child_data->usedservers)--; ci_thread_mutex_unlock(&counters_mtx); ci_thread_cond_signal(&free_server_cond); } srv->running = 0; return 0; }
int thread_main(server_decl_t *srv){ ci_connection_t con; ci_thread_mutex_t cont_mtx; char clientname[CI_MAXHOSTNAMELEN+1]; int max,ret,request_status=0; request_t *tmp; //*********************** thread_signals(); //************************* srv->srv_id=getpid(); //Setting my pid ... srv->srv_pthread=pthread_self(); for(;;){ if(child_data->to_be_killed) return; //Exiting thread..... if((ret=get_from_queue(con_queue,&con))==0){ wait_for_queue(con_queue); //It is better that the wait_for_queue to be //moved into the get_from_queue continue; } ci_thread_mutex_lock(&counters_mtx); (child_data->freeservers)--; (child_data->usedservers)++; ci_thread_mutex_unlock(&counters_mtx); if(ret<0){ //An error has occured debug_printf(1,"Error getting from connections queue\n"); break; } /* icap_addrtohost(&(con.claddr.sin_addr),clientname, CI_MAXHOSTNAMELEN); debug_printf(1,"Client name: %s server %d\n",clientname,srv->srv_id); */ icap_netio_init(con.fd); if(srv->current_req==NULL) srv->current_req=newrequest(&con); else recycle_request(srv->current_req,&con); do{ if((request_status=process_request(srv->current_req))<0){ debug_printf(5,"Process request timeout or interupted....\n"); reset_request(srv->current_req); break;// } srv->served_requests++; srv->served_requests_no_reallocation++; /*Increase served requests. I dont like this. The delay is small but I don't like...*/ ci_thread_mutex_lock(&counters_mtx); (child_data->requests)++; ci_thread_mutex_unlock(&counters_mtx); log_access(srv->current_req,request_status); // break; //No keep-alive ...... if(child_data->to_be_killed) return; //Exiting thread..... debug_printf(8,"Keep-alive:%d\n",srv->current_req->keepalive); if(srv->current_req->keepalive && check_for_keepalive_data(srv->current_req->connection->fd)){ reset_request(srv->current_req); debug_printf(8,"Server %d going to serve new request from client(keep-alive) \n", srv->srv_id); } else break; }while(1); if(srv->current_req){ if(request_status<0) hard_close_connection(srv->current_req->connection); else close_connection(srv->current_req->connection); } if(srv->served_requests_no_reallocation > MAX_REQUESTS_BEFORE_REALLOCATE_MEM){ debug_printf(5,"Max requests reached, reallocate memory and buffers .....\n"); destroy_request(srv->current_req); srv->current_req=NULL; srv->served_requests_no_reallocation=0; } ci_thread_mutex_lock(&counters_mtx); (child_data->freeservers)++; (child_data->usedservers)--; ci_thread_mutex_unlock(&counters_mtx); ci_thread_cond_signal(&free_server_cond); } return 0; }