Last active
October 3, 2019 02:52
-
-
Save AHaliq/5029e872fcf15755f4aa3236044be2e2 to your computer and use it in GitHub Desktop.
CS2106 Lab3 ex2 Haliq
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
/************************************* | |
* Lab 3 Exercise 2 | |
* Name: Abdul Haliq | |
* Student No: A0125431U | |
* Lab Group: B-08 | |
*************************************/ | |
#include <pthread.h> | |
#include "rw_lock.h" | |
void initialise(rw_lock *lock) | |
{ | |
pthread_mutex_init(&(lock->rmutex), NULL); | |
pthread_mutex_init(&(lock->wmutex), NULL); | |
pthread_mutex_init(&(lock->empty), NULL); | |
pthread_mutex_init(&(lock->holdreaders), NULL); | |
lock->reader_count = 0; | |
lock->writer_count = 0; | |
} | |
void writer_acquire(rw_lock *lock) | |
{ | |
pthread_mutex_lock(&(lock->holdreaders)); | |
pthread_mutex_lock(&(lock->empty)); | |
pthread_mutex_lock(&(lock->wmutex)); | |
lock->writer_count++; | |
pthread_mutex_unlock(&(lock->wmutex)); | |
} | |
void writer_release(rw_lock *lock) | |
{ | |
pthread_mutex_unlock(&(lock->empty)); | |
pthread_mutex_lock(&(lock->wmutex)); | |
lock->writer_count--; | |
pthread_mutex_unlock(&(lock->wmutex)); | |
pthread_mutex_unlock(&(lock->holdreaders)); | |
} | |
void reader_acquire(rw_lock *lock) | |
{ | |
pthread_mutex_lock(&(lock->holdreaders)); | |
pthread_mutex_lock(&(lock->rmutex)); | |
if (lock->reader_count == 0) | |
{ | |
pthread_mutex_lock(&(lock->empty)); | |
} | |
lock->reader_count++; | |
pthread_mutex_unlock(&(lock->rmutex)); | |
pthread_mutex_unlock(&(lock->holdreaders)); | |
} | |
void reader_release(rw_lock *lock) | |
{ | |
pthread_mutex_lock(&(lock->rmutex)); | |
lock->reader_count--; | |
if (lock->reader_count == 0) | |
{ | |
pthread_mutex_unlock(&(lock->empty)); | |
} | |
pthread_mutex_unlock(&(lock->rmutex)); | |
} | |
void cleanup(rw_lock *lock) | |
{ | |
pthread_mutex_destroy(&(lock->rmutex)); | |
pthread_mutex_destroy(&(lock->wmutex)); | |
pthread_mutex_destroy(&(lock->empty)); | |
pthread_mutex_destroy(&(lock->holdreaders)); | |
} |
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
/************************************* | |
* Lab 3 Exercise 2 | |
* Name: Abdul Haliq | |
* Student No: A0125431U | |
* Lab Group: B-08 | |
*************************************/ | |
#include <pthread.h> | |
typedef struct | |
{ | |
pthread_mutex_t rmutex; | |
pthread_mutex_t wmutex; | |
pthread_mutex_t empty; | |
pthread_mutex_t holdreaders; | |
int reader_count; | |
int writer_count; | |
} rw_lock; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment