Adding example code in converse tutorial for threads
authorNikhil Jain <nikhil@illinois.edu>
Wed, 29 Feb 2012 20:54:48 +0000 (14:54 -0600)
committerNikhil Jain <nikhil@illinois.edu>
Wed, 29 Feb 2012 20:54:48 +0000 (14:54 -0600)
doc/converse/code/cth-tutorial/Makefile [new file with mode: 0644]
doc/converse/code/cth-tutorial/pgm.C [new file with mode: 0644]

diff --git a/doc/converse/code/cth-tutorial/Makefile b/doc/converse/code/cth-tutorial/Makefile
new file mode 100644 (file)
index 0000000..2a95a40
--- /dev/null
@@ -0,0 +1,21 @@
+CHARMC?=../../../../bin/charmc $(OPTS)
+
+LINKLINE=$(CHARMC) -o pgm pgm.o -language converse++
+
+all: pgm
+
+pgm: pgm.o
+       $(LINKLINE)
+
+pgm.o: pgm.C
+       $(CHARMC) -c pgm.C
+
+test: pgm
+       ./charmrun ./pgm +p1 $(TESTOPTS)
+#      -$(LINKLINE) -thread context && ./charmrun ./pgm +p1  $(TESTOPTS)&& ps -u `whoami`
+#      -$(LINKLINE) -thread pthreads -lpthread && ./charmrun ./pgm +p1  $(TESTOPTS)&& ps -u `whoami`
+#      -$(LINKLINE) -thread qt && ./charmrun ./pgm +p1  $(TESTOPTS)&& ps -u `whoami`
+#      -$(LINKLINE) -thread uJcontext && ./charmrun ./pgm +p1  $(TESTOPTS)&& ps -u `whoami`
+
+clean:
+       rm -f conv-host *.o pgm *.bak pgm.*.log pgm.sts *~ charmrun charmrun.exe pgm.exe pgm.pdb pgm.ilk
diff --git a/doc/converse/code/cth-tutorial/pgm.C b/doc/converse/code/cth-tutorial/pgm.C
new file mode 100644 (file)
index 0000000..3c1074e
--- /dev/null
@@ -0,0 +1,58 @@
+#include <stdio.h>
+#include "converse.h"
+
+#define HIGH_PRIO 0
+#define LOW_PRIO 1
+#define NUM_YIELD 10
+
+int endCounter = 0;
+
+//determine completion based on threads calling it
+void threadDone() {
+  endCounter++;
+  if(endCounter == 2) {
+    CsdExitScheduler();
+  }
+}
+
+//worker function for worker1
+//yields with a low priority
+void worker1Work(void* msg) {
+  printf("start worker1\n");
+  CthYield();
+  printf("worker1 resumed first time\n");
+  unsigned int prio  = LOW_PRIO;
+  for(int i = 0; i < NUM_YIELD; i++) {
+    CthYieldPrio(CQS_QUEUEING_IFIFO,0,&prio);
+    printf("worker1 resumed %dth time\n",i);
+  }
+  threadDone();
+}
+
+//worker function for worker2
+//yields with a high priority
+void worker2Work(void* msg) {
+  printf("start worker2\n");
+  CthYield();
+  printf("worker2 resumed first time\n");
+  unsigned int prio  = HIGH_PRIO;
+  for(int i = 0; i < NUM_YIELD; i++) {
+    CthYieldPrio(CQS_QUEUEING_IFIFO,0,&prio);
+    printf("worker2 resumed %dth time\n",i);
+  }
+  threadDone();
+}
+
+//create two worker threads and push them on scheduler Q
+void initThreads(int argc, char* argv[]) {
+  printf("called initThreads\n");
+  CthThread worker1 = CthCreateMigratable((CthVoidFn)worker1Work, 0, 160000);
+  CthThread worker2 = CthCreateMigratable((CthVoidFn)worker2Work, 0, 160000);
+  CthAwaken(worker1);
+  CthAwaken(worker2);
+}
+
+int main(int argc, char* argv[]) {
+  //initialize converse and calls initThreads
+  ConverseInit(argc, argv, initThreads, 0, 0);
+}