#include <stdio.h>
#include <comm.cvh>

void testStream($gcomm gc){
  $comm comm = $comm_create($here, gc, 1);
  $message message = $comm_dequeue(comm, 0, $COMM_ANY_TAG);
  printf("New kernel message received\n");
  void (*kernel)();
  $message_unpack(message, (void*)&kernel, sizeof(kernel));
  kernel();
  $comm_destroy(comm);
}

void print(int num){
  printf("Inside kernel function with number %d!\n", num);
}

void main(){
  $gcomm gcomm = $gcomm_create($here, 2);
  $comm comm = $comm_create($here, gcomm, 0);
  $proc otherProc = $spawn testStream(gcomm);
  int x;
  void capturedPrint() {
  	print(x);
  }
  void (*printFunc)() = &capturedPrint;
  $message message = $message_pack(0, 1, 0, &printFunc, sizeof(printFunc));
  $comm_enqueue(comm, message);
  x = 25;
  $wait(otherProc);
  $comm_destroy(comm);
  $gcomm_destroy(gcomm, NULL);
}
