00001
00039 #ifndef _RTAI_SCB_H
00040 #define _RTAI_SCB_H
00041
00042 #include <rtai_shm.h>
00043 #include <asm/rtai_atomic.h>
00044
00045 #define SCB ((void *)(scb))
00046 #define SIZE ((volatile int *)scb)[-3]
00047 #define FBYTE ((volatile int *)scb)[-2]
00048 #define LBYTE ((volatile int *)scb)[-1]
00049 #define HDRSIZ (3*sizeof(int))
00050
00051 struct task_struct;
00052
00090 RTAI_PROTO(void *, rt_scb_init, (unsigned long name, int size, unsigned long suprt))
00091 {
00092 void *scb;
00093 scb = suprt > 1000 ? (void *)suprt : rt_shm_alloc(name, size + HDRSIZ + 1, suprt);
00094 if (scb && !atomic_cmpxchg((int *)scb, 0, name)) {
00095 ((int *)scb)[1] = ((int *)scb)[2] = 0;
00096 ((int *)scb)[0] = size + 1;
00097 } else {
00098 while (!((int *)scb)[0]);
00099 }
00100 return scb ? scb + HDRSIZ : 0;
00101 }
00102
00126 RTAI_PROTO(int, rt_scb_delete, (unsigned long name))
00127 {
00128 return rt_shm_free(name);
00129 }
00130
00145 RTAI_PROTO (int, rt_scb_bytes, (void *scb))
00146 {
00147 int size = SIZE, fbyte = FBYTE, lbyte = LBYTE;
00148 return (lbyte >= fbyte ? lbyte - fbyte : size + lbyte - fbyte);
00149 }
00150
00163 RTAI_PROTO(int, rt_scb_get, (void *scb, void *msg, int msg_size))
00164 {
00165 int size = SIZE, fbyte = FBYTE, lbyte = LBYTE;
00166 if (msg_size > 0 && (lbyte >= fbyte ? lbyte - fbyte : size + lbyte - fbyte) >= msg_size) {
00167 int tocpy;
00168 if ((tocpy = size - fbyte) > msg_size) {
00169 memcpy(msg, SCB + fbyte, msg_size);
00170 FBYTE = fbyte + msg_size;
00171 } else {
00172 memcpy(msg, SCB + fbyte, tocpy);
00173 if ((msg_size -= tocpy)) {
00174 memcpy(msg + tocpy, SCB, msg_size);
00175 }
00176 FBYTE = msg_size;
00177 }
00178 return 0;
00179 }
00180 return msg_size;
00181 }
00182
00195 RTAI_PROTO(int, rt_scb_evdrp, (void *scb, void *msg, int msg_size))
00196 {
00197 int size = SIZE, fbyte = FBYTE, lbyte = LBYTE;
00198 if (msg_size > 0 && (lbyte >= fbyte ? lbyte - fbyte : size + lbyte - fbyte) >= msg_size) {
00199 int tocpy;
00200 if ((tocpy = size - fbyte) > msg_size) {
00201 memcpy(msg, SCB + fbyte, msg_size);
00202 } else {
00203 memcpy(msg, SCB + fbyte, tocpy);
00204 if ((msg_size -= tocpy)) {
00205 memcpy(msg + tocpy, SCB, msg_size);
00206 }
00207 }
00208 return 0;
00209 }
00210 return msg_size;
00211 }
00212
00225 RTAI_PROTO(int, rt_scb_put, (void *scb, void *msg, int msg_size))
00226 {
00227 int size = SIZE, fbyte = FBYTE, lbyte = LBYTE;
00228 if (msg_size > 0 && (lbyte >= fbyte ? size - (lbyte - fbyte) : lbyte - fbyte) > msg_size) {
00229 int tocpy;
00230 if ((tocpy = size - lbyte) > msg_size) {
00231 memcpy(SCB + lbyte, msg, msg_size);
00232 LBYTE = lbyte + msg_size;
00233 } else {
00234 memcpy(SCB + lbyte, msg, tocpy);
00235 if ((msg_size -= tocpy)) {
00236 memcpy(SCB, msg + tocpy, msg_size);
00237 }
00238 LBYTE = msg_size;
00239 }
00240 return 0;
00241 }
00242 return msg_size;
00243 }
00244
00245 #endif