problem with a semaphore and thread example

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
egorive
Posts: 24
Joined: Thu Jun 22, 2006 11:50 pm

problem with a semaphore and thread example

Post by egorive »

Hi, Im writting a thread+semaphore example for me to understand this little world but i have a silly problem. Here is the code:

#include <pspkernel.h>
#include <pspdebug.h>
#include <pspctrl.h>
#include <stdlib.h>
#include <string.h>

/* Define the module info section */
PSP_MODULE_INFO("threadstatus", 0, 1, 1);

/* Define the main thread's attribute value (optional) */
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);

/* Define printf, just to make typing easier */
#define printf pspDebugScreenPrintf

int Length=5; //This is the length of the array.
int Quantity=20; //Number of elements I want to put in (and take of) the box.
//int Box[Length]; //The more important share element. The array I take and put the elements.<---------------
int First=0; //This variable is to know the position of the array Box we must take an element.
int Last=0; //This variable is to know the next position of the array Box a have to put an element.
int thereAreElementsSemaID; //This Sema control the limits of the number of elements in the array.
int thereAreSpaceSemaID; //This Sema control the limits of the number of spaces in the array.
int boltSemaID; // This Sema is to access in exclusive way to the shared variables (in this case Box, Last, First)

void Put(int X);
void Take(int *pX);
int Producer (SceSize args, void *argp);
int Consumer(SceSize args, void *argp);


/* Pone en entero en la cola, deteniéndose si no cabe */

void Put(int X) {
sceKernelWaitSema(thereAreSpaceSemaID,1,0);
sceKernelWaitSema(boltSemaID,1,0);
// Box[Last] = X; <-----------------------------------------------------------
Last = (Last + 1) % Length;
sceKernelSignalSema(boltSemaID, 1);
sceKernelSignalSema(thereAreElementsSemaID, 1);
}

/* Toma un entero de la cola, deteniéndose si no hay */

void Take(int *pX) {
sceKernelWaitSema(thereAreElementsSemaID,1,0);
sceKernelWaitSema(boltSemaID,1,0);
//*pX = Box[First]; <-----------------------------------------------------------
First = (First + 1) % Length;
sceKernelSignalSema(boltSemaID, 1);
sceKernelSignalSema(thereAreSpaceSemaID, 1);
}


/* Proceso que produce enteros y los encola void *pId*/

int Producer (SceSize args, void *argp) {

int i;
for (i = 1; i <= Quantity; i++) {
Put(i);// + Id);
// printf("---- Producer produce %6d\n", i);
printf("producer %d\n", i);
sceKernelDelayThread (1000000); //espera un segundo y luego se despierta
}
return 0;
}

/* Proceso que consume enteros */

int Consumer(SceSize args, void *argp) {

int Dato;
int j;
for (j = 1; j <= Quantity; j++) {
Take(&Dato);
// printf("**** Consumer consume %6d\n", Dato);
printf("consumer \n");
sceKernelDelayThread (5000000); //espera un segundo y luego se despierta

}
return 0;
}


int main(int argc, char *argv[]) {
pspDebugScreenInit();
SceCtrlData pad;
int i;
SceUID thidProducer;
SceUID thidConsumer;

thereAreElementsSemaID = sceKernelCreateSema("thereAreElementsSemaID", 0, 0, Length, 0);
thereAreSpaceSemaID = sceKernelCreateSema("thereAreSpaceSemaID", 0, Length, Length, 0);
boltSemaID = sceKernelCreateSema("boltSemaID", 0, 1, 1, 0);


unsigned Id_Producer =1000;
unsigned Id_Consumer =5000;


thidProducer = sceKernelCreateThread("ProducerThread",Producer,20,16384,1,NULL);
thidConsumer = sceKernelCreateThread("ConsumerThread",Consumer,21,16384,1,NULL);

sceKernelStartThread(thidProducer,2,&Id_Producer);
sceKernelStartThread(thidConsumer,2,&Id_Consumer);





while(1){

sceCtrlReadBufferPositive(&pad, 1);

if (pad.Buttons != 0){
pspDebugScreenSetXY (0,24);
printf("pulsando tecla: %X",pad.Buttons);

if (pad.Buttons & PSP_CTRL_SQUARE){
sceKernelTerminateThread (thidProducer);
sceKernelTerminateThread (thidConsumer);
sceKernelExitGame();
}
}

for(i=0; i<5; i++) {
sceDisplayWaitVblankStart(); //esperar a que la pantalla haya sido actualizada 5 veces para evitar desincronización.
}
// pspDebugScreenClear();
}

return 0;
}





By this way it compiles, but I want it to compile with the lines ------ active. That is to say that I want to create an array called box but I don't know why it don't let me (it must be a silly thing :-(). Please, help me !! If you try it the way to get out is pressing square (see the code).

I pass you the make file too:



TARGET = HEBRAS
OBJS = main.o
LIBS = $(PLAYER_LIBS) -lpspaudiolib -lpspaudio -lpsphprm -lpsphprm_driver -lpspgu -lpspgum
CFLAGS = -O2 -G0 -Wall
CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti
ASFLAGS = $(CFLAGS)
EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = Prueba de Hebras


PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/lib/build.mak
jimparis
Posts: 1145
Joined: Fri Jun 10, 2005 4:21 am
Location: Boston

Post by jimparis »

C99 variable-size arrays can't be defined at file scope; you will need to move that declaration into a function, make it a fixed size, or use malloc to allocate the space at runtime.
egorive
Posts: 24
Joined: Thu Jun 22, 2006 11:50 pm

that meens

Post by egorive »

Wow I'm really fool. Thank you so much!.
Post Reply