123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- #include "pte_osal.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include "pthread.h"
- #include "implement.h"
- int
- pthread_create (pthread_t * tid,
- const pthread_attr_t * attr,
- void *(*start) (void *), void *arg)
- {
- pthread_t thread;
- pte_thread_t * tp;
- register pthread_attr_t a;
- int result = EAGAIN;
- int run = PTE_TRUE;
- ThreadParms *parms = NULL;
- long stackSize;
- int priority = 0;
- pthread_t self;
- pte_osResult osResult;
-
- if (tid != NULL)
- {
- tid->x = 0;
- }
- if (attr != NULL)
- {
- a = *attr;
- }
- else
- {
- a = NULL;
- }
- if ((thread = pte_new ()).p == NULL)
- {
- goto FAIL0;
- }
- tp = (pte_thread_t *) thread.p;
- priority = tp->sched_priority;
- if ((parms = (ThreadParms *) malloc (sizeof (*parms))) == NULL)
- {
- goto FAIL0;
- }
- parms->tid = thread;
- parms->start = start;
- parms->arg = arg;
- if (a != NULL)
- {
- stackSize = a->stacksize;
- tp->detachState = a->detachstate;
- priority = a->param.sched_priority;
- if ( (priority > pte_osThreadGetMaxPriority()) ||
- (priority < pte_osThreadGetMinPriority()) )
- {
- result = EINVAL;
- goto FAIL0;
- }
-
-
- if (PTHREAD_INHERIT_SCHED == a->inheritsched)
- {
-
- self = pthread_self ();
- priority = ((pte_thread_t *) self.p)->sched_priority;
- }
- }
- else
- {
-
- stackSize = PTHREAD_STACK_MIN;
- }
- tp->state = run ? PThreadStateInitial : PThreadStateSuspended;
- tp->keys = NULL;
-
- result = pthread_mutex_lock (&tp->threadLock);
- if (0 == result)
- {
-
- tp->sched_priority = priority;
- (void) pthread_mutex_unlock (&tp->threadLock);
- }
- osResult = pte_osThreadCreate(pte_threadStart,
- stackSize,
- priority,
- parms,
- &(tp->threadId));
- if (osResult == PTE_OS_OK)
- {
- pte_osThreadStart(tp->threadId);
- result = 0;
- }
- else
- {
- tp->threadId = 0;
- result = EAGAIN;
- goto FAIL0;
- }
-
-
- FAIL0:
- if (result != 0)
- {
- pte_threadDestroy (thread);
- tp = NULL;
- if (parms != NULL)
- {
- free (parms);
- }
- }
- else
- {
- if (tid != NULL)
- {
- *tid = thread;
- }
- }
- return (result);
- }
|