 |
forums.ps2dev.org Homebrew PS2, PSP & PS3 Development Discussions
|
| View previous topic :: View next topic |
| Author |
Message |
egorive
Joined: 22 Jun 2006 Posts: 24
|
Posted: Mon Jun 25, 2007 8:36 am Post subject: problem with a semaphore and thread example |
|
|
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 |
|
| Back to top |
|
 |
jimparis
Joined: 10 Jun 2005 Posts: 1179 Location: Boston
|
Posted: Mon Jun 25, 2007 8:53 am Post subject: |
|
|
| 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. |
|
| Back to top |
|
 |
egorive
Joined: 22 Jun 2006 Posts: 24
|
Posted: Mon Jun 25, 2007 9:02 am Post subject: that meens |
|
|
| Wow I'm really fool. Thank you so much!. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|