struct queue *queue_free(struct queue *s) { while (s->head) { queue_rem(s); } return s; }
G_MODULE_EXPORT gboolean on_btnDequeue_clicked(void) { char *sel = glist_selected(GTK_TREE_VIEW(treeTransfers)); if(!sel){ status("Uh... select a queue'd file if you please"); return FALSE; } /* gui list */ QUEUE_REM(sel); /* internal linked list */ queue_rem(&file_queue, sel); if(queue_len(file_queue) == 0) gtk_widget_set_sensitive(btnSend, FALSE); return FALSE; }
status_t semaphore_destroy (int32_t id) /* * ARGUMENTS * * id : the semaphore id. * * RESULT * * DNA_BAD_SEM_ID: the id parameter is invalid * * DNA_OK: the operation succeeded * * SOURCE */ { thread_t thread = NULL; semaphore_t sem = NULL; semaphore_id_t sid = { .raw = id }; interrupt_status_t it_status = 0; bool smart_to_reschedule = false; status_t status; watch (status_t) { ensure (sid . s . index < DNA_MAX_SEM, DNA_BAD_SEM_ID); it_status = cpu_trap_mask_and_backup(); lock_acquire (& semaphore_pool . lock); /* * Look for the semaphore with ID id. If found, * remove its entry from the pool. */ sem = semaphore_pool . semaphore[sid . s . index]; check (invalid_semaphore, sem != NULL, DNA_BAD_SEM_ID); check (invalid_semaphore, sem -> id . raw == sid . raw, DNA_BAD_SEM_ID); semaphore_pool . semaphore[sid . s . index] = NULL; lock_acquire (& sem -> lock); lock_release (& semaphore_pool . lock); /* * Reschedule each waiting thread, and * reset its information. */ lock_acquire (& sem -> waiting_queue . lock); while ((thread = queue_rem (& sem -> waiting_queue)) != NULL) { lock_acquire (& thread -> lock); thread -> info . sem_tokens = 0; thread -> info . resource = DNA_NO_RESOURCE; thread -> info . resource_id = -1; if (thread -> info . status == DNA_THREAD_WAITING) { thread -> info . status = DNA_THREAD_READY; status = scheduler_dispatch (thread); smart_to_reschedule = smart_to_reschedule || (status == DNA_INVOKE_SCHEDULER); } lock_release (& thread -> lock); } lock_release (& sem -> waiting_queue . lock); /* * Delete the semaphore's memory. */ kernel_free (sem); return smart_to_reschedule ? DNA_INVOKE_SCHEDULER : DNA_OK; } rescue (invalid_semaphore) { lock_release (& semaphore_pool . lock); cpu_trap_restore(it_status); leave; } }
status_t port_create (char * name, int32_t queue_length, int32_t * p_id) /* * ARGUMENTS * * name : the name of the port. * * queue_length : the length of its queue. * * RESULT * * DNA_NO_MORE_PORT: no more port available * * DNA_OUT_OF_MEM: cannot allocate memory to create a port * * DNA_OK: the operation succeeded * * SOURCE */ { int16_t index; port_t port = NULL; status_t status; interrupt_status_t it_status = 0; watch (status_t) { ensure (name != NULL && p_id != NULL, DNA_BAD_ARGUMENT); ensure (queue_length > 0, DNA_BAD_ARGUMENT); it_status = cpu_trap_mask_and_backup(); lock_acquire (& port_pool . lock); /* * Get an empty port slot. */ port = queue_rem (& port_pool . port); check (pool_error, port != NULL, DNA_NO_MORE_PORT); /* * Make the place clean. */ index = port -> id . s . index; dna_memset (port, 0, sizeof (struct _port)); port -> id . s . index = index; port -> id . s . value = port_pool . counter; semaphore_pool . counter += 1; lock_release (& port_pool . lock); cpu_trap_restore(it_status); /* * Creating the messages. */ port -> data = kernel_malloc (sizeof (struct _message) * queue_length, true); check (no_mem, port -> data != NULL, DNA_OUT_OF_MEM); for (int32_t i = 0; i < queue_length; i += 1) { queue_add (& port -> message, & port -> data[i]); } /* * Creating the semaphores. */ status = semaphore_create (name, queue_length, & port -> write_sem); check (wsem_error, status == DNA_OK, status); status = semaphore_create (name, 0, & port -> read_sem); check (rsem_error, status == DNA_OK, status); dna_strcpy (port -> info . name, name); port -> info . capacity = queue_length; /* * Return the port information. */ *p_id = port -> id . raw; return DNA_OK; } rescue (rsem_error) { semaphore_destroy (port -> write_sem); } rescue (wsem_error) { kernel_free (port -> data); } rescue (no_mem) { it_status = cpu_trap_mask_and_backup(); lock_acquire (& port_pool . lock); queue_add (& port_pool . port, port); } rescue (pool_error) { lock_release (& port_pool . lock); cpu_trap_restore(it_status); leave; } }