Răsfoiți Sursa

add pthread magic assert.

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1112 bbd45198-f89e-11dd-88c7-29a3b14d5316
bernard.xiong@gmail.com 14 ani în urmă
părinte
comite
7191e2220f

+ 3 - 21
components/pthreads/mqueue.c

@@ -55,13 +55,15 @@ static posix_mq_t *posix_mq_find(const char* name)
 
 	for (iter = posix_mq_list; iter != RT_NULL; iter = iter->next)
 	{
-		object = (rt_object_t)&(iter->mq);
+		object = (rt_object_t)(iter->mq);
 
 		if (strncmp(object->name, name, RT_NAME_MAX) == 0)
 		{
 			return iter;
 		}
 	}
+
+	return RT_NULL;
 }
 
 int mq_setattr(mqd_t mqdes, const struct mq_attr *mqstat,
@@ -193,13 +195,6 @@ ssize_t mq_receive(mqd_t mqdes, char *msg_ptr, size_t msg_len, unsigned *msg_pri
 		return -1;
 	}
 
-	/* permission check */
-	if (!(mqdes->flags & O_RDONLY))
-	{
-		rt_set_errno(EBADF);
-		return -1;
-	}
-
 	result = rt_mq_recv(mqdes->mq->mq, msg_ptr, msg_len, RT_WAITING_FOREVER);
 	if (result == RT_EOK)
 		return msg_len;
@@ -218,13 +213,6 @@ int mq_send(mqd_t mqdes, const char *msg_ptr, size_t msg_len, unsigned msg_prio)
 		return -1;
 	}
 
-	/* permission check */
-	if (!(mqdes->flags & O_WRONLY))
-	{
-		rt_set_errno(EBADF);
-		return -1;
-	}
-
 	result = rt_mq_send(mqdes->mq->mq, (void*)msg_ptr, msg_len);
 	if (result == RT_EOK)
 		return 0;
@@ -245,12 +233,6 @@ ssize_t mq_timedreceive(mqd_t mqdes, char *msg_ptr, size_t msg_len,
 		rt_set_errno(EINVAL);
 		return -1;
 	}
-	/* permission check */
-	if (!(mqdes->flags & O_RDONLY))
-	{
-		rt_set_errno(EBADF);
-		return -1;
-	}
 
 	tick = libc_time_to_tick(abs_timeout);
 

+ 1 - 0
components/pthreads/pthread.c

@@ -65,6 +65,7 @@ int pthread_create (pthread_t *tid, const pthread_attr_t *attr,
 	ptd->canceled = 0;
 	ptd->cancelstate = PTHREAD_CANCEL_DISABLE;
 	ptd->canceltype = PTHREAD_CANCEL_DEFERRED;
+	ptd->magic = PTHREAD_MAGIC;
 
 	if (attr != RT_NULL) ptd->attr = *attr;
 	else 

+ 6 - 1
components/pthreads/pthread_internal.h

@@ -48,9 +48,14 @@ typedef struct _pthread_data _pthread_data_t;
 
 rt_inline _pthread_data_t* _pthread_get_data(pthread_t thread)
 {
+	_pthread_data_t* ptd;
 	RT_ASSERT(thread != RT_NULL);
 
-	return (_pthread_data_t*)thread->user_data;
+	ptd = (_pthread_data_t*)thread->user_data;
+	RT_ASSERT(ptd != RT_NULL);
+	RT_ASSERT(ptd->magic == PTHREAD_MAGIC);
+
+	return ptd;
 }
 
 extern int libc_time_to_tick(const struct timespec *time);

+ 95 - 120
examples/libc/mq.c

@@ -1,144 +1,119 @@
+#include <stdio.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <string.h>
 #include <pthread.h>
 #include <mqueue.h>
-#include <sys/fcntl.h>
 
-void child(void* parameter)
+#define MQ_NAME_1       "testmsg1"
+#define MQ_NAME_2       "testmsg2"
+#define MSG_SIZE	128
+#define MAX_MSG		3
+
+const char *s_msg_ptr[] = {"msg test 1", "msg test 2", "msg test 3"};
+char r_msg_ptr_1[MAX_MSG][MSG_SIZE];
+char r_msg_ptr_2[MAX_MSG][MSG_SIZE];
+pthread_t send1, send2, rev1, rev2;
+
+int * send_1(void * mq) 
 {
-	char msg[16];
-	ssize_t len;
-	mqd_t mq;
 	int i;
-	char expect;
-	
-	printf("Child here!\n");
-	
-	if((mq = mq_open("Q001", O_RDONLY, 0, 0)) == -1)
-	{
-		printf("mq_open = %d\n", errno);
-		pthread_exit(0);
-	}
-	else printf("mq = %x\n", &mq);
+	mqd_t mq1 = *(mqd_t *)mq;
 
-	for(expect = 'V'; expect <= 'Z'; ++expect)
-	{
-		memset(msg, 0, sizeof(msg));
-		printf("Child waiting...\n");
-		if((len = mq_receive(mq, msg, sizeof(msg), 0)) == -1)
-		{
-			printf("mq_receive = %d\n", errno);
-			pthread_exit(0);
+	printf("Enter into send_1 \n");
+	for (i = 0; i < MAX_MSG; i++ ) {
+		if ( -1 == mq_send(mq1, s_msg_ptr[i], MSG_SIZE, i)) {
+			perror("mq_send doesn't return success \n");
+			pthread_exit((void *)1);
 		}
-		else printf("GOT MESSAGE %c!\n", expect);
-
-		if(len != 8)
-			printf("expected len 8 got %d\n", len);
-		
-		for(i = 0; i < 8; ++i)
-			if(msg[i] != expect)
-				printf("msg[%d] got %c expected %c\n", i, msg[i], expect);
+		printf("[%d] send '%s' in thread send_1. \n", i+1, s_msg_ptr[i]);
 	}
-	
-	if(mq_close(mq) == -1)
-		printf("mq_close failed\n");
-			
-	printf("child done\n");
-	pthread_exit(0);
-}
-
-
-void send(void* parameter)
-{
-	mqd_t mq;
-	char msg[8];
+	pthread_exit((void *)0);
 
-	printf("Send here!\n");
-
-	if((mq = mq_open("Q001", O_WRONLY, 0, 0)) == -1)
-	{
-		printf("mq_open = %d\n", errno);
-		pthread_exit(0);
-	}	
-	else printf("mq = %x\n", &mq);
-
-	/* task will be blocked waiting for message */	
-	memset(msg, 'Z', 8);
-	if(mq_send(mq, msg, 8, 0) == -1)
-		printf("mq_send = %d\n", errno);
-
-	if(mq_close(mq) == -1)
-		printf("mq_close failed\n");
-
-	printf("Send done\n");
-	pthread_exit(0);
 }
 
-void test_thread(void* parameter)
+int * send_2(void * mq) 
 {
-	struct mq_attr attr;
-	mqd_t mq;
-	char msg[8];
-	pthread_t pthrId; 
-	
-	printf("Main here!\n");
+	int i;
+ 	mqd_t mq2 = *(mqd_t *)mq;
 
-	attr.mq_flags	= 0;
-	attr.mq_maxmsg	= 3;
-	attr.mq_msgsize	= 16;
-	if((mq = mq_open("Q001", O_CREAT | O_RDWR, 0, &attr)) == -1)
-	{
-		printf("mq_open = %d\n", errno);
-		pthread_exit(0);
+	printf("Enter into send_2 \n");
+	for (i = 0; i < MAX_MSG; i++ ) {
+		if ( -1 == mq_send(mq2, s_msg_ptr[i], MSG_SIZE, i)) {
+			perror("mq_send doesn't return success \n");
+			pthread_exit((void *)1);
+		}
+		printf("[%d] send '%s' in thread send_2. \n", i+1, s_msg_ptr[i]);
 	}
-	else printf("mq = %x\n", &mq);
-	
-	memset(msg, 'V', 8);
-	if(mq_send(mq, msg, 8, 0) == -1)
-		printf("mq_send = %d\n", errno);
-
-	memset(msg, 'W', 8);
-	if(mq_send(mq, msg, 8, 0) == -1)
-		printf("mq_send = %d\n", errno);
+	pthread_exit((void *)0);
+}
 
-	memset(msg, 'X', 8);
-	if(mq_send(mq, msg, 8, 0) == -1)
-		printf("mq_send = %d\n", errno);
+int * receive_1(void * mq) 
+{
+	int i;
+	mqd_t mq1 = *(mqd_t *)mq;
 
-	if(pthread_create(&pthrId, 0, &child, 0)!=0)
-	{
-		printf("pthread_create Child = %d\n", errno);
-		pthread_exit(0);
+	printf("Enter into receive_1 \n");
+	for (i = 0; i< MAX_MSG; i++) {
+		if ( -1 == mq_receive(mq1, r_msg_ptr_1[i], MSG_SIZE, NULL) ) {
+			perror("mq_receive doesn't return success \n");
+			pthread_exit((void *)1);
+		}
+		printf("[%d] receive '%s' in thread receive_1. \n", i+1, r_msg_ptr_1[i]);
 	}
+	pthread_exit((void *)0);
+}
+int * receive_2(void * mq) 
+{
+	int i;
+	mqd_t mq2 = *(mqd_t *)mq;
 
-	/* Should block */
-	printf("send Y\n");
-	memset(msg, 'Y', 8);
-	if(mq_send(mq, msg, 8, 0) == -1)
-		printf("mq_send = %d\n", errno);
-	else printf("sent Y\n");
-
-	if(pthread_create(&pthrId, 0, send, 0)!=0)
-	{
-		printf("pthread_create Send = %d\n", errno);
-		pthread_exit(0);
+	printf("Enter into receive_2 \n");
+	for (i = 0; i< MAX_MSG; i++) {
+		if ( -1 == mq_receive(mq2, r_msg_ptr_2[i], MSG_SIZE, NULL) ) {
+			perror("mq_receive doesn't return success \n");
+			pthread_exit((void *)1);
+		}
+		printf("[%d] receive '%s' in thread receive_2. \n", i+1, r_msg_ptr_2[i]);
 	}
-
-	if(mq_close(mq) == -1)
-		printf("mq_close failed\n");
-
-	printf("main done\n");
-	pthread_exit(0);
+	pthread_exit((void *)0);
 }
 
-#include <finsh.h>
-void libc_mq()
+int libc_mq()
 {
-	rt_thread_t tid;
-
-	tid = rt_thread_create("mqtest", test_thread, RT_NULL, 
-		2048, 20, 5);
-	if (tid != RT_NULL)
-	{
-		rt_thread_startup(tid);
+ 	mqd_t mq1 = 0, mq2 = 0;	
+	struct mq_attr mqstat;
+	int oflag = O_CREAT|O_RDWR;
+
+	memset(&mqstat, 0, sizeof(mqstat));
+	mqstat.mq_maxmsg = MAX_MSG;
+	mqstat.mq_msgsize = MSG_SIZE;
+	mqstat.mq_flags = 0;
+  
+  	if( ((mqd_t) -1) == (mq1 = mq_open(MQ_NAME_1,oflag,0777, &mqstat)) ) {
+		printf("mq_open doesn't return success \n");
+		return -1;
 	}
+  	if( ((mqd_t) -1) == (mq2 = mq_open(MQ_NAME_2,oflag,0777, &mqstat)) ) {
+		printf("mq_open doesn't return success \n");
+		return -1;
+	}
+	pthread_create(&send1, NULL, (void *)send_1, (void *)&mq1);
+	pthread_create(&send2, NULL, (void *)send_2, (void *)&mq2);
+        pthread_create(&rev1, NULL, (void *)receive_1, (void *)&mq1);	
+        pthread_create(&rev2, NULL, (void *)receive_2, (void *)&mq2);	
+	pthread_join(send1, NULL);
+	pthread_join(send2, NULL);
+	pthread_join(rev1, NULL);
+	pthread_join(rev2, NULL);
+		
+	mq_close(mq1);
+	mq_close(mq2);
+	mq_unlink(MQ_NAME_1);
+	mq_unlink(MQ_NAME_2);
+
+	printf("PASSED\n");
+	return 0;
 }
-FINSH_FUNCTION_EXPORT(libc_mq, mqueue test);
+#include <finsh.h>
+FINSH_FUNCTION_EXPORT(libc_mq, posix mqueue test);