void gprs_close_handler(void) { #if GPRS_DEBUG serial_write(HAL_UART_2, GPRS_CLOSEING, sizeof(GPRS_CLOSEING)-1); #endif gprs_switch(); gprs_flush_receive_buf(); while (GPRS_QUEUE_HAVE_DATA) //清空发送缓冲区 { queue_receive(&gprs_queue, &n_item); } gprs_restart_cnt = 0; gprs_cmd_send_cnt = 0; gprs_send_data_cnt = 0; gprs_cmd_send_cpin = 0; gprs_cmd_send_cipstart = 0; gprs_cmd_send_cgatt = 0; gprs_mode = GPRS_CLOSE; gprs_cmd_type = GPRS_CMD_NULL; osel_post(GPRS_POLL_EVENT, NULL, OSEL_EVENT_PRIO_LOW); }
void thread_a(void) { int a; printk("starting thread A\n"); while (1) { printk("[A] receiving from queue: "); queue_receive(&queue, &a, 10000); printk("%d\n", a); } }
static void lcd_task(void *params) { LcdMessage msg; const char *string; const char **bitmaps; // Initialise the hardware. This uses delays so must not be called prior // to the scheduler being started. lcd_setup(); while (TRUE) { // Wait for a message to arrive that requires displaying while (queue_receive(lcd_queue, &msg, PORT_MAX_DELAY) != TRUE); switch (msg.type) { case LCD_MESSAGE_CLEAR: lcd_command(LCD_CLEAR_DISPLAY_CMD); break; case LCD_MESSAGE_PUT_STRING: lcd_goto(msg.row, msg.offset); string = msg.data; if (msg.length < 0) { while (*string) { lcd_data(*string++); } } else { while (msg.length--) { lcd_data(*string++); } } break; case LCD_MESSAGE_SET_CUSTOM_CHARS: lcd_command(LCD_CUSTOM_CHARS_CMD + msg.offset); bitmaps = (const char **) msg.data; while (msg.length--) { lcd_data(*bitmaps[0]); lcd_data(*bitmaps[1]); lcd_data(*bitmaps[2]); lcd_data(*bitmaps[3]); lcd_data(*bitmaps[4]); lcd_data(*bitmaps[5]); lcd_data(*bitmaps[6]); lcd_data(*bitmaps[7]); bitmaps++; } break; } } }
void i2c_task(void *params) { I2COperation op; while (TRUE) { state = I2C_STATE_IDLE; queue_peek(op_queue, &op, PORT_MAX_DELAY); // do operation here semaphore_give(op_signal); queue_receive(op_queue, &op, 0); } }
static JSBool jserl_send(JSContext* cx, JSObject* obj, uintN argc, jsval* argv, jsval* rval) { vm_ptr vm = (vm_ptr) JS_GetContextPrivate(cx); ErlNifEnv* env; job_ptr job; ENTERM mesg; if(argc < 0) { return JS_FALSE; } assert(vm != NULL && "Context has no vm."); env = enif_alloc_env(); mesg = vm_mk_message(env, to_erl(env, cx, argv[0])); // If pid is not alive, raise an error. // XXX: Can I make this uncatchable? if(!enif_send(NULL, &(vm->curr_job->pid), env, mesg)) { JS_ReportError(cx, "Context closing."); return JS_FALSE; } job = queue_receive(vm->jobs); if(job->type == job_close) { // XXX: Can I make this uncatchable? job_destroy(job); JS_ReportError(cx, "Context closing."); return JS_FALSE; } assert(job->type == job_response && "Invalid message response."); *rval = to_js(job->env, cx, job->args); job_destroy(job); return JS_TRUE; }
void send_request(Mesg *request, Mesg *response) { queue_send(clients_server_queue, (char *) request); queue_receive(server_clients_queue, (char *) response, pid); }
int main(int argc, char **argv) { gid_t gid; addr_t addr; struct sigaction sa; int c, dFlag, vFlag; unsigned long max_message_size; const char *queuename, *tablename; ctxt = NULL; gid = (gid_t) -1; vFlag = dFlag = 0; tablename = queuename = NULL; if (NULL == (queue = queue_init())) { errx("queue_init failed"); // TODO: better } atexit(cleanup); sa.sa_handler = &on_signal; sigemptyset(&sa.sa_mask); sigaction(SIGINT, &sa, NULL); sigaction(SIGTERM, &sa, NULL); sa.sa_flags = SA_RESTART; sigaction(SIGUSR1, &sa, NULL); if (NULL == (engine = get_default_engine())) { errx("no engine available for your system"); } while (-1 != (c = getopt_long(argc, argv, optstr, long_options, NULL))) { switch (c) { case 'b': { unsigned long val; if (parse_ulong(optarg, &val)) { queue_set_attribute(queue, QUEUE_ATTR_MAX_MESSAGE_SIZE, val); // TODO: check returned value } break; } case 'd': dFlag = 1; break; case 'e': { if (NULL == (engine = get_engine_by_name(optarg))) { errx("unknown engine '%s'", optarg); } break; } case 'g': { struct group *grp; if (NULL == (grp = getgrnam(optarg))) { errc("getgrnam failed"); } gid = grp->gr_gid; break; } case 'l': { logfilename = optarg; if (NULL == (err_file = fopen(logfilename, "a"))) { err_file = NULL; warnc("fopen '%s' failed, falling back to stderr", logfilename); } break; } case 'p': pidfilename = optarg; break; case 'q': queuename = optarg; break; case 's': { unsigned long val; if (parse_ulong(optarg, &val)) { queue_set_attribute(queue, QUEUE_ATTR_MAX_MESSAGE_IN_QUEUE, val); // TODO: check returned value } break; } case 't': tablename = optarg; break; case 'v': vFlag++; break; case 'h': default: usage(); } } argc -= optind; argv += optind; if (0 != argc || NULL == queuename || NULL == tablename) { usage(); } if (dFlag) { if (0 != daemon(0, !vFlag)) { errc("daemon failed"); } } if (NULL != pidfilename) { FILE *fp; if (NULL == (fp = fopen(pidfilename, "w"))) { warnc("can't create pid file '%s'", pidfilename); } else { fprintf(fp, "%ld\n", (long) getpid()); fclose(fp); } } if (((gid_t) -1) != gid) { if (0 != setgid(gid)) { errc("setgid failed"); } if (0 != setgroups(1, &gid)) { errc("setgroups failed"); } } CAP_RIGHTS_LIMIT(STDOUT_FILENO, CAP_WRITE); CAP_RIGHTS_LIMIT(STDERR_FILENO, CAP_WRITE); if (NULL != err_file/* && fileno(err_file) > 2*/) { CAP_RIGHTS_LIMIT(fileno(err_file), CAP_WRITE); } if (QUEUE_ERR_OK != queue_open(queue, queuename, QUEUE_FL_OWNER)) { errx("queue_open failed"); // TODO: better } if (QUEUE_ERR_OK != queue_get_attribute(queue, QUEUE_ATTR_MAX_MESSAGE_SIZE, &max_message_size)) { errx("queue_get_attribute failed"); // TODO: better } if (NULL == (buffer = calloc(++max_message_size, sizeof(*buffer)))) { errx("calloc failed"); } if (NULL != engine->open) { ctxt = engine->open(tablename); } if (0 == getuid() && engine->drop_privileges) { struct passwd *pwd; if (NULL == (pwd = getpwnam("nobody"))) { if (NULL == (pwd = getpwnam("daemon"))) { errx("no nobody or daemon user accounts found on this system"); } } if (0 != setuid(pwd->pw_uid)) { errc("setuid failed"); } } CAP_ENTER(); while (1) { ssize_t read; if (-1 == (read = queue_receive(queue, buffer, max_message_size))) { errc("queue_receive failed"); // TODO: better } else { if (!parse_addr(buffer, &addr)) { errx("parsing of '%s' failed", buffer); // TODO: better } else { engine->handle(ctxt, tablename, addr); } } } /* not reached */ return BANIPD_EXIT_SUCCESS; }