get_thread_id.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 "se_thread.h"
  32. #include <stdlib.h>
  33. static pthread_key_t g_tid_key;
  34. static pthread_once_t g_key_once = PTHREAD_ONCE_INIT;
  35. static void create_key()
  36. {
  37. pthread_key_create(&g_tid_key, NULL);
  38. }
  39. se_thread_id_t get_thread_id()
  40. {
  41. if(pthread_once(&g_key_once, create_key) != 0)
  42. abort();
  43. // If the key is invalid, pthread_getspecific will return NULL.
  44. // No need to check the pthread_key_create and pthread_setspecific return value.
  45. se_thread_id_t tid = (se_thread_id_t)(size_t)pthread_getspecific(g_tid_key);
  46. if( tid == 0)
  47. {
  48. tid = se_get_threadid();
  49. // pthread_setspecific() shall fail if insufficient memory exists to associate the value
  50. // with the key or the input key value is invalid.
  51. // Here we don't check the return value. Then if pthread_setspecific fails in one ecall,
  52. // the following ecall would call the syscall gettid to retrive the tid in one thread
  53. // instead of returning SGX_ERROR_OUT_OF_TCS.
  54. pthread_setspecific(g_tid_key, (void*)(size_t)tid);
  55. }
  56. return tid;
  57. }