thread.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*------------------------------------------------------------------------
  2. * Copyright 2007-2009 (c) Jeff Brown <spadix@users.sourceforge.net>
  3. *
  4. * This file is part of the ZBar Bar Code Reader.
  5. *
  6. * The ZBar Bar Code Reader is free software; you can redistribute it
  7. * and/or modify it under the terms of the GNU Lesser Public License as
  8. * published by the Free Software Foundation; either version 2.1 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * The ZBar Bar Code Reader is distributed in the hope that it will be
  12. * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  13. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser Public License
  17. * along with the ZBar Bar Code Reader; if not, write to the Free
  18. * Software Foundation, Inc., 51 Franklin St, Fifth Floor,
  19. * Boston, MA 02110-1301 USA
  20. *
  21. * http://sourceforge.net/projects/zbar
  22. *------------------------------------------------------------------------*/
  23. #ifndef _ZBAR_THREAD_H_
  24. #define _ZBAR_THREAD_H_
  25. /* simple platform thread abstraction
  26. */
  27. #include <config.h>
  28. #include "event.h"
  29. #if defined(_WIN32)
  30. # include <windows.h>
  31. # define HAVE_THREADS
  32. # define ZTHREAD DWORD WINAPI
  33. typedef ZTHREAD (zbar_thread_proc_t)(void*);
  34. typedef DWORD zbar_thread_id_t;
  35. #elif defined(HAVE_LIBPTHREAD)
  36. # include <pthread.h>
  37. # include <signal.h>
  38. # define HAVE_THREADS
  39. # define ZTHREAD void*
  40. typedef ZTHREAD (zbar_thread_proc_t)(void*);
  41. typedef pthread_t zbar_thread_id_t;
  42. #else
  43. # undef HAVE_THREADS
  44. # undef ZTHREAD
  45. typedef void zbar_thread_proc_t;
  46. typedef int zbar_thread_id_t;
  47. #endif
  48. typedef struct zbar_thread_s {
  49. zbar_thread_id_t tid;
  50. int started, running;
  51. zbar_event_t notify, activity;
  52. } zbar_thread_t;
  53. #if defined(_WIN32)
  54. static inline void _zbar_thread_init (zbar_thread_t *thr)
  55. {
  56. thr->running = 1;
  57. _zbar_event_trigger(&thr->activity);
  58. }
  59. static inline zbar_thread_id_t _zbar_thread_self ()
  60. {
  61. return(GetCurrentThreadId());
  62. }
  63. static inline int _zbar_thread_is_self (zbar_thread_id_t tid)
  64. {
  65. return(tid == GetCurrentThreadId());
  66. }
  67. #elif defined(HAVE_LIBPTHREAD)
  68. static inline void _zbar_thread_init (zbar_thread_t *thr)
  69. {
  70. sigset_t sigs;
  71. sigfillset(&sigs);
  72. pthread_sigmask(SIG_BLOCK, &sigs, NULL);
  73. thr->running = 1;
  74. _zbar_event_trigger(&thr->activity);
  75. }
  76. static inline zbar_thread_id_t _zbar_thread_self (void)
  77. {
  78. return(pthread_self());
  79. }
  80. static inline int _zbar_thread_is_self (zbar_thread_id_t tid)
  81. {
  82. return(pthread_equal(tid, pthread_self()));
  83. }
  84. #else
  85. # define _zbar_thread_start(...) -1
  86. # define _zbar_thread_stop(...) 0
  87. # define _zbar_thread_self(...) 0
  88. # define _zbar_thread_is_self(...) 1
  89. #endif
  90. #ifdef HAVE_THREADS
  91. extern int _zbar_thread_start(zbar_thread_t*, zbar_thread_proc_t*,
  92. void*, zbar_mutex_t*);
  93. extern int _zbar_thread_stop(zbar_thread_t*, zbar_mutex_t*);
  94. #endif
  95. #endif