Simplified implementation of threadOnce.
This commit is contained in:
@@ -361,25 +361,11 @@ void threadOnceOsd(threadOnceId *id, void(*func)(void *), void *arg)
|
||||
rtems_task_mode(mode, RTEMS_PREEMPT_MASK, &mode);
|
||||
}
|
||||
semMutexMustTake(onceMutex);
|
||||
switch (id->state) {
|
||||
case 0:
|
||||
id->state = -1;
|
||||
id->sem = semMutexMustCreate();
|
||||
semMutexMustTake(id->sem);
|
||||
semMutexGive(onceMutex);
|
||||
if (*id == 0) {
|
||||
func(arg);
|
||||
id->state = 1;
|
||||
semBinaryGive(id->sem);
|
||||
break;
|
||||
case -1:
|
||||
semMutexGive(onceMutex);
|
||||
semBinaryMustTake(id->sem);
|
||||
semBinaryGive(id->sem);
|
||||
break;
|
||||
default:
|
||||
semMutexGive(onceMutex);
|
||||
break;
|
||||
*id = 1;
|
||||
}
|
||||
semMutexGive(onceMutex);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -183,25 +183,11 @@ void threadOnceOsd(threadOnceId *id, void (*func)(void *), void *arg)
|
||||
checkStatusQuit(status,"pthread_once","threadOnce");
|
||||
|
||||
semMutexMustTake(onceMutex);
|
||||
switch (id->state) {
|
||||
case 0:
|
||||
id->state = -1;
|
||||
id->mutex = semMutexMustCreate();
|
||||
semMutexMustTake(id->mutex);
|
||||
semMutexGive(onceMutex);
|
||||
func(arg);
|
||||
id->state = 1;
|
||||
semMutexGive(id->mutex);
|
||||
break;
|
||||
case -1:
|
||||
semMutexGive(onceMutex);
|
||||
semMutexMustTake(id->mutex);
|
||||
semMutexGive(id->mutex);
|
||||
break;
|
||||
default:
|
||||
semMutexGive(onceMutex);
|
||||
break;
|
||||
if (*id == 0) {
|
||||
func(arg);
|
||||
*id = 1;
|
||||
}
|
||||
semMutexGive(onceMutex);
|
||||
}
|
||||
|
||||
static void * start_routine(void *arg)
|
||||
|
||||
@@ -77,7 +77,7 @@ void threadOnceOsd(threadOnceId *id, void (*func)(void *), void *arg)
|
||||
{
|
||||
/* not a good implementation (no guarantee that func() has finished before
|
||||
the next task calls this routine); see the Posix implementation */
|
||||
if(vxTas(&id->state))
|
||||
if(vxTas(*id))
|
||||
func(arg);
|
||||
}
|
||||
|
||||
|
||||
@@ -34,11 +34,9 @@ typedef enum {
|
||||
epicsShareFunc unsigned int epicsShareAPI threadGetStackSize(threadStackSizeClass size);
|
||||
|
||||
/* threadOnce is a macro for efficiency (calls threadOnceOsd) */
|
||||
typedef struct {
|
||||
int state; semMutexId mutex;
|
||||
} threadOnceId;
|
||||
typedef int threadOnceId;
|
||||
|
||||
#define OSITHREAD_ONCE_INIT {0,0}
|
||||
#define OSITHREAD_ONCE_INIT 0
|
||||
|
||||
epicsShareFunc void epicsShareAPI threadOnceOsd(
|
||||
threadOnceId *id, void (*func)(void *), void *arg);
|
||||
@@ -46,7 +44,7 @@ epicsShareFunc void epicsShareAPI threadOnceOsd(
|
||||
#define threadOnce(id,func,arg) \
|
||||
do { \
|
||||
threadOnceId *idCopy =(id); \
|
||||
if((idCopy->state) <= 0) \
|
||||
if(*idCopy == 0) \
|
||||
threadOnceOsd(idCopy,func,arg); \
|
||||
} while(0)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user