xref: /relibc/pthreads-emb/pte_new.c (revision a12978ccca4c576f02971f5cfc2cbefe23e4daba)
1062c5bc4SJason Schmidlapp /*
2062c5bc4SJason Schmidlapp  * pte_new.c
3062c5bc4SJason Schmidlapp  *
4062c5bc4SJason Schmidlapp  * Description:
5062c5bc4SJason Schmidlapp  * This translation unit implements miscellaneous thread functions.
6062c5bc4SJason Schmidlapp  *
7062c5bc4SJason Schmidlapp  * --------------------------------------------------------------------------
8062c5bc4SJason Schmidlapp  *
9062c5bc4SJason Schmidlapp  *      Pthreads-embedded (PTE) - POSIX Threads Library for embedded systems
10062c5bc4SJason Schmidlapp  *      Copyright(C) 2008 Jason Schmidlapp
11062c5bc4SJason Schmidlapp  *
12062c5bc4SJason Schmidlapp  *      Contact Email: jschmidlapp@users.sourceforge.net
13062c5bc4SJason Schmidlapp  *
14062c5bc4SJason Schmidlapp  *
15062c5bc4SJason Schmidlapp  *      Based upon Pthreads-win32 - POSIX Threads Library for Win32
16062c5bc4SJason Schmidlapp  *      Copyright(C) 1998 John E. Bossom
17062c5bc4SJason Schmidlapp  *      Copyright(C) 1999,2005 Pthreads-win32 contributors
18062c5bc4SJason Schmidlapp  *
19062c5bc4SJason Schmidlapp  *      Contact Email: rpj@callisto.canberra.edu.au
20062c5bc4SJason Schmidlapp  *
21062c5bc4SJason Schmidlapp  *      The original list of contributors to the Pthreads-win32 project
22062c5bc4SJason Schmidlapp  *      is contained in the file CONTRIBUTORS.ptw32 included with the
23062c5bc4SJason Schmidlapp  *      source code distribution. The list can also be seen at the
24062c5bc4SJason Schmidlapp  *      following World Wide Web location:
25062c5bc4SJason Schmidlapp  *      http://sources.redhat.com/pthreads-win32/contributors.html
26062c5bc4SJason Schmidlapp  *
27062c5bc4SJason Schmidlapp  *      This library is free software; you can redistribute it and/or
28062c5bc4SJason Schmidlapp  *      modify it under the terms of the GNU Lesser General Public
29062c5bc4SJason Schmidlapp  *      License as published by the Free Software Foundation; either
30062c5bc4SJason Schmidlapp  *      version 2 of the License, or (at your option) any later version.
31062c5bc4SJason Schmidlapp  *
32062c5bc4SJason Schmidlapp  *      This library is distributed in the hope that it will be useful,
33062c5bc4SJason Schmidlapp  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
34062c5bc4SJason Schmidlapp  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
35062c5bc4SJason Schmidlapp  *      Lesser General Public License for more details.
36062c5bc4SJason Schmidlapp  *
37062c5bc4SJason Schmidlapp  *      You should have received a copy of the GNU Lesser General Public
38062c5bc4SJason Schmidlapp  *      License along with this library in the file COPYING.LIB;
39062c5bc4SJason Schmidlapp  *      if not, write to the Free Software Foundation, Inc.,
40062c5bc4SJason Schmidlapp  *      59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
41062c5bc4SJason Schmidlapp  */
42062c5bc4SJason Schmidlapp 
43062c5bc4SJason Schmidlapp #include <stdio.h>
44062c5bc4SJason Schmidlapp #include <stdlib.h>
45062c5bc4SJason Schmidlapp 
46062c5bc4SJason Schmidlapp 
47062c5bc4SJason Schmidlapp #include "pthread.h"
48062c5bc4SJason Schmidlapp #include "implement.h"
49062c5bc4SJason Schmidlapp 
50062c5bc4SJason Schmidlapp 
51062c5bc4SJason Schmidlapp pthread_t
pte_new(void)52062c5bc4SJason Schmidlapp pte_new (void)
53062c5bc4SJason Schmidlapp {
54062c5bc4SJason Schmidlapp   pthread_t t;
55*a12978ccSJeremy Soller   pthread_t nil = NULL;
56062c5bc4SJason Schmidlapp   pte_thread_t * tp;
57062c5bc4SJason Schmidlapp 
58062c5bc4SJason Schmidlapp   /*
59062c5bc4SJason Schmidlapp    * If there's a reusable pthread_t then use it.
60062c5bc4SJason Schmidlapp    */
61062c5bc4SJason Schmidlapp   t = pte_threadReusePop ();
62062c5bc4SJason Schmidlapp 
63*a12978ccSJeremy Soller   if (NULL != t)
64062c5bc4SJason Schmidlapp     {
65*a12978ccSJeremy Soller       tp = (pte_thread_t *) t;
66062c5bc4SJason Schmidlapp     }
67062c5bc4SJason Schmidlapp   else
68062c5bc4SJason Schmidlapp     {
69062c5bc4SJason Schmidlapp       /* No reuse threads available */
70062c5bc4SJason Schmidlapp       tp = (pte_thread_t *) calloc (1, sizeof(pte_thread_t));
71062c5bc4SJason Schmidlapp 
72062c5bc4SJason Schmidlapp       if (tp == NULL)
73062c5bc4SJason Schmidlapp         {
74062c5bc4SJason Schmidlapp           return nil;
75062c5bc4SJason Schmidlapp         }
76062c5bc4SJason Schmidlapp 
77062c5bc4SJason Schmidlapp       /* ptHandle.p needs to point to it's parent pte_thread_t. */
78*a12978ccSJeremy Soller       t = tp->ptHandle = tp;
79062c5bc4SJason Schmidlapp     }
80062c5bc4SJason Schmidlapp 
81062c5bc4SJason Schmidlapp   /* Set default state. */
82062c5bc4SJason Schmidlapp   tp->sched_priority = pte_osThreadGetMinPriority();
83062c5bc4SJason Schmidlapp 
84062c5bc4SJason Schmidlapp   tp->detachState = PTHREAD_CREATE_JOINABLE;
85062c5bc4SJason Schmidlapp   tp->cancelState = PTHREAD_CANCEL_ENABLE;
86062c5bc4SJason Schmidlapp   tp->cancelType = PTHREAD_CANCEL_DEFERRED;
87062c5bc4SJason Schmidlapp   tp->cancelLock = PTHREAD_MUTEX_INITIALIZER;
88062c5bc4SJason Schmidlapp   tp->threadLock = PTHREAD_MUTEX_INITIALIZER;
89062c5bc4SJason Schmidlapp 
90062c5bc4SJason Schmidlapp   return t;
91062c5bc4SJason Schmidlapp 
92062c5bc4SJason Schmidlapp }
93