3 volatile int headoffset, tailoffset;
6 pthread_mutexattr_t qlockattr;
9 #define QSIZE 2048 //2 KB
12 extern char bigarray[16*1024*1024];
14 #define LOGEVENT(x) { \
22 void queueInit(void) {
23 /* Intitialize primary queue */
26 memory=malloc(QSIZE+sizeof(int)); //leave space for -1
27 pthread_mutexattr_init(&qlockattr);
28 pthread_mutexattr_settype(&qlockattr, PTHREAD_MUTEX_RECURSIVE_NP);
29 pthread_mutex_init(&qlock, &qlockattr);
30 pthread_cond_init(&qcond, NULL);
33 void * getmemory(int size) {
34 int tmpoffset=headoffset+size+sizeof(int);
35 if (tmpoffset>QSIZE) {
36 //Wait for tail to go past end
37 tmpoffset=size+sizeof(int);
38 if (headoffset<tailoffset) {
39 pthread_cond_signal(&qcond); //wake the other thread up
42 //Wait for tail to go past new start
43 if (tailoffset<=tmpoffset) {
44 pthread_cond_signal(&qcond); //wake the other thread up
47 *((int *)(memory+headoffset))=-1; //safe because we left space
48 *((int*)memory)=size+sizeof(int);
49 return memory+sizeof(int);
51 if (headoffset<tailoffset&&tailoffset<=tmpoffset) {
52 pthread_cond_signal(&qcond); //wake the other thread up
55 *((int*)(memory+headoffset))=size+sizeof(int);
56 return memory+headoffset+sizeof(int);
60 void movehead(int size) {
61 int tmpoffset=headoffset+size+sizeof(int);
62 if (tmpoffset>QSIZE) {
63 headoffset=size+sizeof(int);
66 pthread_cond_signal(&qcond); //wake the other thread up
70 while(tailoffset==headoffset) {
73 pthread_mutex_lock(&qlock);
74 if (tailoffset==headoffset)
75 pthread_cond_wait(&qcond, &qlock);
76 pthread_mutex_unlock(&qlock);
79 if (*((int *)(memory+tailoffset))==-1) {
80 tailoffset=0; //do loop
83 return memory+tailoffset+sizeof(int);
90 if (*((int *)(memory+tmp))==-1) {
93 while(tmp!=headoffset) {
95 tmp=tmp+*((int *)(memory+tmp));
96 if (tmp>QSIZE|| (*((int *)(memory+tmp))==-1)) {
103 void incmulttail(int num) {
105 for(i=0; i<num; i++) {
106 int tmpoffset=tailoffset+*((int *)(memory+tailoffset));
110 tailoffset=tmpoffset;
120 int tmpoffset=tailoffset+*((int *)(memory+tailoffset));
124 tailoffset=tmpoffset;