33 #include <semaphore.h>
39 #include <SUNRT/msgQLib.h>
40 #include <SUNRT/ipcQLib.hh>
41 #include <sys/types.h>
47 #include <UNIX/ThreadsMsgQueue.hh>
56 typedef unsigned int UINT32;
57 typedef unsigned short UINT16;
58 typedef unsigned char UINT8;
62 void *memcpy32(UINT32 *dst, UINT32 *src,
int cou);
73 #define pMain(a) a(char *args)
74 #define pGrabArgs() int argc=1; \
76 char *__next_ = strtok(args," "); \
77 argv[0] = "unknown"; \
79 argv[argc++] = __next_; \
80 __next_ = strtok(NULL, " "); \
83 #define pMain(a) main(int argc, char *argv[])
87 inline void pSleep(
int secs)
97 inline void pTaskDelay(
int ticks)
102 usleep(ticks * 10000);
113 typedef pthread_t pTASKID;
118 inline pTASKID pTaskSelf()
121 return pthread_self();
127 inline pTASKID pTaskSpawn(
char *name,
int priority,
void *f(
void *),
void *args)
133 pthread_attr_init(&attr);
152 if(pthread_create(&tid, &attr, f, args))
155 LOG(NOTE,
"Can't setup custom attributes for task %s, not setting priority",name,0,0,0,0);
156 if(pthread_create(&tid, NULL, f, args)) {
157 LOG(CRIT,
"Error creating %s thread: (%s)", name, strerror(errno));
163 tid = taskSpawn(name,priority,0,4096,(FUNCPTR)f,(
int)args,0,0,0,0,0,0,0,0,0);
169 inline int pChangePriority(
int prio)
187 pTASKID tid = taskIdSelf();
189 taskPriorityGet(tid, &old);
190 taskPrioritySet(tid, prio);
195 inline int pGetMaxPriority()
198 return sched_get_priority_max(SCHED_FIFO);
204 inline void pLockAllMemory()
209 if(mlockall(MCL_CURRENT | MCL_FUTURE) == -1)
211 LOG(DBG,
"Error locking all memory",0,0,0,0,0);
216 inline void pTaskDelete(pTASKID tid)
219 LOG(WARN,
"thread cancelation not supported in solaris");
236 typedef sema_t* SEM_ID;
237 typedef mutex_t* MUX_ID;
239 typedef sem_t* SEM_ID;
240 typedef pthread_mutex_t* MUX_ID;
244 typedef SEM_ID MUX_ID;
249 #define pSEM_PROTO(semName) sema_t rp_##semName; SEM_ID semName = &rp_##semName
250 #define pMUX_PROTO(muxName) mutex_t rp_##muxName; MUX_ID muxName = &rp_##muxName
252 #define pSEM_PROTO(semName) sem_t rp_##semName; SEM_ID semName = &rp_##semName
253 #define pMUX_PROTO(muxName) pthread_mutex_t rp_##muxName; MUX_ID muxName = &rp_##muxName
255 #define pSEM_PROTO(semName) SEM_ID semName
256 #define pMUX_PROTO(muxName) pSEM_PROTO(muxName)
261 inline void pSemCInit(SEM_ID &sem,
int value)
266 ret = sema_init(sem, value, USYNC_THREAD, NULL);
268 ret = sem_init(sem,0,value);
273 LOG(CRIT,
"Error creating semaphore (%s)",strerror(errno));
277 sem = semCCreate(SEM_Q_PRIORITY, value);
282 inline void pSemBInit(SEM_ID &sem,
int value)
285 pSemCInit(sem, value);
287 sem = semBCreate(SEM_Q_PRIORITY, (value == 0) ? SEM_EMPTY : SEM_FULL );
293 inline void pMuxInit(MUX_ID &mux)
296 mutex_init(mux, USYNC_THREAD, NULL);
297 #elif defined unix // linux
298 pthread_mutex_init(mux, NULL);
300 mux = semBCreate(SEM_Q_PRIORITY, SEM_FULL);
306 inline bool pSemTryTake(SEM_ID sem)
311 ret = sema_trywait(sem);
313 ret = sem_trywait(sem);
315 if(ret == -1)
return false;
318 ret = semTake(sem, 0);
319 if(ret == ERROR)
return false;
324 inline bool pSemTake(SEM_ID sem)
332 iret = sema_wait(sem);
334 iret = sem_wait(sem);
336 if((iret) && (errno != EINTR)) {
337 LOG(CRIT,
"semTake error %s",strerror(errno));
342 ret = semTake(sem, 100);
352 inline void pSemGive(SEM_ID sem)
363 inline void pMuxLock(MUX_ID mux)
368 pthread_mutex_lock(mux);
370 semTake(mux,WAIT_FOREVER);
376 #if defined(__linux__) || defined(__APPLE__)
378 inline bool pMuxTryLock(MUX_ID mux)
384 int ret = pthread_mutex_trylock(mux);
385 if(ret == 0)
return true;
388 semTake(mux,WAIT_FOREVER);
394 inline void pMuxUnlock(MUX_ID mux)
400 pthread_mutex_unlock(mux);
420 #if !defined(IPCQLIB) && !defined(THREAD_MSG_QUEUE) && !defined(SYSVQLIB)
425 #if defined(__linux__) || defined(__APPLE__)
426 #define THREAD_MSG_QUEUE
432 #undef THREAD_MSG_QUEUE
437 #define QUELIB_STRING "vxworks"
438 #elif defined(IPCQLIB)
439 #define QUELIB_STRING "ipcqlib"
440 #elif defined(THREAD_MSG_QUEUE)
441 #define QUELIB_STRING "thrMsgQ"
442 #elif defined(SYSVQLIB)
443 #define QUELIB_STRING "sysvqlib"
445 #define QUELIB_STRING "noQs"
448 #ifdef THREAD_MSG_QUEUE
450 #define pALLOC_MSGQARRAY(name) thrMsgQueue<ic_msg> *name[256]
451 #define pMSGQARRAY_PROTO(name) thrMsgQueue<ic_msg> *name[]
455 #elif defined(IPCQLIB)
456 #define pALLOC_MSGQARRAY(name) void *name
457 #define pMSGQARRAY_PROTO(name) void *name
458 inline void pSetupMsgQArray(
void *) {};
459 #elif defined(SYSVQLIB)
460 #define pALLOC_MSGQARRAY(name) void *name
461 #define pMSGQARRAY_PROTO(name) void *name
462 inline void pSetupMsgQArray(
void *) {};
464 extern MSG_Q_ID *pMsgQArray;
465 #define pALLOC_MSGQARRAY(name) MSG_Q_ID name[256]
466 #define pMSGQARRAY_PROTO(name) MSG_Q_ID *name
467 inline void pSetupMsgQArray(MSG_Q_ID *qarray) {
470 LOG(DBG,
"pMsgQArray = 0x%x",pMsgQArray,0,0,0,0);
474 inline bool pMsgQValid(
int node,
int task)
476 #ifdef THREAD_MSG_QUEUE
477 if(pMsgQArray[task] == NULL)
return false;
479 #elif defined(IPCQLIB)
480 ipcQClass *c = ipcQClass::find(node, task, 1);
484 #elif defined(SYSVQLIB)
485 if(msgQTest(task))
return true;
488 if(pMsgQArray[task] == NULL)
return false;
493 inline bool pMsgQCreate(
int task,
int size,
int nmsgs,
int mynode=0)
495 #ifdef THREAD_MSG_QUEUE
496 if(pMsgQArray[task] != NULL)
501 #elif defined(IPCQLIB)
503 if (ipcQClass::daqTasks[task] != NULL)
505 LOG(NOTE,
"Task %d already used",task,0,0,0,0);
510 LOG(NOTE,
"Task %d created",task,0,0,0,0);
513 #elif defined(SYSVQLIB)
514 if(msgQCreate(task, nmsgs, size) == 0)
return true;
519 pMsgQArray[task] = msgQCreate(nmsgs, size, MSG_Q_PRIORITY);
522 if(pMsgQArray[task] == NULL) {
523 LOG(CRIT,
"Can't create message queue %d (%s)",task, strerror(errno),0,0,0);
527 LOG(DBG,
"(pMsgQArray = 0x%x) msq %d = 0x%x",pMsgQArray,task,pMsgQArray[task],0,0);
532 inline void pMsgQDelete(
int task)
535 msgQDelete(pMsgQArray[task]);
536 pMsgQArray[task] = NULL;
542 inline int pMsgQSend(
int task,
void *msg,
int size,
int node=0)
544 #ifdef THREAD_MSG_QUEUE
545 int n=pMsgQArray[task]->free();
547 LOG(WARN,
"The queue %d is nearly full %d free",task,n,0,0,0);
550 LOG(CRIT,
"The queue %d is full, dropping message",task,0,0,0,0);
553 return pMsgQArray[task]->send((
ic_msg *)msg);
554 #elif defined(IPCQLIB)
561 c = ipcQClass::find(node, task, 1);
566 LOG(
"ERR",
"Queue %d/0x%x does not exist",task,node,0,0,0);
570 if(c->send((
void *)msg, size, 0) == size)
return 0;
574 #elif defined(SYSVQLIB)
575 return msgQSend(task, (
char *)msg, size);
577 if(msgQSend(pMsgQArray[task], (
char *)msg, size, NO_WAIT, MSG_PRI_NORMAL) == OK)
return 0;
582 inline int pMsgQReceive(
int task,
void *msg,
int size,
int node=0)
584 #ifdef THREAD_MSG_QUEUE
585 if(pMsgQArray[task]->receive((
ic_msg *)msg))
592 #elif defined(IPCQLIB)
599 c = ipcQClass::find(node, task, 1);
602 LOG(
"ERR",
"Queue %d/0x%x does not exist",task,node,0,0,0);
605 return c->receive((
void *)msg, size, 1);
607 #elif defined(SYSVQLIB)
610 ret = msgQReceive(task, (
char *)msg, size);
611 }
while((ret == -1) && (errno == EINTR));
615 return msgQReceive(pMsgQArray[task], (
char *)msg, size, WAIT_FOREVER);
619 inline void pMemcpy(
void *x,
void *y,
int bytes)