您的位置:首页 > 编程语言

各嵌入式操作系统的任务,信号量,队列测试代码

2009-03-24 21:36 316 查看
#define OS_20
#ifdef OS_LINUX
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/types.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#include<linux/msg.h>
#include<linux/ipc.h>
#define MAXTIME 5
#endif
#ifdef OS_20
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "ostime.h"
#include "message.h"
#include "stos.h"
#include "stddefs.h"
#include "sttbx.h"
#define OSTaskNum 50
#define OSSemNum 50
#define OSQueNum 50
#define MAXTIME 100
typedef struct
{
task_t *taskid;
unsigned int task_handle;
}OS_Task;
static OS_Task TaskMgr[OSTaskNum];

typedef struct
{
semaphore_t *semid;
unsigned int sem_handle;
}Sem_Task;
static Sem_Task SemMgr[OSSemNum];
typedef struct
{
message_queue_t *queid;
unsigned int que_handle;
}Que_Task;
static Que_Task QueMgr[OSQueNum];
#endif
#ifdef OS_VXWORKS
#include <vxWorks.h>
#include <stdlib.h>
#include <msgQLib.h>
#include <taskLib.h>
#include <memLib.h>
#include <string.h>
#include<semLib.h>
#define MAXTIME 10
#endif
#ifdef OS_WIN32
#include <iostream>
#include <queue>
#include <deque>
#include<windows.h>
#include<winbase.h>
#include <process.h>
#include <cstdio>
using namespace std ;
#define MY_MSG WM_USER+100
#define MAXTIME 100
unsigned long tidoneID,tidtowID,tidthreeID,tidfourID,tidfiveID,tidsixID;
#endif
#define MYEERROR -1
#define myprintf printf
#define dprintf myprintf
static unsigned int tidone,tidtow,tidthree,tidfour,tidfive,tidsix;
static unsigned int blank_number, product_number;
static unsigned int product_mutex ;//生产互斥
static unsigned int blank_mutex ;//消费互斥
int qid;//消息队列ID
static int serial_flag=0;

#ifndef NULL
#define NULL (void*)0
#endif
#define NUM 50
struct ROUNTD{
int procpoint;
int conspoint;
int queue[NUM];
};
enum mutex_mode
{
Wait_Forever=1,
EIS_QUEUE_SEM_WAIT,
EIS_QUEUE_SEM_NOWAIT
};
enum QUE_SEM_MODE
{
EIS_TASK_WAIT_FIFO=0,
EIS_TASK_WAIT_PRIO
};
#define EIS_OS_QUEUE_SEM_STATUS_AVAILABLE 0
#define EIS_OS_QUEUE_SEM_STATUS_UNAVAILABLE -1
typedef struct mymsgbuf {
int mtype; /* Message type */
int request; /* Work request number */
int salary; /* Employee's salary */
} MyMsg;
#ifdef OS_WIN32
typedef queue<struct mymsgbuf> MYQUEUE;
MYQUEUE myQueue;
#endif
#define MSGBUF mymsgbuf
struct ROUNTD TestQueue;
typedef void (*TASK_PROC_FUNC)(void);
/**各操作系统接口定义**/
#ifdef OS_WIN32
unsigned int ipanel_porting_task_create(const char *task_name,/
TASK_PROC_FUNC func,/
void *param,/
int priority,/
unsigned int stack_size ,unsigned long &serial_main_task_ID)
#else
unsigned int ipanel_porting_task_create(const char *task_name,/
TASK_PROC_FUNC func,/
void *param,/
int priority,/
unsigned int stack_size)
#endif
{
#ifdef OS_LINUX
int err;
pthread_t tid;
err = pthread_create(&tid, NULL, func,param);
if (err != 0) {
myprintf("can't create thread: %s/n");
return MYEERROR;
}
return (unsigned int)tid;
#endif
#ifdef OS_VXWORKS
unsigned int handle = 0;

if (0 == stack_size)
{
stack_size = 0x1000;
}

if (func && (priority >= 0) && (priority <= 255))
{

handle = (unsigned int)taskSpawn( (char*)task_name,
priority,
VX_FP_TASK,
(int)stack_size,
(FUNCPTR)func,
(unsigned int)param,
0,
0,
0,
0,
0,
0,
0,
0,
0 );
}

myprintf(("[ipanel_porting_task_create] handle=0x%x, name=%s, func=%p, param=%p, prio=%d, stack_size=0x%x/n",
handle, task_name, func, param, priority, stack_size));

return handle;
#endif
#ifdef OS_WIN32
unsigned int serial_main_thread;

serial_main_thread=( unsigned int)CreateThread(NULL, stack_size, (LPTHREAD_START_ROUTINE)(func), param, 0,&serial_main_task_ID);
SetThreadPriority(&serial_main_thread,THREAD_PRIORITY_HIGHEST);
return serial_main_thread;
#ifdef OS_20
int i = 0;
myprintf("[ipanel_porting_task_create]name=%s, stack=0x%x, priority=%d/n",name,stack_size,priority);

if ((func == NULL) || (stack_size%0x400) || (stack_size<0))
{
myprintf("[ipanel_porting_task_create]param error!/n");
return MYEERROR;
}

for (i = 0; i < OSTaskNum; i++)
{
if (TaskMgr[i].task_handle == 0)
{
TaskMgr[i].taskid = task_create(
(void(*) (void *))func,
param,
stack_size,
priority,
name,
0
);
if (!TaskMgr[i].taskid)
{
myprintf("[ipanel_porting_task_create]failed!/n");
return MYEERROR;
}

TaskMgr[i].task_handle = i+1;
break;
}
}

if ( i == OSTaskNum )
{
myprintf("[ipanel_porting_task_create]task exhausted!/n");
return MYEERROR;
}

return (TaskMgr[i].task_handle);
#endif
#endif
}
int ipanel_porting_task_destroy(unsigned int task_handle)
{
#ifdef OS_LINUX
if(0==task_handle)
pthread_exit((void *)1);
else
pthread_cancel((pthread_t)task_handle);
return 1;
#endif
#ifdef OS_VXWORKS
int ret =MYEERROR;

if (0 != task_handle)
{
if (OK == taskDelete(task_handle))
{
ret = 0;
}
}

myprintf(("[ipanel_porting_task_destroy] handle=0x%x, ret=%d/n",
task_handle, ret));

return ret;
#endif
#ifdef OS_WIN32
BOOL rstatus = TerminateThread((HANDLE)task_handle,0 );

if (rstatus != 0)
{
return 1;
}
else
{
return MYEERROR;
}
#endif
#ifdef OS_20
int ret = -1;
int i =0;
myprintf("[ipanel_porting_task_destroy] handle=%d/n",task_handle);

if (task_handle == 0)
{
myprintf("[ipanel_porting_task_destroy] params error!/n");
return MYEERROR;
}

for (i=0; i<OSTaskNum; i++)
{
if (task_handle == TaskMgr[i].task_handle)
{ task_kill(TaskMgr[i].taskid,0,(task_kill_flags_t)0);
task_delete (TaskMgr[i].taskid);
TaskMgr[i].taskid = 0;
TaskMgr[i].task_handle = 0;
break;
}
}

if (i == OSTaskNum)
{
myprintf("[ipanel_porting_task_destroy] NO task is found/n");
return ret;
}

ret = 1 ;

return ret;
#endif
}
void ipanel_porting_task_sleep(int ms)
{
#ifdef OS_LINUX
sleep(ms);
#endif
#ifdef OS_VXWORKS
taskDelay(ms);
#endif
#ifdef OS_WIN32
Sleep(ms);
#endif
#ifdef OS_20
if( ms > 0)
{
task_delay(ms *ST_GetClocksPerSecond()/1000);
}
#endif

}

unsigned int ipanel_porting_queue_create(const char *name,/
unsigned int maxQueueLength,/
unsigned int taskWaitMode)

{
#ifdef OS_LINUX
int qid;
key_t msgkey;
msgkey = ftok(".", 'm');
if((qid = msgget( msgkey, IPC_CREAT | 0660 )) == -1)
{
return(MYEERROR);
}
return(qid);
#endif
#ifdef OS_VXWORKS
unsigned int handle = 0;

if (maxQueueLength > 0)
{
switch (taskWaitMode)
{
case EIS_TASK_WAIT_FIFO:
handle = (unsigned int)msgQCreate(maxQueueLength,
sizeof(struct MSGBUF),
MSG_Q_FIFO);
break;

case EIS_TASK_WAIT_PRIO:
handle = (unsigned int)msgQCreate(maxQueueLength,
sizeof(struct MSGBUF),
MSG_Q_PRIORITY);
break;

default:
handle = 0;
}
}

myprintf(("[ipanel_porting_queue_create] handle=0x%x, maxLen=%u, Mode=%u/n",
handle, maxQueueLength, taskWaitMode));

return handle;
#endif
#ifdef OS_WIN32
return MYEERROR;
#endif
#ifdef OS_20
int i = 0;

myprintf("[ipanel_porting_queue_create] name=%s, maxQueueLength=%d, taskWaitMode=%d/n",name, maxQueueLength, taskWaitMode);

if (taskWaitMode!=EIS_TASK_WAIT_FIFO && taskWaitMode!=EIS_TASK_WAIT_PRIO)
{
myprintf("[ipanel_porting_queue_create] taskWaitMode error!/n");
return MYEERROR;
}

for (i=0; i<OSQueNum; i++)
{
if (QueMgr[i].que_handle == 0)
{
QueMgr[i].queid = message_create_queue(sizeof(struct MSGBUF), maxQueueLength);
if (QueMgr[i].queid == 0)
{
myprintf("[ipanel_porting_queue_create] Failed/n");
return MYEERROR;
}

QueMgr[i].que_handle = i+1;
break;
}
}

if (i == OSQueNum)
{
myprintf("[ipanel_porting_queue_create] NO resource for MessageQueue/n");
return MYEERROR;
}

return (QueMgr[i].que_handle) ;
#endif
}
int ipanel_porting_queue_destroy(unsigned int queue_handle)
{
#ifdef OS_LINUX
msgctl(queue_handle,IPC_RMID,0);
return 1;
#endif
#ifdef OS_VXWORKS
int ret = MYEERROR;

if (0 != queue_handle)
{
if (OK == msgQDelete((MSG_Q_ID)queue_handle))
{
ret = 0;
}
}

myprintf(("[ipanel_porting_queue_destroy] handle=0x%x, ret=%d/n",
queue_handle, ret));

return ret;
#endif
#ifdef OS_WIN32
return 1;
#endif
#ifdef OS_20
int ret = MYEERROR ;
int i = 0;

if (0 == queue_handle)
{
myprintf("[ipanel_porting_queue_destroy] handle NOT exist ./n");
return ret;
}

for (i=0; i<OSQueNum; i++)
{
if (QueMgr[i].que_handle == queue_handle)
{
message_delete_queue(QueMgr[i].queid);
QueMgr[i].queid = 0;
QueMgr[i].que_handle = 0;
break;
}
}

if (i == OSQueNum)
{
myprintf("[ipanel_porting_queue_destroy] NO queue is found/n");
return ret;
}

ret = 1 ;

return ret;
#endif
}
#ifdef OS_WIN32
int ipanel_porting_queue_send(unsigned int threadID,unsigned int msgtype,/
struct MSGBUF *msg)
#else
int ipanel_porting_queue_send(unsigned int queue_handle,/
struct MSGBUF *msg)

#endif
{
#ifdef OS_LINUX
int result, length;
length = sizeof(struct MSGBUF) - sizeof(long);
if((result = msgsnd( queue_handle, msg, length, 0)) == -1)
{
return(MYEERROR);
}
return(result);
#endif
#ifdef OS_VXWORKS
int ret = MYEERROR;

if (msg && queue_handle)
{
if (OK == msgQSend((MSG_Q_ID)queue_handle,
(char*)msg,
sizeof(struct MSGBUF),
0,
MSG_PRI_NORMAL))
{
ret = 0;
}
}

return ret;
#endif
#ifdef OS_WIN32
if(!PostThreadMessage(threadID,msgtype,WPARAM(msg),0))
{
myprintf("post message failed,errno:%d/n",::GetLastError());
return MYEERROR;
}
return 0;
#endif
#ifdef OS_20
int ret = MYEERROR ;
int i = 0;

if( (0 == queue_handle) ||(NULL == msg) )
{
myprintf("[ipanel_porting_queue_send] para error or handle NOT exist ./n");
return ret;
}

for (i=0; i<OSQueNum; i++)
{
if (QueMgr[i].que_handle == queue_handle)
{
void *msg_buffer = message_claim_timeout(QueMgr[i].queid, TIMEOUT_IMMEDIATE);
if (NULL == msg_buffer)
{
myprintf("[ipanel_porting_queue_send] message_claim_timeout() timeout/n ");
return ret;
}

memcpy(msg_buffer, msg, sizeof(struct MSGBUF));
message_send(QueMgr[i].queid, msg_buffer);
break;
}
}

if (i == OSQueNum)
{
myprintf("->[ipanel_porting_queue_send] NO queue is found /n");
return ret;
}

ret = 1 ;

return ret;
#endif
}
int ipanel_porting_queue_recv(unsigned int queue_handle,/
struct MSGBUF *msg,/
unsigned int waitMode,/
int millSecsToWait)
{
#ifdef OS_LINUX
int result, length;
/* The length is essentially the size of the structure minus sizeof(mtype) */
length = sizeof(struct MSGBUF) - sizeof(long);
if((result = msgrcv( queue_handle, msg, length, 0, IPC_NOWAIT)) == -1)
{
return(-1);
}
return(result);
#endif
#ifdef OS_VXWORKS
int ret = EIS_OS_QUEUE_SEM_STATUS_UNAVAILABLE;

if (queue_handle && msg && (millSecsToWait >= 0))
{
switch(waitMode)
{
case Wait_Forever:
if (0 == millSecsToWait)
{
millSecsToWait = WAIT_FOREVER;
}

if (ERROR != msgQReceive((MSG_Q_ID)queue_handle,
(char*)msg,
sizeof(struct MSGBUF),
millSecsToWait))
{
ret = EIS_OS_QUEUE_SEM_STATUS_AVAILABLE;
}
else
{
ret =EIS_OS_QUEUE_SEM_STATUS_UNAVAILABLE;
}
break;

case EIS_QUEUE_SEM_NOWAIT:
if (ERROR != msgQReceive((MSG_Q_ID)queue_handle,
(char*)msg,
sizeof(struct MSGBUF),
NO_WAIT))
{
ret = EIS_OS_QUEUE_SEM_STATUS_AVAILABLE;
}
else
{
ret =EIS_OS_QUEUE_SEM_STATUS_UNAVAILABLE;
}
break;

default:
ret = EIS_OS_QUEUE_SEM_STATUS_UNAVAILABLE;
}
}
return ret;
#endif
#ifdef OS_WIN32
MyMsg tempmsg;
if(myQueue.empty()!=NULL)
{
tempmsg=myQueue.front();
myQueue.pop();
myprintf("get a messgae tempmsg.mtype=%d tempmsg.request=%d tempmsg.salary=%d/n",tempmsg.mtype,tempmsg.request,tempmsg.salary);
return EIS_OS_QUEUE_SEM_STATUS_AVAILABLE;

}
else
{

return EIS_OS_QUEUE_SEM_STATUS_UNAVAILABLE;
}

#endif
#ifdef OS_20
int ret = MYEERROR;
int i = 0;

if( queue_handle == 0 || msg==NULL)
{
dprintf("[ipanel_porting_queue_recv] para error or handle NOT exist ./n");
return ret;
}

for (i=0; i<OSQueNum; i++)
{
if (QueMgr[i].que_handle == queue_handle)
{
void *msg_buffer = message_receive_timeout(QueMgr[i].queid, TIMEOUT_IMMEDIATE);//TIMEOUT_IMMEDIATE
if (NULL == msg_buffer)
{
dprintf("->[ipanel_porting_queue_recv] message_receive_timeout failed/n");
return ret;
}

memcpy(msg, msg_buffer, sizeof(struct MSGBUF));
message_release(QueMgr[i].queid, msg_buffer);
break;
}
}

if ( i == OSQueNum )
{
dprintf("->[ipanel_porting_queue_recv] NO queue is found/n");
return ret;
}

ret = 1 ;

return ret;
#endif
}
unsigned int ipanel_porting_sem_create(const char *name,/
int initialTokenCount,/
unsigned int taskWaitMode)
{
#ifdef OS_LINUX
unsigned int tempHandle;
sem_init((sem_t*)&tempHandle, 0, initialTokenCount);
return (unsigned int)tempHandle;
#endif
#ifdef OS_VXWORKS
unsigned int handle = 0;

if (initialTokenCount >= 0)
{
switch (taskWaitMode)
{
case EIS_TASK_WAIT_FIFO:
handle = (unsigned int)semCCreate(SEM_Q_FIFO, initialTokenCount);
break;

case EIS_TASK_WAIT_PRIO:
handle = (unsigned int)semCCreate(SEM_Q_PRIORITY, initialTokenCount);
break;

default:
handle = 0;
}
}

myprintf(("[ipanel_porting_sem_create] handle=0x%x, count=%d, Mode=%u/n",
handle, initialTokenCount, taskWaitMode));

return handle;
#endif
#ifdef OS_WIN32
unsigned int semHandle;
semHandle=(unsigned int)CreateSemaphore(NULL,initialTokenCount,100,name);
return semHandle;
#endif
#ifdef OS_20
unsigned int ret = MYEERROR;
unsigned int waitmode ;
int i=0;

if(0 > initialTokenCount)
{
dprintf("[ipanel_porting_sem_create] initialTokenCount error!/n");
return ret;
}

if (taskWaitMode!= EIS_TASK_WAIT_FIFO && taskWaitMode!=EIS_TASK_WAIT_PRIO)
{
dprintf("[ipanel_porting_sem_create] taskWaitMode error!/n");
return ret;
}

for (i=0; i<OSSemNum; i++)
{
if (SemMgr[i].sem_handle == 0)
{
if ( EIS_TASK_WAIT_FIFO == taskWaitMode )
{
SemMgr[i].semid = semaphore_create_fifo(initialTokenCount);
}
else if ( EIS_TASK_WAIT_PRIO == taskWaitMode )
{
SemMgr[i].semid = semaphore_create_priority(initialTokenCount);
}
if (SemMgr[i].semid == 0)
{
dprintf("[ipanel_porting_sem_create] Failed/n");
return ret;
}

SemMgr[i].sem_handle = i+1;
break;
}
}

if (i == OSSemNum)
{
dprintf("[ipanel_porting_sem_create] NO resources for create semaphore/n");
return ret;
}

return (SemMgr[i].sem_handle);
#endif
}
int ipanel_porting_sem_destroy(unsigned int sem_handle)
{
#ifdef OS_LINUX
sem_destroy((sem_t*)&sem_handle);
return 1;
#endif
#ifdef OS_VXWORKS
int ret = MYEERROR;

if (0 != sem_handle)
{
if (OK == semDelete((SEM_ID)sem_handle))
{
ret = 0;
}
}

myprintf(("[ipanel_porting_sem_destroy] handle=0x%x, ret=%d/n",
sem_handle, ret));

return ret;
#endif
#ifdef OS_WIN32
return CloseHandle((HANDLE)sem_handle);
#endif
#ifdef OS_20
int ret = MYEERROR;
int i = 0;

if(0 == sem_handle )
{
dprintf("[ipanel_porting_sem_destroy] handle NOT exist ./n");
return MYEERROR ;
}

for (i=0; i<OSSemNum; i++)
{
if (sem_handle == SemMgr[i].sem_handle)
{
semaphore_delete (SemMgr[i].semid);
SemMgr[i].semid = 0;
SemMgr[i].sem_handle = 0;
break;
}
}

if (i == OSSemNum)
{
dprintf("[ipanel_porting_sem_destroy] NO semaphore is found/n");
return ret ;
}

ret = 1;

return ret;
#endif
}

int ipanel_porting_sem_wait(unsigned int sem_handle,/
unsigned int waitMode,/
int milliSecsToWait)
{
#ifdef OS_LINUX
sem_wait((sem_t*)&sem_handle);
return 1;
#endif
#ifdef OS_VXWORKS
int ret = EIS_OS_QUEUE_SEM_STATUS_UNAVAILABLE;

if ((0 == sem_handle) || (milliSecsToWait < 0))
{
return ret;
}

switch (waitMode)
{
case EIS_QUEUE_SEM_WAIT:
if (0 == milliSecsToWait)
{
milliSecsToWait = WAIT_FOREVER;
}

if (OK == semTake((SEM_ID)sem_handle, milliSecsToWait))
{
ret = EIS_OS_QUEUE_SEM_STATUS_AVAILABLE;
}
break;

case EIS_QUEUE_SEM_NOWAIT:
if (OK == semTake((SEM_ID)sem_handle, NO_WAIT))
{
ret = EIS_OS_QUEUE_SEM_STATUS_AVAILABLE;
}
break;

default:
ret = EIS_OS_QUEUE_SEM_STATUS_UNAVAILABLE;
}

return ret;
#endif
#ifdef OS_WIN32
int ret = EIS_OS_QUEUE_SEM_STATUS_UNAVAILABLE;
switch (waitMode)
{
case EIS_QUEUE_SEM_WAIT:

if ( 0==WaitForSingleObject((HANDLE)sem_handle,INFINITE))
{
ret = EIS_OS_QUEUE_SEM_STATUS_AVAILABLE;
}
break;

case EIS_QUEUE_SEM_NOWAIT:
if (0==WaitForSingleObject((HANDLE)sem_handle,0))
{
ret = EIS_OS_QUEUE_SEM_STATUS_AVAILABLE;
}
break;

default:
ret = EIS_OS_QUEUE_SEM_STATUS_UNAVAILABLE;
}
return ret;
#endif
#ifdef OS_20
int ret = MYEERROR;
int i = 0;

if ( 0 == sem_handle )
{
dprintf("[ipanel_porting_sem_wait] handle NOT exist ./n");
return ret;
}

for (i=0; i<OSSemNum; i++)
{
if (SemMgr[i].sem_handle == sem_handle)
{
if (EIS_QUEUE_SEM_NOWAIT == waitMode)
{
semaphore_wait_timeout(SemMgr[i].semid, TIMEOUT_IMMEDIATE);
}
else if (Wait_Forever == waitMode)
{
semaphore_wait_timeout(SemMgr[i].semid, TIMEOUT_INFINITY);
}
else // 设定等待的时间
{
clock_t time = milliSecsToWait*ST_GetClocksPerSecond()/1000;
semaphore_wait_timeout(SemMgr[i].semid, &time);
}

break;
}
}

if (i == OSSemNum)
{
dprintf("->[ipanel_porting_sem_wait] NO semaphore found/n");
return ret;
}

ret = 1 ;

return ret;
#endif

}

int ipanel_porting_sem_release(unsigned int sem_handle)

{
#ifdef OS_LINUX
sem_post((sem_t*)&sem_handle);
#endif
#ifdef OS_VXWORKS
int ret =MYEERROR;

if (0 != sem_handle)
{
if (OK == semGive((SEM_ID)sem_handle))
{
ret = 0;
}
}

return ret;
#endif
#ifdef OS_WIN32
return ReleaseSemaphore((HANDLE)sem_handle,1,NULL);

#endif
#ifdef OS_20
int ret = MYEERROR;
int i = 0;

if(0 == sem_handle )
{
dprintf("[ipanel_porting_sem_release] handle NOT exist ./n");
return ret ;
}

for (i=0; i<OSSemNum; i++)
{
if (SemMgr[i].sem_handle == sem_handle)
{
semaphore_signal(SemMgr[i].semid);
break;
}
}

if (i == OSSemNum)
{
dprintf("[ipanel_porting_sem_release] NO semaphore is found/n");
return ret ;
}

ret = 1 ;

return ret ;

#endif
}
unsigned int ipanel_porting_mutex_create()
{
#ifdef OS_LINUX
unsigned int mutexHandle =(unsigned int) PTHREAD_MUTEX_INITIALIZER;

// pthread_mutex_init((pthread_mutex_t *)&mutex,NULL);
return mutexHandle;
#endif
#ifdef OS_VXWORKS
ipanel_porting_sem_create(NULL,1,0);//互斥量相当与初始值为1的信号量
#endif
#ifdef OS_WIN32
unsigned int MutexHandle;
MutexHandle=(unsigned int)CreateEvent(NULL, FALSE, true, NULL);
return MutexHandle;
#endif
#ifdef OS_20
ipanel_porting_sem_create(NULL,1,0);//互斥量相当与初始值为1的信号量
#endif
}
unsigned int ipanel_porting_mutex_destroy(unsigned int mutex_handle)
{
#ifdef OS_LINUX
return (unsigned int)pthread_mutex_destroy((pthread_mutex_t *)&mutex_handle);

#endif
#ifdef OS_VXWORKS
ipanel_porting_sem_destroy(mutex_handle);
#endif
#ifdef OS_WIN32
return CloseHandle((HANDLE)mutex_handle);

#endif
#ifdef OS_20
ipanel_porting_sem_destroy(mutex_handle);
#endif
}
int ipanel_porting_mutex_wait(unsigned int mutex_handle,/
unsigned int waitMode,/
int milliSecsToWait)
{
#ifdef OS_LINUX
if(Wait_Forever==waitMode)
return pthread_mutex_lock((pthread_mutex_t *)&mutex_handle);
else if(EIS_QUEUE_SEM_NOWAIT==waitMode)
return pthread_mutex_trylock((pthread_mutex_t *)&mutex_handle);

#endif
#ifdef OS_VXWORKS
ipanel_porting_sem_wait(mutex_handle,waitMode,milliSecsToWait);
#endif
#ifdef OS_WIN32
int ret = EIS_OS_QUEUE_SEM_STATUS_UNAVAILABLE;
switch (waitMode)
{
case EIS_QUEUE_SEM_WAIT:

if ( 0==WaitForSingleObject((HANDLE)mutex_handle,INFINITE))
{
ret = EIS_OS_QUEUE_SEM_STATUS_AVAILABLE;
}
break;

case EIS_QUEUE_SEM_NOWAIT:
if (0==WaitForSingleObject((HANDLE)mutex_handle,0))
{
ret = EIS_OS_QUEUE_SEM_STATUS_AVAILABLE;
}
break;

default:
ret = EIS_OS_QUEUE_SEM_STATUS_UNAVAILABLE;
}
return ret;
#endif
#ifdef OS_20
ipanel_porting_sem_wait(mutex_handle,waitMode,milliSecsToWait);
#endif
}
int ipanel_porting_mutex_release(unsigned int mutex_handle)

{
#ifdef OS_LINUX
return pthread_mutex_unlock((pthread_mutex_t *)&mutex_handle);
#endif
#ifdef OS_VXWORKS
ipanel_porting_sem_release(mutex_handle);
#endif
#ifdef OS_WIN32
return SetEvent((HANDLE)mutex_handle);
#endif
#ifdef OS_20
ipanel_porting_sem_release(mutex_handle);
#endif
}

static void pthreadone()

{

while(1){
#ifdef OS_LINUX
sem_wait(&product_number);
pthread_mutex_lock(&product_mutex);
#else
ipanel_porting_sem_wait(product_number,EIS_QUEUE_SEM_WAIT,0);
ipanel_porting_mutex_wait(product_mutex,Wait_Forever,0);
#endif
TestQueue.queue[TestQueue.procpoint] = serial_flag++;
myprintf("pthreadone Produce %d/n", TestQueue.queue[TestQueue.procpoint]);
TestQueue.procpoint = (TestQueue.procpoint+1)%NUM;
#ifdef OS_LINUX
pthread_mutex_unlock(&product_mutex);
sem_post(&blank_number);
#else
ipanel_porting_mutex_release(product_mutex);
ipanel_porting_sem_release(blank_number);
#endif
ipanel_porting_task_sleep(rand()%MAXTIME);
printf("blank_number=%d,product_number=%d",blank_number,product_number);

}
}

static void pthreadthree()

{

while(1)
{
#ifdef OS_LINUX
sem_wait(&product_number);
pthread_mutex_lock(&product_mutex);
#else
ipanel_porting_sem_wait(product_number,EIS_QUEUE_SEM_WAIT,0);
ipanel_porting_mutex_wait(product_mutex,Wait_Forever,0);
#endif
TestQueue.queue[TestQueue.procpoint] = serial_flag++;
myprintf("pthreadthree Produce %d/n", TestQueue.queue[TestQueue.procpoint]);
TestQueue.procpoint = (TestQueue.procpoint+1)%NUM;
#ifdef OS_LINUX
pthread_mutex_unlock(&product_mutex);
sem_post(&blank_number);
#else
ipanel_porting_mutex_release(product_mutex);
ipanel_porting_sem_release(blank_number);
#endif
ipanel_porting_task_sleep(rand()%MAXTIME);
}
}
static void pthreadtow()
{

while(1)
{
#ifdef OS_LINUX
sem_wait(&blank_number);
pthread_mutex_lock(&blank_mutex);
#else
ipanel_porting_sem_wait(blank_number,EIS_QUEUE_SEM_WAIT,0);
ipanel_porting_mutex_wait(blank_mutex,Wait_Forever,0);
#endif

myprintf("pthreadtow Consume %d/n", TestQueue.queue[TestQueue.conspoint]);
TestQueue.conspoint = (TestQueue.conspoint+1)%NUM;
#ifdef OS_LINUX
pthread_mutex_unlock(&blank_mutex);
sem_post(&product_number);
#else
ipanel_porting_mutex_release(blank_mutex);
ipanel_porting_sem_release(product_number);
#endif

ipanel_porting_task_sleep(rand()%MAXTIME);
}
}
static void pthreadfour()
{

while(1)
{
#ifdef OS_LINUX
sem_wait(&blank_number);
pthread_mutex_lock(&blank_mutex);
#else
ipanel_porting_sem_wait(blank_number,EIS_QUEUE_SEM_WAIT,0);
ipanel_porting_mutex_wait(blank_mutex,Wait_Forever,0);
#endif

myprintf("pthreadfour Consume %d/n", TestQueue.queue[TestQueue.conspoint]);
TestQueue.conspoint = (TestQueue.conspoint+1)%NUM;
#ifdef OS_LINUX
pthread_mutex_unlock(&blank_mutex);
sem_post(&product_number);
#else
ipanel_porting_mutex_release(blank_mutex);
ipanel_porting_sem_release(product_number);
#endif

ipanel_porting_task_sleep(rand()%MAXTIME);
}
}
static void pthreadfive()
{
#ifdef OS_WIN32
static i=0;

struct MSGBUF *piont;

while(1)
{

piont=(struct MSGBUF *)malloc(sizeof(struct MSGBUF));
piont->mtype = i++;
piont->request = i++;
piont->salary = 1000;

ipanel_porting_task_sleep(rand()%MAXTIME);
if((ipanel_porting_queue_send( tidsixID, MY_MSG,piont )) == MYEERROR)
{
myprintf("send_message error/n");

}
else
{
myprintf("send a message /n");
}

ipanel_porting_task_sleep(rand()%MAXTIME);

}
#else
struct MSGBUF msg;
static int i=0;
while(1)
{
msg.mtype = i++; /* Message type must be a positive number! */
msg.request = i++; /* Data element #1 */
msg.salary = 1000; /* Data element #2 (my yearly salary!) */

if((ipanel_porting_queue_send( qid, &msg )) == MYEERROR)
{
myprintf("send_message error/n");

}
else
{
myprintf("send a message /n");
}
ipanel_porting_task_sleep(rand()%MAXTIME);
}
#endif
}

static void pthreadsix()
{
#ifdef OS_WIN32
MSG msg;

PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_REMOVE);
while(true)
{
if(GetMessage(&msg,0,0,0)) //get msg from message queue
{
printf("get a message/n");
switch(msg.message)
{
case MY_MSG:
MyMsg *mymsg = ( MyMsg*)msg.wParam;
myprintf("recv a message %d %d %d /n",mymsg->mtype,mymsg->request,mymsg->salary);
delete[] mymsg;

}
}
else
{
myprintf("No message/n");

}
ipanel_porting_task_sleep(rand()%MAXTIME);
}

#else
MyMsg msg;
while(1)
{
if(MYEERROR==ipanel_porting_queue_recv( qid,(struct mymsgbuf *)&msg,EIS_QUEUE_SEM_NOWAIT,0 ))
{

myprintf("message queue is empty/n");
ipanel_porting_task_sleep(rand()%MAXTIME);
continue;

}
else
{
myprintf("get a messgae msg.mtype=%d msg.request=%d msg.salary=%d/n",msg.mtype,msg.request,msg.salary);
}
ipanel_porting_task_sleep(rand()%MAXTIME);
}
#endif
}
#ifdef OS_LINUX
int main()
#endif
#ifdef OS_WIN32
int main()
#endif
#ifdef OS_VXWORKS
int usrAppInit()
#endif
#ifdef OS_20
int TestApplication(void)
#endif
{

blank_number= ipanel_porting_sem_create(NULL,0,0);
product_number= ipanel_porting_sem_create(NULL,NUM,0);
product_mutex= ipanel_porting_mutex_create();
blank_mutex=ipanel_porting_mutex_create();
qid=ipanel_porting_queue_create(NULL,100,0);

// ipanel_porting_task_sleep(2);
if(qid==MYEERROR)
{
myprintf("can't create queue:/n");
return 0;
}
#ifdef OS_LINUX
tidone = ipanel_porting_task_create(NULL, pthreadone,NULL,0,0 );
if (tidone== 0) {
myprintf("can't create thread/n");
return 0;
}
tidthree= ipanel_porting_task_create(NULL, pthreadthree,NULL,0,0 );
if (tidthree== 0) {
myprintf("can't create thread/n");
return 0;
}

tidtow= ipanel_porting_task_create(NULL, pthreadtow,NULL,0,0 );
if (tidtow== 0) {
myprintf("can't create thread/n");
return 0;
}

tidfour= ipanel_porting_task_create(NULL, pthreadfour,NULL,0,0 );
if (tidfour== 0) {
myprintf("can't create thread/n");
return 0;
}
tidfive= ipanel_porting_task_create(NULL, pthreadfive,NULL,0,0 );
if (tidfive== 0) {
myprintf("can't create thread/n");
return 0;
}
tidsix= ipanel_porting_task_create(NULL, pthreadsix,NULL,0,0 );
if (tidsix== 0) {
myprintf("can't create thread/n");
return 0;
}
#endif
#ifdef OS_VXWORKS
tidone = ipanel_porting_task_create(NULL, pthreadone,NULL,91,0 );
if (tidone== 0) {
myprintf("can't create thread/n");
return 0;
}
tidtow= ipanel_porting_task_create(NULL, pthreadtow,NULL,92,0 );
if (tidtow== 0) {
myprintf("can't create thread/n");
return 0;
}
tidthree= ipanel_porting_task_create(NULL, pthreadthree,NULL,93,0 );
if (tidthree== 0) {
myprintf("can't create thread/n");
return 0;
}
tidfour= ipanel_porting_task_create(NULL, pthreadfour,NULL,94,0 );
if (tidfour== 0) {
myprintf("can't create thread/n");
return 0;
}
tidfive= ipanel_porting_task_create(NULL, pthreadfive,NULL,95,0 );
if (tidfive== 0) {
myprintf("can't create thread/n");
return 0;
}
tidsix= ipanel_porting_task_create(NULL, pthreadsix,NULL,96,0 );
if (tidsix== 0) {
myprintf("can't create thread/n");
return 0;
}
taskSuspend(taskIdSelf());
#endif
#ifdef OS_WIN32
tidone = ipanel_porting_task_create(NULL, pthreadone,NULL,91,0,tidoneID );
if (tidone== 0) {
myprintf("can't create thread/n");
return 0;
}
tidtow= ipanel_porting_task_create(NULL, pthreadtow,NULL,92,0 ,tidtowID);
if (tidtow== 0) {
myprintf("can't create thread/n");
return 0;
}
tidthree= ipanel_porting_task_create(NULL, pthreadthree,NULL,93,0 ,tidthreeID);
if (tidthree== 0) {
myprintf("can't create thread/n");
return 0;
}
tidfour= ipanel_porting_task_create(NULL, pthreadfour,NULL,94,0 ,tidfourID);
if (tidfour== 0) {
myprintf("can't create thread/n");
return 0;
}
tidfive= ipanel_porting_task_create(NULL, pthreadfive,NULL,95,0 ,tidfiveID);
if (tidfive== 0) {
myprintf("can't create thread/n");
return 0;
}
tidsix= ipanel_porting_task_create(NULL, pthreadsix,NULL,96,0 ,tidsixID);
if (tidsix== 0) {
myprintf("can't create thread/n");
return 0;
}

#endif
while(1)
{
myprintf("this main pthread/n");
// pthreadsix();
ipanel_porting_task_sleep(100);
}
return 1;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐