Skip to contentSkip to conversation

xenomai-solo vxWorks: know whether a task is safe or not

Xenomai-core Niklaus Giger 2 participants 5 messages Sep 21, 2008 – Sep 26, 2008

lore
Share & tools
lore
#0 lore raw

Hi

For various reasons we were forced to examine under vxWorks 5.5 the private WIND_TCB safeCnt field to determine whether a task was "safe" or not.

With the attached patch I try to add a taskIsSafe procedure to xenomai-solo. It is probably not free from race condition, as I do not know how to access a pthread_mutex.

Would such (may be improved) patch find its way into the "official" trunk?

Best regards

Signature · 1 line
NIklaus Giger
include/vxworks/taskInfo.h +2 -0
index 61315d5..27b3cc6 100644
@@ -50,6 +50,8 @@ TASK_ID taskNameToId(const char *name);
 
 TASK_ID taskIdDefault(TASK_ID task_id);
     
+BOOL taskIsSafe(TASK_ID task_id);
+
 BOOL taskIsReady(TASK_ID task_id);
 
 BOOL taskIsSuspended (TASK_ID task_id);
vxworks/taskLib.c +14 -1
index f6253b2..7dd52ae 100644
@@ -595,7 +595,7 @@ STATUS taskSafe(void)
 	}
 
 	pthread_mutex_lock(&current->safelock);
-
+	current->safeCnt = 1;
 	return OK;
 }
 
@@ -615,11 +615,24 @@ STATUS taskUnsafe(void)
 		return ERROR;
 	}
 
+	current->safeCnt = 0;
 	pthread_mutex_unlock(&current->safelock);
 
 	return OK;
 }
 
+BOOL taskIsSafe(TASK_ID tid)
+{
+	struct wind_task *task = find_wind_task(tid);
+
+	if (task == NULL) {
+		errno = S_objLib_OBJ_ID_ERROR;
+		return ERROR;
+	}
+
+	return (task->safeCnt > 0);
+}
+
 STATUS taskIdVerify(TASK_ID tid)
 {
 	struct wind_task *task = find_wind_task(tid);
vxworks/taskLib.h +1 -0
index 299d059..f743270 100644
@@ -42,6 +42,7 @@ struct wind_task_args {
 struct wind_task {
 
 	pthread_mutex_t safelock;
+	int	safeCnt;
 	sem_t barrier;
 
 	struct WIND_TCB *tcb;
#1 lore raw
Quoted 11 lines · Niklaus Giger

Hi

For various reasons we were forced to examine under vxWorks 5.5 the private WIND_TCB safeCnt field to determine whether a task was "safe" or not.

With the attached patch I try to add a taskIsSafe procedure to xenomai-solo. It is probably not free from race condition, as I do not know how to access a pthread_mutex.

Would such (may be improved) patch find its way into the "official" trunk?

Assuming that VxWorks tracks the locking depth in safeCnt, that implementation would not properly account for nested locking (safelock is a recursive mutex). Additionally, since WIND_TCB is something of a semi-public structure in 5.x and earlier versions, we could just track the safe counter there as well, instead of adding a non-standard taskIsSafe() call; we already do that for the status field anyway. Here is a possible implementation:

include/vxworks/taskLib.h +1 -0
index 510dabb..6334396 100644
@@ -49,6 +49,7 @@ typedef struct WIND_TCB {
 	void *opaque;
 	int magic;
 	int status;
+	int safeCnt;
 	int flags;
 	FUNCPTR entry;
 } WIND_TCB;
vxworks/taskLib.c +6 -4
index f6253b2..a9e12e7 100644
@@ -283,7 +283,6 @@ static STATUS __taskInit(struct wind_task *task,
 	int ret;
 
 	ret = check_task_priority(prio);
-
 	if (ret) {
 		errno = ret;
 		return ERROR;
@@ -307,6 +306,7 @@ static STATUS __taskInit(struct wind_task *task,
 	tcb->magic = task_magic;
 	tcb->opaque = task;
 	tcb->status = WIND_SUSPEND;
+	tcb->safeCnt = 0;
 	tcb->flags = flags;
 	tcb->entry = entry;
 
@@ -588,13 +588,13 @@ STATUS taskSafe(void)
 	}
 
 	current = wind_task_current();
-
 	if (current == NULL) {
 		errno = S_objLib_OBJ_NO_METHOD;
 		return ERROR;
 	}
 
 	pthread_mutex_lock(&current->safelock);
+	current->tcb->safeCnt++;
 
 	return OK;
 }
@@ -602,6 +602,7 @@ STATUS taskSafe(void)
 STATUS taskUnsafe(void)
 {
 	struct wind_task *current;
+	int ret;
 
 	if (threadobj_async_p()) {
 		errno = S_intLib_NOT_ISR_CALLABLE;
@@ -609,13 +610,14 @@ STATUS taskUnsafe(void)
 	}
 
 	current = wind_task_current();
-
 	if (current == NULL) {
 		errno = S_objLib_OBJ_NO_METHOD;
 		return ERROR;
 	}
 
-	pthread_mutex_unlock(&current->safelock);
+	ret = pthread_mutex_unlock(&current->safelock);
+	if (ret == 0)
+		current->tcb->safeCnt--;
 
 	return OK;
 }
Signature · 1 line
Philippe.
#2 lore raw

Am Montag 22 September 2008 18.17:10 schrieb Philippe Gerum:

Quoted 14 lines
Quoted 6 lines · Niklaus Giger

Hi

For various reasons we were forced to examine under vxWorks 5.5 the private WIND_TCB safeCnt field to determine whether a task was "safe" or not.

Quoted 6 lines

With the attached patch I try to add a taskIsSafe procedure to xenomai-solo. It is probably not free from race condition, as I do not know how to access a pthread_mutex.

Would such (may be improved) patch find its way into the "official" trunk?

Quoted 7 lines

Assuming that VxWorks tracks the locking depth in safeCnt, that implementation would not properly account for nested locking (safelock is a recursive mutex). Additionally, since WIND_TCB is something of a semi-public structure in 5.x and earlier versions, we could just track the safe counter there as well, instead of adding a non-standard taskIsSafe() call; we already do that for the status field anyway. Here is a possible implementation:

This implementation is okay for me, too. It seems to work, as far I could test it.

I however still have some issues because we used sometimes (for historical reasons in order to emulate a pre-vxWorks home grown cooperative mulit-tasking operation system) a taskInit/taskActivate/taskRestart combination.

Would it be a lot of work for you to add taskRestart to xenomai-solo?

Best regards

Niklaus

Quoted 72 lines
include/vxworks/taskLib.h +1 -0
index 510dabb..6334396 100644
@@ -49,6 +49,7 @@ typedef struct WIND_TCB {
 	void *opaque;
 	int magic;
 	int status;
+	int safeCnt;
 	int flags;
 	FUNCPTR entry;
 } WIND_TCB;
vxworks/taskLib.c +6 -4
index f6253b2..a9e12e7 100644
@@ -283,7 +283,6 @@ static STATUS __taskInit(struct wind_task *task,
 	int ret;
 
 	ret = check_task_priority(prio);
-
 	if (ret) {
 		errno = ret;
 		return ERROR;
@@ -307,6 +306,7 @@ static STATUS __taskInit(struct wind_task *task,
 	tcb->magic = task_magic;
 	tcb->opaque = task;
 	tcb->status = WIND_SUSPEND;
+	tcb->safeCnt = 0;
 	tcb->flags = flags;
 	tcb->entry = entry;
 
@@ -588,13 +588,13 @@ STATUS taskSafe(void)
 	}
 
 	current = wind_task_current();
-
 	if (current == NULL) {
 		errno = S_objLib_OBJ_NO_METHOD;
 		return ERROR;
 	}
 
 	pthread_mutex_lock(&current->safelock);
+	current->tcb->safeCnt++;
 
 	return OK;
 }
@@ -602,6 +602,7 @@ STATUS taskSafe(void)
 STATUS taskUnsafe(void)
 {
 	struct wind_task *current;
+	int ret;
 
 	if (threadobj_async_p()) {
 		errno = S_intLib_NOT_ISR_CALLABLE;
@@ -609,13 +610,14 @@ STATUS taskUnsafe(void)
 	}
 
 	current = wind_task_current();
-
 	if (current == NULL) {
 		errno = S_objLib_OBJ_NO_METHOD;
 		return ERROR;
 	}
 
-	pthread_mutex_unlock(&current->safelock);
+	ret = pthread_mutex_unlock(&current->safelock);
+	if (ret == 0)
+		current->tcb->safeCnt--;
 
 	return OK;
 }
Signature · 5 lines
NIklaus Giger
Wieshoschet 6
CH-8753 Mollis
+41  (0)55 612 20 54 P
+41  (0)55 618 64 68 G
#3 lore raw
Quoted 22 lines · Niklaus Giger

Am Montag 22 September 2008 18.17:10 schrieb Philippe Gerum:

Quoted 13 lines
Quoted 6 lines · Niklaus Giger

Hi

For various reasons we were forced to examine under vxWorks 5.5 the private WIND_TCB safeCnt field to determine whether a task was "safe" or not.

Quoted 6 lines

With the attached patch I try to add a taskIsSafe procedure to xenomai-solo. It is probably not free from race condition, as I do not know how to access a pthread_mutex.

Would such (may be improved) patch find its way into the "official" trunk?

Quoted 7 lines

Assuming that VxWorks tracks the locking depth in safeCnt, that implementation would not properly account for nested locking (safelock is a recursive mutex). Additionally, since WIND_TCB is something of a semi-public structure in 5.x and earlier versions, we could just track the safe counter there as well, instead of adding a non-standard taskIsSafe() call; we already do that for the status field anyway. Here is a possible implementation:

Quoted 9 lines

This implementation is okay for me, too. It seems to work, as far I could test it.

I however still have some issues because we used sometimes (for historical reasons in order to emulate a pre-vxWorks home grown cooperative mulit-tasking operation system) a taskInit/taskActivate/taskRestart combination.

Would it be a lot of work for you to add taskRestart to xenomai-solo?

I must admit that I always considered the task restart interface as a pure manifestation of the Evil, so it absence in SOLO might not be totally fortuitous. Trashing your system with that stuff is way to easy.

Anyway, here is a possible implementation based on - cough, cough... - sigsetjmp(); I don't like it since it adds code to various hot paths just to provide a rarely used feature, so I may make this optional using a configuration knob at build time if/when I actually merge that, mmmh, thing.

Please let me know how that works for you.

Signature · 1 line
Philippe.
include/vxworks/taskLib.h +3 -0
index 6334396..2f09de6 100644
@@ -39,6 +39,7 @@
 #define WIND_DELAY	0x4
 #define WIND_DEAD	0x8
 #define WIND_STOP	0x10	/* Never reported. */
+#define WIND_RESTART	0x20	/* Internal. */
 
 typedef intptr_t TASK_ID;
 
@@ -78,6 +79,8 @@ STATUS taskInit(WIND_TCB *pTcb,
 
 STATUS taskActivate(TASK_ID tid);
 
+STATUS taskRestart(TASK_ID tid);
+
 STATUS taskDelete(TASK_ID tid);
 
 STATUS taskDeleteForce(TASK_ID tid);
vxworks/msgQLib.c +4 -0
index d591a7b..e3d071d 100644
@@ -149,6 +149,8 @@ int msgQReceive(MSG_Q_ID msgQId, char *buffer, UINT maxNBytes, int timeout)
 		return ERROR;
 	}
 
+	check_task_restart(wind_task_current());
+
 	mq = get_mq_from_id(msgQId);
 	if (mq == NULL) {
 	objid_error:
@@ -217,6 +219,8 @@ STATUS msgQSend(MSG_Q_ID msgQId, const char *buffer, UINT bytes,
 	UINT maxbytes;
 	int ret;
 	
+	check_task_restart(wind_task_current());
+
 	mq = get_mq_from_id(msgQId);
 	if (mq == NULL) {
 	objid_error:
vxworks/semLib.c +8 -3
index 6467354..dfc37cc 100644
@@ -61,7 +61,9 @@ static STATUS xsem_take(struct wind_sem *sem, int timeout)
 
 	if (threadobj_async_p())
 		return S_intLib_NOT_ISR_CALLABLE;
-	  
+
+	check_task_restart(wind_task_current());
+
 	if (syncobj_lock(&sem->u.xsem.sobj, &syns))
 		return S_objLib_OBJ_ID_ERROR;
 
@@ -196,8 +198,11 @@ static STATUS msem_take(struct wind_sem *sem, int timeout)
 		return S_intLib_NOT_ISR_CALLABLE;
 	  
 	current = wind_task_current();
-	if (current != NULL && (sem->options & SEM_DELETE_SAFE))
-		pthread_mutex_lock(&current->safelock);
+	if (current != NULL) {
+		check_task_restart(current);
+		if (sem->options & SEM_DELETE_SAFE)
+			pthread_mutex_lock(&current->safelock);
+	}
 
 	if (timeout == NO_WAIT) {
 		ret = pthread_mutex_trylock(&sem->u.msem.lock);
vxworks/taskLib.c +46 -5

121 lines not on this page · raw message

vxworks/taskLib.h +5 -0
index 299d059..46718e1 100644
@@ -20,6 +20,7 @@
 #define _VXWORKS_TASKLIB_H
 
 #include <semaphore.h>
+#include <setjmp.h>
 #include <xenomai/threadobj.h>
 #include <xenomai/registry.h>
 #include <vxworks/taskLib.h>
@@ -53,6 +54,8 @@ struct wind_task {
 	struct threadobj thobj;
 	struct fsobj fsobj;
 	struct hashobj obj;
+
+	jmp_buf ienv;
 };
 
 int wind_task_get_priority(struct wind_task *task);
@@ -97,6 +100,8 @@ struct wind_task *get_wind_task_or_self(TASK_ID tid);
 
 void put_wind_task(struct wind_task *task);
 
+void check_task_restart(struct wind_task *current);
+
 extern struct hash_table wind_task_table;
 
 #endif /* _VXWORKS_TASKLIB_H */
#4 lore raw
In reply to Niklaus Giger
Quoted 22 lines · Niklaus Giger

Am Montag 22 September 2008 18.17:10 schrieb Philippe Gerum:

Quoted 13 lines
Quoted 6 lines · Niklaus Giger

Hi

For various reasons we were forced to examine under vxWorks 5.5 the private WIND_TCB safeCnt field to determine whether a task was "safe" or not.

Quoted 6 lines

With the attached patch I try to add a taskIsSafe procedure to xenomai-solo. It is probably not free from race condition, as I do not know how to access a pthread_mutex.

Would such (may be improved) patch find its way into the "official" trunk?

Quoted 7 lines

Assuming that VxWorks tracks the locking depth in safeCnt, that implementation would not properly account for nested locking (safelock is a recursive mutex). Additionally, since WIND_TCB is something of a semi-public structure in 5.x and earlier versions, we could just track the safe counter there as well, instead of adding a non-standard taskIsSafe() call; we already do that for the status field anyway. Here is a possible implementation:

Quoted 9 lines

This implementation is okay for me, too. It seems to work, as far I could test it.

I however still have some issues because we used sometimes (for historical reasons in order to emulate a pre-vxWorks home grown cooperative mulit-tasking operation system) a taskInit/taskActivate/taskRestart combination.

Would it be a lot of work for you to add taskRestart to xenomai-solo?

I must admit that I always considered the task restart interface as a pure manifestation of the Evil, so its absence from SOLO might not be totally fortuitous. Trashing your system with that stuff is way to easy.

Anyway, here is a possible implementation based on - cough, cough... - sigsetjmp(); I don't like it since it adds code to various hot paths just to provide a rarely used feature, so I may make this optional using a configuration knob at build time if/when I actually merge that, mmmh, thing.

Please let me know how that works for you.

Signature · 1 line
Philippe.
include/vxworks/taskLib.h +3 -0
index 6334396..2f09de6 100644
@@ -39,6 +39,7 @@
 #define WIND_DELAY	0x4
 #define WIND_DEAD	0x8
 #define WIND_STOP	0x10	/* Never reported. */
+#define WIND_RESTART	0x20	/* Internal. */
 
 typedef intptr_t TASK_ID;
 
@@ -78,6 +79,8 @@ STATUS taskInit(WIND_TCB *pTcb,
 
 STATUS taskActivate(TASK_ID tid);
 
+STATUS taskRestart(TASK_ID tid);
+
 STATUS taskDelete(TASK_ID tid);
 
 STATUS taskDeleteForce(TASK_ID tid);
vxworks/msgQLib.c +4 -0
index d591a7b..e3d071d 100644
@@ -149,6 +149,8 @@ int msgQReceive(MSG_Q_ID msgQId, char *buffer, UINT maxNBytes, int timeout)
 		return ERROR;
 	}
 
+	check_task_restart(wind_task_current());
+
 	mq = get_mq_from_id(msgQId);
 	if (mq == NULL) {
 	objid_error:
@@ -217,6 +219,8 @@ STATUS msgQSend(MSG_Q_ID msgQId, const char *buffer, UINT bytes,
 	UINT maxbytes;
 	int ret;
 	
+	check_task_restart(wind_task_current());
+
 	mq = get_mq_from_id(msgQId);
 	if (mq == NULL) {
 	objid_error:
vxworks/semLib.c +8 -3
index 6467354..dfc37cc 100644
@@ -61,7 +61,9 @@ static STATUS xsem_take(struct wind_sem *sem, int timeout)
 
 	if (threadobj_async_p())
 		return S_intLib_NOT_ISR_CALLABLE;
-	  
+
+	check_task_restart(wind_task_current());
+
 	if (syncobj_lock(&sem->u.xsem.sobj, &syns))
 		return S_objLib_OBJ_ID_ERROR;
 
@@ -196,8 +198,11 @@ static STATUS msem_take(struct wind_sem *sem, int timeout)
 		return S_intLib_NOT_ISR_CALLABLE;
 	  
 	current = wind_task_current();
-	if (current != NULL && (sem->options & SEM_DELETE_SAFE))
-		pthread_mutex_lock(&current->safelock);
+	if (current != NULL) {
+		check_task_restart(current);
+		if (sem->options & SEM_DELETE_SAFE)
+			pthread_mutex_lock(&current->safelock);
+	}
 
 	if (timeout == NO_WAIT) {
 		ret = pthread_mutex_trylock(&sem->u.msem.lock);
vxworks/taskLib.c +46 -5

121 lines not on this page · raw message

vxworks/taskLib.h +5 -0
index 299d059..46718e1 100644
@@ -20,6 +20,7 @@
 #define _VXWORKS_TASKLIB_H
 
 #include <semaphore.h>
+#include <setjmp.h>
 #include <xenomai/threadobj.h>
 #include <xenomai/registry.h>
 #include <vxworks/taskLib.h>
@@ -53,6 +54,8 @@ struct wind_task {
 	struct threadobj thobj;
 	struct fsobj fsobj;
 	struct hashobj obj;
+
+	jmp_buf ienv;
 };
 
 int wind_task_get_priority(struct wind_task *task);
@@ -97,6 +100,8 @@ struct wind_task *get_wind_task_or_self(TASK_ID tid);
 
 void put_wind_task(struct wind_task *task);
 
+void check_task_restart(struct wind_task *current);
+
 extern struct hash_table wind_task_table;
 
 #endif /* _VXWORKS_TASKLIB_H */