-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsleepSort.c
98 lines (86 loc) · 2.59 KB
/
sleepSort.c
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
#include "esotericFunMiscellaneous.h"
#if defined unix || defined __unix || defined __unix__ || defined __APPLE__ || defined __MACH__ || defined __linux__
long int *sortedValues;
int currentIndex;
sem_t lock;
pthread_t *threads;
int LENGTH;
void interruptHandler(int signum) {
for(int i = 0; i < LENGTH; i++)
pthread_cancel(threads[i]);
for(int i = 0; i < LENGTH; i++)
pthread_join(threads[i], NULL);
sem_destroy(&lock);
free(threads);
free(sortedValues);
exit(signum);
}
void *sortingBucket(void *arg) {
long int value = *((long int *)arg);
sleep(value);
sem_wait(&lock);
sortedValues[currentIndex++] = value;
sem_post(&lock);
pthread_exit(NULL);
}
void sleepSort(long int *arr, int len) {
LENGTH = len;
currentIndex ^= currentIndex;
signal(SIGINT, interruptHandler);
sortedValues = malloc(len * sizeof(long int));
threads = malloc(len * sizeof(pthread_t));
sem_init(&lock, 0, LENGTH);
for(int i = 0; i < LENGTH; i++)
pthread_create(&threads[i], NULL, sortingBucket, (void *)&(arr[i]));
for(int i = 0; i < LENGTH; i++)
pthread_join(threads[i], NULL);
for(int i = 0; i < LENGTH; i++)
arr[i] = sortedValues[i];
sem_destroy(&lock);
free(threads);
free(sortedValues);
}
#endif
#if defined _WIN32 || defined _WIN64 || defined __CYGWIN__
long int *sortedValues;
int currentIndex;
HANDLE *threads;
HANDLE lock;
int LENGTH;
void interruptHandler(int signum) {
for(int i = 0; i < LENGTH; i++)
TerminateThread(threads[i], 0);
for(int i = 0; i < LENGTH; i++)
CloseHandle(threads[i]);
CloseHandle(lock);
free(threads);
free(sortedValues);
ExitProcess(signum);
}
DWORD WINAPI sortingBucket(LPVOID arg) {
long int value = *((long int *)arg);
sleep(value);
WaitForSingleObject(lock, INFINITE);
sortedValues[currentIndex++] = value;
ReleaseSemaphore(lock, 1, NULL);
return 0;
}
void sleepSort(long int *arr, int len) {
LENGTH = len;
currentIndex ^= currentIndex;
signal(SIGINT, interruptHandler);
sortedValues = malloc(len * sizeof(long int));
threads = malloc(len * sizeof(HANDLE));
lock = CreateSemaphore(NULL, LENGTH, LENGTH, NULL);
for(int i = 0; i < LENGTH; i++)
threads[i] = CreateThread(NULL, 0, sortingBucket, (LPVOID)&(arr[i]), 0, NULL);
WaitForMultipleObjects(LENGTH, threads, TRUE, INFINITE);
for(int i = 0; i < LENGTH; i++)
CloseHandle(threads[i]);
for(int i = 0; i < LENGTH; i++)
arr[i] = sortedValues[i];
CloseHandle(lock);
free(threads);
free(sortedValues);
}
#endif