-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeadLock.cpp
143 lines (110 loc) · 3.39 KB
/
DeadLock.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "Shapes.hpp"
#if defined(_MSC_VER) || defined(__MINGW32__)
#include <windows.h>
#define THREAD HANDLE
#define THREAD_RETURN_TYPE DWORD WINAPI
#define THREAD_CREATE(thread, func) thread = CreateThread(0, 0, func, 0, 0, 0)
#define LOCK CRITICAL_SECTION
#define LOCK_ACQUIRE(lock) EnterCriticalSection(&lock)
#define LOCK_RELEASE(lock) LeaveCriticalSection(&lock)
#define SLEEP(x) Sleep(x)
#else
#if (defined(__SUNPRO_C) || defined(__SUNPRO_CC) || defined(__GNUC__) || defined(__IBMCPP__) || defined(__ghs)) && !defined(__sh__) && !defined(__VXWORKS__) && !defined(__DCC__) && !defined(__nios__) && !defined(__nios2__) && !defined(__V850__) && (!defined(__ppc__) || defined(__IBMCPP__) || defined(__IBMC__))
#include <pthread.h>
#include <unistd.h>
#else
typedef void* pthread_t;
typedef void* pthread_attr_t;
typedef void* pthread_mutex_t;
int pthread_create (pthread_t * __newthread,
pthread_attr_t *__attr,
void *(*__start_routine) (void *),
void * __arg);
int pthread_mutex_lock (pthread_mutex_t *__mutex);
int pthread_mutex_unlock (pthread_mutex_t *__mutex);
void sleep(int millis);
#endif
#define THREAD pthread_t
#define THREAD_RETURN_TYPE void*
#define THREAD_CREATE(thread, func) pthread_create(&thread, 0, &func, 0)
#define LOCK pthread_mutex_t
#define LOCK_ACQUIRE(lock) pthread_mutex_lock(&lock)
#define LOCK_RELEASE(lock) pthread_mutex_unlock(&lock)
#define SLEEP(x) sleep(x)
#endif
#define MAX_OBJECTS 100
#define STEP 20
void assertion(int condition, const char* message)
{
if (!condition) {
throw message;
}
}
static LOCK changePositionMutex;
Circle ring(Point(0,0), 100);
Shape *participants[MAX_OBJECTS];
int participantsCount;
int exitGame;
THREAD_RETURN_TYPE GameLogic_Thread(void*)
{
// Implementation of function GameLogic_Thread()
return (THREAD_RETURN_TYPE)0;
}
THREAD_RETURN_TYPE Controller_Thread(void*)
{
// Implementation of function Controller_Thread()
return (THREAD_RETURN_TYPE)0;
}
namespace Physics
{
Point *velocityArray[MAX_OBJECTS];
THREAD_RETURN_TYPE Physics_Thread(void*)
{
while (!exitGame) {
for(int i = 0; i < participantsCount; i++) {
LOCK_ACQUIRE(changePositionMutex);
Point& position = participants[i]->getPosition();
position.translate(*velocityArray[i]);
assertion(ring.contains(position), "Participant is out of ring");
// Deadlock: when assertion fails (throws exception)
// which cause game freeze
LOCK_RELEASE(changePositionMutex);
}
SLEEP(STEP);
}
return 0;
}
}
namespace Render
{
Point *currentCameraVelocity;
void draw(const Shape *object)
{
// Implementation of function draw()
}
THREAD_RETURN_TYPE Render_Thread(void*)
{
while (!exitGame) {
LOCK_ACQUIRE(changePositionMutex);
ring.getPosition().translate(*currentCameraVelocity);
LOCK_RELEASE(changePositionMutex);
for(int i = 0; i < participantsCount; i++) {
LOCK_ACQUIRE(changePositionMutex);
participants[i]->getPosition().translate(*currentCameraVelocity);
LOCK_RELEASE(changePositionMutex);
draw(participants[i]);
}
SLEEP(STEP);
}
return 0;
}
}
void runGameThreads()
{
exitGame = 0;
THREAD thread1, thread2, thread3, thread4;
THREAD_CREATE(thread1, GameLogic_Thread);
THREAD_CREATE(thread2, Controller_Thread);
THREAD_CREATE(thread3, Physics::Physics_Thread);
THREAD_CREATE(thread4, Render::Render_Thread);
}