-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from dario-santos/feature-multithread
feature-multithread -> master
- Loading branch information
Showing
13 changed files
with
407 additions
and
47 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#include <semaphore.h> | ||
#include <stdlib.h> | ||
#include "lock.h" | ||
|
||
sem_t sem_lock; | ||
sem_t sem_unlock; | ||
|
||
void lock_sem_init(void) | ||
{ | ||
sem_init(&sem_lock, 0, 1); | ||
sem_init(&sem_unlock, 0, 1); | ||
} | ||
|
||
int lock(int *L, int size, int index) | ||
{ | ||
if (index <= -1 || index >= size) | ||
return 0; | ||
|
||
sem_wait(&sem_lock); | ||
if (L[index] == 0) | ||
L[index] = 1; | ||
else | ||
{ | ||
sem_post(&sem_lock); | ||
return 0; | ||
} | ||
|
||
sem_post(&sem_lock); | ||
return 1; | ||
} | ||
|
||
int unlock(int *L, int size, int index) | ||
{ | ||
if (index <= -1 || index >= size) | ||
return 0; | ||
|
||
sem_wait(&sem_unlock); | ||
|
||
if (L[index] == 1) | ||
L[index] = 0; | ||
else | ||
{ | ||
sem_post(&sem_unlock); | ||
return 0; | ||
} | ||
|
||
sem_post(&sem_unlock); | ||
return 1; | ||
} | ||
|
||
int *locks_insert_node(int *L, int *size) | ||
{ | ||
(*size)++; | ||
|
||
if (*size == 1) | ||
L = (int *)malloc(sizeof(int)); | ||
else | ||
L = (int *)realloc(L, (*size) * sizeof(int)); | ||
|
||
L[*size - 1] = 0; | ||
|
||
return L; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#ifndef LOCK_H | ||
#define LOCK_H | ||
|
||
void lock_sem_init(void); | ||
|
||
int lock(int *L, int size, int index); | ||
|
||
int unlock(int *L, int size, int index); | ||
|
||
int* locks_insert_node(int *L, int *size); | ||
|
||
#endif // LOCK_H |
Oops, something went wrong.