void listTest() { struct Semaphore sem1, sem2, sem3; sem1.id = 1; sem2.id = 2; sem3.id = 3; Dump_All_Semaphore_List(); Add_To_Back_Of_Semaphore_List(&s_semList, &sem1); Add_To_Back_Of_Semaphore_List(&s_semList, &sem3); Add_To_Back_Of_Semaphore_List(&s_semList, &sem2); Dump_All_Semaphore_List(); Remove_From_Semaphore_List(&s_semList, &sem1); Dump_All_Semaphore_List(); }
int Create_Semaphore(char* name, int ival) { struct Semaphore* sema = (&s_semaphoreList)->head; static ulong_t sid = 1; //find sem by sname while (sema != 0) { if (strcmp(sema->name, name) == 0) { //Print("exist sema->name: %s, %d\n", sema->name, sema->sid); return sema->sid; } sema = Get_Next_In_Semaphore_List(sema); } //if there is no sem, create sem sema = (struct Semaphore*)Malloc(sizeof(struct Semaphore)); sema->sid = sid++; memcpy(sema->name, name, 25); sema->count = ival; //sema->refCount = 1; Mutex_Init(&(sema->mutex)); Cond_Init(&(sema->cond)); Add_To_Back_Of_Semaphore_List(&s_semaphoreList, sema); //Print("new sema->name: %s, %d\n", sema->name, sema->sid); return sema->sid; }
int Create_Semaphore (const char *name, int ival) { // struct Semaphore *pSem = 0, *pSemNew = NULL, sem; struct Semaphore *pSem; int id = 0; //Print("ARGS. name: %s, ival: %d\n", name, ival); /* * Obtiene el primer elemento de la lista y lo * recorre hasta el final para ver que ID le asigna * al semáforo solicitado (poco eficiente). */ pSem = Get_Front_Of_Semaphore_List(&s_semList); // Print("LISTA ARRANCA EN %p\n", pSem); while (pSem != 0) { KASSERT(pSem != Get_Next_In_Semaphore_List(pSem)); if (strcmp(pSem->name, name) == 0) { pSem->nref++; #ifdef DEBUG Print("Semaforo %s ya existe con id %d.\n", name, pSem->id); #endif Print("Semaforo %s ya existe con id %d.\n", name, pSem->id); return pSem->id; } pSem = Get_Next_In_Semaphore_List(pSem); id++; } /* La cantidad máxima de semáforos está siendo utilizada. */ if (id >= MAX_SEM) return -1; pSem = Malloc(sizeof (struct Semaphore)); // Print("ANTES: "); // Dump_All_Semaphore_List(); // sem.id = id; pSem->id = id; /* Define nombre del semáforo */ // snprintf(sem.name, sizeof(sem.name), "%s", name); snprintf(pSem->name, NAMESIZE, "%s", name); /* Define valor inicial del semáforo */ // sem.value = ival; pSem->value = ival; /* Referencia a 1 */ // sem.nref = 1; pSem->nref = 1; /* pSemNew = Malloc(sizeof (struct Semaphore)); pSemNew = &sem; */ /* Agrega semáforo a la lista */ // Add_To_Back_Of_Semaphore_List(&s_semList, &sem); // Add_To_Back_Of_Semaphore_List(&s_semList, pSemNew); Add_To_Back_Of_Semaphore_List(&s_semList, pSem); /* sem = Get_Front_Of_Semaphore_List(&s_semList); while (sem != NULL) { KASSERT(sem != Get_Next_In_Semaphore_List(sem)); Print("ESTE ID ya esta: %d\n", sem->id); sem = Get_Next_In_Semaphore_List(sem); } */ Print("DESPUES: "); Dump_All_Semaphore_List(); #ifdef DEBUG Print("ID: %d\n", id); #endif return id; }