/* Seek to a nonbuffered part of a handle by rebuffering the data. */ static void rebuffer_handle(int handle_id, size_t newpos) { struct memory_handle *h = find_handle(handle_id); if (!h) return; /* When seeking foward off of the buffer, if it is a short seek don't rebuffer the whole track, just read enough to satisfy */ if (newpos > h->offset && newpos - h->offset < BUFFERING_DEFAULT_FILECHUNK) { LOGFQUEUE("buffering >| Q_BUFFER_HANDLE %d", handle_id); queue_send(&buffering_queue, Q_BUFFER_HANDLE, handle_id); h->ridx = h->data + newpos; return; } h->offset = newpos; /* Reset the handle to its new offset */ LOGFQUEUE("buffering >| Q_RESET_HANDLE %d", handle_id); queue_send(&buffering_queue, Q_RESET_HANDLE, handle_id); uintptr_t next = ringbuf_offset(h->next); if (ringbuf_sub(next, h->data) < h->filesize - newpos) { /* There isn't enough space to rebuffer all of the track from its new offset, so we ask the user to free some */ DEBUGF("%s(): space is needed\n", __func__); send_event(BUFFER_EVENT_REBUFFER, &handle_id); } /* Now we ask for a rebuffer */ LOGFQUEUE("buffering >| Q_BUFFER_HANDLE %d", handle_id); queue_send(&buffering_queue, Q_BUFFER_HANDLE, handle_id); }
/** * Função de entrada do programa. * * Sintaxe: * sensor NOME * * Inicia o sensor, que utilizará o segmento de memória compartilhada * com o nome fornecido. * * @param argc Número de argumentos na linha de comando. * @param argv Vetor de argumentos da linha de comando. * @return Zero para execução com êxito, diferente de zero para erro. */ int main(int argc, char *argv[]) { if (argc != 2) die_usage(); const char *const shmname = argv[1]; const size_t shmsize = sizeof(my_queue_t); my_queue_t *q; /** * === IMPLEMENTAR === * * - Abrir o segmento de memória compartilhada cujo nome que está em * 'shmname'. * - Mapear a memória compartilhada no espaço virtual do processo, * obtendo um ponteiro 'q' para a memória compartilhada. */ int shmdes = shm_open(shmname, O_CREAT | O_RDWR, 0600); if (shmdes < 0){ perror("shm_open"); exit(EXIT_FAILURE); } if (ftruncate(shmdes,sizeof(int)) < 0){ perror("ftruncate"); exit(EXIT_FAILURE); } int *p = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, shmdes, 0); if(p == MAP_FAILED){ perror("mmap"); exit(EXIT_FAILURE); } printf("Novo valor: ", %d\n, *p); for (int i = 0; i < 50; i++) { double v[2]; rand_normal(25.0, 3.0, v); queue_send(q, v[0]); printf("Enviado valor %lf.\n", v[0]); queue_send(q, v[1]); printf("Enviado valor %lf.\n", v[1]); } queue_send(q, -1.0); /** * === IMPLEMENTAR === * * - Remover o mapeamento de memória em 'q'. * - Fechar o segmento de memória compartilhada. */ munmap(q, shmsize); close(shmdes); return 0; }
// multiple writers/readers void *test2b(void *vptr_args) { int thread_no = (int)vptr_args; char message[128]; int buflen = 128, prio, i; time_t now; switch(thread_no) { case 0: //senders case 1: case 2: queue_recv(&queue, message, &buflen, &prio, 0, NULL); printf("Watek %d otrzymal wiadomosc o priorytecie %d: \"%s\"\n", thread_no, prio, message); break; case 3: //receiver // let's wait a ~second strcpy(message, "Opozniona wiadomosc"); buflen = strlen(message) + 1; for(i=0;i<3;i++) { now = time(NULL); while(time(NULL) == now); queue_send(&queue, message, buflen, i, 0, NULL); } break; } }
// multiple writers void *test2a(void *vptr_args) { int thread_no = (int)vptr_args; char message[128]; int buflen = 128, prio, i; time_t now; switch(thread_no) { case 0: //senders case 1: case 2: prio = 2 - thread_no; snprintf(message, 128, "Wiadomosc od watku %d", thread_no, prio); queue_send(&queue, message, strlen(message) + 1, prio, 0, NULL); break; case 3: //receiver // let's wait a ~second now = time(NULL); while(time(NULL) == now); for(i=0;i<3;i++) { queue_recv(&queue, message, &buflen, &prio, 0, NULL); printf("Odebrano wiadomosc o dlugosci %d i priorytecie %d: \"%s\"\n", buflen, prio, message); } break; } }
/* Tell is voice is still in a playing state */ bool mp3_is_playing(void) { /* TODO: Implement a timeout or state query function for event objects */ LOGFQUEUE("mp3 >| voice Q_VOICE_STATE"); int state = queue_send(&voice_queue, Q_VOICE_STATE, 0); return state != TSTATE_STOPPED; }
int queue_put_nop(t_conf *conf, t_simple_list *client) { t_list *queue; int len; struct dns_hdr *hdr; while (client->control.nop_pending < NOP_SIZE) { if ((queue = queue_find_empty_data_cell(client))) { client->num_seq++; len = create_req_data(conf, client, queue, 0, 0); if (queue_send(conf, client, queue) == -1) { client->num_seq--; return (-1); } client->control.nop_pending++; queue->peer.type = NOP; queue->status = SENT; hdr = (struct dns_hdr *)queue->data; queue->peer.id = hdr->id; return (0); } } return (-1); }
uint8_t gprs_send(void *data_p, uint8_t len, uint16_t tag, uint16_t time) { DBG_ASSERT(data_p != NULL __DBG_LINE); DBG_ASSERT(len != 0 __DBG_LINE); queue_data_type new_item; osel_memset(&new_item, 0x00, sizeof new_item); OSEL_ISR_ENTRY(); osel_memcpy(new_item.gprs_data, data_p, len); new_item.gprs_data_len = len; new_item.gprs_tag = tag; new_item.ack_time = time; OSEL_ISR_EXIT(); if(GET_QUEUE_LEN == QUEUE_MAXLEN) { return QUEUE_MAXLEN+1; } else { queue_send(&gprs_queue, new_item); //入队成功 osel_post(GPRS_SEND_EVENT, NULL, OSEL_EVENT_PRIO_LOW); return(GET_QUEUE_LEN); //返回队列长度 } }
/* Close the handle. Return true for success and false for failure */ bool bufclose(int handle_id) { logf("bufclose(%d)", handle_id); LOGFQUEUE("buffering >| Q_CLOSE_HANDLE %d", handle_id); return queue_send(&buffering_queue, Q_CLOSE_HANDLE, handle_id); }
static void destruct_esqlite_connection(ErlNifEnv *env, void *arg) { esqlite_connection *db = (esqlite_connection *) arg; esqlite_command *cmd = command_create(); /* Send the stop command */ cmd->type = cmd_stop; queue_push(db->commands, cmd); queue_send(db->commands, cmd); /* Wait for the thread to finish */ enif_thread_join(db->tid, NULL); enif_thread_opts_destroy(db->opts); /* The thread has finished... now remove the command queue, and close * the datbase (if it was still open). */ queue_destroy(db->commands); if(db->db) sqlite3_close(db->db); }
bool i2c_operation(I2COperation op) { if (!queue_send(op_queue, &op, PORT_MAX_DELAY)) return FALSE; semaphore_take(bus_lock, PORT_MAX_DELAY); return TRUE; }
void button_que_update(event_queue *queue, button *b, uint8_t ev_base) { uint8_t ev; static queue_element qel; ev = button_update(b); qel.source = ev + ev_base; if (ev!=BUTTON_NO_EVENT) queue_send(queue, &qel); }
int main(int argc, char *argv[]) { assert(argc == 2); op_time = strtol(argv[1], NULL, 10); assert(op_time >= 0); debug("Hello client! Got %ld.\n", op_time); control_queue = get_queue(CONTROL_KEY); clients_server_queue = get_queue(CLIENTS_SERVER_KEY); server_clients_queue = get_queue(SERVER_CLIENTS_KEY); pid = getpid(); debug("My pid is %ld.\n", pid); Mesg msg; msg.mesg_type = pid; queue_send(control_queue, (char *) &msg); char buffer[500]; while (fgets(buffer, sizeof buffer, stdin) != NULL) { char op; int n; const char *p = buffer; sscanf(p, "%c%n", &op, &n); p += n; switch (op) { case 'r': op_read(p); break; case 'w': op_write(p); break; case 's': op_sum(p); break; case 'x': op_swap(p); break; } } msg.op = QUIT; queue_send(clients_server_queue, (char *) &msg); return 0; }
static void keypress_isr(registers_t *regs) { static uint32_t scancode; scancode = inb(0x60); if (!(scancode&0x80)) { asm volatile("cli"); queue_send(keybd_queue, &scancode, QM_NONBLOCKING); asm volatile("sti"); } }
VCL_VOID vmod_mqueue_sendmsg(const struct vrt_ctx *ctx, struct vmod_msgsend_mqueue *q, VCL_STRING message) { CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); CHECK_OBJ_NOTNULL(q, VMOD_MSGSEND_OBJ_MAGIC); if (QUEUE_ERR_OK == queue_send(q->queue, message, -1)) { VSLb(ctx->vsl, SLT_Debug, "Message '%s' sent on mqueue '%s'", message, q->queue_name); } else { VSLb(ctx->vsl, SLT_Error, "Failed sending message '%s' on '%s'", message, q->queue_name); } }
/* Borrow the codec thread and return the ID */ void codec_thread_do_callback(void (*fn)(void), unsigned int *id) { /* Set id before telling thread to call something; it may be * needed before this function returns. */ if (id != NULL) *id = codec_thread_id; /* Codec thread will signal just before entering callback */ LOGFQUEUE("codec >| Q_CODEC_DO_CALLBACK"); queue_send(&codec_queue, Q_CODEC_DO_CALLBACK, (intptr_t)fn); }
/* Stop current voice clip from playing */ void mp3_play_stop(void) { if(!audio_is_thread_ready()) return; mutex_lock(&voice_mutex); /* Sync against voice_stop */ LOGFQUEUE("mp3 >| voice Q_VOICE_STOP: 1"); queue_send(&voice_queue, Q_VOICE_STOP, 1); mutex_unlock(&voice_mutex); }
int queue_resend(t_conf *conf, t_simple_list *client, t_list *queue) { struct dns_hdr *hdr; hdr = (struct dns_hdr *)queue->data; hdr->id = myrand(); queue->peer.id = hdr->id; DPRINTF(3, "Queue resend seq %d id = 0x%x \n", queue->info.num_seq, queue->peer.id); queue_send(conf, client, queue); return (0); }
static int chan_send(lua_State* L) { int type, ret; double timeout; struct msg_t* msg; struct queue_t* q = _lua_arg_queue(L); if (lua_gettop(L) < 2) _lua_usage(L, _usage_send); if (lua_isstring(L, 2)) type = LUA_TSTRING; else if (lua_isnumber(L, 2)) type = LUA_TNUMBER; else if (lua_isboolean(L, 2)) type = LUA_TBOOLEAN; else _lua_usage(L, _usage_send); timeout = _lua_arg_double(L, 3, 1, -1, _usage_send); switch (type) { case LUA_TSTRING: { size_t len = 0; const char* str = lua_tolstring(L, 2, &len); msg = (struct msg_t*)malloc(sizeof(struct msg_t) + len + 1); msg->str = (char*)msg + sizeof(struct msg_t); memcpy(msg->str, str, msg->str_len = len); msg->str[len] = 0; } break; case LUA_TNUMBER: { msg = (struct msg_t*)malloc(sizeof(struct msg_t)); msg->num = lua_tonumber(L, 2); } break; case LUA_TBOOLEAN: { msg = (struct msg_t*)malloc(sizeof(struct msg_t)); msg->bool_val = lua_toboolean(L, 2); } break; } msg->type = type; ret = queue_send(q, msg, timeout); if (!ret) { free(msg); } lua_pushboolean(L, ret); return 1; }
/* Stop any current clip and start playing a new one */ void mp3_play_data(const unsigned char* start, int size, pcm_play_callback_type get_more) { if (get_more != NULL && start != NULL && (ssize_t)size > 0) { struct voice_info voice_clip = { .get_more = get_more, .start = (unsigned char *)start, .size = size, }; LOGFQUEUE("mp3 >| voice Q_VOICE_PLAY"); queue_send(&voice_queue, Q_VOICE_PLAY, (intptr_t)&voice_clip); } } /* Stop current voice clip from playing */ void mp3_play_stop(void) { LOGFQUEUE("mp3 >| voice Q_VOICE_STOP"); queue_send(&voice_queue, Q_VOICE_STOP, 0); }
int queue_get_tcp_data(t_conf *conf, t_simple_list *client) { t_list *queue; int len; char buffer[MAX_HOST_NAME_ENCODED + 1]; size_t max_len; struct dns_hdr *hdr; max_len = MAX_QNAME_DATA(conf->domain) - PACKET_LEN; /* Should exit if !queue */ if ((queue = queue_find_empty_data_cell(client))) { if ((client->control.data_pending >= MAX_DATA_SIZE) || (client->control.data_pending + client->control.nop_pending >= WINDOW_SIZE)) { DPRINTF(1, "Warning Window size full waiting to flush ...\n"); return (0); } client->num_seq++; if (!((len = read(client->sd_tcp, buffer, max_len)) > 0)) { create_req_data(conf, client, queue, 0, -1); queue_send(conf, client, queue); return (-1); } DPRINTF(3, "Read tcp %d bytes on sd %d\n", len, client->sd_tcp); len = create_req_data(conf, client, queue, buffer, len); if (queue_send(conf, client, queue) == -1) return (-1); client->control.data_pending++; queue->peer.type = DATA; hdr = (struct dns_hdr *) queue->data; queue->peer.id = hdr->id; queue->status = SENT; } return (0); }
void* console_device(void * arg) { uint32_t scancode; iorq_t iorq; int i; char c; char * aux; memset(&keybd_device, 0, sizeof(device_t)); strcpy(keybd_device.ln_link.name, "org.era.dev.console"); keybd_device.iorq_queue = create_queue(10, sizeof(iorq_t)); keybd_queue = create_queue(1, sizeof(uint32_t)); register_interrupt_handler(IRQ1, &keypress_isr); monitor_writexy(0,24, " 1", 7, 0); for(;;) { queue_recv(keybd_device.iorq_queue, &iorq, QM_BLOCKING); if(iorq.io_desc == DC_READ) { i = 0; aux = (char *) iorq.io_dptr; memset(aux, 0, iorq.io_sz); while(1) { queue_recv(keybd_queue, &scancode, QM_BLOCKING); c = kbdus[scancode]; if (c == '\n') break; if ((c == '\b') && (i > 0)) { aux[i] = '\0'; i--; monitor_put('\b'); continue; } if (c == 0 || c == '\t' || i == (iorq.io_sz - 1)) continue; if(isprintable(c) ) { aux[i++] = c; monitor_put(c); } } monitor_put('\n'); aux[i] = '\0'; queue_send(iorq.io_response, NULL, QM_NONBLOCKING); } } }
int vm_send(vm_ptr vm, ENTERM data) { job_ptr job = job_create(); if(job == NULL) goto error; job->type = job_response; job->args = enif_make_copy(job->env, data); if(!queue_send(vm->jobs, job)) goto error; return 1; error: if(job != NULL) job_destroy(job); return 0; }
// let's wait for 3 seconds void *test3(void *vptr_args) { int thread_no = (int)vptr_args; char message[128]; int buflen = 128, prio; switch(thread_no) { case 0: //sender printf("Proba wyslania wiadomosci...\n"); fflush(stdout); strncpy(message, "To jest wiadomosc!", 128); if(queue_send(&queue, message, strlen(message) + 1, 1, 3, NULL) == MSG_QUEUE_TIMEOUT) printf("Nastapil timeout.\n"); break; } }
void vm_destroy(ErlNifEnv* env, void* obj) { vm_ptr vm = (vm_ptr) obj; job_ptr job = job_create(); void* resp; assert(job != NULL && "Failed to create job."); job->type = job_close; queue_push(vm->jobs, job); queue_send(vm->jobs, job); enif_thread_join(vm->tid, &resp); queue_destroy(vm->jobs); enif_thread_opts_destroy(vm->opts); }
static bool codec_load_next_track(void) { intptr_t result = Q_CODEC_REQUEST_FAILED; audio_set_prev_elapsed(thistrack_id3->elapsed); #ifdef AB_REPEAT_ENABLE ab_end_of_track_report(); #endif logf("Request new track"); if (ci.new_track == 0) { ci.new_track++; automatic_skip = true; } if (!ci.stop_codec) { trigger_cpu_boost(); LOGFQUEUE("codec >| audio Q_AUDIO_CHECK_NEW_TRACK"); result = queue_send(&audio_queue, Q_AUDIO_CHECK_NEW_TRACK, 0); } switch (result) { case Q_CODEC_REQUEST_COMPLETE: LOGFQUEUE("codec |< Q_CODEC_REQUEST_COMPLETE"); pcmbuf_start_track_change(automatic_skip); return true; case Q_CODEC_REQUEST_FAILED: LOGFQUEUE("codec |< Q_CODEC_REQUEST_FAILED"); ci.new_track = 0; ci.stop_codec = true; codec_requested_stop = true; return false; default: LOGFQUEUE("codec |< default"); ci.stop_codec = true; codec_requested_stop = true; return false; } }
// simple send & receive void *test1(void *vptr_args) { int thread_no = (int)vptr_args; char message[128]; int buflen = 128, prio; switch(thread_no) { case 0: //sender strncpy(message, "To jest wiadomosc!", 128); queue_send(&queue, message, strlen(message) + 1, 1, 0, NULL); printf("Wyslano wiadomosc: %s\n", message); break; case 1: //receiver queue_recv(&queue, message, &buflen, &prio, 0, NULL); printf("Odebrano wiadomosc o dlugosci %d i priorytecie %d: %s\n", buflen, prio, message); break; } }
/* This function is meant to be used by the buffer request functions to ensure the codec is no longer active */ void voice_stop(void) { mutex_lock(&voice_mutex); /* Stop the output and current clip */ mp3_play_stop(); /* Careful if using sync objects in talk.c - make sure locking order is * observed with one or the other always granted first */ /* Unqueue all future clips */ talk_force_shutup(); /* Wait for any final queue_post to be processed */ LOGFQUEUE("mp3 >| voice Q_VOICE_NULL"); queue_send(&voice_queue, Q_VOICE_NULL, 0); mutex_unlock(&voice_mutex); } /* voice_stop */
/* Stop any current clip and start playing a new one */ void mp3_play_data(const unsigned char* start, int size, pcm_more_callback_type get_more) { /* Shared struct to get data to the thread - once it replies, it has * safely cached it in its own private data */ static struct voice_info voice_clip SHAREDBSS_ATTR; if (get_more != NULL && start != NULL && (ssize_t)size > 0) { mutex_lock(&voice_mutex); voice_clip.get_more = get_more; voice_clip.start = (unsigned char *)start; voice_clip.size = size; LOGFQUEUE("mp3 >| voice Q_VOICE_PLAY"); queue_send(&voice_queue, Q_VOICE_PLAY, (intptr_t)&voice_clip); mutex_unlock(&voice_mutex); } }
// let's break sth void *test4(void *vptr_args) { int thread_no = (int)vptr_args; char message[128]; int buflen = 128, prio; time_t now; switch(thread_no) { case 0: //sender stop = 0; printf("Proba wyslania wiadomosci...\n"); fflush(stdout); strncpy(message, "To jest wiadomosc!", 128); if(queue_send(&queue, message, strlen(message) + 1, 1, 0, &stop) == MSG_QUEUE_STOPPED) printf("Nastapilo zatrzymanie przez zmienna stop.\n"); break; case 1: now = time(NULL); while(time(NULL) == now); stop = 1; } }
void send_request(Mesg *request, Mesg *response) { queue_send(clients_server_queue, (char *) request); queue_receive(server_clients_queue, (char *) response, pid); }