Thread.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 "../Enclave.h"
  32. #include "Enclave_t.h"
  33. #include <sgx_thread.h>
  34. static size_t global_counter = 0;
  35. static sgx_thread_mutex_t global_mutex = SGX_THREAD_MUTEX_INITIALIZER;
  36. #define BUFFER_SIZE 50
  37. typedef struct {
  38. int buf[BUFFER_SIZE];
  39. int occupied;
  40. int nextin;
  41. int nextout;
  42. sgx_thread_mutex_t mutex;
  43. sgx_thread_cond_t more;
  44. sgx_thread_cond_t less;
  45. } cond_buffer_t;
  46. static cond_buffer_t buffer = {{0, 0, 0, 0, 0, 0}, 0, 0, 0,
  47. SGX_THREAD_MUTEX_INITIALIZER, SGX_THREAD_COND_INITIALIZER, SGX_THREAD_COND_INITIALIZER};
  48. /*
  49. * ecall_increase_counter:
  50. * Utilize thread APIs inside the enclave.
  51. */
  52. size_t ecall_increase_counter(void)
  53. {
  54. size_t ret = 0;
  55. for (int i = 0; i < LOOPS_PER_THREAD; i++) {
  56. sgx_thread_mutex_lock(&global_mutex);
  57. /* mutually exclusive adding */
  58. size_t tmp = global_counter;
  59. global_counter = ++tmp;
  60. if (4*LOOPS_PER_THREAD == global_counter)
  61. ret = global_counter;
  62. sgx_thread_mutex_unlock(&global_mutex);
  63. }
  64. return ret;
  65. }
  66. void ecall_producer(void)
  67. {
  68. for (int i = 0; i < 4*LOOPS_PER_THREAD; i++) {
  69. cond_buffer_t *b = &buffer;
  70. sgx_thread_mutex_lock(&b->mutex);
  71. while (b->occupied >= BUFFER_SIZE)
  72. sgx_thread_cond_wait(&b->less, &b->mutex);
  73. b->buf[b->nextin] = b->nextin;
  74. b->nextin++;
  75. b->nextin %= BUFFER_SIZE;
  76. b->occupied++;
  77. sgx_thread_cond_signal(&b->more);
  78. sgx_thread_mutex_unlock(&b->mutex);
  79. }
  80. }
  81. void ecall_consumer(void)
  82. {
  83. for (int i = 0; i < LOOPS_PER_THREAD; i++) {
  84. cond_buffer_t *b = &buffer;
  85. sgx_thread_mutex_lock(&b->mutex);
  86. while(b->occupied <= 0)
  87. sgx_thread_cond_wait(&b->more, &b->mutex);
  88. b->buf[b->nextout++] = 0;
  89. b->nextout %= BUFFER_SIZE;
  90. b->occupied--;
  91. sgx_thread_cond_signal(&b->less);
  92. sgx_thread_mutex_unlock(&b->mutex);
  93. }
  94. }