Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "domaci1.h"
- #include <math.h>
- #define UNLOCKED 0
- #define LOCKED 1
- #define TIMED_OUT 2
- typedef struct Node{
- int flag;
- struct Node * next;
- } Node;
- Node* tail = NULL;
- int sucsses = NULL;
- int sucssesTO = NULL;
- double startID[80];
- double minTime = 10.0;
- double maxTime = 0.0;
- void init_MCSLock(){
- }
- int lock_n_threads_with_timeout(int id, int* local, int timeout)
- {
- double start = get_time();
- startID[id] = start;
- double wait = timeout/1000;
- //printf(" start =%d \n", start);
- //fflush(stdout);
- Node* node = (Node*) malloc(sizeof(Node));
- node->flag = LOCKED;
- node->next = NULL;
- *local = node;
- Node* old = (Node*) get_and_set((int*)&tail, (int)node);
- if(old != NULL){
- old->next = node;
- while(1){
- double end = get_time();
- double waitedTime = (double)end - start;
- //printf("end= %lf | waitedTime= %lf\n", end, waitedTime);
- //fflush(stdout);
- if((waitedTime) < 0.2){
- if(node->flag == LOCKED){
- //printf("Sleep ");
- //fflush(stdout);
- sleep(1);
- }else if(node->flag == UNLOCKED){
- //printf("UNLOCKED ");
- //fflush(stdout);
- //node->flag = UNLOCKED;
- return 1;
- }
- }else if((waitedTime) >= 0.2){
- //printf("TO ");
- //fflush(stdout);
- node->flag = TIMED_OUT;
- return 0;
- }
- }
- } else{
- printf("FIRST ");
- fflush(stdout);
- // node->flag = UNLOCKED;
- return 1;
- }
- }
- void unlock_n_threads_with_timeout(int id, int* local)
- {
- Node* node = (Node*) *local;
- if(node->next == NULL){
- sucsses = (Node*) compare_and_set((int*)&tail, (int)node, (int)NULL);
- if(sucsses){
- printf("FIN ");
- fflush(stdout);
- }else{
- while(node->next == NULL){
- printf("sucess_Fail ");
- fflush(stdout);
- sleep(1);
- }
- node->next->flag = UNLOCKED;
- }
- } else{
- Node* nextNode = (Node*) node->next;
- while(1){
- //LOCKED->UNLOCKED
- if((Node*)compare_and_set((int*)&nextNode->flag,(int) LOCKED,(int) UNLOCKED)){
- //printf("LOCK->UNLOCK ");
- //fflush(stdout);
- break;
- }
- else {
- //TIME_OUT
- if((Node*)compare_and_set((int*)&nextNode->next,(int)LOCKED, (int) UNLOCKED )){ //TIME_OUT-ovao, da li je sledeci ok?
- printf("TO_LOCK->UNLOCK\n");
- fflush(stdout);
- break;
- }else if(nextNode->next == NULL){
- if((Node*)compare_and_set((int*)&tail,(int)nextNode,(int)NULL)){ //TIME_OUT-ovao, da li je zadnji?
- printf("FIN_TO\n");
- fflush(stdout);
- break;
- }else{
- while(nextNode->next == NULL){
- printf("sucessTO_Fail ");
- fflush(stdout);
- sleep(1);
- break;
- }
- }
- }else nextNode = nextNode->next;
- }
- }
- /* if(nextNode->flag == TIMED_OUT){
- if(nextNode->next == NULL){
- sucssesTO = (Node*)compare_and_set(&tail, nextNode, NULL);
- if(sucssesTO){
- break;
- }else while (nextNode->next == NULL)
- sleep(1);
- }
- }else{
- // printf("LOCKED->UNLOCKED \n");
- //// fflush(stdout);
- nextNode->flag = UNLOCKED;
- break;
- //free(node);
- } */
- }
- double finalTime = get_time() - startID[id];
- if(finalTime > maxTime) maxTime = finalTime;
- if(finalTime < minTime) minTime = finalTime;
- }
- int main()
- {
- start_timeout_mutex_n_threads_test(200);
- printf("MAX DELTA = %.5lf\n", maxTime);
- printf("MIN DELTA = %.5lf\n", minTime);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement