1 /***************************************************************************
2     begin       : Wed Feb 03 2021
3     copyright   : (C) 2021 by Martin Preuss
4     email       : martin@libchipcard.de
5 
6  ***************************************************************************
7  *                                                                         *
8  *   This library is free software; you can redistribute it and/or         *
9  *   modify it under the terms of the GNU Lesser General Public            *
10  *   License as published by the Free Software Foundation; either          *
11  *   version 2.1 of the License, or (at your option) any later version.    *
12  *                                                                         *
13  *   This library is distributed in the hope that it will be useful,       *
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
16  *   Lesser General Public License for more details.                       *
17  *                                                                         *
18  *   You should have received a copy of the GNU Lesser General Public      *
19  *   License along with this library; if not, write to the Free Software   *
20  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston,                 *
21  *   MA  02111-1307  USA                                                   *
22  *                                                                         *
23  ***************************************************************************/
24 
25 #ifdef HAVE_CONFIG_H
26 # include <config.h>
27 #endif
28 
29 #define DISABLE_DEBUGLOG
30 
31 
32 #include "gwenthread_p.h"
33 
34 #include <gwenhywfar/misc.h>
35 #include <gwenhywfar/debug.h>
36 
37 #include <string.h>
38 #include <errno.h>
39 
40 
41 
42 GWEN_INHERIT_FUNCTIONS(GWEN_THREAD)
43 GWEN_LIST_FUNCTIONS(GWEN_THREAD, GWEN_Thread)
44 
45 
46 
47 static void *_threadRun_cb(void *vargp);
48 
49 
50 
51 
52 
GWEN_Thread_new()53 GWEN_THREAD *GWEN_Thread_new()
54 {
55   GWEN_THREAD *thr;
56 
57   GWEN_NEW_OBJECT(GWEN_THREAD, thr);
58   assert(thr);
59 
60   GWEN_INHERIT_INIT(GWEN_THREAD, thr);
61   GWEN_LIST_INIT(GWEN_THREAD, thr);
62 
63   return thr;
64 }
65 
66 
67 
GWEN_Thread_free(GWEN_THREAD * thr)68 void GWEN_Thread_free(GWEN_THREAD *thr)
69 {
70   if (thr) {
71     GWEN_LIST_FINI(GWEN_THREAD, thr);
72     GWEN_INHERIT_FINI(GWEN_THREAD, thr);
73     GWEN_FREE_OBJECT(thr);
74   }
75 }
76 
77 
78 
GWEN_Thread_Start(GWEN_THREAD * thr)79 int GWEN_Thread_Start(GWEN_THREAD *thr)
80 {
81   int rv;
82 
83   if (thr->runFn==NULL) {
84     DBG_ERROR(GWEN_LOGDOMAIN, "No run function set in thread");
85     return GWEN_ERROR_NOT_IMPLEMENTED;
86   }
87 
88   rv=pthread_create(&(thr->threadId), NULL, _threadRun_cb, thr);
89   if (rv!=0) {
90     DBG_ERROR(GWEN_LOGDOMAIN, "Error on pthread_create: %d (%s)", rv, strerror(rv));
91     return GWEN_ERROR_GENERIC;
92   }
93 
94   return 0;
95 }
96 
97 
98 
GWEN_Thread_Join(GWEN_THREAD * thr)99 int GWEN_Thread_Join(GWEN_THREAD *thr)
100 {
101   int rv;
102 
103   rv=pthread_join(thr->threadId, NULL);
104   if (rv!=0) {
105     DBG_ERROR(GWEN_LOGDOMAIN, "Error on pthread_join: %d (%s)", rv, strerror(rv));
106     return GWEN_ERROR_GENERIC;
107   }
108 
109   return 0;
110 }
111 
112 
113 
GWEN_Thread_SetRunFn(GWEN_THREAD * thr,GWEN_THREAD_RUN_FN fn)114 GWEN_THREAD_RUN_FN GWEN_Thread_SetRunFn(GWEN_THREAD *thr, GWEN_THREAD_RUN_FN fn)
115 {
116   GWEN_THREAD_RUN_FN oldFn;
117 
118   assert(thr);
119 
120   oldFn=thr->runFn;
121   thr->runFn=fn;
122 
123   return oldFn;
124 }
125 
126 
127 
_threadRun_cb(void * vargp)128 void *_threadRun_cb(void *vargp)
129 {
130   GWEN_THREAD *thr;
131 
132   thr=(GWEN_THREAD*) vargp;
133   assert(thr);
134 
135   if (thr->runFn)
136     thr->runFn(thr);
137   else {
138     DBG_ERROR(GWEN_LOGDOMAIN, "No run function set in thread");
139   }
140 
141   return NULL;
142 }
143 
144 
145 
146 
147 
148 
149 
150 
151 
152 
153