aboutsummaryrefslogtreecommitdiffstats
path: root/row_dispenser.c
diff options
context:
space:
mode:
authoradam <adam@volconst.com>2021-09-27 13:13:17 +0300
committeradam <adam@volconst.com>2021-09-27 13:13:17 +0300
commit6e014ca5ce263e96549236b5bf4f2379467b9e2b (patch)
tree56dfb669fdca4118cddedc265dd2ea1768c8aaa7 /row_dispenser.c
downloaddethread-6e014ca5ce263e96549236b5bf4f2379467b9e2b.tar.gz
setting up git on old project
Diffstat (limited to 'row_dispenser.c')
-rw-r--r--row_dispenser.c86
1 files changed, 86 insertions, 0 deletions
diff --git a/row_dispenser.c b/row_dispenser.c
new file mode 100644
index 0000000..0d9b47f
--- /dev/null
+++ b/row_dispenser.c
@@ -0,0 +1,86 @@
+#ifndef GTHREAD_DISPENSER
+#define GTHREAD_DISPENSER GTHREAD_DISPENSER
+#define function_pointer(name) void (*name)()
+#include<pthread.h>
+#include<semaphore.h>
+#include<stdlib.h>//malloc et al.
+#include"gmp.h"
+
+/*
+ * we clean the 'source_row' column using the source row
+ */
+struct row_pair
+{
+ size_t source_row;
+ size_t target_row;
+};
+
+/*
+ * stuff conserning the synchronisation of a single pass ( sync. between passes is in threadet)
+ * could consider these the parameters passed to the threads ( but they are shared )
+ */
+struct Pair_Dispenser
+{
+ /* mutex */
+ sem_t semp;
+
+ /*
+ * current is the next pair to be calculated
+ * pairs given are unique in the scope of the pass
+ *
+ */
+ struct row_pair current;
+ /*
+ * max is the maximum index. = matrix size - 1
+ * this value is not modified by the threads
+ *
+ */
+ size_t max;
+ /*
+ * the matrix that we calculate the matrix for
+ * this value is not modified by the threads
+ *
+ */
+ mpq_t** matrix;
+ /*
+ * should the thread print stuff
+ * this value is not modified by the threads
+ *
+ */
+ char verbose;
+
+};
+
+void Pair_Dispenser_Init(struct Pair_Dispenser *thrd,size_t max,mpq_t** matrix,char verbose)
+{
+ thrd->current.source_row = 0;
+ thrd->current.target_row = 0;
+ thrd->max=max;
+ thrd->matrix=matrix;
+ sem_init(&(thrd->semp),0,1);
+ thrd->verbose = verbose;
+}
+/*
+ * Get a row pair without breaking the row dispenser invariant
+ */
+struct row_pair Pair_Dispenser_Get_Pair(struct Pair_Dispenser *thrd)
+{
+ struct row_pair temp;
+ /* this is 1/2 of what will slow down parallel execution */
+ /* critical part */
+ sem_wait(&(thrd->semp));
+ temp=thrd->current;
+ if(thrd->current.target_row<= thrd->max)
+ {
+ ++thrd->current.target_row;
+ }
+ sem_post(&(thrd->semp));
+ /* critical part */
+ return temp;
+
+}
+
+
+
+
+#endif