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 
26 /* simple platform thread abstraction
27  */
28 
29 #include <config.h>
30 #include "event.h"
31 
32 #if defined(_WIN32)
33 
34 # include <windows.h>
35 # define HAVE_THREADS
36 # define ZTHREAD DWORD WINAPI
37 
38 typedef ZTHREAD (zbar_thread_proc_t)(void*);
39 
40 typedef DWORD zbar_thread_id_t;
41 
42 #elif defined(HAVE_LIBPTHREAD)
43 
44 # include <pthread.h>
45 # include <signal.h>
46 # define HAVE_THREADS
47 # define ZTHREAD void*
48 
49 typedef ZTHREAD (zbar_thread_proc_t)(void*);
50 
51 typedef pthread_t zbar_thread_id_t;
52 
53 #else
54 
55 # undef HAVE_THREADS
56 # undef ZTHREAD
57 
58 typedef void zbar_thread_proc_t;
59 typedef int zbar_thread_id_t;
60 
61 #endif
62 
63 
64 typedef struct zbar_thread_s {
65     zbar_thread_id_t tid;
66     int started, running;
67     zbar_event_t notify, activity;
68 } zbar_thread_t;
69 
70 
71 #if defined(_WIN32)
72 
_zbar_thread_init(zbar_thread_t * thr)73 static inline void _zbar_thread_init (zbar_thread_t *thr)
74 {
75     thr->running = 1;
76     _zbar_event_trigger(&thr->activity);
77 }
78 
_zbar_thread_self()79 static inline zbar_thread_id_t _zbar_thread_self ()
80 {
81     return(GetCurrentThreadId());
82 }
83 
_zbar_thread_is_self(zbar_thread_id_t tid)84 static inline int _zbar_thread_is_self (zbar_thread_id_t tid)
85 {
86     return(tid == GetCurrentThreadId());
87 }
88 
89 
90 #elif defined(HAVE_LIBPTHREAD)
91 
_zbar_thread_init(zbar_thread_t * thr)92 static inline void _zbar_thread_init (zbar_thread_t *thr)
93 {
94     sigset_t sigs;
95     sigfillset(&sigs);
96     pthread_sigmask(SIG_BLOCK, &sigs, NULL);
97     thr->running = 1;
98     _zbar_event_trigger(&thr->activity);
99 }
100 
_zbar_thread_self(void)101 static inline zbar_thread_id_t _zbar_thread_self (void)
102 {
103     return(pthread_self());
104 }
105 
_zbar_thread_is_self(zbar_thread_id_t tid)106 static inline int _zbar_thread_is_self (zbar_thread_id_t tid)
107 {
108     return(pthread_equal(tid, pthread_self()));
109 }
110 
111 
112 #else
113 
114 # define _zbar_thread_start(...) -1
115 # define _zbar_thread_stop(...) 0
116 # define _zbar_thread_self(...) 0
117 # define _zbar_thread_is_self(...) 1
118 
119 #endif
120 
121 #ifdef HAVE_THREADS
122 extern int _zbar_thread_start(zbar_thread_t*, zbar_thread_proc_t*,
123                               void*, zbar_mutex_t*);
124 extern int _zbar_thread_stop(zbar_thread_t*, zbar_mutex_t*);
125 #endif
126 
127 #endif
128