testapp.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright (C) 2023 Midokura Japan KK. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include <assert.h>
  6. #include <pthread.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <stdint.h>
  10. void
  11. set_context(int32_t n) __attribute__((import_module("env")))
  12. __attribute__((import_name("set_context")));
  13. int32_t
  14. get_context() __attribute__((import_module("env")))
  15. __attribute__((import_name("get_context")));
  16. void *
  17. start(void *vp)
  18. {
  19. int32_t v;
  20. printf("thread started\n");
  21. printf("confirming the initial state on thread\n");
  22. v = get_context();
  23. assert(v == -1);
  24. printf("setting the context on thread\n");
  25. set_context(1234);
  26. printf("confirming the context on thread\n");
  27. v = get_context();
  28. assert(v == 1234);
  29. return NULL;
  30. }
  31. int
  32. main()
  33. {
  34. pthread_t t1;
  35. int32_t v;
  36. int ret;
  37. printf("confirming the initial state on main\n");
  38. v = get_context();
  39. assert(v == -1);
  40. printf("creating a thread\n");
  41. ret = pthread_create(&t1, NULL, start, NULL);
  42. assert(ret == 0);
  43. void *val;
  44. ret = pthread_join(t1, &val);
  45. assert(ret == 0);
  46. printf("joined the thread\n");
  47. printf("confirming the context propagated from the thread on main\n");
  48. v = get_context();
  49. assert(v == 1234);
  50. printf("success\n");
  51. return 0;
  52. }