pthreadex.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. voltlogger_oscilloscope
  3. Copyright (C) 2015 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  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 General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include <string.h>
  16. #include <pthread.h>
  17. #include "pthreadex.h"
  18. #include "malloc.h"
  19. int pthread_mutex_init_shared(pthread_mutex_t **mutex_p) {
  20. static pthread_mutex_t mutex_initial = PTHREAD_MUTEX_INITIALIZER;
  21. *mutex_p = shm_malloc_try(sizeof(**mutex_p));
  22. memcpy(*mutex_p, &mutex_initial, sizeof(mutex_initial));
  23. pthread_mutexattr_t attr;
  24. pthread_mutexattr_init(&attr);
  25. pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
  26. return pthread_mutex_init(*mutex_p, &attr);
  27. }
  28. int pthread_mutex_destroy_shared(pthread_mutex_t *mutex_p) {
  29. int rc;
  30. rc = pthread_mutex_destroy(mutex_p);
  31. shm_free(mutex_p);
  32. return rc;
  33. }
  34. int pthread_cond_init_shared(pthread_cond_t **cond_p) {
  35. static pthread_cond_t cond_initial = PTHREAD_COND_INITIALIZER;
  36. *cond_p = shm_malloc(sizeof(**cond_p));
  37. memcpy(*cond_p, &cond_initial, sizeof(cond_initial));
  38. pthread_condattr_t attr;
  39. pthread_condattr_init(&attr);
  40. pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
  41. return pthread_cond_init(*cond_p, &attr);
  42. }
  43. int pthread_cond_destroy_shared(pthread_cond_t *cond_p) {
  44. int rc;
  45. rc = pthread_cond_destroy(cond_p);
  46. shm_free(cond_p);
  47. return rc;
  48. }
  49. int pthread_mutex_reltimedlock(pthread_mutex_t *mutex_p, long tv_sec, long tv_nsec) {
  50. struct timespec abs_time;
  51. if (clock_gettime(CLOCK_REALTIME, &abs_time))
  52. return -1;
  53. abs_time.tv_sec += tv_sec;
  54. abs_time.tv_nsec += tv_nsec;
  55. if (abs_time.tv_nsec > 1000*1000*1000) {
  56. abs_time.tv_sec++;
  57. abs_time.tv_nsec -= 1000*1000*1000;
  58. }
  59. return pthread_mutex_timedlock(mutex_p, &abs_time);
  60. }