Revert "Revert "and added files""
[bcm963xx.git] / userapps / opensource / libosip2 / src / osip2 / port_thread.c
1 /*
2   The oSIP library implements the Session Initiation Protocol (SIP -rfc3261-)
3   Copyright (C) 2001,2002,2003  Aymeric MOIZARD jack@atosc.org
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10   This library is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   Lesser General Public License for more details.
14   
15   You should have received a copy of the GNU Lesser General Public
16   License along with this library; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19
20 #ifdef OSIP_MT
21
22 #include <stdio.h>
23
24 #include <osip2/internal.h>
25 #include <osip2/osip_mt.h>
26
27 #if defined _WIN32_WCE
28 #include <winbase.h>
29 #define _beginthreadex  CreateThread
30 #define _endthreadex    ExitThread
31 #elif defined WIN32
32 #include <process.h>
33 #endif
34
35 /* stack size is only needed on VxWorks. */
36
37 #ifndef __VXWORKS_OS__
38 #if defined(HAVE_PTHREAD) || defined(HAVE_PTH_PTHREAD_H)
39 struct osip_thread *
40 osip_thread_create (int stacksize, void *(*func) (void *), void *arg)
41 {
42   int i;
43   osip_thread_t *thread =
44     (osip_thread_t *) osip_malloc (sizeof (osip_thread_t));
45   if (thread == NULL)
46     return NULL;
47
48   i = pthread_create (thread, NULL, func, (void *) arg);
49   if (i != 0)
50     {
51       OSIP_TRACE (osip_trace
52                   (__FILE__, __LINE__, OSIP_ERROR, NULL,
53                    "Error while creating a new thread\n"));
54       return NULL;
55     }
56   return (struct osip_thread *) thread;
57 }
58
59 int
60 osip_thread_set_priority (struct osip_thread *thread, int priority)
61 {
62   return 0;
63 }
64
65 int
66 osip_thread_join (struct osip_thread *_thread)
67 {
68   osip_thread_t *thread = (osip_thread_t *) _thread;
69   if (thread == NULL)
70     return -1;
71   return pthread_join (*thread, NULL);
72 }
73
74 void
75 osip_thread_exit ()
76 {
77   pthread_exit (NULL);
78 }
79
80 #endif
81 #endif
82
83 #ifdef WIN32
84 struct osip_thread *
85 osip_thread_create (int stacksize, void *(*func) (void *), void *arg)
86 {
87   osip_thread_t *thread =
88     (osip_thread_t *) osip_malloc (sizeof (osip_thread_t));
89   if (thread == NULL)
90     return NULL;
91   thread->h = (HANDLE) _beginthreadex (NULL,    /* default security attr */
92                                        0,       /* use default one */
93                                        (void *) func, arg, 0, &(thread->id));
94   if (thread->h == 0)
95     {
96       osip_free (thread);
97       return NULL;
98     }
99   return (struct osip_thread *) thread;
100 }
101
102 int
103 osip_thread_join (struct osip_thread *_thread)
104 {
105   int i;
106   osip_thread_t *thread = (osip_thread_t *) _thread;
107
108   if (thread == NULL)
109     return -1;
110   i = WaitForSingleObject (thread->h, INFINITE);
111   if (i == WAIT_OBJECT_0)
112     {
113       /* fprintf (stdout, "thread joined!\n"); */
114     }
115   else
116     {
117       /* fprintf (stdout, "ERROR!! thread joined ERROR!!\n"); */
118       return -1;
119     }
120   CloseHandle (thread->h);
121   return (0);
122 }
123
124 void
125 osip_thread_exit ()
126 {
127   /* ExitThread(0); */
128   _endthreadex (0);
129 }
130
131 int
132 osip_thread_set_priority (struct osip_thread *thread, int priority)
133 {
134   return 0;
135 }
136
137
138 #endif
139
140 #ifndef __VXWORKS_OS__
141 #ifdef __PSOS__
142 struct osip_thread *
143 osip_thread_create (int stacksize, void *(*func) (void *), void *arg)
144 {
145   osip_thread_t *thread =
146     (osip_thread_t *) osip_malloc (sizeof (osip_thread_t));
147   if (thread == NULL)
148     return (NULL);
149   if (t_create ("sip", 150, stacksize, 0, 0, &thread->tid) != 0)
150     {
151       osip_free (thread);
152       return (NULL);
153     }
154
155   if (t_start (thread->tid, T_PREEMPT | T_ISR, func, 0) != 0)
156     {
157       osip_free (thread);
158       return (NULL);
159     }
160
161   return (struct osip_thread *) thread;
162 }
163
164 int
165 osip_thread_set_priority (struct osip_thread *_thread, int priority)
166 {
167   unsigned long oldprio;
168   osip_thread_t *thread = (osip_thread_t *) _thread;
169
170   if (thread == NULL)
171     return -1;
172   t_set_pri (thread->tid, priority, &oldprio);
173   return 0;
174 }
175
176 int
177 osip_thread_join (struct osip_thread *_thread)
178 {
179   osip_thread_t *thread = (osip_thread_t *) _thread;
180   if (thread == NULL)
181     return -1;
182   t_delete (thread->tid);
183
184   return (0);
185 }
186
187 void
188 osip_thread_exit ()
189 {
190   t_delete (0);
191 }
192 #endif
193 #endif
194
195 #ifdef __VXWORKS_OS__
196 struct osip_thread *
197 osip_thread_create (int stacksize, void *(*func) (void *), void *arg)
198 {
199   osip_thread_t *thread =
200     (osip_thread_t *) osip_malloc (sizeof (osip_thread_t));
201   if (thread == NULL)
202     return NULL;
203   thread->id = taskSpawn (NULL, 5, 0, stacksize, (FUNCPTR) func, (int) arg,
204                           0, 0, 0, 0, 0, 0, 0, 0, 0);
205   if (thread->id < 0)
206     osip_free (thread);
207   return (struct osip_thread *) thread;
208 }
209
210 int
211 osip_thread_set_priority (struct osip_thread *_thread, int priority)
212 {
213   osip_thread_t *thread = (osip_thread_t *) _thread;
214   if (thread == NULL)
215     return -1;
216   taskPrioritySet (thread->id, 1);
217   return 0;
218 }
219
220 int
221 osip_thread_join (struct osip_thread *_thread)
222 {
223   osip_thread_t *thread = (osip_thread_t *) _thread;
224   if (thread == NULL)
225     return -1;
226   return taskDelete (thread->id);
227 }
228
229 void
230 osip_thread_exit ()
231 {
232   /*?? */
233 }
234 #endif
235
236 #endif