commit 7747f400627024c08922761106a945ca448f3e18
parent d1bbf57555b5b43252252e584dd3db3ab792658c
Author: vaplv <vaplv@free.fr>
Date: Wed, 18 Sep 2013 11:46:21 +0200
Add a the `condition' test
Diffstat:
2 files changed, 162 insertions(+), 8 deletions(-)
diff --git a/src/test_condition.c b/src/test_condition.c
@@ -1,9 +1,156 @@
#include "condition.h"
+#include "list.h"
#include <omp.h>
+#include <string.h>
+
+static const char* src_str[] = {
+"Rcvfbqr 1, XARR-QRRC VA GUR QRNQ:\n\
+---------------------------------\n\
+\n\
+BAPR LBH ORNG GUR OVT ONQNFFRF NAQ PYRNA BHG GUR ZBBA ONFR LBH'ER FHCCBFRQ GB\n\
+JVA, NERA'G LBH? NERA'G LBH? JURER'F LBHE SNG ERJNEQ NAQ GVPXRG UBZR? JUNG\n\
+GUR URYY VF GUVF? VG'F ABG FHCCBFRQ GB RAQ GUVF JNL!\n\
+\n\
+VG FGVAXF YVXR EBGGRA ZRNG, OHG YBBXF YVXR GUR YBFG QRVZBF ONFR. YBBXF YVXR\n\
+LBH'ER FGHPX BA GUR FUBERF BS URYY. GUR BAYL JNL BHG VF GUEBHTU.\n\
+\n\
+GB PBAGVAHR GUR QBBZ RKCREVRAPR, CYNL GUR FUBERF BS URYY NAQ VGF NZNMVAT\n\
+FRDHRY, VASREAB!",
+
+"Rcvfbqr 2, GUR FUBERF BS URYY:\n\
+------------------------------\n\
+\n\
+LBH'IR QBAR VG! GUR UVQRBHF PLORE- QRZBA YBEQ GUNG EHYRQ GUR YBFG QRVZBF ZBBA\n\
+ONFR UNF ORRA FYNVA NAQ LBH NER GEVHZCUNAG! OHG ... JURER NER LBH? LBH\n\
+PYNZORE GB GUR RQTR BS GUR ZBBA NAQ YBBX QBJA GB FRR GUR NJSHY GEHGU.\n\
+\n\
+QRVZBF SYBNGF NOBIR URYY VGFRYS! LBH'IR ARIRE URNEQ BS NALBAR RFPNCVAT SEBZ\n\
+URYY, OHG LBH'YY ZNXR GUR ONFGNEQF FBEEL GURL RIRE URNEQ BS LBH! DHVPXYL, LBH\n\
+ENCCRY QBJA GB GUR FHESNPR BS URYY.\n\
+\n\
+ABJ, VG'F BA GB GUR SVANY PUNCGRE BS QBBZ! -- VASREAB.",
+
+"Rcvfbqr 3, VASREAB:\n\
+-------------------\n\
+\n\
+GUR YBNGUFBZR FCVQREQRZBA GUNG ZNFGREZVAQRQ GUR VAINFVBA BS GUR ZBBA ONFRF\n\
+NAQ PNHFRQ FB ZHPU QRNGU UNF UNQ VGF NFF XVPXRQ SBE NYY GVZR.\n\
+\n\
+N UVQQRA QBBEJNL BCRAF NAQ LBH RAGRE. LBH'IR CEBIRA GBB GBHTU SBE URYY GB\n\
+PBAGNVA, NAQ ABJ URYY NG YNFG CYNLF SNVE -- SBE LBH RZRETR SEBZ GUR QBBE GB\n\
+FRR GUR TERRA SVRYQF BS RNEGU! UBZR NG YNFG.\n\
+\n\
+LBH JBAQRE JUNG'F ORRA UNCCRAVAT BA RNEGU JUVYR LBH JRER ONGGYVAT RIVY\n\
+HAYRNFURQ. VG'F TBBQ GUNG AB URYY- FCNJA PBHYQ UNIR PBZR GUEBHTU GUNG QBBE\n\
+JVGU LBH ...",
+
+"Rcvfbqr 4, GUL SYRFU PBAFHZRQ:\n\
+------------------------------\n\
+\n\
+GUR FCVQRE ZNFGREZVAQ ZHFG UNIR FRAG SBEGU VGF YRTVBAF BS URYYFCNJA ORSBER\n\
+LBHE SVANY PBASEBAGNGVBA JVGU GUNG GREEVOYR ORNFG SEBZ URYY. OHG LBH FGRCCRQ\n\
+SBEJNEQ NAQ OEBHTUG SBEGU RGREANY QNZANGVBA NAQ FHSSREVAT HCBA GUR UBEQR NF N\n\
+GEHR UREB JBHYQ VA GUR SNPR BS FBZRGUVAT FB RIVY.\n\
+\n\
+ORFVQRF, FBZRBAR JNF TBAAN CNL SBE JUNG UNCCRARQ GB QNVFL, LBHE CRG ENOOVG.\n\
+\n\
+OHG ABJ, LBH FRR FCERNQ ORSBER LBH ZBER CBGRAGVNY CNVA NAQ TVOOVGHQR NF N\n\
+ANGVBA BS QRZBAF EHA NZBX VA BHE PVGVRF.\n\
+\n\
+ARKG FGBC, URYY BA RNEGU!"
+};
+
+struct stream
+{
+ struct list_node list_fill;
+ struct list_node list_flush;
+ struct mutex mutex;
+ struct cond cond_fill;
+ struct cond cond_flush;
+};
+
+struct buff
+{
+ struct list_node node;
+ char scratch[1024];
+};
+
+static void
+read(struct stream* stream)
+{
+ ASSERT(stream);
+
+ for(size_t i = 0; i < sizeof(src_str)/sizeof(const char*); ++i) {
+ mutex_lock(&stream->mutex);
+ if(is_list_empty(&stream->list_flush)) {
+ cond_wait(&stream->cond_flush, &stream->mutex);
+ }
+ mutex_unlock(&stream->mutex);
+
+ struct list_node* buff_node = list_head(&stream->list_flush);
+ struct buff* buff = CONTAINER_OF(buff_node, struct buff, node);
+ CHECK(strcmp(buff->scratch, src_str[i]), 0);
+ printf("\n%s\n", buff->scratch);
+
+ mutex_lock(&stream->mutex);
+ list_move_tail(buff_node, &stream->list_fill);
+ mutex_unlock(&stream->mutex);
+
+ cond_broadcast(&stream->cond_fill);
+ }
+}
+
+static void
+write(struct stream* stream)
+{
+ ASSERT(stream);
+
+ for(size_t i = 0; i < sizeof(src_str)/sizeof(const char*); ++i) {
+ mutex_lock(&stream->mutex);
+ if(is_list_empty(&stream->list_fill)) {
+ cond_wait(&stream->cond_fill, &stream->mutex);
+ }
+ mutex_unlock(&stream->mutex);
+
+ struct list_node* buff_node = list_head(&stream->list_fill);
+ struct buff* buff = CONTAINER_OF(buff_node, struct buff, node);
+
+ ASSERT(sizeof(buff->scratch)/sizeof(char) > strlen(src_str[i]));
+ strcpy(buff->scratch, src_str[i]);
+
+ mutex_lock(&stream->mutex);
+ list_move_tail(buff_node, &stream->list_flush);
+ mutex_unlock(&stream->mutex);
+
+ cond_broadcast(&stream->cond_flush);
+ }
+}
int
main(int argc, char** argv)
{
(void)argc, (void)argv;
+
+ struct stream stream;
+ list_init(&stream.list_fill);
+ list_init(&stream.list_flush);
+ mutex_init(&stream.mutex);
+ cond_init(&stream.cond_flush);
+ cond_init(&stream.cond_fill);
+
+ struct buff buff[2];
+ list_init(&buff[0].node);
+ list_init(&buff[1].node);
+ list_add(&stream.list_fill, &buff[0].node);
+ list_add(&stream.list_fill, &buff[1].node);
+
+ #pragma omp parallel sections shared(stream) num_threads(2)
+ {
+ #pragma omp section
+ read(&stream);
+ #pragma omp section
+ write(&stream);
+ }
return 0;
}
+
diff --git a/src/test_mutex.c b/src/test_mutex.c
@@ -164,17 +164,24 @@ test_mutex(const enum mutex_type type)
time_T time_start, time_end, time_res;
time_current(&time_start);
- #pragma omp parallel sections
+
+ #pragma omp parallel
{
- #pragma omp section
- if(type == MUTEX_RW) {
- string_read(&string);
+ #pragma omp single nowait
+ {
+ if(type == MUTEX_RW) {
+ #pragma omp task shared(string)
+ string_read(&string);
+ }
}
- #pragma omp section
- string_write(&string, type);
- #pragma omp section
- string_write(&string, type);
+ #pragma omp sections
+ {
+ #pragma omp section
+ string_write(&string, type);
+ #pragma omp section
+ string_write(&string, type);
+ }
}
time_current(&time_end);
time_sub(&time_res, &time_end, &time_start);