sgx_thread.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #ifndef _SGX_THREAD_H_
  32. #define _SGX_THREAD_H_
  33. #include <stdint.h>
  34. #include <stddef.h>
  35. #include "sgx_defs.h"
  36. typedef uintptr_t sgx_thread_t;
  37. typedef struct sgx_thread_queue
  38. {
  39. sgx_thread_t m_first; /* first element */
  40. sgx_thread_t m_last; /* last element */
  41. } sgx_thread_queue_t;
  42. /* Mutex */
  43. typedef struct sgx_thread_mutex
  44. {
  45. size_t m_refcount;
  46. uint32_t m_control;
  47. volatile uint32_t m_lock; /* use sgx_spinlock_t */
  48. sgx_thread_t m_owner;
  49. sgx_thread_queue_t m_queue;
  50. } sgx_thread_mutex_t;
  51. #define SGX_THREAD_T_NULL ((sgx_thread_t)(NULL))
  52. #define SGX_THREAD_MUTEX_NONRECURSIVE 0x01
  53. #define SGX_THREAD_MUTEX_RECURSIVE 0x02
  54. #define SGX_THREAD_NONRECURSIVE_MUTEX_INITIALIZER \
  55. {0, SGX_THREAD_MUTEX_NONRECURSIVE, 0, SGX_THREAD_T_NULL, {SGX_THREAD_T_NULL, SGX_THREAD_T_NULL}}
  56. #define SGX_THREAD_RECURSIVE_MUTEX_INITIALIZER \
  57. {0, SGX_THREAD_MUTEX_RECURSIVE, 0, SGX_THREAD_T_NULL, {SGX_THREAD_T_NULL, SGX_THREAD_T_NULL}}
  58. #define SGX_THREAD_MUTEX_INITIALIZER \
  59. SGX_THREAD_NONRECURSIVE_MUTEX_INITIALIZER
  60. typedef struct sgx_thread_mutex_attr
  61. {
  62. unsigned char m_dummy; /* for C syntax check */
  63. } sgx_thread_mutexattr_t;
  64. /* Condition Variable */
  65. typedef struct sgx_thread_cond
  66. {
  67. volatile uint32_t m_lock; /* use sgx_spinlock_t */
  68. sgx_thread_queue_t m_queue;
  69. } sgx_thread_cond_t;
  70. #define SGX_THREAD_COND_INITIALIZER {0, {SGX_THREAD_T_NULL, SGX_THREAD_T_NULL}}
  71. typedef struct sgx_thread_cond_attr
  72. {
  73. unsigned char m_dummy; /* for C syntax check */
  74. } sgx_thread_condattr_t;
  75. #ifdef __cplusplus
  76. extern "C" {
  77. #endif
  78. /* Mutex */
  79. int SGXAPI sgx_thread_mutex_init(sgx_thread_mutex_t *mutex, const sgx_thread_mutexattr_t *unused);
  80. int SGXAPI sgx_thread_mutex_destroy(sgx_thread_mutex_t *mutex);
  81. int SGXAPI sgx_thread_mutex_lock(sgx_thread_mutex_t *mutex);
  82. int SGXAPI sgx_thread_mutex_trylock(sgx_thread_mutex_t *mutex);
  83. int SGXAPI sgx_thread_mutex_unlock(sgx_thread_mutex_t *mutex);
  84. /* Condition Variable */
  85. int SGXAPI sgx_thread_cond_init(sgx_thread_cond_t *cond, const sgx_thread_condattr_t *unused);
  86. int SGXAPI sgx_thread_cond_destroy(sgx_thread_cond_t *cond);
  87. int SGXAPI sgx_thread_cond_wait(sgx_thread_cond_t *cond, sgx_thread_mutex_t *mutex);
  88. int SGXAPI sgx_thread_cond_signal(sgx_thread_cond_t *cond);
  89. int SGXAPI sgx_thread_cond_broadcast(sgx_thread_cond_t *cond);
  90. sgx_thread_t SGXAPI sgx_thread_self(void);
  91. int sgx_thread_equal(sgx_thread_t a, sgx_thread_t b);
  92. #ifdef __cplusplus
  93. }
  94. #endif
  95. #endif /* _SGX_THREAD_H_ */