Thread.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (C) 2011-2017 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include <thread>
  32. #include <stdio.h>
  33. using namespace std;
  34. #include "../App.h"
  35. #include "Enclave_u.h"
  36. static size_t counter = 0;
  37. void increase_counter(void)
  38. {
  39. size_t cnr = 0;
  40. sgx_status_t ret = SGX_ERROR_UNEXPECTED;
  41. ret = ecall_increase_counter(global_eid, &cnr);
  42. if (cnr != 0) counter = cnr;
  43. if (ret != SGX_SUCCESS)
  44. abort();
  45. }
  46. void data_producer(void)
  47. {
  48. sgx_status_t ret = SGX_ERROR_UNEXPECTED;
  49. ret = ecall_producer(global_eid);
  50. if (ret != SGX_SUCCESS)
  51. abort();
  52. }
  53. void data_consumer(void)
  54. {
  55. sgx_status_t ret = SGX_ERROR_UNEXPECTED;
  56. ret = ecall_consumer(global_eid);
  57. if (ret != SGX_SUCCESS)
  58. abort();
  59. }
  60. /* ecall_thread_functions:
  61. * Invokes thread functions including mutex, condition variable, etc.
  62. */
  63. void ecall_thread_functions(void)
  64. {
  65. thread adder1(increase_counter);
  66. thread adder2(increase_counter);
  67. thread adder3(increase_counter);
  68. thread adder4(increase_counter);
  69. adder1.join();
  70. adder2.join();
  71. adder3.join();
  72. adder4.join();
  73. assert(counter == 4*LOOPS_PER_THREAD);
  74. printf("Info: executing thread synchronization, please wait... \n");
  75. /* condition variable */
  76. thread consumer1(data_consumer);
  77. thread producer0(data_producer);
  78. thread consumer2(data_consumer);
  79. thread consumer3(data_consumer);
  80. thread consumer4(data_consumer);
  81. consumer1.join();
  82. consumer2.join();
  83. consumer3.join();
  84. consumer4.join();
  85. producer0.join();
  86. }