コード例 #1
0
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;
	
}
コード例 #2
0
int P(int sid)
{
	struct Semaphore* sema = (&s_semaphoreList)->head;
	struct Mutex* mutex;
	struct Condition* cond;

	//find sem by sid
	while (sema != 0)
	{
		if (sema->sid == sid)
		{
			mutex = &(sema->mutex); // important
			cond = &(sema->cond);

			Enable_Interrupts();
			Mutex_Lock(mutex);
			while(sema->count <= 0)
			{
				Cond_Wait(cond, mutex);
				//Print("WAKE UP!");
			}
			sema->count--;
			Mutex_Unlock(mutex);
			Disable_Interrupts();
			
			return 0;
		}
		sema = Get_Next_In_Semaphore_List(sema);
	}
	
	return -1;
}
コード例 #3
0
int V(int sid)
{
	struct Semaphore* sema = (&s_semaphoreList)->head;
	struct Mutex* mutex;
	struct Condition* cond;

	//find sem by sid
	while (sema != 0)
	{
		if (sema->sid == sid)
		{
			mutex = &(sema->mutex);
			cond = &(sema->cond);
			
			Enable_Interrupts();
			Mutex_Lock(mutex);
			sema->count = sema->count + 1;
			//Print("wait queue : %d", cond->waitQueue);
			Cond_Broadcast(cond);
			Mutex_Unlock(mutex);
			Disable_Interrupts();
			
			return 0;
		}
		sema = Get_Next_In_Semaphore_List(sema);
	}
	
	return -1;
}
コード例 #4
0
ファイル: sem.c プロジェクト: rjrpaz/os-implementation
/*
 * Encuentra un semaforo a partir del ID
 * Params:
 *   id: ID del semaforo que intenta ubicar
 *   sem: puntero donde almacena el semaforo encontrado
 * Returns: 0 si tiene exito o -1 si falla
 */
struct Semaphore *Get_Sem_By_Id(int id)
{
 //   int *semaphores = NULL;
//    bool acceso = false;
    struct Semaphore *sem;

//    sem = NULL;

    Dump_All_Semaphore_List();
Print("Busco SEM %d\n", id);

    /* Chequea que el thread tenga permisos para acceder al semáforo solicitado */
/*
    semaphores = (int *) g_currentThread->userContext->semaphores;
    while (*semaphores != -1) {
        if (*semaphores == id) {
            acceso = true;
            break;
        }
        semaphores++;
    }
    if (! acceso)
        return -1;
*/

    /* Ubico al semáforo solicitado */
    sem = Get_Front_Of_Semaphore_List(&s_semList);
    while (sem != NULL) {
        KASSERT(sem != Get_Next_In_Semaphore_List(sem));
//Print("Comparo con el de direcci�n %p\n", sem);
        if (sem->id == id) {
//Print("ES ESTE\n");
            break;
	    }
        sem = Get_Next_In_Semaphore_List(sem);
    }
//Print("ENCONTRADO. Es el de direccion %p. Se llama %s\n", sem, sem->name);

Print("ENCONTRADO. Es el de direccion %p. Se llama %s\n", sem, sem->name);
    return sem;
}
コード例 #5
0
ファイル: sem.c プロジェクト: rjrpaz/os-implementation
void Dump_All_Semaphore_List(void)
{
    struct Semaphore *sem;
    int count = 0;

    sem = Get_Front_Of_Semaphore_List(&s_semList);
//    Print("LISTA ARRANCA EN %p\n", sem);
//    Dump_All_Thread_List();

    while (sem != 0) {
        ++count;
        Print("<%lx> <%d>(%s) ", (ulong_t) sem, sem->id, sem->name);
        sem = Get_Next_In_Semaphore_List(sem);
    }
    Print("%d Number of sems\n", count);
}
コード例 #6
0
int Destroy_Semaphore(int sid)
{
	struct Semaphore* sema = (&s_semaphoreList)->head;

	//find sem by sid
	while (sema != 0)
	{
		if (sema->sid == sid)
		{
			Remove_From_Semaphore_List(&s_semaphoreList, sema);
			Free(sema);
			return 0;
		}
		sema = Get_Next_In_Semaphore_List(sema);
	}
	
	return -1;
}
コード例 #7
0
ファイル: sem.c プロジェクト: rjrpaz/os-implementation
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;
}