director_util.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Copyright 2005 - 2016 Zarafa and its licensors
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License, version 3,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. #include <kopano/platform.h>
  18. #include <pthread.h>
  19. static pthread_key_t g_key;
  20. static pthread_once_t g_key_once = PTHREAD_ONCE_INIT;
  21. struct thread_info {
  22. bool bCalledFromPython;
  23. };
  24. static void make_key() {
  25. pthread_key_create(&g_key, NULL); // We need cleanup here
  26. }
  27. static thread_info *get_thread_info() {
  28. thread_info *pti = NULL;
  29. pthread_once(&g_key_once, make_key);
  30. if ((pti = (thread_info *)pthread_getspecific(g_key)) == NULL) {
  31. pti = new thread_info;
  32. pthread_setspecific(g_key, (void *)pti);
  33. }
  34. return pti;
  35. }
  36. void mark_call_from_python() {
  37. thread_info *pti = get_thread_info();
  38. pti->bCalledFromPython = true;
  39. }
  40. void unmark_call_from_python() {
  41. thread_info *pti = get_thread_info();
  42. pti->bCalledFromPython = false;
  43. }
  44. bool check_call_from_python() {
  45. thread_info *pti = NULL;
  46. pthread_once(&g_key_once, make_key);
  47. pti = (thread_info *)pthread_getspecific(g_key);
  48. return pti && pti->bCalledFromPython;
  49. }