Compare commits
No commits in common. "808c511a7b3111684ea4f6ff28fb57787b2c51f5" and "1043680b195f0ba03f893a2aa9ebce8d0d7bf0c2" have entirely different histories.
808c511a7b
...
1043680b19
1 changed files with 32 additions and 65 deletions
97
main.cpp
97
main.cpp
|
@ -3,24 +3,18 @@
|
||||||
* @author Blake North <blake.north@digipen.edu>
|
* @author Blake North <blake.north@digipen.edu>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <cstddef> // size_t
|
#include <cstddef> // size_t
|
||||||
#include <cstring> // memset
|
#include <cstring> // memset
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include <iostream> // cerr
|
#include <iostream> // cout
|
||||||
#include <vector> // vector
|
#include <vector> // vector
|
||||||
|
|
||||||
// used for threads
|
// used for threads
|
||||||
#include <pthread.h> // pthread
|
#include <pthread.h> // pthread
|
||||||
#include <unistd.h> // sleep() - testing
|
#include <unistd.h> // sleep() - testing
|
||||||
|
|
||||||
#define THREAD_LOOP_COUNT 10
|
|
||||||
#define NUM_THREADS 8
|
#define NUM_THREADS 8
|
||||||
|
|
||||||
// different lengths of sleep, so the test can run in a reasonable time
|
|
||||||
#define RANDSLEEP(c) usleep(rand() % c) // random sleep delay
|
|
||||||
#define SLEEP_LIGHT /* RANDSLEEP(10) */ // short sleep, for blocking code
|
|
||||||
#define SLEEP_HEAVY RANDSLEEP(100) // long sleep, for non-blocking code
|
|
||||||
|
|
||||||
typedef void *(*ThreadTask)(const struct thread_data &);
|
typedef void *(*ThreadTask)(const struct thread_data &);
|
||||||
typedef void *(*PthreadFun)(void *arg);
|
typedef void *(*PthreadFun)(void *arg);
|
||||||
|
|
||||||
|
@ -45,70 +39,54 @@ struct thread_data {
|
||||||
|
|
||||||
void *thread_task_increment(const struct thread_data &thread) {
|
void *thread_task_increment(const struct thread_data &thread) {
|
||||||
// exists inside the loop
|
// exists inside the loop
|
||||||
SLEEP_HEAVY;
|
const size_t max_loops = 10;
|
||||||
const size_t max_loops = THREAD_LOOP_COUNT;
|
|
||||||
|
|
||||||
SLEEP_HEAVY;
|
|
||||||
size_t loops = 0;
|
size_t loops = 0;
|
||||||
|
|
||||||
SLEEP_HEAVY;
|
std::cout << "Hello from thread " << thread.id << "! (before run loop)"
|
||||||
|
|
||||||
std::cerr << "Hello from thread " << thread.id << "! (before run loop)"
|
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
|
|
||||||
SLEEP_HEAVY;
|
|
||||||
|
|
||||||
// thread loop, with an exit condition
|
// thread loop, with an exit condition
|
||||||
while (!(*thread.is_finished)) {
|
while (!(*thread.is_finished)) {
|
||||||
SLEEP_HEAVY;
|
|
||||||
|
|
||||||
// non-mutex operations
|
// non-mutex operations
|
||||||
{
|
{
|
||||||
std::cerr << "Hello from thread " << thread.id
|
std::cout << "Hello from thread " << thread.id
|
||||||
<< "! (inside run loop, before mutex)" << std::endl;
|
<< "! (inside run loop, before mutex)" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
SLEEP_HEAVY;
|
usleep(rand() % 200);
|
||||||
|
|
||||||
// mutex
|
// mutex
|
||||||
{
|
{
|
||||||
|
|
||||||
// wait for permission to ask for the mutex
|
// wait for permission to ask for the mutex
|
||||||
while (!*thread.can_ask_for_mutex)
|
while (!*thread.can_ask_for_mutex)
|
||||||
SLEEP_LIGHT;
|
;
|
||||||
SLEEP_HEAVY;
|
|
||||||
// say we want the mutex
|
// say we want the mutex
|
||||||
(*thread.wants_mutex) = true;
|
(*thread.wants_mutex) = true;
|
||||||
SLEEP_HEAVY;
|
usleep(rand() % 200);
|
||||||
// block until we have the mutex
|
// block until we have the mutex
|
||||||
while (!*thread.has_mutex)
|
while (!*thread.has_mutex)
|
||||||
SLEEP_LIGHT;
|
;
|
||||||
|
|
||||||
// enter the mutex
|
// enter the mutex
|
||||||
|
|
||||||
std::cerr << "Hello from thread " << thread.id
|
std::cout << "Hello from thread " << thread.id
|
||||||
<< "! (inside run loop, inside mutex)" << std::endl;
|
<< "! (inside run loop, inside mutex)" << std::endl;
|
||||||
|
|
||||||
SLEEP_LIGHT;
|
usleep(rand() % 200);
|
||||||
|
|
||||||
// increment the int (requires casting from void*)
|
|
||||||
*static_cast<int *>(thread.data) += 1;
|
*static_cast<int *>(thread.data) += 1;
|
||||||
|
|
||||||
SLEEP_LIGHT;
|
|
||||||
|
|
||||||
// tell the thread manager we don't need the mutex
|
// tell the thread manager we don't need the mutex
|
||||||
(*thread.wants_mutex) = false;
|
(*thread.wants_mutex) = false;
|
||||||
SLEEP_HEAVY;
|
|
||||||
// mutex exit
|
// mutex exit
|
||||||
}
|
}
|
||||||
loops++;
|
loops++;
|
||||||
if (max_loops < loops) {
|
if (max_loops < loops)
|
||||||
SLEEP_HEAVY;
|
|
||||||
(*thread.is_finished) = true;
|
(*thread.is_finished) = true;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cerr << "Hello from thread " << thread.id << "! (done)" << std::endl;
|
std::cout << "Hello from thread " << thread.id << "! (done)" << std::endl;
|
||||||
|
|
||||||
pthread_exit(nullptr);
|
pthread_exit(nullptr);
|
||||||
}
|
}
|
||||||
|
@ -151,54 +129,52 @@ void do_threading(struct thread_group threads) {
|
||||||
|
|
||||||
// spawn threads (none will enter the mutex yet)
|
// spawn threads (none will enter the mutex yet)
|
||||||
for (size_t tid = 0; tid < threads.total_threads; ++tid) {
|
for (size_t tid = 0; tid < threads.total_threads; ++tid) {
|
||||||
SLEEP_HEAVY;
|
usleep(rand() % 200);
|
||||||
pthread_create(&my_pthreads[tid], NULL,
|
pthread_create(&my_pthreads[tid], NULL,
|
||||||
reinterpret_cast<PthreadFun>(thread_task_increment),
|
reinterpret_cast<PthreadFun>(thread_task_increment),
|
||||||
&thread_data[tid]);
|
&thread_data[tid]);
|
||||||
SLEEP_HEAVY;
|
usleep(rand() % 200);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cerr << "Threads have been spawned." << std::endl;
|
std::cout << "Threads have been spawned." << std::endl;
|
||||||
|
|
||||||
// loop until all threads are done
|
// loop until all threads are done
|
||||||
for (size_t finished_threads = 0; finished_threads < threads.total_threads;) {
|
for (size_t finished_threads = 0; finished_threads < threads.total_threads;) {
|
||||||
|
|
||||||
SLEEP_LIGHT;
|
usleep(rand() % 200);
|
||||||
// TODO: make sure we cycle the mutex through threads round-robin style
|
// TODO: make sure we cycle the mutex through threads round-robin style
|
||||||
|
|
||||||
// hand off the mutex to threads that want it
|
// hand off the mutex to threads that want it
|
||||||
for (size_t tid_wants = 0; tid_wants < threads.total_threads; ++tid_wants) {
|
for (size_t tid_wants = 0; tid_wants < threads.total_threads; ++tid_wants) {
|
||||||
|
usleep(rand() % 200);
|
||||||
|
|
||||||
SLEEP_LIGHT;
|
|
||||||
if (threads.wants_mutex[tid_wants]) {
|
if (threads.wants_mutex[tid_wants]) {
|
||||||
// in case the mutex isn't used at all
|
// in case the mutex isn't used at all
|
||||||
bool mutex_was_found = false;
|
bool mutex_was_found = false;
|
||||||
|
usleep(rand() % 200);
|
||||||
|
|
||||||
SLEEP_LIGHT;
|
|
||||||
// find which thread has the mutex and hand it off if it's done
|
// find which thread has the mutex and hand it off if it's done
|
||||||
for (size_t tid_has = 0; tid_has < threads.total_threads; ++tid_has) {
|
for (size_t tid_has = 0; tid_has < threads.total_threads; ++tid_has) {
|
||||||
|
|
||||||
if (threads.has_mutex[tid_has]) {
|
if (threads.has_mutex[tid_has]) {
|
||||||
|
|
||||||
SLEEP_LIGHT;
|
|
||||||
// we found who has the mutex!
|
// we found who has the mutex!
|
||||||
mutex_was_found = true;
|
mutex_was_found = true;
|
||||||
SLEEP_LIGHT;
|
usleep(rand() % 200);
|
||||||
|
|
||||||
// is the thread still using the mutex?
|
// is the thread still using the mutex?
|
||||||
if (!threads.wants_mutex[tid_has]) {
|
if (!threads.wants_mutex[tid_has]) {
|
||||||
|
|
||||||
SLEEP_LIGHT;
|
usleep(rand() % 200);
|
||||||
// take the mutex from the thread that has it
|
// take the mutex from the thread that has it
|
||||||
threads.has_mutex[tid_has] = false;
|
threads.has_mutex[tid_has] = false;
|
||||||
SLEEP_LIGHT;
|
usleep(rand() % 200);
|
||||||
threads.can_ask_for_mutex[tid_has] = true;
|
threads.can_ask_for_mutex[tid_has] = true;
|
||||||
SLEEP_HEAVY;
|
usleep(rand() % 200);
|
||||||
// give the mutex to the thread that wants it
|
// give the mutex to the thread that wants it
|
||||||
threads.can_ask_for_mutex[tid_wants] = false;
|
threads.can_ask_for_mutex[tid_wants] = false;
|
||||||
SLEEP_LIGHT;
|
usleep(rand() % 200);
|
||||||
threads.has_mutex[tid_wants] = true;
|
threads.has_mutex[tid_wants] = true;
|
||||||
SLEEP_HEAVY;
|
usleep(rand() % 200);
|
||||||
}
|
}
|
||||||
|
|
||||||
break; // no need to look at the rest if we found who has the mutex
|
break; // no need to look at the rest if we found who has the mutex
|
||||||
|
@ -207,22 +183,19 @@ void do_threading(struct thread_group threads) {
|
||||||
|
|
||||||
// give the thread the mutex if it wasn't found to be in use
|
// give the thread the mutex if it wasn't found to be in use
|
||||||
if (!mutex_was_found) {
|
if (!mutex_was_found) {
|
||||||
SLEEP_LIGHT;
|
usleep(rand() % 200);
|
||||||
threads.has_mutex[tid_wants] = true;
|
threads.has_mutex[tid_wants] = true;
|
||||||
SLEEP_LIGHT;
|
|
||||||
}
|
}
|
||||||
SLEEP_LIGHT;
|
usleep(rand() % 200);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// find how many threads are done with the mutex
|
// find how many threads are done with the mutex
|
||||||
finished_threads = 0;
|
finished_threads = 0;
|
||||||
for (size_t tid = 0; tid < threads.total_threads; ++tid) {
|
for (size_t tid = 0; tid < threads.total_threads; ++tid) {
|
||||||
if (threads.threads_finished[tid]) {
|
if (threads.threads_finished[tid])
|
||||||
finished_threads += 1;
|
finished_threads += 1;
|
||||||
SLEEP_LIGHT;
|
usleep(rand() % 200);
|
||||||
}
|
|
||||||
SLEEP_LIGHT;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -237,16 +210,12 @@ void do_threading(struct thread_group threads) {
|
||||||
int main(void) {
|
int main(void) {
|
||||||
struct thread_group mythreads;
|
struct thread_group mythreads;
|
||||||
|
|
||||||
// initialize random (for random sleep amounts)
|
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
|
|
||||||
// the count
|
// the count
|
||||||
int count = 0;
|
int count = 0;
|
||||||
|
|
||||||
// expected result
|
std::cout << "Pre: " << DBG_PRINT(count) << std::endl;
|
||||||
const int count_expected = NUM_THREADS * (THREAD_LOOP_COUNT + 1) + count;
|
|
||||||
|
|
||||||
std::cout << "Expected: " << count_expected << std::endl;
|
|
||||||
|
|
||||||
mythreads.data = &count;
|
mythreads.data = &count;
|
||||||
mythreads.total_threads = NUM_THREADS;
|
mythreads.total_threads = NUM_THREADS;
|
||||||
|
@ -254,7 +223,5 @@ int main(void) {
|
||||||
|
|
||||||
do_threading(mythreads);
|
do_threading(mythreads);
|
||||||
|
|
||||||
std::cout << "Got: " << count << std::endl;
|
std::cout << "Post: " << DBG_PRINT(count) << std::endl;
|
||||||
|
|
||||||
return !(count_expected == count);
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue