00001
00014 #ifndef BUFFER_H
00015 #define BUFFER_H
00016
00018 namespace SMP{
00019
00020
00022 const int BUF_MAX = 1024;
00023
00025 typedef unsigned char byte;
00026
00032 struct packet
00033 {
00034 int header;
00035 unsigned size;
00036 unsigned start_time;
00037 unsigned duration;
00038 char priority;
00039 void *data;
00040 };
00041
00043 enum { NORMAL = 0, RESET, INPUT_EOF, OUTPUT_EOF};
00045 enum { DATA_BUFFER = 0, SIGNAL_BUFFER};
00046
00047
00051 class SMPBufferBase
00052 {
00054 friend class SMPKernel;
00055
00056 protected:
00058 int id;
00059
00060
00061
00062
00064 int buf_src;
00066 int buf_src_port;
00067
00071 int *buf_dest;
00075 int *buf_dest_port;
00077 int num_dest;
00078 public:
00079 SMPBufferBase()
00080 : buf_dest(NULL), buf_dest_port(NULL)
00081 { }
00082 };
00083
00084
00085
00090 class SMPBuffer : public SMPBufferBase
00091 {
00093 friend class SMPKernel;
00095 enum op{ READ=0, WRITE };
00096 private:
00097
00098 #ifdef USE_PTHREAD
00099
00100 pthread_mutex_t mutex;
00101
00102 pthread_mutex_t cond_mutex_w, cond_mutex_r;
00103
00104 pthread_cond_t cond_w, cond_r;
00105
00106 bool cond_met_w, cond_met_r;
00107 #endif
00108
00109
00110
00111
00113 long size;
00115 long used_size;
00117 long *used;
00119 byte *data;
00120
00122 int status;
00123
00125 op last_op;
00126
00127
00128
00129
00131 long w_flag;
00133 long *r_flag;
00134
00140 void reset_flag(void);
00144 long get_r_flag(void);
00145
00146 public:
00148 SMPBuffer(void);
00149
00154 SMPBuffer(size_t buf_size);
00155
00157 ~SMPBuffer(void);
00158
00159
00161 void initialize();
00162
00167 inline void set_status(int b_status);
00171 inline int get_status(void);
00172
00177 inline size_t available(void);
00178
00180 ssize_t read_space(int fid);
00181
00186 inline size_t occuppied(void);
00191 inline size_t occuppied(int fid);
00192
00199 int write(const void *buff, size_t nbytes);
00200
00208 int read(void *buff, size_t nbytes, int fid=0);
00209
00216 void print_status(int nbytes=0);
00217
00218
00219 #ifdef USE_PTHREAD
00220
00223 void cond_wait_w();
00224 void cond_broadcast_w(bool trylock=false);
00225 void cond_wait_r();
00226 void cond_broadcast_r(bool trylock=false);
00227
00228 #endif
00229
00230 };
00231
00232 void SMPBuffer::set_status(int b_status)
00233 {
00234 status = b_status;
00235
00236 }
00237
00238 int SMPBuffer::get_status(void)
00239 {
00240 return status;
00241 }
00242
00243 size_t SMPBuffer::available(void)
00244 {
00245 return size - used_size;
00246 }
00247
00248 size_t SMPBuffer::occuppied(void)
00249 {
00250 return used_size;
00251 }
00252
00253 size_t SMPBuffer::occuppied(int fid)
00254 {
00255
00256 int index;
00257 for(index = 0; index < num_dest ; index++)
00258 if(buf_dest[index] == fid)
00259 break;
00260 return used[index];
00261 }
00262
00263
00264
00268 class SMPSignalBuffer : public SMPBufferBase
00269 {
00270
00271 };
00272
00273
00274
00275 }
00276
00277 #endif