00001 /* 00002 Copyright 2003 by Marc J. Rochkind. All rights reserved. 00003 May be copied only for purposes and under conditions described 00004 on the Web page www.basepath.com/aup/copyright.htm. 00005 00006 The Example Files are provided "as is," without any warranty; 00007 without even the implied warranty of merchantability or fitness 00008 for a particular purpose. The author and his publisher are not 00009 responsible for any damages, direct or incidental, resulting 00010 from the use or non-use of these Example Files. 00011 00012 The Example Files may contain defects, and some contain deliberate 00013 coding mistakes that were included for educational reasons. 00014 You are responsible for determining if and how the Example Files 00015 are to be used. 00016 00017 */ 00018 #ifndef _UXPOSIXIPC_HPP_ 00019 #define _UXPOSIXIPC_HPP_ 00020 00021 #if _POSIX_MESSAGE_PASSING > 0 00022 #include <mqueue.h> 00023 #else 00024 typedef void *mqd_t; 00025 struct mq_attr { 00026 long mq_flags; 00027 long mq_maxmsg; 00028 long mq_msgsize; 00029 long mq_curmsgs; 00030 }; 00031 #endif 00032 #if _POSIX_SEMAPHORES > 0 00033 #include <semaphore.h> 00034 #else 00035 typedef int sem_t; 00036 #endif 00037 #include <sys/mman.h> 00038 00039 namespace Ux { 00040 00041 /** 00042 \ingroup Ux 00043 */ 00044 class PosixMsg : public Base { 00045 protected: 00046 mqd_t mqd; 00047 public: 00048 PosixMsg(mqd_t m = (mqd_t)-1) 00049 : mqd(m) 00050 { } 00051 void close(void); 00052 void getattr(struct mq_attr *attr); 00053 void notify(const struct sigevent *ep); 00054 void open(const char *name, int flags, mode_t perms = PERM_FILE, struct mq_attr *attr = NULL); 00055 ssize_t receive(char *msg, size_t msgsize, unsigned *priorityp = NULL); 00056 void send(const char *msg, size_t msgsize, unsigned priority = 0); 00057 void setattr(const struct mq_attr *attr, struct mq_attr *oldattr = NULL); 00058 ssize_t timedreceive(char *msg, size_t msgsize, unsigned *priorityp, const struct timespec *tmout); 00059 void timedsend(const char *msg, size_t msgsize, unsigned priority, const struct timespec *tmout); 00060 void unlink(const char *name); 00061 }; 00062 00063 /** 00064 \ingroup Ux 00065 */ 00066 class PosixShm : public File { 00067 public: 00068 void open(const char *name, int flags, mode_t perms = PERM_FILE); 00069 void unlink(const char *name); 00070 void * mmap(size_t len, void *addr = NULL, int prot = PROT_READ | PROT_WRITE, 00071 int flags = MAP_SHARED, off_t off = 0); 00072 void munmap(void *addr, size_t len); 00073 }; 00074 00075 /** 00076 \ingroup Ux 00077 */ 00078 class PosixSem : public Base { 00079 protected: 00080 sem_t *sem; 00081 00082 public: 00083 PosixSem(sem_t *s = NULL) 00084 : sem(s) 00085 { } 00086 void close(void); 00087 void destroy(void); 00088 void getvalue(int *valuep); 00089 void init(int pshared = 1, unsigned value = 0); 00090 void open(const char *name, int flags, mode_t perms = PERM_FILE, unsigned value = 0); 00091 void post(void); 00092 void timedwait(const struct timespec *time); 00093 void trywait(void); 00094 void unlink(const char *name); 00095 void wait(void); 00096 }; 00097 00098 } // namespace 00099 00100 #endif // _UXPOSIXIPC_HPP_